requestPatchCustomerPasswordUpdatesCustomerPassword()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 28
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 14
dl 0
loc 28
rs 9.7998
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
/**
4
 * This file is part of the Spryker Commerce OS.
5
 * For full license information, please view the LICENSE file that was distributed with this source code.
6
 */
7
8
declare(strict_types = 1);
9
10
namespace PyzTest\Glue\Customer\RestApi;
11
12
use Codeception\Example;
13
use Codeception\Util\HttpCode;
14
use Generated\Shared\Transfer\CustomerTransfer;
15
use Generated\Shared\Transfer\RestCustomerPasswordAttributesTransfer;
16
use Generated\Shared\Transfer\RestErrorMessageTransfer;
17
use PyzTest\Glue\Customer\CustomerApiTester;
18
use Spryker\Glue\CustomersRestApi\CustomersRestApiConfig;
19
use Spryker\Glue\RestRequestValidator\RestRequestValidatorConfig;
20
use Symfony\Component\HttpFoundation\Response;
21
22
/**
23
 * Auto-generated group annotations
24
 *
25
 * @group PyzTest
26
 * @group Glue
27
 * @group Customer
28
 * @group RestApi
29
 * @group CustomerPasswordCest
30
 * Add your own group annotations below this line
31
 * @group EndToEnd
32
 */
33
class CustomerPasswordCest
34
{
35
    /**
36
     * @var \PyzTest\Glue\Customer\RestApi\CustomerRestApiFixtures
37
     */
38
    protected CustomerRestApiFixtures $fixtures;
39
40
    /**
41
     * @var \Generated\Shared\Transfer\CustomerTransfer
42
     */
43
    protected CustomerTransfer $customerTransfer;
44
45
    /**
46
     * @param \PyzTest\Glue\Customer\CustomerApiTester $I
47
     *
48
     * @return void
49
     */
50
    public function _before(CustomerApiTester $I): void
51
    {
52
        /** @var \PyzTest\Glue\Customer\RestApi\CustomerRestApiFixtures $fixtures */
53
        $fixtures = $I->loadFixtures(CustomerRestApiFixtures::class);
54
55
        $this->fixtures = $fixtures;
56
57
        $this->customerTransfer = $I->haveCustomer(
58
            [
59
                CustomerTransfer::NEW_PASSWORD => 'change123',
60
                CustomerTransfer::PASSWORD => 'change123',
61
            ],
62
        );
63
        $I->confirmCustomer($this->customerTransfer);
64
65
        $oauthResponseTransfer = $I->haveAuthorizationToGlue($this->customerTransfer);
66
        $I->amBearerAuthenticated($oauthResponseTransfer->getAccessToken());
67
    }
68
69
    /**
70
     * @param \PyzTest\Glue\Customer\CustomerApiTester $I
71
     *
72
     * @return void
73
     */
74
    public function requestPatchCustomerPasswordUpdatesCustomerPassword(CustomerApiTester $I): void
75
    {
76
        // Arrange
77
        $restCustomerPasswordAttributesTransfer = (new RestCustomerPasswordAttributesTransfer())
78
            ->setConfirmPassword('Change!23456')
79
            ->setNewPassword('Change!23456')
80
            ->setPassword('change123');
81
82
        // Act
83
        $I->sendPatch(
84
            $I->formatUrl(
85
                '{resourceCustomerPassword}/{customerReference}',
86
                [
87
                    'resourceCustomerPassword' => CustomersRestApiConfig::RESOURCE_CUSTOMER_PASSWORD,
88
                    'customerReference' => $this->customerTransfer->getCustomerReference(),
89
                ],
90
            ),
91
            [
92
                'data' => [
93
                    'type' => CustomersRestApiConfig::RESOURCE_CUSTOMER_PASSWORD,
94
                    'id' => $this->customerTransfer->getCustomerReference(),
95
                    'attributes' => $restCustomerPasswordAttributesTransfer->modifiedToArray(true, true),
96
                ],
97
            ],
98
        );
99
100
        // Assert
101
        $I->seeResponseCodeIs(HttpCode::NO_CONTENT);
102
    }
103
104
    /**
105
     * @param \PyzTest\Glue\Customer\CustomerApiTester $I
106
     *
107
     * @return void
108
     */
109
    public function requestPatchCustomerPasswordFailsToUseAnotherCustomerReference(CustomerApiTester $I): void
110
    {
111
        // Arrange
112
        $firstCustomerTransfer = $I->haveCustomer(
113
            [
114
                CustomerTransfer::NEW_PASSWORD => 'change123',
115
                CustomerTransfer::PASSWORD => 'change123',
116
            ],
117
        );
118
        $I->confirmCustomer($firstCustomerTransfer);
119
120
        $restCustomerPasswordAttributesTransfer = (new RestCustomerPasswordAttributesTransfer())
121
            ->setConfirmPassword('Change!23456')
122
            ->setNewPassword('Change!23456')
123
            ->setPassword('change123');
124
125
        // Act
126
        $I->sendPatch(
127
            $I->formatUrl(
128
                '{resourceCustomerPassword}/{customerReference}',
129
                [
130
                    'resourceCustomerPassword' => CustomersRestApiConfig::RESOURCE_CUSTOMER_PASSWORD,
131
                    'customerReference' => $firstCustomerTransfer->getCustomerReference(),
132
                ],
133
            ),
134
            [
135
                'data' => [
136
                    'type' => CustomersRestApiConfig::RESOURCE_CUSTOMER_PASSWORD,
137
                    'id' => $firstCustomerTransfer->getCustomerReference(),
138
                    'attributes' => $restCustomerPasswordAttributesTransfer->modifiedToArray(true, true),
139
                ],
140
            ],
141
        );
142
143
        // Assert
144
        $I->seeResponseCodeIs(Response::HTTP_FORBIDDEN);
145
        $I->seeResponseIsJson();
146
        $I->seeResponseMatchesOpenApiSchema();
147
148
        $I->seeResponseErrorsHaveCode(CustomersRestApiConfig::RESPONSE_CODE_CUSTOMER_UNAUTHORIZED);
149
        $I->seeResponseErrorsHaveStatus(Response::HTTP_FORBIDDEN);
150
        $I->seeResponseErrorsHaveDetail(CustomersRestApiConfig::RESPONSE_DETAILS_CUSTOMER_UNAUTHORIZED);
151
    }
152
153
    /**
154
     * @dataProvider requestPatchCustomerPasswordFailsValidationDataProvider
155
     *
156
     * @param \PyzTest\Glue\Customer\CustomerApiTester $I
157
     * @param \Codeception\Example $example
158
     *
159
     * @return void
160
     */
161
    public function requestPatchCustomerPasswordFailsValidation(CustomerApiTester $I, Example $example): void
162
    {
163
        // Act
164
        $I->sendPatch(
165
            $I->formatUrl(
166
                '{resourceCustomerPassword}/{customerReference}',
167
                [
168
                    'resourceCustomerPassword' => CustomersRestApiConfig::RESOURCE_CUSTOMER_PASSWORD,
169
                    'customerReference' => $this->customerTransfer->getCustomerReference(),
170
                ],
171
            ),
172
            [
173
                'data' => [
174
                    'type' => CustomersRestApiConfig::RESOURCE_CUSTOMER_PASSWORD,
175
                    'id' => $this->customerTransfer->getCustomerReference(),
176
                    'attributes' => $example['attributes'],
177
                ],
178
            ],
179
        );
180
181
        // Assert
182
        $I->seeResponseCodeIs($example[RestErrorMessageTransfer::STATUS]);
183
        $I->seeResponseIsJson();
184
        $I->seeResponseMatchesOpenApiSchema();
185
186
        foreach ($example['errors'] as $index => $error) {
187
            $I->seeResponseErrorsHaveCode($error[RestErrorMessageTransfer::CODE], (string)$index);
188
            $I->seeResponseErrorsHaveStatus($error[RestErrorMessageTransfer::STATUS], (string)$index);
189
            $I->seeResponseErrorsHaveDetail($error[RestErrorMessageTransfer::DETAIL], (string)$index);
190
        }
191
    }
192
193
    /**
194
     * @return array
195
     */
196
    protected function requestPatchCustomerPasswordFailsValidationDataProvider(): array
197
    {
198
        return [
199
            [
200
                'attributes' => [
201
                    RestCustomerPasswordAttributesTransfer::PASSWORD => 'change123',
202
                    RestCustomerPasswordAttributesTransfer::NEW_PASSWORD => 'Change!23456pqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuioppqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwert',
203
                    RestCustomerPasswordAttributesTransfer::CONFIRM_PASSWORD => 'Change!23456pqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuioppqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwert',
204
                ],
205
                RestErrorMessageTransfer::STATUS => Response::HTTP_UNPROCESSABLE_ENTITY,
206
                'errors' => [
207
                    [
208
                        RestErrorMessageTransfer::CODE => RestRequestValidatorConfig::RESPONSE_CODE_REQUEST_INVALID,
209
                        RestErrorMessageTransfer::STATUS => Response::HTTP_UNPROCESSABLE_ENTITY,
210
                        RestErrorMessageTransfer::DETAIL => 'newPassword => This value is too long. It should have 128 characters or less.',
211
                    ],
212
                    [
213
                        RestErrorMessageTransfer::CODE => RestRequestValidatorConfig::RESPONSE_CODE_REQUEST_INVALID,
214
                        RestErrorMessageTransfer::STATUS => Response::HTTP_UNPROCESSABLE_ENTITY,
215
                        RestErrorMessageTransfer::DETAIL => 'confirmPassword => This value is too long. It should have 128 characters or less.',
216
                    ],
217
                ],
218
            ],
219
            [
220
                'attributes' => [
221
                    RestCustomerPasswordAttributesTransfer::PASSWORD => 'change123',
222
                    RestCustomerPasswordAttributesTransfer::NEW_PASSWORD => 'qwe',
223
                    RestCustomerPasswordAttributesTransfer::CONFIRM_PASSWORD => 'qwe',
224
                ],
225
                RestErrorMessageTransfer::STATUS => Response::HTTP_UNPROCESSABLE_ENTITY,
226
                'errors' => [
227
                    [
228
                        RestErrorMessageTransfer::CODE => RestRequestValidatorConfig::RESPONSE_CODE_REQUEST_INVALID,
229
                        RestErrorMessageTransfer::STATUS => Response::HTTP_UNPROCESSABLE_ENTITY,
230
                        RestErrorMessageTransfer::DETAIL => 'newPassword => This value is too short. It should have 12 characters or more.',
231
                    ],
232
                    [
233
                        RestErrorMessageTransfer::CODE => RestRequestValidatorConfig::RESPONSE_CODE_REQUEST_INVALID,
234
                        RestErrorMessageTransfer::STATUS => Response::HTTP_UNPROCESSABLE_ENTITY,
235
                        RestErrorMessageTransfer::DETAIL => 'newPassword => Your password must include at least one uppercase letter, one lowercase letter, one number, and one special character from the following list: !@#$%^&*()_-+=[]{}|;:<>.,/?\~. Non-Latin and other special characters are not allowed.',
236
                    ],
237
                    [
238
                        RestErrorMessageTransfer::CODE => RestRequestValidatorConfig::RESPONSE_CODE_REQUEST_INVALID,
239
                        RestErrorMessageTransfer::STATUS => Response::HTTP_UNPROCESSABLE_ENTITY,
240
                        RestErrorMessageTransfer::DETAIL => 'confirmPassword => This value is too short. It should have 12 characters or more.',
241
                    ],
242
                ],
243
            ],
244
            [
245
                'attributes' => [
246
                    RestCustomerPasswordAttributesTransfer::PASSWORD => 'change123',
247
                    RestCustomerPasswordAttributesTransfer::NEW_PASSWORD => 'Change!23456eee',
248
                    RestCustomerPasswordAttributesTransfer::CONFIRM_PASSWORD => 'Change!23456eee',
249
                ],
250
                RestErrorMessageTransfer::STATUS => Response::HTTP_BAD_REQUEST,
251
                'errors' => [
252
                    [
253
                        RestErrorMessageTransfer::CODE => CustomersRestApiConfig::RESPONSE_CODE_CUSTOMER_PASSWORD_SEQUENCE_NOT_ALLOWED,
254
                        RestErrorMessageTransfer::STATUS => Response::HTTP_BAD_REQUEST,
255
                        RestErrorMessageTransfer::DETAIL => CustomersRestApiConfig::RESPONSE_MESSAGE_CUSTOMER_PASSWORD_SEQUENCE_NOT_ALLOWED,
256
                    ],
257
                ],
258
            ],
259
        ];
260
    }
261
}
262