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