-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathAccessTokenControllerTest.php
More file actions
141 lines (117 loc) · 5.26 KB
/
AccessTokenControllerTest.php
File metadata and controls
141 lines (117 loc) · 5.26 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<?php
declare(strict_types=1);
namespace SimpleSAML\Test\Module\oidc\unit\Controllers;
use Laminas\Diactoros\Response;
use Laminas\Diactoros\ServerRequest;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\ResponseInterface;
use SimpleSAML\Module\oidc\Bridges\PsrHttpBridge;
use SimpleSAML\Module\oidc\Controllers\AccessTokenController;
use SimpleSAML\Module\oidc\Controllers\Traits\RequestTrait;
use SimpleSAML\Module\oidc\Repositories\AllowedOriginRepository;
use SimpleSAML\Module\oidc\Server\AuthorizationServer;
use SimpleSAML\Module\oidc\Services\ErrorResponder;
use Symfony\Bridge\PsrHttpMessage\Factory\HttpFoundationFactory;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
/**
* @covers \SimpleSAML\Module\oidc\Controllers\AccessTokenController
*/
class AccessTokenControllerTest extends TestCase
{
protected MockObject $authorizationServerMock;
protected MockObject $allowedOriginRepository;
protected MockObject $serverRequestMock;
protected MockObject $responseMock;
protected MockObject $psrHttpBridgeMock;
protected MockObject $errorResponderMock;
protected MockObject $requestFactoryMock;
protected MockObject $responseFactoryMock;
protected MockObject $symfonyRequestMock;
protected MockObject $symfonyResponseMock;
protected MockObject $httpFoundationFactoryMock;
protected MockObject $responseHeaderBagMock;
/**
* @throws \Exception
*/
protected function setUp(): void
{
$this->authorizationServerMock = $this->createMock(AuthorizationServer::class);
$this->allowedOriginRepository = $this->createMock(AllowedOriginRepository::class);
$this->serverRequestMock = $this->createMock(ServerRequest::class);
$this->responseMock = $this->createMock(Response::class);
$this->errorResponderMock = $this->createMock(ErrorResponder::class);
$this->psrHttpBridgeMock = $this->createMock(PsrHttpBridge::class);
$this->responseFactoryMock = $this->createMock(ResponseFactoryInterface::class);
$this->responseFactoryMock->method('createResponse')->willReturn($this->responseMock);
$this->psrHttpBridgeMock->method('getResponseFactory')->willReturn($this->responseFactoryMock);
$this->symfonyRequestMock = $this->createMock(Request::class);
$this->symfonyResponseMock = $this->createMock(\Symfony\Component\HttpFoundation\Response::class);
$this->responseHeaderBagMock = $this->createMock(ResponseHeaderBag::class);
$this->symfonyResponseMock->headers = $this->responseHeaderBagMock;
$this->httpFoundationFactoryMock = $this->createMock(HttpFoundationFactory::class);
$this->httpFoundationFactoryMock->method('createResponse')->willReturn($this->symfonyResponseMock);
$this->psrHttpBridgeMock->method('getHttpFoundationFactory')->willReturn($this->httpFoundationFactoryMock);
}
protected function mock(): AccessTokenController
{
return new AccessTokenController(
$this->authorizationServerMock,
$this->allowedOriginRepository,
$this->psrHttpBridgeMock,
$this->errorResponderMock,
);
}
public function testItIsInitializable(): void
{
$this->assertInstanceOf(
AccessTokenController::class,
$this->mock(),
);
}
/**
* @throws \League\OAuth2\Server\Exception\OAuthServerException
*/
public function testItRespondsToAccessTokenRequest(): void
{
$this->authorizationServerMock
->expects($this->once())
->method('respondToAccessTokenRequest')
->with($this->serverRequestMock, $this->isInstanceOf(ResponseInterface::class))
->willReturn($this->responseMock);
$this->assertSame(
$this->responseMock,
$this->mock()->__invoke($this->serverRequestMock),
);
}
public function testItHandlesCorsRequest(): void
{
$this->serverRequestMock->expects($this->once())->method('getMethod')->willReturn('OPTIONS');
$this->serverRequestMock->expects($this->once())->method('getHeaderLine')->with('Origin')
->willReturn('http://localhost');
$this->allowedOriginRepository->expects($this->once())->method('has')
->with('http://localhost')
->willReturn(true);
$this->responseMock->expects($this->atLeast(4))->method('withHeader')
->willReturnSelf();
$this->responseMock->method('withBody')->willReturnSelf();
$this->mock()->__invoke($this->serverRequestMock);
}
public function testItAlwaysReturnsAccessControlAllowOrigin(): void
{
$this->authorizationServerMock
->expects($this->once())
->method('respondToAccessTokenRequest')
->willReturn($this->responseMock);
$this->responseHeaderBagMock->expects($this->once())
->method('set')
->with('Access-Control-Allow-Origin', '*');
$this->mock()->token($this->symfonyRequestMock);
}
public function testItUsesRequestTrait(): void
{
$this->assertContains(RequestTrait::class, class_uses(AccessTokenController::class));
}
}