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

CreatePasswordCest::createPasswordSuccess()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 9.584
c 0
b 0
f 0
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 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
            'success' => true,
56
            'data' => [
57
                'password' => [
58
                    'owner' => [
59
                        'email' => '[email protected]',
60
                    ],
61
                    'active' => 1,
62
                ],
63
            ],
64
        ]);
65
    }
66
67
    /**
68
     * @param ApiTester $I
69
     * @param int $userId
70
     * @throws \Exception
71
     */
72
    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...
73
    {
74
        $I->haveHttpHeader('Content-Type', 'application/json');
75
        $I->sendPOST('/gettmptoken/' . $userId, [
76
            'resources' => [
77
                'create_password'
78
            ],
79
        ]);
80
        $I->seeResponseCodeIs(HttpCode::OK);
81
        $tmpToken = $I->grabDataFromResponseByJsonPath('data.token.token')[0];
82
83
        $I->amBearerAuthenticated($tmpToken);
84
    }
85
86
    /**
87
     * @param ApiTester $I
88
     * @throws \Exception
89
     */
90
    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...
91
    {
92
        $I->wantTo('create password while not providing one');
93
        $this->authForUser($I, $this->userId);
94
        $I->haveHttpHeader('Content-Type', 'application/json');
95
        $I->sendPOST('/password', [
96
            'bar' => 'baz',
97
        ]);
98
        $I->seeResponseCodeIs(HttpCode::BAD_REQUEST);
99
        $I->seeResponseContainsJson([
100
            'success' => false,
101
            'data' => [
102
                'password' => null,
103
                'validation' => [
104
                    'field' => 'password',
105
                ]
106
            ],
107
        ]);
108
    }
109
110
    /**
111
     * @param ApiTester $I
112
     * @throws \Exception
113
     */
114
    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...
115
    {
116
        $I->wantTo('create password for user already having one');
117
        $this->authForUser($I, $this->userId);
118
        $I->haveHttpHeader('Content-Type', 'application/json');
119
120
        $user = $I->grabEntityFromRepository(User::class, ['id' => $this->userId]);
121
        $I->haveInRepository(Password::class, [
122
            'hash' => 'this is hash',
123
            'createdAt' => new \DateTime(),
124
            'due' => new \DateTime('+1 day'),
125
            'active' => 1,
126
            'owner' => $user,
127
        ]);
128
        $I->sendPOST('/password', [
129
            'password' => 'abra cadabra',
130
        ]);
131
        $I->seeResponseCodeIs(HttpCode::PRECONDITION_FAILED);
132
        $I->seeResponseContainsJson([
133
            'success' => false,
134
        ]);
135
    }
136
137
    /**
138
     * @param ApiTester $I
139
     * @throws \Exception
140
     */
141
    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...
142
    {
143
        $I->wantTo('create password short password');
144
        $this->authForUser($I, $this->userId);
145
        $I->haveHttpHeader('Content-Type', 'application/json');
146
        $I->sendPOST('/password', [
147
            'password' => 'test123',
148
            'owner' => 'mr twister',
149
        ]);
150
        $I->seeResponseCodeIs(HttpCode::BAD_REQUEST);
151
        $I->seeResponseContainsJson([
152
            'success' => false,
153
            'data' => [
154
                'password' => null,
155
                'validation' => [
156
                    [
157
                        'field' => 'password'
158
                    ]
159
                ],
160
            ],
161
        ]);
162
    }
163
164
    /**
165
     * @param ApiTester $I
166
     * @throws \Exception
167
     */
168
    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...
169
    {
170
        $I->wantTo('send password creation request without any body');
171
        $this->authForUser($I, $this->userId);
172
        $I->haveHttpHeader('Content-Type', 'application/json');
173
        $I->sendPOST('/password');
174
        $I->seeResponseCodeIs(HttpCode::BAD_REQUEST);
175
        $I->seeResponseContainsJson([
176
            'success' => false,
177
            'data' => [
178
                'password' => null,
179
            ],
180
        ]);
181
    }
182
}
183