Completed
Pull Request — master (#12)
by ABDULMALIK
01:19
created

AuthMiddlewareTest::testSuccessCase()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 7
Ratio 100 %

Importance

Changes 0
Metric Value
dl 7
loc 7
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Tests\Unit;
4
5
use GuzzleHttp\Psr7\Request;
6
use GuzzleHttp\Psr7\Response;
7
use GuzzleHttp\Psr7\ServerRequest;
8
use Potievdev\SlimRbac\Component\AuthManager;
9
use Potievdev\SlimRbac\Component\AuthMiddleware;
10
use Potievdev\SlimRbac\Structure\AuthOptions;
11
12
/**
13
 * Class for testing AuthMiddleware
14
 * Class AuthMiddlewareTest
15
 * @package Tests\Unit
16
 */
17
class AuthMiddlewareTest extends BaseTestCase
18
{
19
    /** @var callable $callable */
20
    protected $callable;
21
22
    /** @var ServerRequest $request */
23
    protected $request;
24
25
    /** @var Response $response */
26
    protected $response;
27
28
    /**
29
     * @throws \Potievdev\SlimRbac\Exception\CyclicException
30
     * @throws \Potievdev\SlimRbac\Exception\DatabaseException
31
     * @throws \Potievdev\SlimRbac\Exception\NotUniqueException
32
     * @throws \Doctrine\ORM\Query\QueryException
33
     */
34
    public function setUp()
35
    {
36
        parent::setUp();
37
38
        $authManager = new AuthManager($this->authOptions);
39
        $authManager->removeAll();
40
41
        $edit = $authManager->createPermission('edit');
42
        $edit->setDescription('Edit permission');
43
        $authManager->addPermission($edit);
44
45
        $write = $authManager->createPermission('write');
46
        $write->setDescription('Write permission');
47
        $authManager->addPermission($write);
48
49
        $moderator = $authManager->createRole('moderator');
50
        $moderator->setDescription('Moderator role');
51
        $authManager->addRole($moderator);
52
53
        $admin = $authManager->createRole('admin');
54
        $admin->setDescription('Admin role');
55
        $authManager->addRole($admin);
56
57
        $authManager->addChildPermission($moderator, $edit);
58
        $authManager->addChildPermission($admin, $write);
59
        $authManager->addChildRole($admin, $moderator);
60
61
        $authManager->assign($moderator, self::MODERATOR_USER_ID);
62
        $authManager->assign($admin, self::ADMIN_USER_ID);
63
64
        $this->callable = function (Request $request, Response $response) {
65
            return $response;
66
        };
67
        $this->request = new ServerRequest('GET', 'write');
68
        $this->response = new Response();
69
    }
70
71
    /**
72
     * @throws \Doctrine\ORM\Query\QueryException
73
     * @throws \Potievdev\SlimRbac\Exception\InvalidArgumentException
74
     */
75 View Code Duplication
    public function testSuccessCase()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
76
    {
77
        $middleware = new AuthMiddleware($this->authOptions);
78
        $request = $this->request->withAttribute($this->authOptions->getVariableName(), self::ADMIN_USER_ID);
79
        $response = $middleware->__invoke($request, $this->response, $this->callable);
80
        $this->assertEquals(200, $response->getStatusCode());
81
    }
82
83
    /**
84
     * @throws \Doctrine\ORM\Query\QueryException
85
     * @throws \Potievdev\SlimRbac\Exception\InvalidArgumentException
86
     */
87 View Code Duplication
    public function testFailureCase()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
88
    {
89
        $middleware = new AuthMiddleware($this->authOptions);
90
        $request = $this->request->withAttribute($this->authOptions->getVariableName(), self::MODERATOR_USER_ID);
91
        $response = $middleware->__invoke($request, $this->response, $this->callable);
92
        $this->assertEquals(403, $response->getStatusCode());
93
    }
94
95
    /**
96
     * @throws \Doctrine\ORM\Query\QueryException
97
     * @throws \Potievdev\SlimRbac\Exception\InvalidArgumentException
98
     */
99 View Code Duplication
    public function testReadingUserIdFromHeader()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
100
    {
101
        $authOptions = $this->authOptions;
102
        $authOptions->setVariableStorageType(AuthOptions::HEADER_STORAGE_TYPE);
103
        $middleware = new AuthMiddleware($authOptions);
104
        $request = $this->request->withHeader($authOptions->getVariableName(), self::ADMIN_USER_ID);
105
        $response = $middleware->__invoke($request, $this->response, $this->callable);
106
        $this->assertEquals(200, $response->getStatusCode());
107
    }
108
109
    /**
110
     * @throws \Doctrine\ORM\Query\QueryException
111
     * @throws \Potievdev\SlimRbac\Exception\InvalidArgumentException
112
     */
113 View Code Duplication
    public function testReadingUserIdFromCookie()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
114
    {
115
        $authOptions = $this->authOptions;
116
        $authOptions->setVariableStorageType(AuthOptions::COOKIE_STORAGE_TYPE);
117
        $middleware = new AuthMiddleware($authOptions);
118
        $request = $this->request->withCookieParams([$authOptions->getVariableName() => self::ADMIN_USER_ID]);
119
        $response = $middleware->__invoke($request, $this->response, $this->callable);
120
        $this->assertEquals(200, $response->getStatusCode());
121
    }
122
123
}
124