-
-
Notifications
You must be signed in to change notification settings - Fork 133
test: cover EmailToken and TokenService signature method behaviors #8092
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -8,6 +8,7 @@ | |||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| namespace OCA\Libresign\Tests\Unit\Service\IdentifyMethod\SignatureMethod; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| use OCA\Libresign\Exception\LibresignException; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| use OCA\Libresign\Service\IdentifyMethod\SignatureMethod\TokenService; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| use OCA\Libresign\Service\MailService; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| use OCA\Libresign\Service\TwofactorGatewayService; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -43,6 +44,19 @@ public function setUp(): void { | |||||||||||||||||||||||||||||||||||||||||||||||||
| $this->logger = $this->createMock(LoggerInterface::class); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| public function testSendCodeByGatewayThrowsWhenGatewayAppIsNotEnabled(): void { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| $this->appManager->method('isEnabledForAnyone')->with('twofactor_gateway')->willReturn(false); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| $this->container->expects($this->never()) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| ->method('get'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| $this->secureRandom->expects($this->never()) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| ->method('generate'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| $this->expectException(LibresignException::class); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| $this->expectExceptionMessage('App Two-Factor Gateway is not enabled.'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be a good idea to avoid adding translatable or easily mutable text to test scenarios. If a non-developer needs to update the text, it becomes complicated because they would need to know to change it in two different places. The
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed — asserting on the message couples the test to text that can change for non-technical reasons, and in Two things I checked:
For the long-term approach I see two directions, and I'd rather not pick one on my own since both touch production code:
The suite currently has 171
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Only a curiosity:
At some places we already do this but to prevent mistakes, we added a more digit. I found some developers considering that 4xx is a HTTP error, not another kind of error.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As example: libresign/lib/Helper/JSActions.php Lines 12 to 22 in b33a20a
libresign/src/helpers/ActionMapping.ts Lines 30 to 42 in b33a20a
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Nice idea, could you do this?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done: #8117 has the survey and a proposal to settle the convention (codes with constants and/or the existing typed exceptions) before splitting the work into small PRs. |
||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| $this->createService()->sendCodeByGateway('+5511999999999', 'sms'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| public function testSendCodeByGatewayThrowsWhenGatewayIsIncomplete(): void { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| $this->appManager->method('isEnabledForAnyone')->with('twofactor_gateway')->willReturn(true); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| $this->container->method('get') | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -86,6 +100,22 @@ public function testSendCodeByGatewayUsesGatewayServiceAndReturnsHashedCode(): v | |||||||||||||||||||||||||||||||||||||||||||||||||
| ], $integrationService->sentMessages); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| public function testSendCodeByEmailSendsCodeAndReturnsHashedCode(): void { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| $this->secureRandom->expects($this->once()) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| ->method('generate') | ||||||||||||||||||||||||||||||||||||||||||||||||||
| ->with(TokenService::TOKEN_LENGTH, ISecureRandom::CHAR_DIGITS) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| ->willReturn('123456'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| $this->mailService->expects($this->once()) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| ->method('sendCodeToSign') | ||||||||||||||||||||||||||||||||||||||||||||||||||
| ->with('signer@domain.coop', 'John Doe', '123456'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| $this->hasher->expects($this->once()) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| ->method('hash') | ||||||||||||||||||||||||||||||||||||||||||||||||||
| ->with('123456') | ||||||||||||||||||||||||||||||||||||||||||||||||||
| ->willReturn('hashed-code'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| self::assertSame('hashed-code', $this->createService()->sendCodeByEmail('signer@domain.coop', 'John Doe')); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| private function createService(): TokenService { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| return new TokenService( | ||||||||||||||||||||||||||||||||||||||||||||||||||
| $this->secureRandom, | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be a good idea to avoid adding translatable or easily mutable text to test scenarios. If a non-developer needs to update the text, it becomes complicated because they would need to know to change it in two different places.
The
LibresignExceptionclass could use an exception code instead, allowing the use of theexpectExceptionCodemethod, but is necessary to check the best approach.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same point applies here — answered in the
TokenServiceTestthread above; the follow-up PR will cover both files.