Completed
Push — master ( 3eb757...3bc7c8 )
by Oleg
03:46
created

GetTokenCest::createTokenSuccess()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
c 0
b 0
f 0
rs 9.52
cc 1
nc 1
nop 1
1
<?php
2
declare(strict_types=1);
3
4
namespace codecept\authentication;
5
6
use codecept\ApiTester;
7
use codecept\Helper\CleanDoctrine2;
8
use Codeception\Util\HttpCode;
9
use SlayerBirden\DataFlowServer\Authentication\Entities\Password;
10
use SlayerBirden\DataFlowServer\Authentication\Repository\PasswordRepository;
11
use SlayerBirden\DataFlowServer\Authentication\Service\PasswordManager;
12
use SlayerBirden\DataFlowServer\Authorization\Entities\Permission;
13
use SlayerBirden\DataFlowServer\Domain\Entities\User;
14
15
class GetTokenCest
16
{
17
    /**
18
     * @var CleanDoctrine2
19
     */
20
    private $doctrine;
21
22
    public function _inject(CleanDoctrine2 $cleanDoctrine2)
23
    {
24
        $this->doctrine = $cleanDoctrine2;
25
    }
26
27
    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...
28
    {
29
        $userId = $I->haveInRepository(User::class, [
30
            'first' => 'Tester2',
31
            'last' => 'Tester2',
32
            'email' => '[email protected]',
33
        ]);
34
35
        $user = $I->grabEntityFromRepository(User::class, ['id' => $userId]);
36
37
        $logger = new \Monolog\Logger('log', [
38
            new \Monolog\Handler\NoopHandler()
39
        ]);
40
        $passwordManager = new PasswordManager(
41
            new PasswordRepository($this->doctrine->registry),
42
            $logger
43
        );
44
        $I->haveInRepository(Password::class, [
45
            'owner' => $user,
46
            'hash' => $passwordManager->getHash('test123'),
47
            'createdAt' => new \DateTime(),
48
            'due' => new \DateTime('+1 year'),
49
            'active' => true,
50
        ]);
51
52
        $resources = [
53
            'create_password',
54
        ];
55
        foreach ($resources as $key => $resource) {
56
            $I->haveInRepository(Permission::class, [
57
                'id' => ++$key,
58
                'user' => $user,
59
                'resource' => $resource,
60
            ]);
61
        }
62
        // cancel current Auth header
63
        $I->deleteHeader('Authorization');
64
    }
65
66
    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...
67
    {
68
        $I->wantTo('get token for performing operations with the app');
69
70
        $I->haveHttpHeader('Content-Type', 'application/json');
71
        $I->sendPOST('/gettoken', [
72
            'user' => '[email protected]',
73
            'password' => 'test123',
74
            'resources' => [
75
                'create_password',
76
            ],
77
        ]);
78
        $I->seeResponseCodeIs(HttpCode::OK);
79
        $I->seeResponseContainsJson([
80
            'success' => true,
81
            'data' => [
82
                'token' => [
83
                    'owner' => [
84
                        'email' => '[email protected]',
85
                    ],
86
                    'active' => 1,
87
                ],
88
            ],
89
        ]);
90
    }
91
92
    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...
93
    {
94
        $I->wantTo('attempt to get token, but specify wrong password');
95
96
        $I->haveHttpHeader('Content-Type', 'application/json');
97
        $I->sendPOST('/gettoken', [
98
            'user' => '[email protected]',
99
            'password' => 'abracadabra111',
100
            'resources' => [
101
                'create_password',
102
            ],
103
        ]);
104
        $I->seeResponseCodeIs(HttpCode::UNAUTHORIZED);
105
        $I->seeResponseContainsJson([
106
            'success' => false,
107
            'data' => [
108
                'token' => null,
109
            ],
110
        ]);
111
    }
112
113
    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...
114
    {
115
        $I->wantTo('attempt to get token for resource that is not permitted');
116
117
        $I->haveHttpHeader('Content-Type', 'application/json');
118
        $I->sendPOST('/gettoken', [
119
            'user' => '[email protected]',
120
            'password' => 'test123',
121
            'resources' => [
122
                'get_tmp_token',
123
            ],
124
        ]);
125
        $I->seeResponseCodeIs(HttpCode::FORBIDDEN);
126
        $I->seeResponseContainsJson([
127
            'success' => false,
128
            'data' => [
129
                'token' => null,
130
            ],
131
        ]);
132
    }
133
134
    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...
135
    {
136
        $I->wantTo('attempt to get token with wrong parameters');
137
138
        $I->haveHttpHeader('Content-Type', 'application/json');
139
        $I->sendPOST('/gettoken', [
140
            'user' => '[email protected]',
141
            'resources' => [
142
                'create_password',
143
            ],
144
        ]);
145
        $I->seeResponseCodeIs(HttpCode::BAD_REQUEST);
146
        $I->seeResponseContainsJson([
147
            'success' => false,
148
            'data' => [
149
                'token' => null,
150
                'validation' => [
151
                    [
152
                        'field' => 'password',
153
                    ],
154
                ]
155
            ],
156
        ]);
157
    }
158
159
    public function createTokenEmptyResources(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...
160
    {
161
        $I->wantTo('attempt to get token with empty resources');
162
163
        $I->haveHttpHeader('Content-Type', 'application/json');
164
        $I->sendPOST('/gettoken', [
165
            'user' => '[email protected]',
166
            'password' => 'test123',
167
        ]);
168
        $I->seeResponseCodeIs(HttpCode::BAD_REQUEST);
169
        $I->seeResponseContainsJson(['validation' => [
170
            [
171
                'field' => 'resources',
172
            ],
173
        ]]);
174
    }
175
}
176