-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathExpiryDateTest.php
More file actions
197 lines (179 loc) · 5.83 KB
/
ExpiryDateTest.php
File metadata and controls
197 lines (179 loc) · 5.83 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
<?php
declare(strict_types=1);
namespace SimpleSAML\Test\Module\expirycheck\Auth\Process;
use PHPUnit\Framework\TestCase;
use SimpleSAML\Assert\AssertionFailedException;
use SimpleSAML\Error;
use SimpleSAML\Module\expirycheck\Auth\Process\ExpiryDate;
final class ExpiryDateTest extends TestCase
{
/** @var array valid configuration */
private static $config = [
'class' => 'expirycheck:ExpiryDate',
'netid_attr' => 'userPrincipalName',
'expirydate_attr' => 'accountExpires',
'convert_expirydate_to_unixtime' => false,
'warndaysbefore' => 60,
'date_format' => 'd.m.Y', # php date syntax
];
/** @var array minimal request */
private static $minRequest = [
'Source' => [
'entityid' => 'https://localhost/sp',
],
'Destination' => [
'entityid' => 'https://localhost/idp',
],
'Attributes' => [],
];
/**
*/
public function testInvalidWarndaysbefore(): void
{
$config = ['warndaysbefore' => 'X'];
$this->expectException(Error\Exception::class);
$this->expectExceptionMessage("Invalid value for number of days given to expirycheck::ExpiryDate filter.");
$filter = new ExpiryDate($config, null);
self::fail();
$filter->checkDate(1);
}
/**
*/
public function testInvalidNetidAttr(): void
{
$config = ['netid_attr' => 1];
$this->expectException(Error\Exception::class);
$msg = "Invalid attribute name given as eduPersonPrincipalName to expirycheck::ExpiryDate filter.";
$this->expectExceptionMessage($msg);
$filter = new ExpiryDate($config, null);
self::fail();
$filter->checkDate(1);
}
/**
*/
public function testInvalidExpirydateAttr(): void
{
$config = ['expirydate_attr' => 1];
$this->expectException(Error\Exception::class);
$msg = "Invalid attribute name given as schacExpiryDate to expirycheck::ExpiryDate filter.";
$this->expectExceptionMessage($msg);
$filter = new ExpiryDate($config, null);
self::fail();
$filter->checkDate(1);
}
/**
*/
public function testInvalidDateFormat(): void
{
$config = ['date_format' => 1];
$this->expectException(Error\Exception::class);
$msg = "Invalid date format given to expirycheck::ExpiryDate filter.";
$this->expectExceptionMessage($msg);
$filter = new ExpiryDate($config, null);
self::fail();
$filter->checkDate(1);
}
/**
*/
public function testInvalidConvertToUnixtime(): void
{
$config = ['convert_expirydate_to_unixtime' => 1];
$this->expectException(Error\Exception::class);
$msg = "Invalid value for convert_expirydate_to_unixtime given to expirycheck::ExpiryDate filter.";
$this->expectExceptionMessage($msg);
$filter = new ExpiryDate($config, null);
self::fail();
$filter->checkDate(1);
}
/**
*/
public function testValidConfiguration(): void
{
$filter = new ExpiryDate(self::$config, null);
self::assertFalse($filter->checkDate(1));
}
/**
*/
public function testWarningPast(): void
{
$filter = new ExpiryDate(self::$config, null);
$state = [];
$warning = $filter->shWarning($state, strtotime("10 September 2000"), 30);
self::assertFalse($warning);
}
/**
*/
public function testWarningNextWeek(): void
{
$filter = new ExpiryDate(self::$config, null);
$state = [];
$nextWeek = time() + (7 * 24 * 60 * 60);
$warning = $filter->shWarning($state, $nextWeek, 30);
self::assertTrue($warning);
}
/**
*/
public function testWarningDistantFuture(): void
{
$filter = new ExpiryDate(self::$config, null);
$state = [];
$nextCentury = strtotime("2150/06/30");
$warning = $filter->shWarning($state, $nextCentury, 30);
self::assertFalse($warning);
}
/**
*/
public function testProcessMissingAttributes(): void
{
$filter = new ExpiryDate(self::$config, null);
$state = [];
$this->expectException(AssertionFailedException::class);
$msg = "Expected the key \"Attributes\" to exist.";
$this->expectExceptionMessage($msg);
$filter->process($state);
}
/**
*/
public function testProcessMissingNetidAttr(): void
{
$filter = new ExpiryDate(self::$config, null);
$initialState = ['Attributes' => []];
$state = $initialState;
$this->expectException(AssertionFailedException::class);
$msg = "Expected the key \"userPrincipalName\" to exist.";
$this->expectExceptionMessage($msg);
$filter->process($state);
}
/**
*/
public function testProcessMissingExpirydate(): void
{
$filter = new ExpiryDate(self::$config, null);
$initialState = ['Attributes' =>
[
'userPrincipalName' => ['test'],
],
];
$state = $initialState;
$filter->process($state);
self::assertEquals($initialState, $state);
}
/**
*/
public function testProcessNoExpiry(): void
{
$skipReason = 'https://github.com/simplesamlphp/simplesamlphp-test-framework/issues/3#issuecomment-1836154792';
$this->markTestSkipped($skipReason);
$filter = new ExpiryDate(self::$config, null);
$initialState = ["Attributes" =>
[
'userPrincipalName' => ['test'],
'accountExpires' => ['20231228112510Z'],
],
];
$request = array_merge(self::$minRequest, $initialState);
$state = $initialState;
$filter->process($request);
self::assertEquals($initialState, $state);
}
}