UpdatePasswordCest::_before()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 39

Duplication

Lines 0
Ratio 0 %

Importance

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