Skip to content

Commit 501c554

Browse files
test(email): add DataProvider coverage for throwIfIsAuthenticatedWithDifferentAccount
Six cases covering all branches of the guard: - not authenticated → pass - authenticated email matches signer email → pass - authenticated user has no email set → throw - authenticated user has different email → throw - wrong email but token in progress (code set, not yet identified) → pass - wrong email and token already used (identified) → throw Signed-off-by: Vitor Mattos <1079143+vitormattos@users.noreply.github.com>
1 parent a8812bd commit 501c554

1 file changed

Lines changed: 82 additions & 0 deletions

File tree

tests/php/Unit/Service/IdentifyMethod/EmailTest.php

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,4 +136,86 @@ public static function providerThrowIfNeedToCreateAccount(): array {
136136
'method_enabled_user_exists_not_logged_in' => [false, true, true, false, true, false, 'User already exists. Please login.'],
137137
];
138138
}
139+
140+
#[DataProvider('providerThrowIfIsAuthenticatedWithDifferentAccount')]
141+
public function testThrowIfIsAuthenticatedWithDifferentAccount(
142+
?string $userEmail,
143+
string $signerEmail,
144+
?string $code,
145+
bool $identified,
146+
string $errorMessage = '',
147+
): void {
148+
if ($errorMessage) {
149+
$this->expectException(LibresignException::class);
150+
$this->expectExceptionMessageMatches("/.*$errorMessage.*/");
151+
} else {
152+
$this->expectNotToPerformAssertions();
153+
}
154+
155+
if ($userEmail !== null) {
156+
$user = $this->createMock(IUser::class);
157+
$user->method('getEMailAddress')->willReturn($userEmail);
158+
$this->userSession->method('getUser')->willReturn($user);
159+
} else {
160+
$this->userSession->method('getUser')->willReturn(null);
161+
}
162+
163+
$identifyMethod = $this->getClass();
164+
$identifyMethod->getEntity()->setIdentifierValue($signerEmail);
165+
if ($code !== null) {
166+
$identifyMethod->getEntity()->setCode($code);
167+
}
168+
if ($identified) {
169+
$identifyMethod->getEntity()->setIdentifiedAtDate(new \DateTime());
170+
}
171+
172+
self::invokePrivate($identifyMethod, 'throwIfIsAuthenticatedWithDifferentAccount');
173+
}
174+
175+
public static function providerThrowIfIsAuthenticatedWithDifferentAccount(): array {
176+
return [
177+
'not_authenticated' => [
178+
'userEmail' => null,
179+
'signerEmail' => 'signer@example.com',
180+
'code' => null,
181+
'identified' => false,
182+
'errorMessage' => '',
183+
],
184+
'authenticated_email_matches' => [
185+
'userEmail' => 'signer@example.com',
186+
'signerEmail' => 'signer@example.com',
187+
'code' => null,
188+
'identified' => false,
189+
'errorMessage' => '',
190+
],
191+
'authenticated_no_email_on_user' => [
192+
'userEmail' => '',
193+
'signerEmail' => 'signer@example.com',
194+
'code' => null,
195+
'identified' => false,
196+
'errorMessage' => 'This document is not yours',
197+
],
198+
'authenticated_wrong_email' => [
199+
'userEmail' => 'admin@example.com',
200+
'signerEmail' => 'signer@example.com',
201+
'code' => null,
202+
'identified' => false,
203+
'errorMessage' => 'This document is not yours',
204+
],
205+
'authenticated_wrong_email_token_in_progress' => [
206+
'userEmail' => 'admin@example.com',
207+
'signerEmail' => 'signer@example.com',
208+
'code' => 'abc123',
209+
'identified' => false,
210+
'errorMessage' => '',
211+
],
212+
'authenticated_wrong_email_token_already_identified' => [
213+
'userEmail' => 'admin@example.com',
214+
'signerEmail' => 'signer@example.com',
215+
'code' => 'abc123',
216+
'identified' => true,
217+
'errorMessage' => 'This document is not yours',
218+
],
219+
];
220+
}
139221
}

0 commit comments

Comments
 (0)