CreatePasswordCest::authForUser()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.8333
c 0
b 0
f 0
cc 1
nc 1
nop 2
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\Authentication\Entities\Password;
9
use SlayerBirden\DataFlowServer\Authorization\Entities\Permission;
10
use SlayerBirden\DataFlowServer\Domain\Entities\User;
11
12
class CreatePasswordCest
13
{
14
    /**
15
     * @var int
16
     */
17
    private $userId;
18
19
    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...
20
    {
21
        $this->userId = $I->haveInRepository(User::class, [
22
            'first' => 'Tester2',
23
            'last' => 'Tester2',
24
            'email' => '[email protected]',
25
        ]);
26
27
        $user = $I->grabEntityFromRepository(User::class, ['id' => $this->userId]);
28
        $resources = [
29
            'create_password',
30
            'get_tmp_token',
31
        ];
32
        foreach ($resources as $key => $resource) {
33
            $I->haveInRepository(Permission::class, [
34
                'id' => ++$key,
35
                'user' => $user,
36
                'resource' => $resource,
37
            ]);
38
        }
39
    }
40
41
    /**
42
     * @param ApiTester $I
43
     * @throws \Exception
44
     */
45
    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...
46
    {
47
        $I->wantTo('create password');
48
        $this->authForUser($I, $this->userId);
49
        $I->haveHttpHeader('Content-Type', 'application/json');
50
        $I->sendPOST('/password', [
51
            'password' => 'abra cadabra',
52
        ]);
53
        $I->seeResponseCodeIs(HttpCode::OK);
54
        $I->seeResponseContainsJson([
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
            'data' => [
100
                'password' => null,
101
                'validation' => [
102
                    'field' => 'password',
103
                ]
104
            ],
105
        ]);
106
    }
107
108
    /**
109
     * @param ApiTester $I
110
     * @throws \Exception
111
     */
112
    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...
113
    {
114
        $I->wantTo('create password for user already having one');
115
        $this->authForUser($I, $this->userId);
116
        $I->haveHttpHeader('Content-Type', 'application/json');
117
118
        $user = $I->grabEntityFromRepository(User::class, ['id' => $this->userId]);
119
        $I->haveInRepository(Password::class, [
120
            'hash' => 'this is hash',
121
            'createdAt' => new \DateTime(),
122
            'due' => new \DateTime('+1 day'),
123
            'active' => 1,
124
            'owner' => $user,
125
        ]);
126
        $I->sendPOST('/password', [
127
            'password' => 'abra cadabra',
128
        ]);
129
        $I->seeResponseCodeIs(HttpCode::PRECONDITION_FAILED);
130
    }
131
132
    /**
133
     * @param ApiTester $I
134
     * @throws \Exception
135
     */
136
    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...
137
    {
138
        $I->wantTo('create password short password');
139
        $this->authForUser($I, $this->userId);
140
        $I->haveHttpHeader('Content-Type', 'application/json');
141
        $I->sendPOST('/password', [
142
            'password' => 'test123',
143
            'owner' => 'mr twister',
144
        ]);
145
        $I->seeResponseCodeIs(HttpCode::BAD_REQUEST);
146
        $I->seeResponseContainsJson([
147
            'data' => [
148
                'password' => null,
149
                'validation' => [
150
                    [
151
                        'field' => 'password'
152
                    ]
153
                ],
154
            ],
155
        ]);
156
    }
157
158
    /**
159
     * @param ApiTester $I
160
     * @throws \Exception
161
     */
162
    public function createPasswordNoPost(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...
163
    {
164
        $I->wantTo('send password creation request without any body');
165
        $this->authForUser($I, $this->userId);
166
        $I->haveHttpHeader('Content-Type', 'application/json');
167
        $I->sendPOST('/password');
168
        $I->seeResponseCodeIs(HttpCode::BAD_REQUEST);
169
        $I->seeResponseContainsJson([
170
            'data' => [
171
                'password' => null,
172
            ],
173
        ]);
174
    }
175
}
176