Completed
Push — master ( 630401...3eb757 )
by Oleg
02:43
created

CreatePasswordCest   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 152
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

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

6 Methods

Rating   Name   Duplication   Size   Complexity  
A _before() 0 21 2
A createPasswordSuccess() 0 21 1
A authForUser() 0 13 1
A createPasswordNoPasswordProvided() 0 19 1
A createPasswordUserAlreadyHasPassword() 0 22 1
A createPasswordException() 0 22 1
1
<?php
2
declare(strict_types=1);
3
4
namespace codecept;
5
6
use Codeception\Util\HttpCode;
7
use SlayerBirden\DataFlowServer\Authentication\Entities\Password;
8
use SlayerBirden\DataFlowServer\Authorization\Entities\Permission;
9
use SlayerBirden\DataFlowServer\Domain\Entities\User;
10
11
class CreatePasswordCest
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 createPasswordSuccess(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 password');
47
        $this->authForUser($I, $this->userId);
48
        $I->haveHttpHeader('Content-Type', 'application/json');
49
        $I->sendPOST('/password', [
50
            'password' => 'abra cadabra',
51
        ]);
52
        $I->seeResponseCodeIs(HttpCode::OK);
53
        $I->seeResponseContainsJson([
54
            'success' => true,
55
            'data' => [
56
                'password' => [
57
                    'owner' => [
58
                        'email' => '[email protected]',
59
                    ],
60
                    'active' => 1,
61
                ],
62
            ],
63
        ]);
64
    }
65
66
    /**
67
     * @param ApiTester $I
68
     * @param int $userId
69
     * @throws Exception
70
     */
71
    private function authForUser(ApiTester $I, int $userId)
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->haveHttpHeader('Content-Type', 'application/json');
74
        $I->sendPOST('/gettmptoken/' . $userId, [
75
            'resources' => [
76
                'create_password'
77
            ],
78
        ]);
79
        $I->seeResponseCodeIs(HttpCode::OK);
80
        $tmpToken = $I->grabDataFromResponseByJsonPath('data.token.token')[0];
81
82
        $I->amBearerAuthenticated($tmpToken);
83
    }
84
85
    /**
86
     * @param ApiTester $I
87
     * @throws Exception
88
     */
89
    public function createPasswordNoPasswordProvided(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...
90
    {
91
        $I->wantTo('create password while not providing one');
92
        $this->authForUser($I, $this->userId);
93
        $I->haveHttpHeader('Content-Type', 'application/json');
94
        $I->sendPOST('/password', [
95
            'bar' => 'baz',
96
        ]);
97
        $I->seeResponseCodeIs(HttpCode::BAD_REQUEST);
98
        $I->seeResponseContainsJson([
99
            'success' => false,
100
            'data' => [
101
                'password' => null,
102
                'validation' => [
103
                    'field' => 'password',
104
                ]
105
            ],
106
        ]);
107
    }
108
109
    /**
110
     * @param ApiTester $I
111
     * @throws \Exception
112
     */
113
    public function createPasswordUserAlreadyHasPassword(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('create password for user already having one');
116
        $this->authForUser($I, $this->userId);
117
        $I->haveHttpHeader('Content-Type', 'application/json');
118
119
        $user = $I->grabEntityFromRepository(User::class, ['id' => $this->userId]);
120
        $I->haveInRepository(Password::class, [
121
            'hash' => 'this is hash',
122
            'createdAt' => new \DateTime(),
123
            'due' => new \DateTime('+1 day'),
124
            'active' => 1,
125
            'owner' => $user,
126
        ]);
127
        $I->sendPOST('/password', [
128
            'password' => 'abra cadabra',
129
        ]);
130
        $I->seeResponseCodeIs(HttpCode::PRECONDITION_FAILED);
131
        $I->seeResponseContainsJson([
132
            'success' => false,
133
        ]);
134
    }
135
136
    /**
137
     * @param ApiTester $I
138
     * @throws \Exception
139
     */
140
    public function createPasswordException(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...
141
    {
142
        $I->wantTo('create password short password');
143
        $this->authForUser($I, $this->userId);
144
        $I->haveHttpHeader('Content-Type', 'application/json');
145
        $I->sendPOST('/password', [
146
            'password' => 'test123',
147
            'owner' => 'mr twister',
148
        ]);
149
        $I->seeResponseCodeIs(HttpCode::BAD_REQUEST);
150
        $I->seeResponseContainsJson([
151
            'success' => false,
152
            'data' => [
153
                'password' => null,
154
                'validation' => [
155
                    [
156
                        'field' => 'password'
157
                    ]
158
                ],
159
            ],
160
        ]);
161
    }
162
}
163