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

updatePasswordOldPasswordWrong()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

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