Mocking
1 year ago
ApiCallTest.php
1 year ago
AuthenticationTest.php
1 year ago
ClientTest.php
1 year ago
CoreTestCaseTest.php
1 year ago
EndToEndTest.php
1 year ago
LoggerTest.php
1 year ago
TypesTest.php
1 year ago
UtilsTest.php
1 year ago
bootstrap.php
1 year ago
AuthenticationTest.php
258 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Core\Tests; |
| 4 | |
| 5 | use Core\Authentication\Auth; |
| 6 | use Core\Exceptions\AuthValidationException; |
| 7 | use Core\Request\Request; |
| 8 | use Core\Tests\Mocking\Authentication\HeaderAuthManager; |
| 9 | use Core\Tests\Mocking\MockHelper; |
| 10 | use InvalidArgumentException; |
| 11 | use PHPUnit\Framework\TestCase; |
| 12 | |
| 13 | class AuthenticationTest extends TestCase |
| 14 | { |
| 15 | public function testHeaderAuthWithoutGroupAndValidation() |
| 16 | { |
| 17 | $request = new Request('http://localhost:3000'); |
| 18 | $auth = new HeaderAuthManager(null, "accessToken"); |
| 19 | $auth->apply($request); |
| 20 | |
| 21 | $this->assertEquals([], $request->getHeaders()); |
| 22 | } |
| 23 | |
| 24 | public function testHeaderAuth() |
| 25 | { |
| 26 | $request = new Request('http://localhost:3000'); |
| 27 | $auth = Auth::or('header'); |
| 28 | MockHelper::getClient()->validateAuth($auth)->apply($request); |
| 29 | |
| 30 | $this->assertEquals([ |
| 31 | 'token' => 'someAuthToken', |
| 32 | 'authorization' => 'accessToken' |
| 33 | ], $request->getHeaders()); |
| 34 | } |
| 35 | |
| 36 | public function testHeaderAuthWithMissingField() |
| 37 | { |
| 38 | $this->expectException(AuthValidationException::class); |
| 39 | $this->expectExceptionMessage("Following authentication credentials are required:" . |
| 40 | "\n-> Missing required header field: authorization"); |
| 41 | |
| 42 | $auth = Auth::or('headerWithNull'); |
| 43 | MockHelper::getClient()->validateAuth($auth); |
| 44 | } |
| 45 | |
| 46 | public function testHeaderAuthWithEmptyField() |
| 47 | { |
| 48 | $this->expectException(AuthValidationException::class); |
| 49 | $this->expectExceptionMessage("Following authentication credentials are required:" . |
| 50 | "\n-> Missing required header field: token"); |
| 51 | |
| 52 | $auth = Auth::or('headerWithEmpty'); |
| 53 | MockHelper::getClient()->validateAuth($auth); |
| 54 | } |
| 55 | |
| 56 | public function testHeaderOrQueryAuth1() |
| 57 | { |
| 58 | $request = new Request('http://localhost:3000'); |
| 59 | $auth = Auth::or('header', 'query'); |
| 60 | MockHelper::getClient()->validateAuth($auth)->apply($request); |
| 61 | |
| 62 | $this->assertEquals([ |
| 63 | 'token' => 'someAuthToken', |
| 64 | 'authorization' => 'accessToken' |
| 65 | ], $request->getHeaders()); |
| 66 | $this->assertEquals( |
| 67 | 'http://localhost:3000', |
| 68 | $request->getQueryUrl() |
| 69 | ); |
| 70 | } |
| 71 | |
| 72 | public function testHeaderOrQueryAuth2() |
| 73 | { |
| 74 | $request = new Request('http://localhost:3000'); |
| 75 | $auth = Auth::or('query', 'header'); |
| 76 | MockHelper::getClient()->validateAuth($auth)->apply($request); |
| 77 | |
| 78 | $this->assertEquals([], $request->getHeaders()); |
| 79 | $this->assertEquals( |
| 80 | 'http://localhost:3000?token=someAuthToken&authorization=accessToken', |
| 81 | $request->getQueryUrl() |
| 82 | ); |
| 83 | } |
| 84 | |
| 85 | public function testHeaderWithMissingFieldOrQueryAuth() |
| 86 | { |
| 87 | $request = new Request('http://localhost:3000'); |
| 88 | $auth = Auth::or('headerWithNull', 'query'); |
| 89 | MockHelper::getClient()->validateAuth($auth)->apply($request); |
| 90 | |
| 91 | $this->assertEquals([], $request->getHeaders()); |
| 92 | $this->assertEquals( |
| 93 | 'http://localhost:3000?token=someAuthToken&authorization=accessToken', |
| 94 | $request->getQueryUrl() |
| 95 | ); |
| 96 | } |
| 97 | |
| 98 | public function testHeaderWithEmptyFieldOrQueryAuth() |
| 99 | { |
| 100 | $request = new Request('http://localhost:3000'); |
| 101 | $auth = Auth::or('headerWithEmpty', 'query'); |
| 102 | MockHelper::getClient()->validateAuth($auth)->apply($request); |
| 103 | |
| 104 | $this->assertEquals([], $request->getHeaders()); |
| 105 | $this->assertEquals( |
| 106 | 'http://localhost:3000?token=someAuthToken&authorization=accessToken', |
| 107 | $request->getQueryUrl() |
| 108 | ); |
| 109 | } |
| 110 | |
| 111 | public function testHeaderOrQueryAuthWithMissingFields() |
| 112 | { |
| 113 | $this->expectException(AuthValidationException::class); |
| 114 | $this->expectExceptionMessage("Following authentication credentials are required:" . |
| 115 | "\n-> Missing required header field: authorization" . |
| 116 | "\n-> Missing required query field: token"); |
| 117 | |
| 118 | $auth = Auth::or('headerWithNull', 'queryWithNull'); |
| 119 | MockHelper::getClient()->validateAuth($auth); |
| 120 | } |
| 121 | |
| 122 | public function testHeaderAndQueryAuth() |
| 123 | { |
| 124 | $request = new Request('http://localhost:3000'); |
| 125 | $auth = Auth::and('header', 'query'); |
| 126 | MockHelper::getClient()->validateAuth($auth)->apply($request); |
| 127 | |
| 128 | $this->assertEquals([ |
| 129 | 'token' => 'someAuthToken', |
| 130 | 'authorization' => 'accessToken' |
| 131 | ], $request->getHeaders()); |
| 132 | $this->assertEquals( |
| 133 | 'http://localhost:3000?token=someAuthToken&authorization=accessToken', |
| 134 | $request->getQueryUrl() |
| 135 | ); |
| 136 | } |
| 137 | |
| 138 | public function testHeaderWithMissingFieldAndQueryAuth() |
| 139 | { |
| 140 | $this->expectException(AuthValidationException::class); |
| 141 | $this->expectExceptionMessage("Following authentication credentials are required:" . |
| 142 | "\n-> Missing required header field: authorization"); |
| 143 | |
| 144 | $auth = Auth::and('headerWithNull', 'query'); |
| 145 | MockHelper::getClient()->validateAuth($auth); |
| 146 | } |
| 147 | |
| 148 | public function testHeaderWithEmptyFieldAndQueryAuth() |
| 149 | { |
| 150 | $this->expectException(AuthValidationException::class); |
| 151 | $this->expectExceptionMessage("Following authentication credentials are required:" . |
| 152 | "\n-> Missing required header field: token"); |
| 153 | |
| 154 | $auth = Auth::and('headerWithEmpty', 'query'); |
| 155 | MockHelper::getClient()->validateAuth($auth); |
| 156 | } |
| 157 | |
| 158 | public function testHeaderAndQueryAuthWithMissingFields() |
| 159 | { |
| 160 | $this->expectException(AuthValidationException::class); |
| 161 | $this->expectExceptionMessage("Following authentication credentials are required:" . |
| 162 | "\n-> Missing required header field: authorization"); |
| 163 | |
| 164 | $auth = Auth::and('headerWithNull', 'queryWithNull'); |
| 165 | MockHelper::getClient()->validateAuth($auth); |
| 166 | } |
| 167 | |
| 168 | public function testFormOrHeaderAndQueryAuthWithMissingFields() |
| 169 | { |
| 170 | $request = new Request('http://localhost:3000'); |
| 171 | $auth = Auth::or('form', Auth::and('header', 'queryWithNull')); |
| 172 | MockHelper::getClient()->validateAuth($auth)->apply($request); |
| 173 | |
| 174 | $this->assertEquals([ |
| 175 | 'token' => 'someAuthToken', |
| 176 | 'authorization' => 'accessToken' |
| 177 | ], $request->getParameters()); |
| 178 | $this->assertEquals([], $request->getHeaders()); |
| 179 | $this->assertEquals('http://localhost:3000', $request->getQueryUrl()); |
| 180 | } |
| 181 | |
| 182 | public function testFormOrHeaderOrQueryAuthWithMissingFields1() |
| 183 | { |
| 184 | $request = new Request('http://localhost:3000'); |
| 185 | $auth = Auth::or('form', Auth::or('header', 'queryWithNull')); |
| 186 | MockHelper::getClient()->validateAuth($auth)->apply($request); |
| 187 | |
| 188 | $this->assertEquals([ |
| 189 | 'token' => 'someAuthToken', |
| 190 | 'authorization' => 'accessToken' |
| 191 | ], $request->getParameters()); |
| 192 | $this->assertEquals([], $request->getHeaders()); |
| 193 | $this->assertEquals('http://localhost:3000', $request->getQueryUrl()); |
| 194 | } |
| 195 | |
| 196 | public function testFormOrHeaderOrQueryAuthWithMissingFields2() |
| 197 | { |
| 198 | $request = new Request('http://localhost:3000'); |
| 199 | $auth = Auth::or(Auth::or('header', 'queryWithNull'), 'form'); |
| 200 | MockHelper::getClient()->validateAuth($auth)->apply($request); |
| 201 | |
| 202 | $this->assertEquals([ |
| 203 | 'token' => 'someAuthToken', |
| 204 | 'authorization' => 'accessToken' |
| 205 | ], $request->getHeaders()); |
| 206 | $this->assertEquals('http://localhost:3000', $request->getQueryUrl()); |
| 207 | } |
| 208 | |
| 209 | public function testFormAndHeaderWithNullOrHeaderOrQueryWithNull() |
| 210 | { |
| 211 | $request = new Request('http://localhost:3000'); |
| 212 | $auth = Auth::or(Auth::and('form', 'headerWithNull'), Auth::or('header', 'queryWithNull')); |
| 213 | MockHelper::getClient()->validateAuth($auth)->apply($request); |
| 214 | |
| 215 | $this->assertEquals([], $request->getParameters()); |
| 216 | $this->assertEquals([ |
| 217 | 'token' => 'someAuthToken', |
| 218 | 'authorization' => 'accessToken' |
| 219 | ], $request->getHeaders()); |
| 220 | $this->assertEquals('http://localhost:3000', $request->getQueryUrl()); |
| 221 | } |
| 222 | |
| 223 | public function testFormOrHeaderWithNullAndHeaderOrQueryWithNull() |
| 224 | { |
| 225 | $request = new Request('http://localhost:3000'); |
| 226 | $auth = Auth::and(Auth::or('form', 'headerWithNull', 'formWithNull'), Auth::or('queryWithNull', 'header')); |
| 227 | MockHelper::getClient()->validateAuth($auth)->apply($request); |
| 228 | |
| 229 | $this->assertEquals([ |
| 230 | 'token' => 'someAuthToken', |
| 231 | 'authorization' => 'accessToken' |
| 232 | ], $request->getParameters()); |
| 233 | $this->assertEquals([ |
| 234 | 'token' => 'someAuthToken', |
| 235 | 'authorization' => 'accessToken' |
| 236 | ], $request->getHeaders()); |
| 237 | $this->assertEquals('http://localhost:3000', $request->getQueryUrl()); |
| 238 | } |
| 239 | |
| 240 | public function testFormOrHeaderWithNullAndHeaderAndQueryWithNull() |
| 241 | { |
| 242 | $this->expectException(AuthValidationException::class); |
| 243 | $this->expectExceptionMessage("Following authentication credentials are required:" . |
| 244 | "\n-> Missing required query field: token"); |
| 245 | |
| 246 | $auth = Auth::and(Auth::or('form', 'headerWithNull'), Auth::and('header', 'queryWithNull')); |
| 247 | MockHelper::getClient()->validateAuth($auth); |
| 248 | } |
| 249 | |
| 250 | public function testInvalidAuthName() |
| 251 | { |
| 252 | $this->expectException(InvalidArgumentException::class); |
| 253 | $this->expectExceptionMessage("AuthManager not found with name: \"myAuth\""); |
| 254 | |
| 255 | MockHelper::getClient()->validateAuth(Auth::or('myAuth')); |
| 256 | } |
| 257 | } |
| 258 |