|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Bloatless\Endocore\Components\BasicAuth\Tests\Unit; |
| 4 | + |
| 5 | +use Bloatless\Endocore\Components\BasicAuth\BasicAuth; |
| 6 | +use Bloatless\Endocore\Http\Request; |
| 7 | +use Bloatless\Endocore\Http\Response; |
| 8 | +use PHPUnit\Framework\TestCase; |
| 9 | + |
| 10 | +class BasicAuthTest extends TestCase |
| 11 | +{ |
| 12 | + protected $config; |
| 13 | + |
| 14 | + public function setUp(): void |
| 15 | + { |
| 16 | + $this->config = [ |
| 17 | + 'auth' => [ |
| 18 | + 'users' => [ |
| 19 | + 'foo' => '$2y$10$hJpespHOJUYzFtHIQk57OusBdwIOXz.8tUdbb9j545Meh2wmeshMm', |
| 20 | + ] |
| 21 | + ] |
| 22 | + ]; |
| 23 | + } |
| 24 | + |
| 25 | + public function testGetSetUsers() |
| 26 | + { |
| 27 | + // set via config |
| 28 | + $auth = new BasicAuth($this->config['auth']['users']); |
| 29 | + $users = $auth->getUsers(); |
| 30 | + $this->assertIsArray($users); |
| 31 | + $this->assertArrayHasKey('foo', $users); |
| 32 | + $this->assertEquals($this->config['auth']['users']['foo'], $users['foo']); |
| 33 | + |
| 34 | + // set via setter |
| 35 | + $auth = new BasicAuth; |
| 36 | + $this->assertEquals([], $auth->getUsers()); |
| 37 | + $auth->setUsers($this->config['auth']['users']); |
| 38 | + $users = $auth->getUsers(); |
| 39 | + $this->assertArrayHasKey('foo', $users); |
| 40 | + $this->assertEquals($this->config['auth']['users']['foo'], $users['foo']); |
| 41 | + } |
| 42 | + |
| 43 | + public function testIsAuthenticatedWithoutHTTPHeader() |
| 44 | + { |
| 45 | + // test without auth header |
| 46 | + $auth = new BasicAuth; |
| 47 | + $request = new Request; |
| 48 | + $result = $auth->isAuthenticated($request); |
| 49 | + $this->assertFalse($result); |
| 50 | + } |
| 51 | + |
| 52 | + public function testIsAuthenticatedWithValidHttpHeader() |
| 53 | + { |
| 54 | + $request = new Request([], [], [ |
| 55 | + 'HTTP_AUTHORIZATION' => 'Basic ' . base64_encode('foo:bar'), |
| 56 | + ]); |
| 57 | + $auth = new BasicAuth; |
| 58 | + $auth->setUsers($this->config['auth']['users']); |
| 59 | + $result = $auth->isAuthenticated($request); |
| 60 | + $this->assertTrue($result); |
| 61 | + } |
| 62 | + |
| 63 | + public function testIsAuthenticatedWithInvalidHttpHeader() |
| 64 | + { |
| 65 | + $auth = new BasicAuth; |
| 66 | + |
| 67 | + // Header is missing "Basic" keyword |
| 68 | + $request = new Request([], [], [ |
| 69 | + 'HTTP_AUTHORIZATION' => 'cisaB ' . base64_encode('foo:bar'), |
| 70 | + ]); |
| 71 | + $result = $auth->isAuthenticated($request); |
| 72 | + $this->assertFalse($result); |
| 73 | + |
| 74 | + // Header with invalid base64 encoding |
| 75 | + $request = new Request([], [], [ |
| 76 | + 'HTTP_AUTHORIZATION' => 'Basic FooBarBz', |
| 77 | + ]); |
| 78 | + $result = $auth->isAuthenticated($request); |
| 79 | + $this->assertFalse($result); |
| 80 | + } |
| 81 | + |
| 82 | + public function testIsAuthenticatedWithNoUsersSet() |
| 83 | + { |
| 84 | + $request = new Request([], [], [ |
| 85 | + 'HTTP_AUTHORIZATION' => 'Basic ' . base64_encode('foo:bar'), |
| 86 | + ]); |
| 87 | + $auth = new BasicAuth; |
| 88 | + $result = $auth->isAuthenticated($request); |
| 89 | + $this->assertFalse($result); |
| 90 | + |
| 91 | + } |
| 92 | + |
| 93 | + public function testIsAuthenticatedWithInvalidUsername() |
| 94 | + { |
| 95 | + $request = new Request([], [], [ |
| 96 | + 'HTTP_AUTHORIZATION' => 'Basic ' . base64_encode('invalid:bar'), |
| 97 | + ]); |
| 98 | + $auth = new BasicAuth; |
| 99 | + $auth->setUsers($this->config['auth']['users']); |
| 100 | + $result = $auth->isAuthenticated($request); |
| 101 | + $this->assertFalse($result); |
| 102 | + } |
| 103 | + |
| 104 | + public function testIsAuthenticatedWithInvalidPassword() |
| 105 | + { |
| 106 | + $request = new Request([], [], [ |
| 107 | + 'HTTP_AUTHORIZATION' => 'Basic ' . base64_encode('foo:naa'), |
| 108 | + ]); |
| 109 | + $auth = new BasicAuth; |
| 110 | + $auth->setUsers($this->config['auth']['users']); |
| 111 | + $result = $auth->isAuthenticated($request); |
| 112 | + $this->assertFalse($result); |
| 113 | + } |
| 114 | + |
| 115 | + public function testRequestAuthorization() |
| 116 | + { |
| 117 | + $auth = new BasicAuth; |
| 118 | + $response = $auth->requestAuthorization(); |
| 119 | + $this->assertInstanceOf(Response::class, $response); |
| 120 | + $this->assertEquals(401, $response->getStatus()); |
| 121 | + |
| 122 | + $headers = $response->getHeaders(); |
| 123 | + $this->assertIsArray($headers); |
| 124 | + $this->assertEquals([ |
| 125 | + 'WWW-Authenticate' => 'Basic realm="Restricted access"', |
| 126 | + ], $headers); |
| 127 | + } |
| 128 | +} |
0 commit comments