Completed
Push — master ( 83caa4...00fff0 )
by Philip
04:58 queued 01:42
created

AccessRightsTest::testAdminRights()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 13

Duplication

Lines 17
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 17
loc 17
rs 9.4285
nc 1
cc 1
eloc 13
nop 0
1
<?php
2
3
4
namespace Dontdrinkandroot\Gitki\WebBundle\Tests\Acceptance;
5
6
use Dontdrinkandroot\Gitki\WebBundle\DataFixtures\ORM\UserReferenceTrait;
7
use Dontdrinkandroot\Gitki\WebBundle\DataFixtures\ORM\Users;
8
use Dontdrinkandroot\Gitki\WebBundle\Entity\User;
9
10
class AccessRightsTest extends BaseAcceptanceTest
11
{
12
13
    use UserReferenceTrait;
14
15
    /**
16
     * {@inheritdoc}
17
     */
18
    protected function getFixtureClasses()
19
    {
20
        return [Users::class];
21
    }
22
23
    public function testAnonymousRights()
24
    {
25
        $this->assertAccessRights('/history');
26
27
        $this->assertAccessRights('/browse/');
28
        $this->assertAccessRights('/browse/?action=list');
29
        $this->assertAccessRights('/browse/?action=file.upload');
30
        $this->assertAccessRights('/browse/?action=file.create&extension=txt');
31
        $this->assertAccessRights('/browse/?action=file.create&extension=md');
32
        $this->assertAccessRights('/browse/?action=subdirectory.create');
33
34
        $this->assertAccessRights('/browse/index.md');
35
        $this->assertAccessRights('/browse/index.md?action=history');
36
        $this->assertAccessRights('/browse/index.md?action=edit');
37
        $this->assertAccessRights('/browse/index.md?action=move');
38
        $this->assertAccessRights('/browse/index.md?action=delete');
39
    }
40
41
    public function testWatcherRights()
42
    {
43
        $this->assertAccessRights('/history', 200, $this->getUser(Users::WATCHER));
44
45
        $this->assertAccessRights('/browse/', 302, $this->getUser(Users::WATCHER));
46
        $this->assertAccessRights('/browse/?action=list', 200, $this->getUser(Users::WATCHER));
47
        $this->assertAccessRights('/browse/?action=file.upload', null, $this->getUser(Users::WATCHER));
48
        $this->assertAccessRights('/browse/?action=file.create&extension=txt', null, $this->getUser(Users::WATCHER));
49
        $this->assertAccessRights('/browse/?action=file.create&extension=md', null, $this->getUser(Users::WATCHER));
50
        $this->assertAccessRights('/browse/?action=subdirectory.create', null, $this->getUser(Users::WATCHER));
51
52
        $this->assertAccessRights('/browse/index.md', 200, $this->getUser(Users::WATCHER));
53
        $this->assertAccessRights('/browse/index.md?action=history', 200, $this->getUser(Users::WATCHER));
54
        $this->assertAccessRights('/browse/index.md?action=edit', null, $this->getUser(Users::WATCHER));
55
        $this->assertAccessRights('/browse/index.md?action=move', null, $this->getUser(Users::WATCHER));
56
        $this->assertAccessRights('/browse/index.md?action=delete', null, $this->getUser(Users::WATCHER));
57
    }
58
59 View Code Duplication
    public function testCommitterRights()
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...
60
    {
61
        $this->assertAccessRights('/history', 200, $this->getUser(Users::COMMITTER));
62
63
        $this->assertAccessRights('/browse/', 302, $this->getUser(Users::COMMITTER));
64
        $this->assertAccessRights('/browse/?action=list', 200, $this->getUser(Users::COMMITTER));
65
        $this->assertAccessRights('/browse/?action=file.upload', 200, $this->getUser(Users::COMMITTER));
66
        $this->assertAccessRights('/browse/?action=file.create&extension=txt', 200, $this->getUser(Users::COMMITTER));
67
        $this->assertAccessRights('/browse/?action=file.create&extension=md', 200, $this->getUser(Users::COMMITTER));
68
        $this->assertAccessRights('/browse/?action=subdirectory.create', 200, $this->getUser(Users::COMMITTER));
69
70
        $this->assertAccessRights('/browse/index.md', 200, $this->getUser(Users::COMMITTER));
71
        $this->assertAccessRights('/browse/index.md?action=history', 200, $this->getUser(Users::COMMITTER));
72
        $this->assertAccessRights('/browse/index.md?action=edit', 200, $this->getUser(Users::COMMITTER));
73
        $this->assertAccessRights('/browse/index.md?action=move', 200, $this->getUser(Users::COMMITTER));
74
        $this->assertAccessRights('/browse/index.md?action=delete', 302, $this->getUser(Users::COMMITTER));
75
    }
76
77 View Code Duplication
    public function testAdminRights()
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...
78
    {
79
        $this->assertAccessRights('/history', 200, $this->getUser(Users::ADMIN));
80
81
        $this->assertAccessRights('/browse/', 302, $this->getUser(Users::ADMIN));
82
        $this->assertAccessRights('/browse/?action=list', 200, $this->getUser(Users::ADMIN));
83
        $this->assertAccessRights('/browse/?action=file.upload', 200, $this->getUser(Users::ADMIN));
84
        $this->assertAccessRights('/browse/?action=file.create&extension=txt', 200, $this->getUser(Users::ADMIN));
85
        $this->assertAccessRights('/browse/?action=file.create&extension=md', 200, $this->getUser(Users::ADMIN));
86
        $this->assertAccessRights('/browse/?action=subdirectory.create', 200, $this->getUser(Users::ADMIN));
87
88
        $this->assertAccessRights('/browse/index.md', 200, $this->getUser(Users::ADMIN));
89
        $this->assertAccessRights('/browse/index.md?action=history', 200, $this->getUser(Users::ADMIN));
90
        $this->assertAccessRights('/browse/index.md?action=edit', 200, $this->getUser(Users::ADMIN));
91
        $this->assertAccessRights('/browse/index.md?action=move', 200, $this->getUser(Users::ADMIN));
92
        $this->assertAccessRights('/browse/index.md?action=delete', 302, $this->getUser(Users::ADMIN));
93
    }
94
95
    /**
96
     * @param string $url            The url to test.
97
     * @param null   $expectedStatus The expected status code. Null if login is expected.
98
     * @param User   $user           The user to test or null for anonymous.
0 ignored issues
show
Documentation introduced by
Should the type for parameter $user not be null|User?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
99
     */
100
    protected function assertAccessRights($url, $expectedStatus = null, User $user = null)
101
    {
102
        $this->logOut();
103
        if (null !== $user) {
104
            $this->logIn($user);
105
        }
106
        $this->client->request('GET', $url);
107
        $response = $this->client->getResponse();
108
109
        if (null === $expectedStatus) {
110
            $this->assertEquals(302, $response->getStatusCode(), sprintf('%s: Login expected', $url));
111
            $this->assertEquals('http://localhost/login/', $response->headers->get('Location'));
112
113
            return;
114
        }
115
116
        $this->assertEquals(
117
            $expectedStatus,
118
            $response->getStatusCode(),
119
            sprintf('%s [%s]', $url, $user !== null ? $user->getUsername() : null)
120
        );
121
    }
122
}
123