forked from CakeDC/users
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPasswordBehaviorTest.php
More file actions
200 lines (184 loc) · 5.5 KB
/
Copy pathPasswordBehaviorTest.php
File metadata and controls
200 lines (184 loc) · 5.5 KB
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
195
196
197
198
199
200
<?php
/**
* Copyright 2010 - 2015, Cake Development Corporation (http://cakedc.com)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2010 - 2015, Cake Development Corporation (http://cakedc.com)
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
namespace CakeDC\Users\Test\TestCase\Model\Behavior;
use CakeDC\Users\Exception\UserAlreadyActiveException;
use CakeDC\Users\Model\Table\UsersTable;
use Cake\Mailer\Email;
use Cake\ORM\TableRegistry;
use Cake\Routing\Router;
use Cake\TestSuite\TestCase;
use InvalidArgumentException;
/**
* Test Case
*/
class PasswordBehaviorTest extends TestCase
{
/**
* Fixtures
*
* @var array
*/
public $fixtures = [
'plugin.CakeDC/Users.users',
];
/**
* setup
*
* @return void
*/
public function setUp()
{
parent::setUp();
$this->table = TableRegistry::get('CakeDC/Users.Users');
$this->Behavior = $this->getMockBuilder('CakeDC\Users\Model\Behavior\PasswordBehavior')
->setMethods(['sendResetPasswordEmail'])
->setConstructorArgs([$this->table])
->getMock();
$this->Behavior->Email = $this->getMockBuilder('CakeDC\Users\Email\EmailSender')
->setMethods(['sendResetPasswordEmail'])
->getMock();
}
/**
* tearDown
*
* @return void
*/
public function tearDown()
{
unset($this->table, $this->Behavior);
parent::tearDown();
}
/**
* Test resetToken
*
*/
public function testResetToken()
{
$user = $this->table->findByUsername('user-1')->first();
$token = $user->token;
$this->Behavior->Email->expects($this->never())
->method('sendResetPasswordEmail')
->with($user);
$result = $this->Behavior->resetToken('user-1', [
'expiration' => 3600,
'checkActive' => true,
]);
$this->assertNotEquals($token, $result->token);
$this->assertEmpty($result->activation_date);
$this->assertFalse($result->active);
}
/**
* Test resetToken
*
*/
public function testResetTokenSendEmail()
{
$user = $this->table->findByUsername('user-1')->first();
$token = $user->token;
$tokenExpires = $user->token_expires;
$this->Behavior->Email->expects($this->once())
->method('sendResetPasswordEmail');
$result = $this->Behavior->resetToken('user-1', [
'expiration' => 3600,
'checkActive' => true,
'sendEmail' => true
]);
$this->assertNotEquals($token, $result->token);
$this->assertNotEquals($tokenExpires, $result->token_expires);
$this->assertEmpty($result->activation_date);
$this->assertFalse($result->active);
}
/**
* Test resetToken
*
* @expectedException InvalidArgumentException
*/
public function testResetTokenWithNullParams()
{
$this->Behavior->resetToken(null);
}
/**
* Test resetTokenNoExpiration
*
* @expectedException InvalidArgumentException
* @expectedExceptionMessage Token expiration cannot be empty
*/
public function testResetTokenNoExpiration()
{
$this->Behavior->resetToken('ref');
}
/**
* Test resetToken
*
* @expectedException CakeDC\Users\Exception\UserNotFoundException
*/
public function testResetTokenNotExistingUser()
{
$this->Behavior->resetToken('user-not-found', [
'expiration' => 3600
]);
}
/**
* Test resetToken
*
* @expectedException CakeDC\Users\Exception\UserAlreadyActiveException
*/
public function testResetTokenUserAlreadyActive()
{
$activeUser = TableRegistry::get('CakeDC/Users.Users')->findByUsername('user-4')->first();
$this->assertTrue($activeUser->active);
$this->table = $this->getMockForModel('CakeDC/Users.Users', ['save']);
$this->table->expects($this->never())
->method('save');
$this->Behavior->expects($this->never())
->method('sendResetPasswordEmail');
$this->Behavior->resetToken('user-4', [
'expiration' => 3600,
'checkActive' => true,
]);
}
/**
* Test resetToken
*
* @expectedException CakeDC\Users\Exception\UserNotActiveException
*/
public function testResetTokenUserNotActive()
{
$user = TableRegistry::get('CakeDC/Users.Users')->findByUsername('user-1')->first();
$this->Behavior->resetToken('user-1', [
'ensureActive' => true,
'expiration' => 3600
]);
}
/**
* Test resetToken
*/
public function testResetTokenUserActive()
{
$user = TableRegistry::get('CakeDC/Users.Users')->findByUsername('user-2')->first();
$result = $this->Behavior->resetToken('user-2', [
'ensureActive' => true,
'expiration' => 3600
]);
$this->assertEquals($user->id, $result->id);
}
/**
* Test changePassword
*/
public function testChangePassword()
{
$user = TableRegistry::get('CakeDC/Users.Users')->findByUsername('user-6')->first();
$user->current_password = '12345';
$user->password = 'new';
$user->password_confirmation = 'new';
$result = $this->Behavior->changePassword($user);
}
}