|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Yiisoft\Auth\Tests\Method; |
| 6 | + |
| 7 | +use Nyholm\Psr7\Response; |
| 8 | +use Nyholm\Psr7\ServerRequest; |
| 9 | +use PHPUnit\Framework\TestCase; |
| 10 | +use Psr\Http\Message\ServerRequestInterface; |
| 11 | +use Yiisoft\Auth\IdentityInterface; |
| 12 | +use Yiisoft\Auth\Method\HttpCookie; |
| 13 | +use Yiisoft\Auth\Tests\Stub\FakeIdentity; |
| 14 | +use Yiisoft\Auth\Tests\Stub\FakeIdentityRepository; |
| 15 | +use Yiisoft\Http\Method; |
| 16 | + |
| 17 | +final class HttpCookieTest extends TestCase |
| 18 | +{ |
| 19 | + public function testSuccessfulAuthentication(): void |
| 20 | + { |
| 21 | + $identity = $this->createIdentity(); |
| 22 | + $identityRepository = new FakeIdentityRepository($identity); |
| 23 | + $result = (new HttpCookie($identityRepository))->authenticate( |
| 24 | + $this->createRequest(['access-token' => 'access-token-value']) |
| 25 | + ); |
| 26 | + |
| 27 | + $this->assertSame($result, $identity); |
| 28 | + $this->assertEquals( |
| 29 | + [ |
| 30 | + 'findIdentityByToken' => [ |
| 31 | + 'token' => 'access-token-value', |
| 32 | + 'type' => null, |
| 33 | + ], |
| 34 | + ], |
| 35 | + $identityRepository->getCallParams() |
| 36 | + ); |
| 37 | + } |
| 38 | + |
| 39 | + public function testWithoutToken(): void |
| 40 | + { |
| 41 | + $identity = $this->createIdentity(); |
| 42 | + $identityRepository = new FakeIdentityRepository($identity); |
| 43 | + |
| 44 | + $result = (new HttpCookie($identityRepository))->authenticate( |
| 45 | + $this->createRequest() |
| 46 | + ); |
| 47 | + |
| 48 | + $this->assertNull($result); |
| 49 | + } |
| 50 | + |
| 51 | + public function testIdentityNotFoundByToken(): void |
| 52 | + { |
| 53 | + $identityRepository = new FakeIdentityRepository(null); |
| 54 | + $authenticationMethod = new HttpCookie($identityRepository); |
| 55 | + |
| 56 | + $this->assertNull( |
| 57 | + $authenticationMethod->authenticate( |
| 58 | + $this->createRequest(['access-token' => 'access-token-value']) |
| 59 | + ) |
| 60 | + ); |
| 61 | + } |
| 62 | + |
| 63 | + public function testChallengeImmutabilityStatus(): void |
| 64 | + { |
| 65 | + $response = new Response(400); |
| 66 | + $identityRepository = new FakeIdentityRepository($this->createIdentity()); |
| 67 | + $authenticationMethod = new HttpCookie($identityRepository); |
| 68 | + |
| 69 | + $this->assertSame($response, $authenticationMethod->challenge($response)); |
| 70 | + } |
| 71 | + |
| 72 | + public function testCustomTokenParam(): void |
| 73 | + { |
| 74 | + $identityRepository = new FakeIdentityRepository($this->createIdentity()); |
| 75 | + $authenticationMethod = (new HttpCookie($identityRepository)) |
| 76 | + ->withCookieName('AuthToken'); |
| 77 | + |
| 78 | + $result = $authenticationMethod->authenticate( |
| 79 | + $this->createRequest(['AuthToken' => 'AccessTokenValue']) |
| 80 | + ); |
| 81 | + |
| 82 | + $this->assertNotNull($result); |
| 83 | + $this->assertEquals('test-id', $result->getId()); |
| 84 | + } |
| 85 | + |
| 86 | + public function testImmutability(): void |
| 87 | + { |
| 88 | + $identityRepository = new FakeIdentityRepository($this->createIdentity()); |
| 89 | + $original = (new HttpCookie($identityRepository)); |
| 90 | + $this->assertNotSame($original, $original->withCookieName('cookieName')); |
| 91 | + $this->assertNotSame($original, $original->withTokenType('another-token-type')); |
| 92 | + } |
| 93 | + |
| 94 | + public function testWithTokenType(): void |
| 95 | + { |
| 96 | + $identityRepository = new FakeIdentityRepository($this->createIdentity()); |
| 97 | + (new HttpCookie($identityRepository)) |
| 98 | + ->withTokenType('another-token-type') |
| 99 | + ->authenticate( |
| 100 | + $this->createRequest(['access-token' => 'access-token-value']) |
| 101 | + ); |
| 102 | + |
| 103 | + $this->assertEquals( |
| 104 | + [ |
| 105 | + 'findIdentityByToken' => [ |
| 106 | + 'token' => 'access-token-value', |
| 107 | + 'type' => 'another-token-type', |
| 108 | + ], |
| 109 | + ], |
| 110 | + $identityRepository->getCallParams() |
| 111 | + ); |
| 112 | + } |
| 113 | + |
| 114 | + private function createIdentity(): IdentityInterface |
| 115 | + { |
| 116 | + return new FakeIdentity('test-id'); |
| 117 | + } |
| 118 | + |
| 119 | + private function createRequest(array $cookieParams = []): ServerRequestInterface |
| 120 | + { |
| 121 | + return (new ServerRequest(Method::GET, '/')) |
| 122 | + ->withCookieParams($cookieParams); |
| 123 | + } |
| 124 | +} |
0 commit comments