-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAuthenticatorTest.php
More file actions
221 lines (195 loc) · 8.39 KB
/
AuthenticatorTest.php
File metadata and controls
221 lines (195 loc) · 8.39 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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
<?php
/**
* Definition of AuthenticatorTest.php
*
* @copyright 2015-today Justso GmbH
* @author j.schirrmacher@justso.de
*/
namespace justso\justauth;
use justso\justapi\NotFoundException;
use justso\justapi\testutil\ServiceTestBase;
use justso\justapi\testutil\TestEnvironment;
class AuthenticatorTest extends ServiceTestBase
{
/**
* Provides data for testRegister()
*
* @return array
* @codeCoverageIgnore
*/
public function provideRegisterParams()
{
return[
[false, false, false], // don't auto-register new users
//[false, false, true], // if new users aren't registered automatically, there is no need to log them in
//[false, true, false], // or send an activation link. Only pre-defined users are possible.
//[false, true, true], //
[true, false, false], // New users aren't logged in automatically, they have to login manually
[true, false, true], // New users must activate their account first
[true, true, false], // New users are logged in automatically, activation not necessary
[true, true, true], // New users are logged in automatically, later logins require activation
];
}
/**
* Check registering of new users.
*
* @param bool $autoRegister
* @param bool $autoLogin
* @dataProvider provideRegisterParams
*/
public function testRegister($autoRegister, $autoLogin = false, $requireActivation = false)
{
$env = $this->setupEnvironment($autoRegister, $autoLogin, $requireActivation);
$user = $this->mockInterface('justso\\justauth', 'UserInterface', $env);
if ($autoRegister) {
$user->expects($this->any())->method('getId')->willReturn(123);
if ($requireActivation) {
$user->expects($this->once())->method('setToken');
}
if (!$autoLogin) {
$user->expects($this->any())->method('getToken')->willReturn('token');
}
$user->expects($this->once())->method('setFromRequest');
} else {
$user->expects($this->never())->method('setFromRequest');
}
$this->checkActivationLink($requireActivation, $env, $user);
$repo = $this->mockInterface('justso\\justauth', 'UserRepositoryInterface', $env);
$repo->expects($this->once())->method('getByEMail')->willThrowException(new NotFoundException());
$auth = new Authenticator($env);
$auth->auth();
$this->assertSame($autoLogin, $auth->isAuth());
}
/**
* Check 'normal' login with email and password
*/
public function testLoginWithPassword()
{
$env = $this->setupEnvironment(false, false, false);
$env->getRequestHelper()->fillWithData(['email' => 'test@justso.de', 'password' => 'test123']);
$user = $this->mockInterface('justso\\justauth', 'UserInterface', $env);
$user->expects($this->any())->method('getId')->willReturn(123);
$user->expects($this->once())->method('checkPassword')->with('test123')->willReturn(true);
$user->expects($this->never())->method('setFromRequest');
$this->mockUserRepository($env, $user);
$this->checkActivationLink(false, $env, $user);
$authenticator = new Authenticator($env);
$authenticator->auth();
$this->assertSame(123, $authenticator->getUserId());
$this->assertFalse($authenticator->isNewUser());
}
public function testLoginWithWrongPassword()
{
$env = $this->setupEnvironment(false, false, false);
$env->getRequestHelper()->fillWithData(['email' => 'test@justso.de', 'password' => 'wrong-password']);
$user = $this->mockInterface('justso\\justauth', 'UserInterface', $env);
$user->expects($this->once())->method('checkPassword')->with('wrong-password')->willReturn(false);
$this->mockUserRepository($env, $user);
$this->checkActivationLink(false, $env, $user);
$authenticator = new Authenticator($env);
$authenticator->auth();
$this->assertNull($authenticator->getUserId());
}
/**
* Mock LoginNotificatorInterface
*
* @param bool $needsActivation
* @param TestEnvironment $env
* @param UserInterface $user
*/
private function checkActivationLink($needsActivation, TestEnvironment $env, UserInterface $user)
{
$notiMock = $this->mockInterface('justso\\justauth', 'LoginNotificatorInterface', $env);
if ($needsActivation) {
$notiMock->expects($this->once())->method('sendActivation')->with($user);
} else {
$notiMock->expects($this->never())->method('sendActivation');
}
}
/**
* Check trying to login with an unknown combination of userid and password
*/
public function testLoginWithUnknownUser()
{
$env = $this->setupEnvironment(false, false, false);
$this->mockUserRepository($env);
$authenticator = new Authenticator($env);
$authenticator->auth();
$this->assertNull($authenticator->getUserId());
}
public function testActivation()
{
$code = 'this-is-the-code';
$env = $this->setupEnvironment(false, false, true);
$user = $this->mockInterface('justso\\justauth', 'UserInterface', $env);
$user->expects($this->once())->method('getDestination')->willReturn('http://example.com');
$user->expects($this->once())->method('setToken')->with(null);
$user->expects($this->once())->method('setDestination')->with(null);
$user->expects($this->once())->method('setActive')->with(true);
$repo = $this->mockInterface('justso\\justauth', 'UserRepositoryInterface', $env);
$repo->expects($this->once())->method('getByAccessCode')->with($code)->willReturn($user);
$repo->expects($this->once())->method('persist')->with($user);
$activator = $this->mockInterface('justso\\justauth', 'LoginNotificatorInterface', $env);
$activator->expects($this->once())->method('activateUser')->with($user);
$session = $this->createMock('justso\\justauth\\Session');
$session->expects($this->once())->method('loginUser')->with($user);
$env->setDICEntry('Auth.Session', $session);
$authenticator = new Authenticator($env);
$authenticator->activate($code);
}
public function testLogout()
{
$env = $this->setupEnvironment(false, false, true);
$user = $this->mockInterface('justso\\justauth', 'UserInterface', $env);
$user->expects($this->any())->method('getId')->willReturn(123);
$user->expects($this->never())->method('setFromRequest');
$this->mockUserRepository($env, $user);
$this->checkActivationLink(true, $env, $user);
$authenticator = new Authenticator($env);
$authenticator->auth();
$authenticator->logout();
$this->assertNull($authenticator->getUserId());
}
/**
* Setup test environment
*
* @param $autoRegister
* @param $autoLogin
* @param $requireActivation
* @return \justso\justapi\testutil\TestEnvironment
*/
private function setupEnvironment($autoRegister, $autoLogin, $requireActivation)
{
$env = $this->createTestEnvironment(['email' => 'test@justso.de']);
$config = [
'environments' => [
'test' => [
'approot' => '/my/approot',
'apiurl' => 'http://test.com/api',
]
],
'auth' => [
'auto-register' => $autoRegister,
'needs-activation' => $requireActivation,
'login-new-users' => $autoLogin
]
];
$env->getBootstrap()->setTestConfiguration('/my/approot', $config);
$env->setDICEntry('Auth.Session', 'justso\\justauth\\Session');
return $env;
}
/**
* @param $env
* @param $user
*/
private function mockUserRepository(TestEnvironment $env, UserInterface $user = null)
{
$repo = $this->mockInterface('justso\\justauth', 'UserRepositoryInterface', $env);
$mocker = $repo->expects($this->once())->method('getByEmail')->with('test@justso.de');
if ($user === null) {
$mocker->willThrowException(new NotFoundException());
} else {
$mocker->willReturn($user);
}
}
}