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

updatePasswordWithoutProvidingOld()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
c 0
b 0
f 0
rs 9.8333
cc 1
nc 1
nop 1
1
<?php
2
declare(strict_types=1);
3
4
namespace codecept;
5
6
use codecept\Helper\CleanDoctrine2;
7
use Codeception\Util\HttpCode;
8
use SlayerBirden\DataFlowServer\Authentication\Entities\Grant;
9
use SlayerBirden\DataFlowServer\Authentication\Entities\Password;
10
use SlayerBirden\DataFlowServer\Authentication\Entities\Token;
11
use SlayerBirden\DataFlowServer\Authentication\Repository\PasswordRepository;
12
use SlayerBirden\DataFlowServer\Authentication\Service\PasswordManager;
13
use SlayerBirden\DataFlowServer\Domain\Entities\User;
14
15
class UpdatePasswordCest
0 ignored issues
show
Complexity introduced by
The class UpdatePasswordCest has a coupling between objects value of 15. Consider to reduce the number of dependencies under 13.
Loading history...
16
{
17
    /**
18
     * @var CleanDoctrine2
19
     */
20
    private $doctrine;
21
    /**
22
     * @var int
23
     */
24
    private $userId;
25
    /**
26
     * @var \Psr\Log\LoggerInterface
27
     */
28
    private $logger;
29
    /**
30
     * @var PasswordManager
31
     */
32
    private $passwordManager;
33
34
    public function _inject(CleanDoctrine2 $cleanDoctrine2)
35
    {
36
        $this->doctrine = $cleanDoctrine2;
37
        $this->logger = new \Monolog\Logger('log', [
38
            new \Monolog\Handler\NoopHandler()
39
        ]);
40
    }
41
42
    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...
43
    {
44
        $this->userId = $I->haveInRepository(User::class, [
45
            'first' => 'Tester2',
46
            'last' => 'Tester2',
47
            'email' => '[email protected]',
48
        ]);
49
50
        $user = $I->grabEntityFromRepository(User::class, ['id' => $this->userId]);
51
        $this->passwordManager = new PasswordManager(
52
            new PasswordRepository($this->doctrine->registry),
53
            $this->logger
54
        );
55
56
        $I->haveInRepository(Password::class, [
57
            'owner' => $user,
58
            'hash' => $this->passwordManager->getHash('test123'),
59
            'createdAt' => new \DateTime(),
60
            'due' => new \DateTime('+1 year'),
61
            'active' => true,
62
        ]);
63
64
        $tokenId = $I->haveInRepository(Token::class, [
65
            'owner' => $user,
66
            'active' => true,
67
            'token' => 'yyy',
68
            'due' => new \DateTime('+1 year'),
69
            'createdAt' => new \DateTime(),
70
        ]);
71
72
        $token = $I->grabEntityFromRepository(Token::class, ['id' => $tokenId]);
73
74
        $I->haveInRepository(Grant::class, [
75
            'token' => $token,
76
            'resource' => 'update_password',
77
        ]);
78
79
        $I->amBearerAuthenticated('yyy');
80
    }
81
82
    public function updatePasswordSuccess(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...
83
    {
84
        $I->wantTo('update my password');
85
86
        $I->haveHttpHeader('Content-Type', 'application/json');
87
        $I->sendPost('/updatepassword', [
88
            'password' => 'test123',
89
            'new_password' => 'there is a clown on a wing',
90
        ]);
91
        $I->seeResponseCodeIs(HttpCode::OK);
92
        $I->seeResponseContainsJson([
93
            'success' => true,
94
        ]);
95
96
        // check that new password works
97
        /** @var User $user */
98
        $user = $I->grabEntityFromRepository(User::class, ['id' => $this->userId]);
99
        $valid = $this->passwordManager->isValidForUser('there is a clown on a wing', $user);
100
101
        $I->assertSame(true, $valid);
102
    }
103
104
    public function updatePasswordValidationError(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...
105
    {
106
        $I->wantTo('update my password but providing invalid data');
107
108
        $I->haveHttpHeader('Content-Type', 'application/json');
109
        $I->sendPost('/updatepassword', [
110
            'password' => 'test123',
111
            'new_password' => 'short',
112
        ]);
113
        $I->seeResponseCodeIs(HttpCode::BAD_REQUEST);
114
        $I->seeResponseContainsJson([
115
            'success' => false,
116
            'data' => [
117
                'validation' => [
118
                    [
119
                        'field' => 'new_password',
120
                    ]
121
                ]
122
            ]
123
        ]);
124
    }
125
126
    public function updatePasswordOldPasswordWrong(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...
127
    {
128
        $I->wantTo('update my password but providing wrong old pw');
129
130
        $I->haveHttpHeader('Content-Type', 'application/json');
131
        $I->sendPost('/updatepassword', [
132
            'password' => 'forgot',
133
            'new_password' => 'cool new password',
134
        ]);
135
        $I->seeResponseCodeIs(HttpCode::PRECONDITION_FAILED);
136
        $I->seeResponseContainsJson([
137
            'success' => false,
138
        ]);
139
    }
140
141
    public function updatePasswordWithoutProvidingOld(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('update my password without providing old one');
144
145
        $I->haveHttpHeader('Content-Type', 'application/json');
146
        $I->sendPost('/updatepassword', [
147
            'new_password' => 'cool new password',
148
        ]);
149
        $I->seeResponseCodeIs(HttpCode::PRECONDITION_FAILED);
150
        $I->seeResponseContainsJson([
151
            'success' => false,
152
        ]);
153
    }
154
155
    public function updatePasswordUseUsedPw(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...
156
    {
157
        $I->wantTo('update my password using old password which is already in the system');
158
159
        $user = $I->grabEntityFromRepository(User::class, ['id' => $this->userId]);
160
        $I->haveInRepository(Password::class, [
161
            'owner' => $user,
162
            'hash' => $this->passwordManager->getHash('old cool long password'),
163
            'createdAt' => new \DateTime('-1 year'),
164
            'due' => new \DateTime('-1 month'),
165
            'active' => false,
166
        ]);
167
168
        $I->haveHttpHeader('Content-Type', 'application/json');
169
        $I->sendPost('/updatepassword', [
170
            'password' => 'test123',
171
            'new_password' => 'old cool long password',
172
        ]);
173
        $I->seeResponseCodeIs(HttpCode::BAD_REQUEST);
174
        $I->seeResponseContainsJson([
175
            'success' => false,
176
        ]);
177
    }
178
}
179