1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Class RealMeSetupTaskTest |
5
|
|
|
* Setup to unit test the Setup task to make sure metadata is being generated correctly. |
6
|
|
|
*/ |
7
|
|
|
class RealMeSetupTaskTest extends SapphireTest |
|
|
|
|
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* Valid entity id's tobe used for context. |
11
|
|
|
* @var array |
12
|
|
|
*/ |
13
|
|
|
private static $validEntityIDs = array( |
14
|
|
|
RealMeService::ENV_MTS => "https://dev.your-website.govt.nz/p-realm/s-name", |
15
|
|
|
RealMeService::ENV_ITE => 'https://uat.your-website.govt.nz/p-realm/s-name', |
16
|
|
|
RealMeService::ENV_PROD => 'https://www.your-website.govt.nz/p-realm/s-name' |
17
|
|
|
); |
18
|
|
|
|
19
|
|
|
private static $authnEnvContexts = array( |
20
|
|
|
RealMeService::ENV_MTS => 'urn:nzl:govt:ict:stds:authn:deployment:GLS:SAML:2.0:ac:classes:LowStrength', |
21
|
|
|
RealMeService::ENV_ITE => 'urn:nzl:govt:ict:stds:authn:deployment:GLS:SAML:2.0:ac:classes:LowStrength', |
22
|
|
|
RealMeService::ENV_PROD => 'urn:nzl:govt:ict:stds:authn:deployment:GLS:SAML:2.0:ac:classes:LowStrength' |
23
|
|
|
); |
24
|
|
|
|
25
|
|
|
private static $metadata_assertion_urls = array( |
26
|
|
|
RealMeService::ENV_MTS => "https://dev.your-website.govt.nz/", |
27
|
|
|
RealMeService::ENV_ITE => "https://staging.your-website.govt.nz/", |
28
|
|
|
RealMeService::ENV_PROD => "https://www.your-website.govt.nz/" |
29
|
|
|
); |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* We need to make sure that if an invalid environment, it raises the correct errors for correction. |
33
|
|
|
* - invalid environment |
34
|
|
|
* - no environment |
35
|
|
|
* |
36
|
|
|
* We must also not raise an error if we pass a correctly configured environment. |
37
|
|
|
*/ |
38
|
|
|
public function testEnvironmentValidation() |
39
|
|
|
{ |
40
|
|
|
// Setup our objects for testing through reflection |
41
|
|
|
$realMeService = new RealMeService(); |
42
|
|
|
$realMeSetupTask = new RealMeSetupTask(); |
43
|
|
|
|
44
|
|
|
$errors = new ReflectionProperty($realMeSetupTask, 'errors'); |
45
|
|
|
$errors->setAccessible(true); |
46
|
|
|
|
47
|
|
|
$service = new ReflectionProperty($realMeSetupTask, 'service'); |
48
|
|
|
$service->setAccessible(true); |
49
|
|
|
$service->setValue($realMeSetupTask, $realMeService); |
50
|
|
|
|
51
|
|
|
// Make sure there's no errors to begin. |
52
|
|
|
$this->assertCount(0, $errors->getValue($realMeSetupTask)); |
|
|
|
|
53
|
|
|
|
54
|
|
|
// Test: Make an error |
55
|
|
|
$invalidEnv = "wrong-environment"; |
56
|
|
|
$validateEnvironments = new ReflectionMethod($realMeSetupTask, 'validateRealMeEnvironments'); |
57
|
|
|
$validateEnvironments->setAccessible(true); |
58
|
|
|
$validateEnvironments->invoke($realMeSetupTask, $invalidEnv); |
59
|
|
|
$this->assertCount(1, $errors->getValue($realMeSetupTask), "An invalid environment should raise an error"); |
|
|
|
|
60
|
|
|
|
61
|
|
|
// reset errors & Make sure there's no errors to begin. |
62
|
|
|
$errors->setValue($realMeSetupTask, array()); |
63
|
|
|
$this->assertCount(0, $errors->getValue($realMeSetupTask)); |
|
|
|
|
64
|
|
|
|
65
|
|
|
// Test: No environment passed |
66
|
|
|
$noEnvironment = null; |
67
|
|
|
$validateEnvironments = new ReflectionMethod($realMeSetupTask, 'validateRealMeEnvironments'); |
68
|
|
|
$validateEnvironments->setAccessible(true); |
69
|
|
|
$validateEnvironments->invoke($realMeSetupTask, $noEnvironment); |
70
|
|
|
$this->assertCount(1, $errors->getValue($realMeSetupTask), "Missing environment should raise an error"); |
|
|
|
|
71
|
|
|
|
72
|
|
|
// reset errors && Make sure there's no errors to begin. |
73
|
|
|
$errors->setValue($realMeSetupTask, array()); |
74
|
|
|
$this->assertCount(0, $errors->getValue($realMeSetupTask)); |
|
|
|
|
75
|
|
|
|
76
|
|
|
// Test: allowed environments pass without error. |
77
|
|
|
$reflectionMethod = new ReflectionMethod($realMeService, 'getAllowedRealMeEnvironments'); |
78
|
|
|
$reflectionMethod->setAccessible(true); |
79
|
|
|
foreach ($reflectionMethod->invoke($realMeService) as $validEnvironment) { |
80
|
|
|
$validateEnvironments->invoke($realMeSetupTask, $validEnvironment); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
// Make sure there's no errors, they should all be valid |
84
|
|
|
$this->assertCount(0, $errors->getValue($realMeSetupTask), "valid environments should not raise an error"); |
|
|
|
|
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* We need to make sure that there is an entity ID and that it's in the correct format for realme consumption |
89
|
|
|
* - It's present in the config |
90
|
|
|
* - it's not localhost |
91
|
|
|
* - it's not http (must be https) |
92
|
|
|
* - service name and privacy realm < 10 char. |
93
|
|
|
*/ |
94
|
|
|
public function testValidateEntityID() |
95
|
|
|
{ |
96
|
|
|
$realMeService = new RealMeService(); |
97
|
|
|
$realMeSetupTask = new RealMeSetupTask(); |
98
|
|
|
|
99
|
|
|
$errors = new ReflectionProperty($realMeSetupTask, 'errors'); |
100
|
|
|
$errors->setAccessible(true); |
101
|
|
|
|
102
|
|
|
$service = new ReflectionProperty($realMeSetupTask, 'service'); |
103
|
|
|
$service->setAccessible(true); |
104
|
|
|
$service->setValue($realMeSetupTask, $realMeService); |
105
|
|
|
|
106
|
|
|
// Make sure there's no errors to begin. |
107
|
|
|
$this->assertCount(0, $errors->getValue($realMeSetupTask)); |
|
|
|
|
108
|
|
|
|
109
|
|
|
// Test valid entityIds just in case they're different in this configuration. |
110
|
|
|
$config = Config::inst(); |
111
|
|
|
$config->update('RealMeService', 'entity_ids', self::$validEntityIDs); |
112
|
|
|
|
113
|
|
|
// validate our list of valid entity IDs; |
114
|
|
|
$validateEntityId = new ReflectionMethod($realMeSetupTask, 'validateEntityID'); |
115
|
|
|
$validateEntityId->setAccessible(true); |
116
|
|
|
$validateEntityId->invoke($realMeSetupTask); |
117
|
|
|
|
118
|
|
|
// valid entityID's shouldn't have any issues |
119
|
|
|
$this->assertCount(0, $errors->getValue($realMeSetupTask)); |
|
|
|
|
120
|
|
|
|
121
|
|
|
// TEST entityId missing. |
122
|
|
|
$entityIdList = self::$validEntityIDs; |
123
|
|
|
$entityIdList[RealMeService::ENV_MTS] = 'destroy-humans-with-incorrect-entity-ids'; |
124
|
|
|
$config->update('RealMeService', 'entity_ids', $entityIdList); |
125
|
|
|
$validateEntityId->invoke($realMeSetupTask); |
126
|
|
|
$this->assertCount(1, $errors->getValue($realMeSetupTask), 'validate entity id should fail for an invalid url'); |
|
|
|
|
127
|
|
|
|
128
|
|
|
// reset errors && Make sure there's no errors to begin. |
129
|
|
|
$errors->setValue($realMeSetupTask, array()); |
130
|
|
|
$this->assertCount(0, $errors->getValue($realMeSetupTask)); |
|
|
|
|
131
|
|
|
|
132
|
|
|
// TEST entityId localhost. |
133
|
|
|
$entityIdList = self::$validEntityIDs; |
134
|
|
|
$entityIdList[RealMeService::ENV_MTS] = 'https://localhost/'; |
135
|
|
|
$config->update('RealMeService', 'entity_ids', $entityIdList); |
136
|
|
|
$validateEntityId->invoke($realMeSetupTask); |
137
|
|
|
$this->assertCount(1, $errors->getValue($realMeSetupTask), 'validate entity id should fail for localhost'); |
|
|
|
|
138
|
|
|
|
139
|
|
|
$errors->setValue($realMeSetupTask, array()); |
140
|
|
|
$this->assertCount(0, $errors->getValue($realMeSetupTask)); |
|
|
|
|
141
|
|
|
|
142
|
|
|
// TEST entityId not http |
143
|
|
|
$entityIdList = self::$validEntityIDs; |
144
|
|
|
$entityIdList[RealMeService::ENV_MTS] = 'http://dev.realme-integration.govt.nz/p-realm/s-name'; |
145
|
|
|
$config->update('RealMeService', 'entity_ids', $entityIdList); |
146
|
|
|
$validateEntityId->invoke($realMeSetupTask); |
147
|
|
|
$this->assertCount(1, $errors->getValue($realMeSetupTask), 'validate entity id should fail for http'); |
|
|
|
|
148
|
|
|
|
149
|
|
|
$errors->setValue($realMeSetupTask, array()); |
150
|
|
|
$this->assertCount(0, $errors->getValue($realMeSetupTask)); |
|
|
|
|
151
|
|
|
|
152
|
|
|
// TEST privacy realm /service name missing |
153
|
|
|
$entityIdList = self::$validEntityIDs; |
154
|
|
|
$entityIdList[RealMeService::ENV_MTS] = 'https://dev.realme-integration.govt.nz/'; |
155
|
|
|
$config->update('RealMeService', 'entity_ids', $entityIdList); |
156
|
|
|
$validateEntityId->invoke($realMeSetupTask); |
157
|
|
|
$this->assertCount(2, |
|
|
|
|
158
|
|
|
$errors->getValue($realMeSetupTask), |
159
|
|
|
'validate entity id should fail for missing service name and privacy realm' |
160
|
|
|
); |
161
|
|
|
|
162
|
|
|
$errors->setValue($realMeSetupTask, array()); |
163
|
|
|
$this->assertCount(0, $errors->getValue($realMeSetupTask)); |
|
|
|
|
164
|
|
|
|
165
|
|
|
// TEST privacy realm |
166
|
|
|
// "https://www.domain.govt.nz/<privacy-realm>/<service-name>" |
167
|
|
|
$entityIdList = self::$validEntityIDs; |
168
|
|
|
$entityIdList[RealMeService::ENV_MTS] = 'https://dev.realme-integration.govt.nz/s-name/privacy-realm-is-too-big'; |
169
|
|
|
$config->update('RealMeService', 'entity_ids', $entityIdList); |
170
|
|
|
$validateEntityId->invoke($realMeSetupTask); |
171
|
|
|
$this->assertCount(1, $errors->getValue($realMeSetupTask), 'validate entity id should fail for privacy-realm-is-too-big'); |
|
|
|
|
172
|
|
|
|
173
|
|
|
$errors->setValue($realMeSetupTask, array()); |
174
|
|
|
$this->assertCount(0, $errors->getValue($realMeSetupTask)); |
|
|
|
|
175
|
|
|
|
176
|
|
|
// "https://www.domain.govt.nz/<privacy-realm>/<service-name>" |
177
|
|
|
$entityIdList = self::$validEntityIDs; |
178
|
|
|
$entityIdList[RealMeService::ENV_MTS] = 'https://dev.realme-integration.govt.nz/s-name'; |
179
|
|
|
$config->update('RealMeService', 'entity_ids', $entityIdList); |
180
|
|
|
$validateEntityId->invoke($realMeSetupTask); |
181
|
|
|
$this->assertCount(1, $errors->getValue($realMeSetupTask), 'validate entity id should fail if privacy realm is missing'); |
|
|
|
|
182
|
|
|
|
183
|
|
|
$errors->setValue($realMeSetupTask, array()); |
184
|
|
|
$this->assertCount(0, $errors->getValue($realMeSetupTask)); |
|
|
|
|
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* We require an authn context for each environment to determine how secure to ask realme to validate. |
190
|
|
|
* - it should be present for each environment, and one of four pre-determined authncontexts. |
191
|
|
|
*/ |
192
|
|
|
public function testValidateAuthNContext() |
193
|
|
|
{ |
194
|
|
|
$realMeService = new RealMeService(); |
195
|
|
|
$realMeSetupTask = new RealMeSetupTask(); |
196
|
|
|
|
197
|
|
|
$errors = new ReflectionProperty($realMeSetupTask, 'errors'); |
198
|
|
|
$errors->setAccessible(true); |
199
|
|
|
|
200
|
|
|
$service = new ReflectionProperty($realMeSetupTask, 'service'); |
201
|
|
|
$service->setAccessible(true); |
202
|
|
|
$service->setValue($realMeSetupTask, $realMeService); |
203
|
|
|
|
204
|
|
|
// Make sure there's no errors to begin. |
205
|
|
|
$this->assertCount(0, $errors->getValue($realMeSetupTask)); |
|
|
|
|
206
|
|
|
|
207
|
|
|
// Test valid authnContexts just in case they're different in this configuration. |
208
|
|
|
$config = Config::inst(); |
209
|
|
|
$config->update('RealMeService', 'authn_contexts', self::$authnEnvContexts); |
210
|
|
|
|
211
|
|
|
// validate our list of valid entity IDs; |
212
|
|
|
$validateAuthNContext = new ReflectionMethod($realMeSetupTask, 'validateAuthNContext'); |
213
|
|
|
$validateAuthNContext->setAccessible(true); |
214
|
|
|
$validateAuthNContext->invoke($realMeSetupTask); |
215
|
|
|
$this->assertCount(0, $errors->getValue($realMeSetupTask)); |
|
|
|
|
216
|
|
|
|
217
|
|
|
$invalidAuthNContextList = self::$authnEnvContexts; |
218
|
|
|
$invalidAuthNContextList[RealMeService::ENV_MTS] = 'im-an-invalid-context'; |
219
|
|
|
$config->update('RealMeService', 'authn_contexts', $invalidAuthNContextList); |
220
|
|
|
|
221
|
|
|
$validateAuthNContext->invoke($realMeSetupTask); |
222
|
|
|
$this->assertCount(1, $errors->getValue($realMeSetupTask), "The authncontext validation should fail if invalid."); |
|
|
|
|
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* Check the consumer assertion url is being validated from config correctly. |
227
|
|
|
* - ensure it's present |
228
|
|
|
* - ensure it's https |
229
|
|
|
* - ensure it's a valid URL |
230
|
|
|
* - ensure it's not localhost. |
231
|
|
|
*/ |
232
|
|
|
public function testValidateConsumerAssertionURL() |
233
|
|
|
{ |
234
|
|
|
$realMeService = new RealMeService(); |
235
|
|
|
$realMeSetupTask = new RealMeSetupTask(); |
236
|
|
|
|
237
|
|
|
$errors = new ReflectionProperty($realMeSetupTask, 'errors'); |
238
|
|
|
$errors->setAccessible(true); |
239
|
|
|
|
240
|
|
|
$service = new ReflectionProperty($realMeSetupTask, 'service'); |
241
|
|
|
$service->setAccessible(true); |
242
|
|
|
$service->setValue($realMeSetupTask, $realMeService); |
243
|
|
|
|
244
|
|
|
// Make sure there's no errors to begin. |
245
|
|
|
$this->assertCount(0, $errors->getValue($realMeSetupTask)); |
|
|
|
|
246
|
|
|
|
247
|
|
|
// Test valid entityIds just in case they're different in this configuration. |
248
|
|
|
$config = Config::inst(); |
249
|
|
|
$config->update('RealMeService', 'metadata_assertion_service_domains', self::$metadata_assertion_urls); |
250
|
|
|
|
251
|
|
|
// validate our list of valid entity IDs; |
252
|
|
|
$validateAuthNContext = new ReflectionMethod($realMeSetupTask, 'validateConsumerAssertionURL'); |
253
|
|
|
$validateAuthNContext->setAccessible(true); |
254
|
|
|
$validateAuthNContext->invoke($realMeSetupTask, RealMeService::ENV_MTS); |
255
|
|
|
$this->assertCount(0, $errors->getValue($realMeSetupTask)); |
|
|
|
|
256
|
|
|
|
257
|
|
|
// Test an invalid metadata assertion URL. |
258
|
|
|
$metadataAssertionUrls = self::$metadata_assertion_urls; |
259
|
|
|
$metadataAssertionUrls[RealMeService::ENV_MTS] = 'invalid-url'; |
260
|
|
|
$config->update('RealMeService', 'metadata_assertion_service_domains', $metadataAssertionUrls); |
261
|
|
|
|
262
|
|
|
$validateAuthNContext->invoke($realMeSetupTask, RealMeService::ENV_MTS); |
263
|
|
|
$this->assertCount(1, $errors->getValue($realMeSetupTask), "The validation should fail for an invalid URL"); |
|
|
|
|
264
|
|
|
|
265
|
|
|
// Make sure there's no errors to begin. |
266
|
|
|
$errors->setValue($realMeSetupTask, array()); |
267
|
|
|
$this->assertCount(0, $errors->getValue($realMeSetupTask)); |
|
|
|
|
268
|
|
|
|
269
|
|
|
// Test should fail for non HTTPs |
270
|
|
|
$metadataAssertionUrls = self::$metadata_assertion_urls; |
271
|
|
|
$metadataAssertionUrls[RealMeService::ENV_MTS] = 'http://my-broken-url.govt.nz'; |
272
|
|
|
$config->update('RealMeService', 'metadata_assertion_service_domains', $metadataAssertionUrls); |
273
|
|
|
|
274
|
|
|
$validateAuthNContext->invoke($realMeSetupTask, RealMeService::ENV_MTS); |
275
|
|
|
$this->assertCount(1, $errors->getValue($realMeSetupTask), "The validation should fail for non-HTTPs"); |
|
|
|
|
276
|
|
|
|
277
|
|
|
// Make sure there's no errors to begin. |
278
|
|
|
$errors->setValue($realMeSetupTask, array()); |
279
|
|
|
$this->assertCount(0, $errors->getValue($realMeSetupTask)); |
|
|
|
|
280
|
|
|
|
281
|
|
|
// Test should fail for localhost |
282
|
|
|
$metadataAssertionUrls = self::$metadata_assertion_urls; |
283
|
|
|
$metadataAssertionUrls[RealMeService::ENV_MTS] = 'https://localhost'; |
284
|
|
|
$config->update('RealMeService', 'metadata_assertion_service_domains', $metadataAssertionUrls); |
285
|
|
|
|
286
|
|
|
$validateAuthNContext->invoke($realMeSetupTask, RealMeService::ENV_MTS); |
287
|
|
|
$this->assertCount(1, $errors->getValue($realMeSetupTask), "The validation should fail for non-HTTPs"); |
|
|
|
|
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
/** |
291
|
|
|
* Ensure that setting the RealMeSetupTask template_config_dir config value |
292
|
|
|
* can adjust the path that the task looks in for its templates. |
293
|
|
|
* - ensure the default works |
294
|
|
|
* - ensure configuring a bad directory name falls back to the default |
295
|
|
|
* - ensure that configuring a good value uses the configured directory |
296
|
|
|
*/ |
297
|
|
|
public function testConfigurationTemplateDir() |
298
|
|
|
{ |
299
|
|
|
$realMeSetupTask = new RealMeSetupTask(); |
300
|
|
|
|
301
|
|
|
$getConfigurationTemplateDirMethod = new ReflectionMethod('RealMeSetupTask', 'getConfigurationTemplateDir'); |
302
|
|
|
$getConfigurationTemplateDirMethod->setAccessible(true); |
303
|
|
|
|
304
|
|
|
$config = Config::inst(); |
305
|
|
|
|
306
|
|
|
$config->update('RealMeSetupTask', 'template_config_dir', ''); |
307
|
|
|
$this->assertEquals( |
|
|
|
|
308
|
|
|
BASE_PATH . DIRECTORY_SEPARATOR . REALME_MODULE_PATH . '/templates/simplesaml-configuration', |
309
|
|
|
$getConfigurationTemplateDirMethod->invoke($realMeSetupTask), |
310
|
|
|
'Using no configuration for template_config_dir should use the default template directory.' |
311
|
|
|
); |
312
|
|
|
|
313
|
|
|
$config->update('RealMeSetupTask', 'template_config_dir', 'xyzzy'); |
314
|
|
|
$this->assertEquals( |
|
|
|
|
315
|
|
|
BASE_PATH . DIRECTORY_SEPARATOR . REALME_MODULE_PATH . '/templates/simplesaml-configuration', |
316
|
|
|
$getConfigurationTemplateDirMethod->invoke($realMeSetupTask), |
317
|
|
|
'Configuring a directory that does not exist should use the default template directory.' |
318
|
|
|
); |
319
|
|
|
|
320
|
|
|
$config->update('RealMeSetupTask', 'template_config_dir', REALME_MODULE_PATH); |
321
|
|
|
$this->assertEquals( |
|
|
|
|
322
|
|
|
BASE_PATH . DIRECTORY_SEPARATOR . REALME_MODULE_PATH, // doesn't contain templates, but does exist |
323
|
|
|
$getConfigurationTemplateDirMethod->invoke($realMeSetupTask), |
324
|
|
|
'Configuring a directory that exists should use the configured template directory.' |
325
|
|
|
); |
326
|
|
|
} |
327
|
|
|
} |
328
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.