forked from nette/security
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUser.authentication.phpt
More file actions
119 lines (87 loc) · 2.62 KB
/
Copy pathUser.authentication.phpt
File metadata and controls
119 lines (87 loc) · 2.62 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
<?php
/**
* Test: Nette\Security\User authentication.
*/
declare(strict_types=1);
use Nette\Security\IIdentity;
use Nette\Security\SimpleIdentity;
use Tester\Assert;
require __DIR__ . '/../bootstrap.php';
require __DIR__ . '/MockUserStorage.php';
// Setup environment
$_COOKIE = [];
ob_start();
class Authenticator implements Nette\Security\Authenticator
{
public function authenticate(string $username, string $password): IIdentity
{
if ($username !== 'john') {
throw new Nette\Security\AuthenticationException('Unknown user', self::IdentityNotFound);
} elseif ($password !== 'xxx') {
throw new Nette\Security\AuthenticationException('Password not match', self::InvalidCredential);
} else {
return new SimpleIdentity('John Doe', 'admin');
}
}
}
$user = new Nette\Security\User(new MockUserStorage);
$counter = (object) [
'login' => 0,
'logout' => 0,
];
$user->onLoggedIn[] = function () use ($counter) {
$counter->login++;
};
$user->onLoggedOut[] = function () use ($counter) {
$counter->logout++;
};
Assert::false($user->isLoggedIn());
Assert::null($user->getIdentity());
Assert::null($user->getId());
// authenticate
Assert::exception(
fn() => $user->login('jane', ''),
Nette\InvalidStateException::class,
'Authenticator has not been set.',
);
$handler = new Authenticator;
$user->setAuthenticator($handler);
Assert::exception(
fn() => $user->login('jane', ''),
Nette\Security\AuthenticationException::class,
'Unknown user',
);
Assert::exception(
fn() => $user->login('john', ''),
Nette\Security\AuthenticationException::class,
'Password not match',
);
// login as john#2
$user->login('john', 'xxx');
Assert::same(1, $counter->login);
Assert::true($user->isLoggedIn());
Assert::equal(new SimpleIdentity('John Doe', 'admin'), $user->getIdentity());
Assert::same('John Doe', $user->getId());
// login as john#3
$user->logout(true);
Assert::same(1, $counter->logout);
$user->login(new SimpleIdentity('John Doe', 'admin'));
Assert::same(2, $counter->login);
Assert::true($user->isLoggedIn());
Assert::equal(new SimpleIdentity('John Doe', 'admin'), $user->getIdentity());
// log out
// logging out...
$user->logout(false);
Assert::same(2, $counter->logout);
Assert::false($user->isLoggedIn());
Assert::equal(new SimpleIdentity('John Doe', 'admin'), $user->getIdentity());
// logging out and clearing identity...
$user->logout(true);
Assert::same(2, $counter->logout); // not logged in -> logout event not triggered
Assert::false($user->isLoggedIn());
Assert::null($user->getIdentity());
// namespace
// login as john#2?
$user->login('john', 'xxx');
Assert::same(3, $counter->login);
Assert::true($user->isLoggedIn());