-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathConsentTest.php
More file actions
180 lines (149 loc) · 7.61 KB
/
ConsentTest.php
File metadata and controls
180 lines (149 loc) · 7.61 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
<?php
/**
* Copyright 2021 Stichting Kennisnet
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
use OpenConext\EngineBlock\Authentication\Value\ConsentType;
use OpenConext\EngineBlock\Authentication\Value\ConsentVersion;
use OpenConext\EngineBlock\Metadata\Entity\ServiceProvider;
use OpenConext\EngineBlock\Service\Consent\ConsentHashServiceInterface;
use PHPUnit\Framework\TestCase;
class EngineBlock_Corto_Model_ConsentTest extends TestCase
{
use MockeryPHPUnitIntegration;
private $consentDisabled;
private $consent;
private $consentService;
public function setUp(): void
{
$mockedResponse = Phake::mock('EngineBlock_Saml2_ResponseAnnotationDecorator');
Phake::when($mockedResponse)->getNameIdValue()->thenReturn('urn:collab:person:example.org:user1');
$this->consentService = Mockery::mock(ConsentHashServiceInterface::class);
$this->consentDisabled = new EngineBlock_Corto_Model_Consent(
"consent",
true,
$mockedResponse,
[],
false,
false,
$this->consentService
);
$this->consent = new EngineBlock_Corto_Model_Consent(
"consent",
true,
$mockedResponse,
[],
false,
true,
$this->consentService
);
}
public function testConsentDisabledDoesNotWriteToDatabase()
{
$serviceProvider = new ServiceProvider("service-provider-entity-id");
$this->consentService->shouldNotReceive('storeConsentHash');
$this->consentService->shouldNotReceive('retrieveConsentHash');
$this->consentService->shouldNotReceive('updateConsentHash');
$this->assertTrue($this->consentDisabled->explicitConsentWasGivenFor($serviceProvider)->given());
$this->assertTrue($this->consentDisabled->implicitConsentWasGivenFor($serviceProvider)->given());
$this->assertTrue($this->consentDisabled->giveExplicitConsentFor($serviceProvider));
$this->assertTrue($this->consentDisabled->giveImplicitConsentFor($serviceProvider));
}
public function testUpgradeAttributeHashSkippedWhenConsentDisabled()
{
$serviceProvider = new ServiceProvider("service-provider-entity-id");
$this->consentService->shouldNotReceive('retrieveConsentHash');
$this->consentService->shouldNotReceive('updateConsentHash');
$this->consentDisabled->upgradeAttributeHashFor($serviceProvider, ConsentType::Explicit, ConsentVersion::stable());
$this->consentDisabled->upgradeAttributeHashFor($serviceProvider, ConsentType::Implicit, ConsentVersion::stable());
}
public function testConsentWriteToDatabase()
{
$serviceProvider = new ServiceProvider("service-provider-entity-id");
$this->consentService->shouldReceive('getUnstableAttributesHash')->andReturn(sha1('unstable'));
$this->consentService->shouldReceive('getStableAttributesHash')->andReturn(sha1('stable'));
$this->consentService->shouldReceive('retrieveConsentHash')->andReturn(ConsentVersion::stable());
$this->consentService->shouldReceive('storeConsentHash')->andReturn(true);
$this->assertTrue($this->consent->explicitConsentWasGivenFor($serviceProvider)->given());
$this->assertTrue($this->consent->implicitConsentWasGivenFor($serviceProvider)->given());
$this->assertTrue($this->consent->giveExplicitConsentFor($serviceProvider));
$this->assertTrue($this->consent->giveImplicitConsentFor($serviceProvider));
}
public function testCountTotalConsent()
{
// Arrange
$this->consentService->shouldReceive('countTotalConsent')
->with('urn:collab:person:example.org:user1')
->once()
->andReturn(5);
// Act + Assert
$this->assertEquals(5, $this->consent->countTotalConsent());
}
public function testConsentUidFromAmPriorToConsentEnabled()
{
// When amPriorToConsentEnabled is true the consent UID must come from
// getOriginalResponse()->getCollabPersonId(), NOT from getNameIdValue().
$originalResponse = Phake::mock('EngineBlock_Saml2_ResponseAnnotationDecorator');
Phake::when($originalResponse)->getCollabPersonId()->thenReturn('urn:collab:person:example.org:user-am');
$mockedResponse = Phake::mock('EngineBlock_Saml2_ResponseAnnotationDecorator');
Phake::when($mockedResponse)->getOriginalResponse()->thenReturn($originalResponse);
$consentWithAmPrior = new EngineBlock_Corto_Model_Consent(
'consent',
true,
$mockedResponse,
[],
true, // amPriorToConsentEnabled = true
true,
$this->consentService
);
$serviceProvider = new ServiceProvider('service-provider-entity-id');
$this->consentService->shouldReceive('getUnstableAttributesHash')->andReturn(sha1('unstable'));
$this->consentService->shouldReceive('getStableAttributesHash')->andReturn(sha1('stable'));
$this->consentService->shouldReceive('retrieveConsentHash')->andReturn(ConsentVersion::stable());
// Act: trigger a code path that calls _getConsentUid()
$result = $consentWithAmPrior->explicitConsentWasGivenFor($serviceProvider);
// Assert: consent check succeeded and the AM-prior uid path was used
$this->assertTrue($result->given());
Phake::verify($originalResponse)->getCollabPersonId();
Phake::verify($mockedResponse, Phake::never())->getNameIdValue();
}
public function testNullNameIdReturnsNoConsentWithoutCallingRepository()
{
$mockedResponse = Phake::mock('EngineBlock_Saml2_ResponseAnnotationDecorator');
Phake::when($mockedResponse)->getNameIdValue()->thenReturn(null);
$consentWithNullUid = new EngineBlock_Corto_Model_Consent(
"consent",
true,
$mockedResponse,
[],
false,
true,
$this->consentService
);
$serviceProvider = new ServiceProvider("service-provider-entity-id");
// No DB calls should occur when the NameID is null
$this->consentService->shouldNotReceive('retrieveConsentHash');
$this->consentService->shouldNotReceive('storeConsentHash');
$this->consentService->shouldNotReceive('updateConsentHash');
// _hasStoredConsent returns notGiven() when UID is null -> consent methods return false
$this->assertFalse($consentWithNullUid->explicitConsentWasGivenFor($serviceProvider)->given());
$this->assertFalse($consentWithNullUid->implicitConsentWasGivenFor($serviceProvider)->given());
// giveConsent returns false when UID is null (_storeConsent returns early)
$this->assertFalse($consentWithNullUid->giveExplicitConsentFor($serviceProvider));
$this->assertFalse($consentWithNullUid->giveImplicitConsentFor($serviceProvider));
// upgradeAttributeHashFor should not throw when UID is null
$consentWithNullUid->upgradeAttributeHashFor($serviceProvider, ConsentType::Explicit, ConsentVersion::notGiven());
}
}