-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathAuthorizationMiddlewareTest.php
More file actions
178 lines (149 loc) · 7.46 KB
/
AuthorizationMiddlewareTest.php
File metadata and controls
178 lines (149 loc) · 7.46 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
<?php
declare(strict_types=1);
namespace ApiTest\Unit\App\Middleware;
use Api\Admin\Entity\Admin;
use Api\Admin\Entity\AdminRole;
use Api\Admin\Enum\AdminStatusEnum;
use Api\Admin\Repository\AdminRepository;
use Api\App\Message;
use Api\App\Middleware\AuthorizationMiddleware as Subject;
use Api\App\UserIdentity;
use Api\User\Entity\User;
use Api\User\Entity\UserRole;
use Api\User\Repository\UserRepository;
use Laminas\Diactoros\ServerRequest;
use Laminas\Http\Response;
use Mezzio\Authentication\UserInterface;
use Mezzio\Authorization\AuthorizationInterface;
use PHPUnit\Framework\MockObject\Exception;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use function json_decode;
use function sprintf;
class AuthorizationMiddlewareTest extends TestCase
{
private Subject|MockObject $subject;
private UserRepository|MockObject $userRepository;
private AdminRepository|MockObject $adminRepository;
private AuthorizationInterface|MockObject $authorization;
private ServerRequestInterface $request;
private RequestHandlerInterface|MockObject $handler;
private ResponseInterface $response;
/**
* @throws Exception
*/
public function setUp(): void
{
$this->userRepository = $this->createMock(UserRepository::class);
$this->adminRepository = $this->createMock(AdminRepository::class);
$this->authorization = $this->createMock(AuthorizationInterface::class);
$this->handler = $this->createMock(RequestHandlerInterface::class);
$this->response = $this->createMock(ResponseInterface::class);
$this->request = new ServerRequest();
$this->subject = new Subject(
$this->authorization,
$this->userRepository,
$this->adminRepository
);
}
public function testAuthorizationInvalidClientIdProvided(): void
{
$identity = new UserIdentity('test@dotkernel.com', ['user'], ['oauth_client_id' => 'invalid_client_id']);
$this->request = $this->request->withAttribute(UserInterface::class, $identity);
$response = $this->subject->process($this->request, $this->handler);
$this->assertSame(Response::STATUS_CODE_403, $response->getStatusCode());
$data = json_decode($response->getBody()->getContents(), true);
$this->assertArrayHasKey('error', $data);
$this->assertArrayHasKey('messages', $data['error']);
$this->assertContains(Message::INVALID_CLIENT_ID, $data['error']['messages']);
}
public function testAuthorizationInactiveAdmin(): void
{
$user = (new Admin())
->setIdentity('admin@dotkernel.com')
->setStatus(AdminStatusEnum::Inactive)
->addRole((new AdminRole())->setName(AdminRole::ROLE_ADMIN));
$this->adminRepository->method('findOneBy')->willReturn($user);
$identity = new UserIdentity('admin@dotkernel.com', ['admin'], ['oauth_client_id' => 'admin']);
$this->request = $this->request->withAttribute(UserInterface::class, $identity);
$response = $this->subject->process($this->request, $this->handler);
$this->assertSame(Response::STATUS_CODE_403, $response->getStatusCode());
$data = json_decode($response->getBody()->getContents(), true);
$this->assertArrayHasKey('error', $data);
$this->assertArrayHasKey('messages', $data['error']);
$this->assertContains(Message::ADMIN_NOT_ACTIVATED, $data['error']['messages']);
}
public function testAuthorizationInactiveUser(): void
{
$this->userRepository->method('findOneBy')->willReturn(new User());
$identity = new UserIdentity('test@dotkernel.com', ['user'], ['oauth_client_id' => 'frontend']);
$this->request = $this->request->withAttribute(UserInterface::class, $identity);
$response = $this->subject->process($this->request, $this->handler);
$this->assertSame(Response::STATUS_CODE_403, $response->getStatusCode());
$data = json_decode($response->getBody()->getContents(), true);
$this->assertArrayHasKey('error', $data);
$this->assertArrayHasKey('messages', $data['error']);
$this->assertContains(Message::USER_NOT_ACTIVATED, $data['error']['messages']);
}
public function testAuthorizationUserNotFoundOrDeleted(): void
{
$user = (new User())->markAsDeleted();
$this->userRepository->method('findOneBy')->willReturn($user);
$this->authorization->method('isGranted')->willReturn(false);
$identity = new UserIdentity('test@dotkernel.com', ['user'], ['oauth_client_id' => 'frontend']);
$this->request = $this->request->withAttribute(UserInterface::class, $identity);
$response = $this->subject->process($this->request, $this->handler);
$this->assertSame(Response::STATUS_CODE_403, $response->getStatusCode());
$data = json_decode($response->getBody()->getContents(), true);
$this->assertArrayHasKey('error', $data);
$this->assertArrayHasKey('messages', $data['error']);
$this->assertContains(
sprintf(Message::USER_NOT_FOUND_BY_IDENTITY, $identity->getIdentity()),
$data['error']['messages']
);
}
public function testAuthorizationNotGranted(): void
{
$user = (new User())
->setIdentity('test@dotkernel.com')
->activate()
->addRole((new UserRole())->setName(UserRole::ROLE_USER));
$this->userRepository->method('findOneBy')->willReturn($user);
$identity = new UserIdentity('test@dotkernel.com', ['user'], ['oauth_client_id' => 'frontend']);
$this->request = $this->request->withAttribute(UserInterface::class, $identity);
$response = $this->subject->process($this->request, $this->handler);
$this->assertSame(Response::STATUS_CODE_403, $response->getStatusCode());
$data = json_decode($response->getBody()->getContents(), true);
$this->assertArrayHasKey('error', $data);
$this->assertArrayHasKey('messages', $data['error']);
$this->assertContains(
Message::RESOURCE_NOT_ALLOWED,
$data['error']['messages']
);
}
public function testAuthorizationAccessGranted(): void
{
$user = (new User())
->setIdentity('test@dotkernel.com')
->activate()
->addRole((new UserRole())->setName(UserRole::ROLE_USER));
$this->userRepository->method('findOneBy')->willReturn($user);
$this->authorization->method('isGranted')->willReturn(true);
$identity = new UserIdentity('test@dotkernel.com', ['user'], ['oauth_client_id' => 'frontend']);
$this->request = $this->request->withAttribute(UserInterface::class, $identity);
$this->handler
->expects($this->once())
->method('handle')
->will($this->returnCallback(function (ServerRequestInterface $request) use ($identity) {
$user = $request->getAttribute(UserInterface::class);
$this->assertSame($identity->getIdentity(), $user->getIdentity());
$this->assertSame($identity->getDetails(), $user->getDetails());
$this->assertSame($identity->getRoles(), $user->getRoles());
return $this->response;
}));
$this->subject->process($this->request, $this->handler);
}
}