Completed
Push — master ( 178a08...5c0b6f )
by Oleg
05:22
created

GetTokenCest::createTokenValidationError()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 9.536
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
declare(strict_types=1);
3
4
use Codeception\Module\CleanDoctrine2;
5
use Codeception\Util\HttpCode;
6
use SlayerBirden\DataFlowServer\Authentication\Entities\Password;
7
use SlayerBirden\DataFlowServer\Authentication\Service\PasswordManager;
8
use SlayerBirden\DataFlowServer\Authorization\Entities\Permission;
9
use SlayerBirden\DataFlowServer\Domain\Entities\User;
10
11
class GetTokenCest
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
12
{
13
    /**
14
     * @var CleanDoctrine2
15
     */
16
    private $doctrine;
17
18
    public function _inject(CleanDoctrine2 $cleanDoctrine2)
19
    {
20
        $this->doctrine = $cleanDoctrine2;
21
    }
22
23
    public function _before(ApiTester $I)
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $I. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
24
    {
25
        $userId = $I->haveInRepository(User::class, [
26
            'first' => 'Tester2',
27
            'last' => 'Tester2',
28
            'email' => '[email protected]',
29
        ]);
30
31
        $user = $I->grabEntityFromRepository(User::class, ['id' => $userId]);
32
33
        $logger = new \Monolog\Logger('log', [
34
            new \Monolog\Handler\NoopHandler()
35
        ]);
36
        $passwordManager = new PasswordManager(
37
            $this->doctrine->em,
0 ignored issues
show
Compatibility introduced by
$this->doctrine->em of type object<Doctrine\ORM\EntityManagerInterface> is not a sub-type of object<Doctrine\ORM\EntityManager>. It seems like you assume a concrete implementation of the interface Doctrine\ORM\EntityManagerInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
38
            $logger
39
        );
40
        $I->haveInRepository(Password::class, [
41
            'owner' => $user,
42
            'hash' => $passwordManager->getHash('test123'),
43
            'createdAt' => new DateTime(),
44
            'due' => new DateTime('+1 year'),
45
            'active' => true,
46
        ]);
47
48
        $resources = [
49
            'do_something_awesome',
50
        ];
51
        foreach ($resources as $key => $resource) {
52
            $I->haveInRepository(Permission::class, [
53
                'id' => ++$key,
54
                'user' => $user,
55
                'resource' => $resource,
56
            ]);
57
        }
58
        // cancel current Auth header
59
        $I->deleteHeader('Authorization');
60
    }
61
62
    public function createTokenSuccess(ApiTester $I)
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $I. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
63
    {
64
        $I->wantTo('get token for performing operations with the app');
65
66
        $I->haveHttpHeader('Content-Type', 'application/json');
67
        $I->sendPOST('/gettoken', [
68
            'user' => '[email protected]',
69
            'password' => 'test123',
70
            'resources' => [
71
                'do_something_awesome',
72
            ],
73
        ]);
74
        $I->seeResponseCodeIs(HttpCode::OK);
75
        $I->seeResponseContainsJson([
76
            'success' => true,
77
            'data' => [
78
                'token' => [
79
                    'owner' => [
80
                        'email' => '[email protected]',
81
                    ],
82
                    'active' => 1,
83
                ],
84
            ],
85
        ]);
86
    }
87
88
    public function createTokenWrongPassword(ApiTester $I)
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $I. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
89
    {
90
        $I->wantTo('attempt to get token, but specify wrong password');
91
92
        $I->haveHttpHeader('Content-Type', 'application/json');
93
        $I->sendPOST('/gettoken', [
94
            'user' => '[email protected]',
95
            'password' => 'abracadabra111',
96
            'resources' => [
97
                'do_something_awesome',
98
            ],
99
        ]);
100
        $I->seeResponseCodeIs(HttpCode::UNAUTHORIZED);
101
        $I->seeResponseContainsJson([
102
            'success' => false,
103
            'data' => [
104
                'token' => null,
105
            ],
106
        ]);
107
    }
108
109
    public function createTokenWrongNoPermissions(ApiTester $I)
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $I. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
110
    {
111
        $I->wantTo('attempt to get token for resource that is not permitted');
112
113
        $I->haveHttpHeader('Content-Type', 'application/json');
114
        $I->sendPOST('/gettoken', [
115
            'user' => '[email protected]',
116
            'password' => 'test123',
117
            'resources' => [
118
                'do_something_less_awesome',
119
            ],
120
        ]);
121
        $I->seeResponseCodeIs(HttpCode::FORBIDDEN);
122
        $I->seeResponseContainsJson([
123
            'success' => false,
124
            'data' => [
125
                'token' => null,
126
            ],
127
        ]);
128
    }
129
130
    public function createTokenValidationError(ApiTester $I)
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $I. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
131
    {
132
        $I->wantTo('attempt to get token with wrong parameters');
133
134
        $I->haveHttpHeader('Content-Type', 'application/json');
135
        $I->sendPOST('/gettoken', [
136
            'user' => '[email protected]',
137
            'resources' => [
138
                'do_something_awesome',
139
            ],
140
        ]);
141
        $I->seeResponseCodeIs(HttpCode::BAD_REQUEST);
142
        $I->seeResponseContainsJson([
143
            'success' => false,
144
            'data' => [
145
                'token' => null,
146
                'validation' => [
147
                    [
148
                        'field' => 'password',
149
                    ],
150
                ]
151
            ],
152
        ]);
153
    }
154
}
155