|
12 | 12 | use OxidEsales\Eshop\Core\UtilsView; |
13 | 13 | use OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Controller\TwoFactorAuthController; |
14 | 14 | use OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Exception\InvalidCodeException; |
15 | | -use OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Service\AuthorizeServiceInterface; |
| 15 | +use OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Exception\ResendCooldownException; |
16 | 16 | use OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Service\TwoFAServiceInterface; |
17 | 17 | use OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Service\TwoFAUserServiceInterface; |
18 | 18 | use OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Transput\AuthCodeRequestInterface; |
@@ -72,63 +72,65 @@ public function handleOTPDisplaysErrorOnInvalidCode(): void |
72 | 72 | $sut->handleOTP(); |
73 | 73 | } |
74 | 74 |
|
75 | | - public function testResendCodeSendsSuccessResponse(): void |
| 75 | + #[Test] |
| 76 | + public function resendCodeSendsSuccessResponse(): void |
76 | 77 | { |
77 | | - $authServiceMock = $this->createMock(AuthorizeServiceInterface::class); |
78 | | - $authServiceMock->expects($this->once()) |
| 78 | + $twoFAUserServiceStub = $this->createStub(TwoFAUserServiceInterface::class); |
| 79 | + $twoFAUserServiceStub->method('getPendingUserId') |
| 80 | + ->willReturn($userId = uniqid()); |
| 81 | + |
| 82 | + $twoFAServiceSpy = $this->createMock(TwoFAServiceInterface::class); |
| 83 | + $twoFAServiceSpy->expects($this->once()) |
79 | 84 | ->method('resend') |
80 | | - ->willReturn(true); |
| 85 | + ->with($userId); |
81 | 86 |
|
82 | | - $jsonResponseMock = $this->createMock(JsonResponseInterface::class); |
83 | | - $jsonResponseMock->expects($this->never()) |
84 | | - ->method('setStatusCode'); |
85 | | - $jsonResponseMock->expects($this->once()) |
| 87 | + $jsonResponseSpy = $this->createMock(JsonResponseInterface::class); |
| 88 | + $jsonResponseSpy->expects($this->once()) |
86 | 89 | ->method('send') |
87 | | - ->with(['success' => true]); |
88 | | - |
89 | | - $controller = $this->getSut( |
90 | | - authService: $authServiceMock, |
91 | | - jsonResponse: $jsonResponseMock, |
92 | | - ); |
| 90 | + ->with(['success' => true], 200); |
93 | 91 |
|
94 | | - $controller->resendCode(); |
| 92 | + $this->getSut( |
| 93 | + twoFAService: $twoFAServiceSpy, |
| 94 | + twoFAUserService: $twoFAUserServiceStub, |
| 95 | + jsonResponse: $jsonResponseSpy, |
| 96 | + )->resendCode(); |
95 | 97 | } |
96 | 98 |
|
97 | | - public function testResendCodeSends429WhenCooldownActive(): void |
| 99 | + #[Test] |
| 100 | + public function resendCodeSends429OnCooldown(): void |
98 | 101 | { |
99 | | - $authServiceMock = $this->createMock(AuthorizeServiceInterface::class); |
100 | | - $authServiceMock->expects($this->once()) |
| 102 | + $twoFAUserServiceStub = $this->createStub(TwoFAUserServiceInterface::class); |
| 103 | + $twoFAUserServiceStub->method('getPendingUserId') |
| 104 | + ->willReturn($userId = uniqid()); |
| 105 | + |
| 106 | + $twoFAServiceStub = $this->createMock(TwoFAServiceInterface::class); |
| 107 | + $twoFAServiceStub->expects($this->once()) |
101 | 108 | ->method('resend') |
102 | | - ->willReturn(false); |
| 109 | + ->with($userId) |
| 110 | + ->willThrowException(new ResendCooldownException()); |
103 | 111 |
|
104 | | - $jsonResponseMock = $this->createMock(JsonResponseInterface::class); |
105 | | - $jsonResponseMock->expects($this->once()) |
106 | | - ->method('setStatusCode') |
107 | | - ->with(429); |
108 | | - $jsonResponseMock->expects($this->once()) |
| 112 | + $jsonResponseSpy = $this->createMock(JsonResponseInterface::class); |
| 113 | + $jsonResponseSpy->expects($this->once()) |
109 | 114 | ->method('send') |
110 | | - ->with(['success' => false]); |
| 115 | + ->with(['success' => false], 429); |
111 | 116 |
|
112 | | - $controller = $this->getSut( |
113 | | - authService: $authServiceMock, |
114 | | - jsonResponse: $jsonResponseMock, |
115 | | - ); |
116 | | - |
117 | | - $controller->resendCode(); |
| 117 | + $this->getSut( |
| 118 | + twoFAService: $twoFAServiceStub, |
| 119 | + twoFAUserService: $twoFAUserServiceStub, |
| 120 | + jsonResponse: $jsonResponseSpy, |
| 121 | + )->resendCode(); |
118 | 122 | } |
119 | 123 |
|
120 | 124 | private function getSut( |
121 | 125 | TwoFAServiceInterface $twoFAService = null, |
122 | 126 | TwoFAUserServiceInterface $twoFAUserService = null, |
123 | | - AuthorizeServiceInterface $authService = null, |
124 | 127 | AuthCodeRequestInterface $authCodeRequest = null, |
125 | 128 | UtilsView $utilsView = null, |
126 | 129 | JsonResponseInterface $jsonResponse = null, |
127 | 130 | ): TwoFactorAuthController { |
128 | 131 | return new TwoFactorAuthController( |
129 | 132 | twoFAService: $twoFAService ?? $this->createStub(TwoFAServiceInterface::class), |
130 | 133 | twoFAUserService: $twoFAUserService ?? $this->createStub(TwoFAUserServiceInterface::class), |
131 | | - authService: $authService ?? $this->createStub(AuthorizeServiceInterface::class), |
132 | 134 | authCodeRequest: $authCodeRequest ?? $this->createStub(AuthCodeRequestInterface::class), |
133 | 135 | utilsView: $utilsView ?? $this->createStub(UtilsView::class), |
134 | 136 | jsonResponse: $jsonResponse ?? $this->createStub(JsonResponseInterface::class), |
|
0 commit comments