|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace PhpList\WebFrontend\Tests\Unit\Controller; |
| 6 | + |
| 7 | +use PhpList\WebFrontend\Controller\AuthController; |
| 8 | +use PhpList\WebFrontend\Service\ApiClient; |
| 9 | +use PHPUnit\Framework\TestCase; |
| 10 | +use RuntimeException; |
| 11 | +use Symfony\Component\HttpFoundation\ParameterBag; |
| 12 | +use Symfony\Component\HttpFoundation\RedirectResponse; |
| 13 | +use Symfony\Component\HttpFoundation\Request; |
| 14 | +use Symfony\Component\HttpFoundation\Response; |
| 15 | +use Symfony\Component\HttpFoundation\Session\SessionInterface; |
| 16 | + |
| 17 | +class AuthControllerTest extends TestCase |
| 18 | +{ |
| 19 | + private ApiClient $apiClient; |
| 20 | + private AuthController $controller; |
| 21 | + |
| 22 | + protected function setUp(): void |
| 23 | + { |
| 24 | + $this->apiClient = $this->createMock(ApiClient::class); |
| 25 | + |
| 26 | + $this->controller = $this->getMockBuilder(AuthController::class) |
| 27 | + ->setConstructorArgs([$this->apiClient]) |
| 28 | + ->onlyMethods(['render', 'redirectToRoute', 'generateUrl']) |
| 29 | + ->getMock(); |
| 30 | + |
| 31 | + $this->controller->method('render') |
| 32 | + ->willReturnCallback(function ($template, $params = []) { |
| 33 | + return new Response( |
| 34 | + "Rendered template: $template with params: " . json_encode($params) |
| 35 | + ); |
| 36 | + }); |
| 37 | + |
| 38 | + $this->controller->method('redirectToRoute') |
| 39 | + ->willReturnCallback(function ($route) { |
| 40 | + return new RedirectResponse("/mocked-route-to-$route"); |
| 41 | + }); |
| 42 | + |
| 43 | + $this->controller->method('generateUrl') |
| 44 | + ->willReturnCallback(function ($route) { |
| 45 | + return "/mocked-url-to-$route"; |
| 46 | + }); |
| 47 | + } |
| 48 | + |
| 49 | + public function testLoginWithGetRequest(): void |
| 50 | + { |
| 51 | + $session = $this->createMock(SessionInterface::class); |
| 52 | + $session->method('has') |
| 53 | + ->willReturnMap([ |
| 54 | + ['auth_token', false], |
| 55 | + ['login_error', false] |
| 56 | + ]); |
| 57 | + |
| 58 | + $request = $this->createMock(Request::class); |
| 59 | + $request->method('getSession') |
| 60 | + ->willReturn($session); |
| 61 | + $request->method('isMethod') |
| 62 | + ->with('POST') |
| 63 | + ->willReturn(false); |
| 64 | + |
| 65 | + $response = $this->controller->login($request); |
| 66 | + |
| 67 | + $this->assertStringContainsString('security/login.html.twig', $response->getContent()); |
| 68 | + $this->assertStringContainsString('with params', $response->getContent()); |
| 69 | + } |
| 70 | + |
| 71 | + public function testLoginWithGetRequestAndError(): void |
| 72 | + { |
| 73 | + $session = $this->createMock(SessionInterface::class); |
| 74 | + $session->method('has') |
| 75 | + ->willReturnMap([ |
| 76 | + ['auth_token', false], |
| 77 | + ['login_error', true] |
| 78 | + ]); |
| 79 | + $session->method('get') |
| 80 | + ->with('login_error') |
| 81 | + ->willReturn('Test error message'); |
| 82 | + $session->expects($this->once()) |
| 83 | + ->method('remove') |
| 84 | + ->with('login_error'); |
| 85 | + |
| 86 | + $request = $this->createMock(Request::class); |
| 87 | + $request->method('getSession') |
| 88 | + ->willReturn($session); |
| 89 | + $request->method('isMethod') |
| 90 | + ->with('POST') |
| 91 | + ->willReturn(false); |
| 92 | + |
| 93 | + $response = $this->controller->login($request); |
| 94 | + |
| 95 | + $this->assertStringContainsString('security/login.html.twig', $response->getContent()); |
| 96 | + $this->assertStringContainsString('Test error message', $response->getContent()); |
| 97 | + } |
| 98 | + |
| 99 | + public function testLoginWithPostRequestSuccess(): void |
| 100 | + { |
| 101 | + $session = $this->createMock(SessionInterface::class); |
| 102 | + $session->method('has') |
| 103 | + ->willReturnMap([ |
| 104 | + ['auth_token', false], |
| 105 | + ['login_error', false] |
| 106 | + ]); |
| 107 | + |
| 108 | + $session->expects($this->exactly(2)) |
| 109 | + ->method('set') |
| 110 | + ->withConsecutive( |
| 111 | + ['auth_token', 'test-token'], |
| 112 | + ['auth_expiry_date', 'test-token'] |
| 113 | + ); |
| 114 | + |
| 115 | + $requestParams = $this->createMock(ParameterBag::class); |
| 116 | + $requestParams->method('get') |
| 117 | + ->willReturnMap([ |
| 118 | + ['username', null, 'testuser'], |
| 119 | + ['password', null, 'testpass'] |
| 120 | + ]); |
| 121 | + |
| 122 | + $request = $this->createMock(Request::class); |
| 123 | + $request->method('getSession') |
| 124 | + ->willReturn($session); |
| 125 | + $request->method('isMethod') |
| 126 | + ->with('POST') |
| 127 | + ->willReturn(true); |
| 128 | + $request->request = $requestParams; |
| 129 | + |
| 130 | + $this->apiClient->method('authenticate') |
| 131 | + ->with('testuser', 'testpass') |
| 132 | + ->willReturn(['key' => 'test-token']); |
| 133 | + |
| 134 | + $this->apiClient->expects($this->once()) |
| 135 | + ->method('setAuthToken') |
| 136 | + ->with('test-token'); |
| 137 | + |
| 138 | + $response = $this->controller->login($request); |
| 139 | + |
| 140 | + $this->assertInstanceOf(RedirectResponse::class, $response); |
| 141 | + $this->assertStringContainsString('empty_start_page', $response->getTargetUrl()); |
| 142 | + } |
| 143 | + |
| 144 | + public function testLoginWithPostRequestFailure(): void |
| 145 | + { |
| 146 | + $session = $this->createMock(SessionInterface::class); |
| 147 | + $session->method('has') |
| 148 | + ->willReturnMap([ |
| 149 | + ['auth_token', false], |
| 150 | + ['login_error', false] |
| 151 | + ]); |
| 152 | + |
| 153 | + $requestParams = $this->createMock(ParameterBag::class); |
| 154 | + $requestParams->method('get') |
| 155 | + ->willReturnMap([ |
| 156 | + ['username', null, 'testuser'], |
| 157 | + ['password', null, 'wrongpass'] |
| 158 | + ]); |
| 159 | + |
| 160 | + $request = $this->createMock(Request::class); |
| 161 | + $request->method('getSession') |
| 162 | + ->willReturn($session); |
| 163 | + $request->method('isMethod') |
| 164 | + ->with('POST') |
| 165 | + ->willReturn(true); |
| 166 | + $request->request = $requestParams; |
| 167 | + |
| 168 | + $this->apiClient->method('authenticate') |
| 169 | + ->with('testuser', 'wrongpass') |
| 170 | + ->willThrowException(new RuntimeException('Invalid credentials')); |
| 171 | + |
| 172 | + $response = $this->controller->login($request); |
| 173 | + |
| 174 | + $this->assertStringContainsString('security/login.html.twig', $response->getContent()); |
| 175 | + $this->assertStringContainsString('Invalid credentials', $response->getContent()); |
| 176 | + } |
| 177 | + |
| 178 | + public function testLoginWithExistingSession(): void |
| 179 | + { |
| 180 | + $session = $this->createMock(SessionInterface::class); |
| 181 | + $session->method('has') |
| 182 | + ->willReturnMap([ |
| 183 | + ['auth_token', true], |
| 184 | + ['login_error', false] |
| 185 | + ]); |
| 186 | + |
| 187 | + $request = $this->createMock(Request::class); |
| 188 | + $request->method('getSession') |
| 189 | + ->willReturn($session); |
| 190 | + |
| 191 | + $response = $this->controller->login($request); |
| 192 | + |
| 193 | + $this->assertInstanceOf(RedirectResponse::class, $response); |
| 194 | + $this->assertStringContainsString('empty_start_page', $response->getTargetUrl()); |
| 195 | + } |
| 196 | + |
| 197 | + public function testLogout(): void |
| 198 | + { |
| 199 | + $session = $this->createMock(SessionInterface::class); |
| 200 | + $session->expects($this->once()) |
| 201 | + ->method('remove') |
| 202 | + ->with('auth_token'); |
| 203 | + |
| 204 | + $request = $this->createMock(Request::class); |
| 205 | + $request->method('getSession') |
| 206 | + ->willReturn($session); |
| 207 | + |
| 208 | + $response = $this->controller->logout($request); |
| 209 | + |
| 210 | + $this->assertInstanceOf(RedirectResponse::class, $response); |
| 211 | + $this->assertStringContainsString('login', $response->getTargetUrl()); |
| 212 | + } |
| 213 | +} |
0 commit comments