Passed
Push — master ( a1c8ef...32b200 )
by ABDULMALIK
10:56
created

RbacMiddlewareTest::createRbacConfig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
dl 0
loc 14
rs 9.9
c 1
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Tests\Unit;
4
5
use Doctrine\ORM\ORMException;
6
use Doctrine\ORM\Query\QueryException;
7
use GuzzleHttp\Psr7\Request;
8
use GuzzleHttp\Psr7\Response;
9
use GuzzleHttp\Psr7\ServerRequest;
10
use Potievdev\SlimRbac\Component\Config\RbacConfig;
11
use Potievdev\SlimRbac\Component\RbacContainer;
12
use Potievdev\SlimRbac\Exception\ConfigNotFoundException;
13
use Potievdev\SlimRbac\Exception\CyclicException;
14
use Potievdev\SlimRbac\Exception\DatabaseException;
15
use Potievdev\SlimRbac\Exception\InvalidArgumentException;
16
use Potievdev\SlimRbac\Exception\NotUniqueException;
17
18
/**
19
 * Class for testing RbacMiddleware
20
 * Class RbacMiddlewareTest
21
 * @package Tests\Unit
22
 */
23
class RbacMiddlewareTest extends BaseTestCase
24
{
25
    /** @var callable $callable */
26
    protected $callable;
27
28
    /** @var ServerRequest $request */
29
    protected $request;
30
31
    /** @var Response $response */
32
    protected $response;
33
34
    /**
35
     * @throws CyclicException
36
     * @throws DatabaseException
37
     * @throws NotUniqueException
38
     * @throws QueryException|ORMException
39
     */
40
    public function setUp(): void
41
    {
42
        parent::setUp();
43
44
        $this->rbacManager->removeAll();
45
46
        $edit = $this->rbacManager->createPermission('edit', 'Edit permission');
47
        $write = $this->rbacManager->createPermission('write', 'Write permission');
48
49
        $moderator = $this->rbacManager->createRole('moderator', 'Moderator role');
50
        $admin = $this->rbacManager->createRole('admin', 'Admin role');
51
52
        $this->rbacManager->attachPermission($moderator, $edit);
53
        $this->rbacManager->attachPermission($admin, $write);
54
        $this->rbacManager->attachChildRole($admin, $moderator);
55
56
        $this->rbacManager->assignRoleToUser($moderator, self::MODERATOR_USER_ID);
57
        $this->rbacManager->assignRoleToUser($admin, self::ADMIN_USER_ID);
58
59
        $this->callable = function (Request $request, Response $response) {
60
            return $response;
61
        };
62
        $this->request = new ServerRequest('GET', 'write');
63
        $this->response = new Response();
64
    }
65
66
    /**
67
     * @throws QueryException
68
     * @throws InvalidArgumentException
69
     */
70
    public function testCheckAccessSuccessCase()
71
    {
72
        $middleware = (new RbacContainer())->getRbacMiddleware();
73
        $request = $this->request->withAttribute('userId', self::ADMIN_USER_ID);
74
        $response = $middleware->__invoke($request, $this->response, $this->callable);
75
        $this->assertEquals(200, $response->getStatusCode());
76
    }
77
78
    /**
79
     * @throws QueryException
80
     * @throws InvalidArgumentException
81
     */
82
    public function testCheckAccessDeniedCase()
83
    {
84
        $middleware = (new RbacContainer())->getRbacMiddleware();
85
        $request = $this->request->withAttribute('userId', self::MODERATOR_USER_ID);
86
        $response = $middleware->__invoke($request, $this->response, $this->callable);
87
        $this->assertEquals(403, $response->getStatusCode());
88
    }
89
90
    /**
91
     * @throws QueryException
92
     * @throws InvalidArgumentException
93
     * @throws ConfigNotFoundException
94
     */
95
    public function testCheckReadingUserIdFromHeader()
96
    {
97
        $middleware = (new RbacContainer($this->createRbacConfig(RbacConfig::HEADER_RESOURCE_TYPE)))
98
            ->getRbacMiddleware();
99
        $request = $this->request->withHeader('userId', self::ADMIN_USER_ID);
100
        $response = $middleware->__invoke($request, $this->response, $this->callable);
101
        $this->assertEquals(200, $response->getStatusCode());
102
    }
103
104
    /**
105
     * @throws QueryException
106
     * @throws InvalidArgumentException
107
     * @throws ConfigNotFoundException
108
     */
109
    public function testCheckReadingUserIdFromCookie()
110
    {
111
        $middleware = (new RbacContainer($this->createRbacConfig(RbacConfig::COOKIE_RESOURCE_TYPE)))
112
            ->getRbacMiddleware();
113
        $request = $this->request->withCookieParams(['userId' => self::ADMIN_USER_ID]);
114
        $response = $middleware->__invoke($request, $this->response, $this->callable);
115
        $this->assertEquals(200, $response->getStatusCode());
116
    }
117
118
    /**
119
     * @throws ConfigNotFoundException
120
     */
121
    private function createRbacConfig(?string $resourceTypeId): RbacConfig
122
    {
123
        $rbacConfig = RbacConfig::createFromConfigFile();
124
125
        return new RbacConfig(
126
            $rbacConfig->getDatabaseDriver(),
127
            $rbacConfig->getDatabaseHost(),
128
            $rbacConfig->getDatabaseUser(),
129
            $rbacConfig->getDatabasePassword(),
130
            $rbacConfig->getDatabasePort(),
131
            $rbacConfig->getDatabaseName(),
132
            $rbacConfig->getDatabaseCharset(),
133
            $rbacConfig->getUserIdFieldName(),
134
            $resourceTypeId ?? $rbacConfig->getUserIdResourceType()
135
        );
136
    }
137
138
}
139