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

GenerateTmpTokenCest   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 137
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 1
dl 0
loc 137
c 0
b 0
f 0
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A _before() 0 21 2
A createTmpTokenSuccess() 0 22 1
A createTmpTokenForNonExistingUser() 0 14 1
A createTmpTokenNotPermitted() 0 14 1
A createTmpTokenValidationError() 0 19 1
A createTmpTokenValidationErrorNonExistingResource() 0 14 1
1
<?php
2
declare(strict_types=1);
3
4
namespace codecept\authentication;
5
6
use codecept\ApiTester;
7
use Codeception\Util\HttpCode;
8
use SlayerBirden\DataFlowServer\Authorization\Entities\Permission;
9
use SlayerBirden\DataFlowServer\Domain\Entities\User;
10
11
class GenerateTmpTokenCest
12
{
13
    /**
14
     * @var int
15
     */
16
    private $userId;
17
18
    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...
19
    {
20
        $this->userId = $I->haveInRepository(User::class, [
21
            'first' => 'Tester2',
22
            'last' => 'Tester2',
23
            'email' => '[email protected]',
24
        ]);
25
26
        $user = $I->grabEntityFromRepository(User::class, ['id' => $this->userId]);
27
        $resources = [
28
            'create_password',
29
            'get_tmp_token',
30
        ];
31
        foreach ($resources as $key => $resource) {
32
            $I->haveInRepository(Permission::class, [
33
                'id' => ++$key,
34
                'user' => $user,
35
                'resource' => $resource,
36
            ]);
37
        }
38
    }
39
40
    /**
41
     * @param ApiTester $I
42
     * @throws \Exception
43
     */
44
    public function createTmpTokenSuccess(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...
45
    {
46
        $I->wantTo('create tmp token');
47
        $I->haveHttpHeader('Content-Type', 'application/json');
48
        $I->sendPOST('/gettmptoken/' . (string)$this->userId, [
49
            'resources' => [
50
                'create_password'
51
            ],
52
        ]);
53
        $I->seeResponseCodeIs(HttpCode::OK);
54
        $I->seeResponseContainsJson([
55
            'success' => true,
56
            'data' => [
57
                'token' => [
58
                    'owner' => [
59
                        'email' => '[email protected]',
60
                    ],
61
                    'active' => 1,
62
                ],
63
            ],
64
        ]);
65
    }
66
67
    /**
68
     * @param ApiTester $I
69
     * @throws \Exception
70
     */
71
    public function createTmpTokenForNonExistingUser(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...
72
    {
73
        $I->wantTo('create tmp token for non existing user');
74
        $I->haveHttpHeader('Content-Type', 'application/json');
75
        $I->sendPOST('/gettmptoken/' . (string)($this->userId + 100), [
76
            'resources' => [
77
                'create_password'
78
            ],
79
        ]);
80
        $I->seeResponseCodeIs(HttpCode::NOT_FOUND);
81
        $I->seeResponseContainsJson([
82
            'success' => false,
83
        ]);
84
    }
85
86
    /**
87
     * @param ApiTester $I
88
     * @throws \Exception
89
     */
90
    public function createTmpTokenNotPermitted(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...
91
    {
92
        $I->wantTo('create tmp token for resource without granted permission');
93
        $I->haveHttpHeader('Content-Type', 'application/json');
94
        $I->sendPOST('/gettmptoken/' . (string)$this->userId, [
95
            'resources' => [
96
                'update_password'
97
            ],
98
        ]);
99
        $I->seeResponseCodeIs(HttpCode::BAD_REQUEST);
100
        $I->seeResponseContainsJson([
101
            'success' => false,
102
        ]);
103
    }
104
105
    /**
106
     * @param ApiTester $I
107
     * @throws \Exception
108
     */
109
    public function createTmpTokenValidationError(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('create tmp token wrong input');
112
        $I->haveHttpHeader('Content-Type', 'application/json');
113
        $I->sendPOST('/gettmptoken/' . (string)$this->userId, [
114
            'bar' => 'baz',
115
        ]);
116
        $I->seeResponseCodeIs(HttpCode::BAD_REQUEST);
117
        $I->seeResponseContainsJson([
118
            'success' => false,
119
            'data' => [
120
                'validation' => [
121
                    [
122
                        'field' => 'resources'
123
                    ]
124
                ]
125
            ]
126
        ]);
127
    }
128
129
    /**
130
     * @param ApiTester $I
131
     * @throws \Exception
132
     */
133
    public function createTmpTokenValidationErrorNonExistingResource(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...
134
    {
135
        $I->wantTo('create tmp token for non existing resource');
136
        $I->haveHttpHeader('Content-Type', 'application/json');
137
        $I->sendPOST('/gettmptoken/' . (string)$this->userId, [
138
            'resources' => [
139
                'bar'
140
            ],
141
        ]);
142
        $I->seeResponseCodeIs(HttpCode::BAD_REQUEST);
143
        $I->seeResponseContainsJson([
144
            'success' => false,
145
        ]);
146
    }
147
}
148