|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace MinVWS\OpenIDConnectLaravel\Tests\Unit\Http\Controllers; |
| 6 | + |
| 7 | +use Exception; |
| 8 | +use Illuminate\Contracts\Support\Responsable; |
| 9 | +use Illuminate\Http\Request; |
| 10 | +use Jumbojett\OpenIDConnectClientException; |
| 11 | +use MinVWS\OpenIDConnectLaravel\Http\Controllers\LoginController; |
| 12 | +use MinVWS\OpenIDConnectLaravel\Http\Responses\LoginResponse; |
| 13 | +use MinVWS\OpenIDConnectLaravel\Http\Responses\LoginResponseInterface; |
| 14 | +use MinVWS\OpenIDConnectLaravel\OpenIDConnectClient; |
| 15 | +use MinVWS\OpenIDConnectLaravel\Services\JWE\JweDecryptException; |
| 16 | +use MinVWS\OpenIDConnectLaravel\Services\OpenIDConnectExceptionHandler; |
| 17 | +use Mockery; |
| 18 | +use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration; |
| 19 | +use PHPUnit\Framework\TestCase; |
| 20 | + |
| 21 | +class LoginControllerTest extends TestCase |
| 22 | +{ |
| 23 | + use MockeryPHPUnitIntegration; |
| 24 | + |
| 25 | + protected function setUp(): void |
| 26 | + { |
| 27 | + parent::setUp(); |
| 28 | + |
| 29 | + // Bind the LoginResponseInterface to the LoginResponse class |
| 30 | + app()->bind(LoginResponseInterface::class, LoginResponse::class); |
| 31 | + } |
| 32 | + |
| 33 | + protected function tearDown(): void |
| 34 | + { |
| 35 | + // Flush so the LoginResponseInterface binding is removed |
| 36 | + app()->flush(); |
| 37 | + |
| 38 | + parent::tearDown(); |
| 39 | + } |
| 40 | + |
| 41 | + public function testLoginControllerCanBeCreated(): void |
| 42 | + { |
| 43 | + $loginController = new LoginController( |
| 44 | + new OpenIDConnectClient(), |
| 45 | + new OpenIDConnectExceptionHandler(), |
| 46 | + ); |
| 47 | + $this->assertInstanceOf(LoginController::class, $loginController); |
| 48 | + } |
| 49 | + |
| 50 | + public function testExceptionHandlerIsCalledWhenAuthenticateThrowsException(): void |
| 51 | + { |
| 52 | + $mockClient = Mockery::mock(OpenIDConnectClient::class); |
| 53 | + $mockClient |
| 54 | + ->shouldReceive('authenticate') |
| 55 | + ->andThrow(OpenIDConnectClientException::class); |
| 56 | + |
| 57 | + $mockExceptionHandler = Mockery::mock(OpenIDConnectExceptionHandler::class); |
| 58 | + $mockExceptionHandler |
| 59 | + ->shouldReceive('handleExceptionWhileAuthenticate') |
| 60 | + ->once(); |
| 61 | + |
| 62 | + $loginController = new LoginController( |
| 63 | + $mockClient, |
| 64 | + $mockExceptionHandler, |
| 65 | + ); |
| 66 | + |
| 67 | + $loginController->__invoke(); |
| 68 | + } |
| 69 | + |
| 70 | + public function testExceptionHandlerIsCalledWhenRequestUserInfoDoesNotReturnAnObject(): void |
| 71 | + { |
| 72 | + $mockClient = Mockery::mock(OpenIDConnectClient::class); |
| 73 | + $mockClient->shouldReceive('authenticate')->once(); |
| 74 | + $mockClient |
| 75 | + ->shouldReceive('requestUserInfo') |
| 76 | + ->andReturn('not an object') |
| 77 | + ->once(); |
| 78 | + |
| 79 | + $mockExceptionHandler = Mockery::mock(OpenIDConnectExceptionHandler::class); |
| 80 | + $mockExceptionHandler |
| 81 | + ->shouldReceive('handleExceptionWhileRequestUserInfo') |
| 82 | + ->withArgs(function (OpenIDConnectClientException $e) { |
| 83 | + return $e->getMessage() === 'Received user info is not an object'; |
| 84 | + }) |
| 85 | + ->once(); |
| 86 | + |
| 87 | + $loginController = new LoginController( |
| 88 | + $mockClient, |
| 89 | + $mockExceptionHandler, |
| 90 | + ); |
| 91 | + |
| 92 | + $loginController->__invoke(); |
| 93 | + } |
| 94 | + |
| 95 | + public function testExceptionHandlerIsCalledWhenRequestUserInfoThrowsAnException(): void |
| 96 | + { |
| 97 | + $mockClient = Mockery::mock(OpenIDConnectClient::class); |
| 98 | + $mockClient->shouldReceive('authenticate')->once(); |
| 99 | + $mockClient |
| 100 | + ->shouldReceive('requestUserInfo') |
| 101 | + ->andThrow(OpenIDConnectClientException::class, 'Something went wrong') |
| 102 | + ->once(); |
| 103 | + |
| 104 | + $mockExceptionHandler = Mockery::mock(OpenIDConnectExceptionHandler::class); |
| 105 | + $mockExceptionHandler |
| 106 | + ->shouldReceive('handleExceptionWhileRequestUserInfo') |
| 107 | + ->withArgs(function (OpenIDConnectClientException $e) { |
| 108 | + return $e->getMessage() === 'Something went wrong'; |
| 109 | + }) |
| 110 | + ->once(); |
| 111 | + |
| 112 | + $loginController = new LoginController( |
| 113 | + $mockClient, |
| 114 | + $mockExceptionHandler, |
| 115 | + ); |
| 116 | + |
| 117 | + $loginController->__invoke(); |
| 118 | + } |
| 119 | + |
| 120 | + public function testExceptionHandlerIsCalledWhenRequestUserInfoThrowsAnJweDecryptException(): void |
| 121 | + { |
| 122 | + $mockClient = Mockery::mock(OpenIDConnectClient::class); |
| 123 | + $mockClient->shouldReceive('authenticate')->once(); |
| 124 | + $mockClient |
| 125 | + ->shouldReceive('requestUserInfo') |
| 126 | + ->andThrow(JweDecryptException::class, 'Something went wrong') |
| 127 | + ->once(); |
| 128 | + |
| 129 | + $mockExceptionHandler = Mockery::mock(OpenIDConnectExceptionHandler::class); |
| 130 | + $mockExceptionHandler |
| 131 | + ->shouldReceive('handleException') |
| 132 | + ->withArgs(function (Exception $e) { |
| 133 | + return $e->getMessage() === 'Something went wrong'; |
| 134 | + }) |
| 135 | + ->once(); |
| 136 | + |
| 137 | + $loginController = new LoginController( |
| 138 | + $mockClient, |
| 139 | + $mockExceptionHandler, |
| 140 | + ); |
| 141 | + |
| 142 | + $loginController->__invoke(); |
| 143 | + } |
| 144 | + |
| 145 | + public function testLoginResponseIsReturnedWithUserInfo(): void |
| 146 | + { |
| 147 | + $mockClient = Mockery::mock(OpenIDConnectClient::class); |
| 148 | + $mockClient->shouldReceive('authenticate')->once(); |
| 149 | + $mockClient |
| 150 | + ->shouldReceive('requestUserInfo') |
| 151 | + ->andReturn($this->exampleUserInfo()) |
| 152 | + ->once(); |
| 153 | + |
| 154 | + $mockExceptionHandler = Mockery::mock(OpenIDConnectExceptionHandler::class); |
| 155 | + |
| 156 | + $loginController = new LoginController( |
| 157 | + $mockClient, |
| 158 | + $mockExceptionHandler, |
| 159 | + ); |
| 160 | + |
| 161 | + $response = $loginController->__invoke(); |
| 162 | + |
| 163 | + $this->assertInstanceOf(LoginResponseInterface::class, $response); |
| 164 | + $this->assertInstanceOf(Responsable::class, $response); |
| 165 | + } |
| 166 | + |
| 167 | + public function testUserInfoIsReturned(): void |
| 168 | + { |
| 169 | + $mockClient = Mockery::mock(OpenIDConnectClient::class); |
| 170 | + $mockClient->shouldReceive('authenticate')->once(); |
| 171 | + $mockClient |
| 172 | + ->shouldReceive('requestUserInfo') |
| 173 | + ->andReturn($this->exampleUserInfo()) |
| 174 | + ->once(); |
| 175 | + |
| 176 | + $mockExceptionHandler = Mockery::mock(OpenIDConnectExceptionHandler::class); |
| 177 | + |
| 178 | + $loginController = new LoginController( |
| 179 | + $mockClient, |
| 180 | + $mockExceptionHandler, |
| 181 | + ); |
| 182 | + |
| 183 | + $loginResponse = $loginController->__invoke(); |
| 184 | + $response = $loginResponse->toResponse(Mockery::mock(Request::class)); |
| 185 | + |
| 186 | + $this->assertSame(json_encode([ |
| 187 | + 'userInfo' => $this->exampleUserInfo(), |
| 188 | + ]), $response->getContent()); |
| 189 | + } |
| 190 | + |
| 191 | + protected function exampleUserInfo(): object |
| 192 | + { |
| 193 | + return (object) [ |
| 194 | + 'sub' => '1234567890', |
| 195 | + 'name' => 'John Doe', |
| 196 | + 'given_name' => 'John', |
| 197 | + 'family_name' => 'Doe', |
| 198 | + 'middle_name' => 'Middle', |
| 199 | + 'nickname' => 'JD', |
| 200 | + 'preferred_username' => 'johndoe', |
| 201 | + 'email' => '', |
| 202 | + ]; |
| 203 | + } |
| 204 | +} |
0 commit comments