1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* The MIT License (MIT) |
7
|
|
|
* |
8
|
|
|
* Copyright (c) 2014-2018 Spomky-Labs |
9
|
|
|
* |
10
|
|
|
* This software may be modified and distributed under the terms |
11
|
|
|
* of the MIT license. See the LICENSE file for details. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace OAuth2Framework\Component\AuthorizationEndpoint\Tests\UserAccount; |
15
|
|
|
|
16
|
|
|
use OAuth2Framework\Component\AuthorizationEndpoint\Authorization; |
17
|
|
|
use OAuth2Framework\Component\AuthorizationEndpoint\Exception\RedirectToLoginPageException; |
18
|
|
|
use OAuth2Framework\Component\AuthorizationEndpoint\UserAccount\MaxAgeParameterAccountChecker; |
19
|
|
|
use OAuth2Framework\Component\Core\Client\Client; |
20
|
|
|
use OAuth2Framework\Component\Core\UserAccount\UserAccount; |
21
|
|
|
use PHPUnit\Framework\TestCase; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @group UserAccountChecker |
25
|
|
|
* @group MaxAgeParameterCheckerAccountChecker |
26
|
|
|
*/ |
27
|
|
|
class MaxAgeParameterAccountCheckerTest extends TestCase |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @test |
31
|
|
|
*/ |
32
|
|
|
public function theUserAccountIsNotAvailableThenAnExceptionIsThrown() |
33
|
|
|
{ |
34
|
|
|
$client = $this->prophesize(Client::class); |
35
|
|
|
|
36
|
|
|
$authorization = $this->prophesize(Authorization::class); |
37
|
|
|
$authorization->hasQueryParam('max_age')->willReturn(true); |
38
|
|
|
$authorization->getQueryParam('max_age')->willReturn(3600); |
39
|
|
|
$authorization->getUserAccount()->willReturn(null); |
40
|
|
|
$authorization->getClient()->willReturn($client->reveal()); |
41
|
|
|
$checker = new MaxAgeParameterAccountChecker(); |
42
|
|
|
|
43
|
|
|
try { |
44
|
|
|
$checker->check($authorization->reveal()); |
45
|
|
|
$this->fail('The expected exception has not been thrown.'); |
46
|
|
|
} catch (RedirectToLoginPageException $e) { |
47
|
|
|
self::assertTrue(true); |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @test |
53
|
|
|
*/ |
54
|
|
|
public function thereIsNoMaxAgeConstraintThenTheCheckSucceeded() |
55
|
|
|
{ |
56
|
|
|
$client = $this->prophesize(Client::class); |
57
|
|
|
$client->has('default_max_age')->willReturn(false); |
58
|
|
|
|
59
|
|
|
$userAccount = $this->prophesize(UserAccount::class); |
60
|
|
|
|
61
|
|
|
$authorization = $this->prophesize(Authorization::class); |
62
|
|
|
$authorization->hasQueryParam('max_age')->willReturn(false); |
63
|
|
|
$authorization->getUserAccount()->willReturn($userAccount->reveal()); |
64
|
|
|
$authorization->getClient()->willReturn($client->reveal()); |
65
|
|
|
$checker = new MaxAgeParameterAccountChecker(); |
66
|
|
|
|
67
|
|
|
$checker->check($authorization->reveal()); |
68
|
|
|
self::assertTrue(true); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @test |
73
|
|
|
*/ |
74
|
|
|
public function thereIsConstraintFromTheClientThatIsSatisfied() |
75
|
|
|
{ |
76
|
|
|
$client = $this->prophesize(Client::class); |
77
|
|
|
$client->has('default_max_age')->willReturn(true); |
78
|
|
|
$client->get('default_max_age')->willReturn(3600); |
79
|
|
|
|
80
|
|
|
$userAccount = $this->prophesize(UserAccount::class); |
81
|
|
|
$userAccount->getLastLoginAt()->willReturn(time() - 100); |
82
|
|
|
|
83
|
|
|
$authorization = $this->prophesize(Authorization::class); |
84
|
|
|
$authorization->hasQueryParam('max_age')->willReturn(false); |
85
|
|
|
$authorization->getUserAccount()->willReturn($userAccount->reveal()); |
86
|
|
|
$authorization->getClient()->willReturn($client->reveal()); |
87
|
|
|
$checker = new MaxAgeParameterAccountChecker(); |
88
|
|
|
|
89
|
|
|
$checker->check($authorization->reveal()); |
90
|
|
|
self::assertTrue(true); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @test |
95
|
|
|
*/ |
96
|
|
|
public function thereIsConstraintFromTheAuthorizationThatIsSatisfied() |
97
|
|
|
{ |
98
|
|
|
$client = $this->prophesize(Client::class); |
99
|
|
|
$client->has('default_max_age')->willReturn(false); |
100
|
|
|
|
101
|
|
|
$userAccount = $this->prophesize(UserAccount::class); |
102
|
|
|
$userAccount->getLastLoginAt()->willReturn(time() - 100); |
103
|
|
|
|
104
|
|
|
$authorization = $this->prophesize(Authorization::class); |
105
|
|
|
$authorization->hasQueryParam('max_age')->willReturn(true); |
106
|
|
|
$authorization->getQueryParam('max_age')->willReturn(3600); |
107
|
|
|
$authorization->getUserAccount()->willReturn($userAccount->reveal()); |
108
|
|
|
$authorization->getClient()->willReturn($client->reveal()); |
109
|
|
|
$checker = new MaxAgeParameterAccountChecker(); |
110
|
|
|
|
111
|
|
|
$checker->check($authorization->reveal()); |
112
|
|
|
self::assertTrue(true); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @test |
117
|
|
|
*/ |
118
|
|
|
public function thereIsAConstraintButTheUserNeverLoggedIn() |
119
|
|
|
{ |
120
|
|
|
$client = $this->prophesize(Client::class); |
121
|
|
|
$client->has('default_max_age')->willReturn(false); |
122
|
|
|
|
123
|
|
|
$userAccount = $this->prophesize(UserAccount::class); |
124
|
|
|
$userAccount->getLastLoginAt()->willReturn(null); |
125
|
|
|
|
126
|
|
|
$authorization = $this->prophesize(Authorization::class); |
127
|
|
|
$authorization->hasQueryParam('max_age')->willReturn(true); |
128
|
|
|
$authorization->getQueryParam('max_age')->willReturn(3600); |
129
|
|
|
$authorization->getUserAccount()->willReturn($userAccount->reveal()); |
130
|
|
|
$authorization->getClient()->willReturn($client->reveal()); |
131
|
|
|
$checker = new MaxAgeParameterAccountChecker(); |
132
|
|
|
|
133
|
|
|
try { |
134
|
|
|
$checker->check($authorization->reveal()); |
135
|
|
|
$this->fail('The expected exception has not been thrown.'); |
136
|
|
|
} catch (RedirectToLoginPageException $e) { |
137
|
|
|
self::assertTrue(true); |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @test |
143
|
|
|
*/ |
144
|
|
|
public function thereIsAConstraintThatIsNotSatisfied() |
145
|
|
|
{ |
146
|
|
|
$client = $this->prophesize(Client::class); |
147
|
|
|
$client->has('default_max_age')->willReturn(false); |
148
|
|
|
|
149
|
|
|
$userAccount = $this->prophesize(UserAccount::class); |
150
|
|
|
$userAccount->getLastLoginAt()->willReturn(time() - 10000); |
151
|
|
|
|
152
|
|
|
$authorization = $this->prophesize(Authorization::class); |
153
|
|
|
$authorization->hasQueryParam('max_age')->willReturn(true); |
154
|
|
|
$authorization->getQueryParam('max_age')->willReturn(3600); |
155
|
|
|
$authorization->getUserAccount()->willReturn($userAccount->reveal()); |
156
|
|
|
$authorization->getClient()->willReturn($client->reveal()); |
157
|
|
|
$checker = new MaxAgeParameterAccountChecker(); |
158
|
|
|
|
159
|
|
|
try { |
160
|
|
|
$checker->check($authorization->reveal()); |
161
|
|
|
$this->fail('The expected exception has not been thrown.'); |
162
|
|
|
} catch (RedirectToLoginPageException $e) { |
163
|
|
|
self::assertTrue(true); |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|