My favorites | Sign in
Project Home Downloads Wiki Issues Source
Repository:
Checkout   Browse   Changes   Clones    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
<?php
/*
Authwell User Model Unit Test

NOTES

REFERENCES
http://book.cakephp.org/view/485/Testing-plugins
*/

class AuthwellUserTestCase extends CakeTestCase {

var $AuthwellUser = null;
var $fixtures = array(
'plugin.authwell.authwell_user',
'plugin.authwell.authwell_role',
'plugin.authwell.authwell_privilege',
'plugin.authwell.authwell_user_authwell_role',
'plugin.authwell.authwell_role_authwell_privilege',
);

function start()
{
parent::start();
$this->AuthwellUser = ClassRegistry::init('Authwell.AuthwellUser');
$this->RecordObj = new AuthwellUserRecord($this->AuthwellUser);
$this->RoleRecordObj = new AuthwellRoleRecord($this->AuthwellUser->AuthwellRole);
$this->PrivRecordObj = new AuthwellPrivilegeRecord(
$this->AuthwellUser->AuthwellRole->AuthwellPrivilege);
}

function testInstance() {
$this->assertTrue($this->AuthwellUser instanceof AppModel);
#debug(get_class($this->AuthwellUser));
}

function testProperties()
{
$this->assertEqual($this->AuthwellUser->useTable, 'authwell_users');
$this->assertEqual($this->AuthwellUser->name, 'AuthwellUser');
}

function testSchema()
{
$Cols = array_keys($this->AuthwellUser->_schema);
$this->assertEqual(7, count($Cols));
#debug($this->AuthwellUser->_schema);
#debug(implode(' ', $Cols));
}

function testHabtmAssociation()
{
$this->assertTrue(isset($this->AuthwellUser->AuthwellRole));
$this->assertTrue($this->AuthwellUser->AuthwellRole instanceof AuthwellRole);
#debug($this->AuthwellUser);
}

function testRecord()
{
$name = 'ichibod';
$Record = $this->RecordObj->create(array('name'=>$name));
$this->assertEqual($Record['name'], $name);
}

function testInsertDeleteRecords()
{
$Data = $this->AuthwellUser->save($this->RecordObj->create());
$this->assertTrue($this->AuthwellUser->id);
$this->AuthwellUser->delete($this->AuthwellUser->id, 0);
}

function testFindByEmail()
{
$email = 'test@cakewell.com';
$Record = $this->RecordObj->create(array('email'=>$email));
$this->AuthwellUser->save($Record);
$SavedRecord = $this->AuthwellUser->findByEmail($email);
$NotFound = $this->AuthwellUser->findByEmail('not@cakewell.com');

$this->assertEqual($SavedRecord['AuthwellUser']['email'], $email);
$this->assertEqual($NotFound, false);
}

function testFindUserByEmail()
{
$Record = $this->RecordObj->create();
$this->AuthwellUser->save($Record);
$SavedRecord = $this->AuthwellUser->find_user_by_email($Record['email'], 1);
$NotFound = $this->AuthwellUser->find_user_by_email('not@cakewell.com', 1);

$this->assertEqual($SavedRecord['User']['email'], $Record['email']);
$this->assertEqual($NotFound, false);
}

function testInvalidateLogin()
{
$message = 'unit test';
$is_valid = $this->AuthwellUser->invalidate_login($message);
$this->assertFalse($is_valid);
$this->assertTrue(in_array($message, $this->AuthwellUser->loginFormErrors));
}

function testHoneypot()
{
$field = 'honeypot_unit_test';
$tval = null;
$fval = 'honey';
$data = null;

$this->AuthwellUser->data[$this->AuthwellUser->name][$field] = $tval;
$true = $this->AuthwellUser->is_honeypot($data, $field);
$this->assertTrue($true);

$this->AuthwellUser->data[$this->AuthwellUser->name][$field] = $fval;
$false = $this->AuthwellUser->is_honeypot($data, $field);
$this->assertFalse($false);
}

function testPassword()
{
$plain = 'cakewell';
$_0x_password = $this->AuthwellUser->_0x_password($plain);

$this->assertEqual( $_0x_password,
$this->AuthwellUser->as_binary($this->AuthwellUser->password($plain)) );

#debug($_0x_password);
}

function testSimpleLoginRequest() {

// first save a random user
$Record = $this->RecordObj->create(array('active'=>1));
$this->AuthwellUser->create();
$this->assertTrue($this->AuthwellUser->save($Record));
#debug($this->AuthwellUser->invalidFields());

// then call
$FormData = array(
'AuthwellUser' => array(
'email_login' => $Record['email'],
'password_login' => $Record['password']
)
);

$is_logged_in = $this->AuthwellUser->is_valid_login_request($FormData);
$Cache = $this->AuthwellUser->UserDataCache;

$this->assertTrue($is_logged_in);
$this->assertEqual($Cache['User']['email'], $Record['email']);
}

function testFindUserByEmailWithRolesAndPrivileges()
{
# create user record
$UserRecord = $this->RecordObj->create();
$user_name = $UserRecord['name'];

# create role and privilege records
$RoleRecord = $this->RoleRecordObj->create(
array( 'name'=>sprintf('priv1_for_%s',$user_name) ) );
$PrivRecord1 = $this->PrivRecordObj->create(
array( 'dotpath'=>sprintf('%s.priv1',$user_name) ) );
$PrivRecord2 = $this->PrivRecordObj->create(
array( 'dotpath'=>sprintf('%s.priv2',$user_name) ) );
#debug(array($UserRecord, $RoleRecord, $PrivRecord1, $PrivRecord2));

# save records
$this->AuthwellUser->AuthwellRole->AuthwellPrivilege->save($PrivRecord1);
$PrivIds[] = $this->AuthwellUser->AuthwellRole->AuthwellPrivilege->id;
$this->AuthwellUser->AuthwellRole->AuthwellPrivilege->save($PrivRecord2);
$PrivIds[] = $this->AuthwellUser->AuthwellRole->AuthwellPrivilege->id;

$this->AuthwellUser->AuthwellRole->save( array(
'AuthwellRole'=>$RoleRecord,
'AuthwellPrivilege'=>$PrivIds ) );
$role_id = $this->AuthwellUser->AuthwellRole->id;

$this->AuthwellUser->save( array(
'AuthwellUser'=>$UserRecord,
'AuthwellRole'=>array($role_id) ) );

$UserDbRecord = $this->AuthwellUser->find_user_by_email($UserRecord['email'], 1);

$this->assertEqual($UserDbRecord['User']['email'], $UserRecord['email']);
$this->assertEqual($UserDbRecord['Roles'][0]['description'],
$RoleRecord['description']);
$this->assertTrue( in_array( $PrivRecord1['dotpath'],
$UserDbRecord['User']['dotpaths'] ));

#debug($UserDbRecord);
}
}
?>

Change log

ef931f181c98 by klenwell on Oct 19, 2009   Diff
T466: authwell examples completed
Go to: 
Project members, sign in to write a code review

Older revisions

60d63957feac by klenwell on Oct 16, 2009   Diff
T464: basic login form complete, model
fattened, and user tests updated
e89d3a86e398 by klenwell on Oct 5, 2009   Diff
T462: updated AuthwellUser model and
tests
9675ac5b525c by klenwell on Sep 14, 2009   Diff
T441: fixed authwell plugin models,
fixtures, and tests
All revisions of this file

File info

Size: 6727 bytes, 194 lines
Powered by Google Project Hosting