Passed
Pull Request — master (#365)
by Dmitry
21:01
created

CustomerPasswordCest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 233
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 107
dl 0
loc 233
c 1
b 0
f 0
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A requestPatchCustomerPasswordFailsValidationDataProvider() 0 70 1
A requestPatchCustomerPasswordFailsToUseAnotherCustomerReference() 0 42 1
A requestPatchCustomerPasswordFailsValidation() 0 29 2
A _before() 0 17 1
A requestPatchCustomerPasswordUpdatesCustomerPassword() 0 28 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
namespace PyzTest\Glue\Customer\RestApi;
9
10
use Codeception\Example;
11
use Codeception\Util\HttpCode;
12
use Generated\Shared\Transfer\CustomerTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\CustomerTransfer was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
use Generated\Shared\Transfer\RestCustomerPasswordAttributesTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...swordAttributesTransfer was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
14
use Generated\Shared\Transfer\RestErrorMessageTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\RestErrorMessageTransfer was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
use PyzTest\Glue\Customer\CustomerApiTester;
16
use Spryker\Glue\CustomersRestApi\CustomersRestApiConfig;
17
use Spryker\Glue\RestRequestValidator\RestRequestValidatorConfig;
18
use Symfony\Component\HttpFoundation\Response;
19
20
/**
21
 * Auto-generated group annotations
22
 *
23
 * @group PyzTest
24
 * @group Glue
25
 * @group Customer
26
 * @group RestApi
27
 * @group CustomerPasswordCest
28
 * Add your own group annotations below this line
29
 * @group EndToEnd
30
 */
31
class CustomerPasswordCest
32
{
33
    /**
34
     * @var \PyzTest\Glue\Customer\RestApi\CustomerRestApiFixtures
35
     */
36
    protected CustomerRestApiFixtures $fixtures;
37
38
    /**
39
     * @var \Generated\Shared\Transfer\CustomerTransfer
40
     */
41
    protected CustomerTransfer $customerTransfer;
42
43
    /**
44
     * @param \PyzTest\Glue\Customer\CustomerApiTester $I
45
     *
46
     * @return void
47
     */
48
    public function _before(CustomerApiTester $I): void
49
    {
50
        /** @var \PyzTest\Glue\Customer\RestApi\CustomerRestApiFixtures $fixtures */
51
        $fixtures = $I->loadFixtures(CustomerRestApiFixtures::class);
52
53
        $this->fixtures = $fixtures;
54
55
        $this->customerTransfer = $I->haveCustomer(
56
            [
57
                CustomerTransfer::NEW_PASSWORD => 'change123',
58
                CustomerTransfer::PASSWORD => 'change123',
59
            ],
60
        );
61
        $I->confirmCustomer($this->customerTransfer);
62
63
        $oauthResponseTransfer = $I->haveAuthorizationToGlue($this->customerTransfer);
64
        $I->amBearerAuthenticated($oauthResponseTransfer->getAccessToken());
65
    }
66
67
    /**
68
     * @param \PyzTest\Glue\Customer\CustomerApiTester $I
69
     *
70
     * @return void
71
     */
72
    public function requestPatchCustomerPasswordUpdatesCustomerPassword(CustomerApiTester $I): void
73
    {
74
        // Arrange
75
        $restCustomerPasswordAttributesTransfer = (new RestCustomerPasswordAttributesTransfer())
76
            ->setConfirmPassword('qwertyuI1!')
77
            ->setNewPassword('qwertyuI1!')
78
            ->setPassword('change123');
79
80
        // Act
81
        $I->sendPatch(
82
            $I->formatUrl(
83
                '{resourceCustomerPassword}/{customerReference}',
84
                [
85
                    'resourceCustomerPassword' => CustomersRestApiConfig::RESOURCE_CUSTOMER_PASSWORD,
86
                    'customerReference' => $this->customerTransfer->getCustomerReference(),
87
                ],
88
            ),
89
            [
90
                'data' => [
91
                    'type' => CustomersRestApiConfig::RESOURCE_CUSTOMER_PASSWORD,
92
                    'id' => $this->customerTransfer->getCustomerReference(),
93
                    'attributes' => $restCustomerPasswordAttributesTransfer->modifiedToArray(true, true),
94
                ],
95
            ],
96
        );
97
98
        // Assert
99
        $I->seeResponseCodeIs(HttpCode::NO_CONTENT);
100
    }
101
102
    /**
103
     * @param \PyzTest\Glue\Customer\CustomerApiTester $I
104
     *
105
     * @return void
106
     */
107
    public function requestPatchCustomerPasswordFailsToUseAnotherCustomerReference(CustomerApiTester $I): void
108
    {
109
        // Arrange
110
        $firstCustomerTransfer = $I->haveCustomer(
111
            [
112
                CustomerTransfer::NEW_PASSWORD => 'change123',
113
                CustomerTransfer::PASSWORD => 'change123',
114
            ],
115
        );
116
        $I->confirmCustomer($firstCustomerTransfer);
117
118
        $restCustomerPasswordAttributesTransfer = (new RestCustomerPasswordAttributesTransfer())
119
            ->setConfirmPassword('qwertyuI1!')
120
            ->setNewPassword('qwertyuI1!')
121
            ->setPassword('change123');
122
123
        // Act
124
        $I->sendPatch(
125
            $I->formatUrl(
126
                '{resourceCustomerPassword}/{customerReference}',
127
                [
128
                    'resourceCustomerPassword' => CustomersRestApiConfig::RESOURCE_CUSTOMER_PASSWORD,
129
                    'customerReference' => $firstCustomerTransfer->getCustomerReference(),
130
                ],
131
            ),
132
            [
133
                'data' => [
134
                    'type' => CustomersRestApiConfig::RESOURCE_CUSTOMER_PASSWORD,
135
                    'id' => $firstCustomerTransfer->getCustomerReference(),
136
                    'attributes' => $restCustomerPasswordAttributesTransfer->modifiedToArray(true, true),
137
                ],
138
            ],
139
        );
140
141
        // Assert
142
        $I->seeResponseCodeIs(Response::HTTP_FORBIDDEN);
143
        $I->seeResponseIsJson();
144
        $I->seeResponseMatchesOpenApiSchema();
145
146
        $I->seeResponseErrorsHaveCode(CustomersRestApiConfig::RESPONSE_CODE_CUSTOMER_UNAUTHORIZED);
147
        $I->seeResponseErrorsHaveStatus(Response::HTTP_FORBIDDEN);
148
        $I->seeResponseErrorsHaveDetail(CustomersRestApiConfig::RESPONSE_DETAILS_CUSTOMER_UNAUTHORIZED);
149
    }
150
151
    /**
152
     * @dataProvider requestPatchCustomerPasswordFailsValidationDataProvider
153
     *
154
     * @param \PyzTest\Glue\Customer\CustomerApiTester $I
155
     * @param \Codeception\Example $example
156
     *
157
     * @return void
158
     */
159
    public function requestPatchCustomerPasswordFailsValidation(CustomerApiTester $I, Example $example): void
160
    {
161
        // Act
162
        $I->sendPatch(
163
            $I->formatUrl(
164
                '{resourceCustomerPassword}/{customerReference}',
165
                [
166
                    'resourceCustomerPassword' => CustomersRestApiConfig::RESOURCE_CUSTOMER_PASSWORD,
167
                    'customerReference' => $this->customerTransfer->getCustomerReference(),
168
                ],
169
            ),
170
            [
171
                'data' => [
172
                    'type' => CustomersRestApiConfig::RESOURCE_CUSTOMER_PASSWORD,
173
                    'id' => $this->customerTransfer->getCustomerReference(),
174
                    'attributes' => $example['attributes'],
175
                ],
176
            ],
177
        );
178
179
        // Assert
180
        $I->seeResponseCodeIs($example[RestErrorMessageTransfer::STATUS]);
181
        $I->seeResponseIsJson();
182
        $I->seeResponseMatchesOpenApiSchema();
183
184
        foreach ($example['errors'] as $index => $error) {
185
            $I->seeResponseErrorsHaveCode($error[RestErrorMessageTransfer::CODE], $index);
186
            $I->seeResponseErrorsHaveStatus($error[RestErrorMessageTransfer::STATUS], $index);
187
            $I->seeResponseErrorsHaveDetail($error[RestErrorMessageTransfer::DETAIL], $index);
188
        }
189
    }
190
191
    /**
192
     * @return array
193
     */
194
    protected function requestPatchCustomerPasswordFailsValidationDataProvider(): array
195
    {
196
        return [
197
            [
198
                'attributes' => [
199
                    RestCustomerPasswordAttributesTransfer::PASSWORD => 'change123',
200
                    RestCustomerPasswordAttributesTransfer::NEW_PASSWORD => 'qwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiop',
201
                    RestCustomerPasswordAttributesTransfer::CONFIRM_PASSWORD => 'qwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiop',
202
                ],
203
                RestErrorMessageTransfer::STATUS => Response::HTTP_UNPROCESSABLE_ENTITY,
204
                'errors' => [
205
                    [
206
                        RestErrorMessageTransfer::CODE => RestRequestValidatorConfig::RESPONSE_CODE_REQUEST_INVALID,
207
                        RestErrorMessageTransfer::STATUS => Response::HTTP_UNPROCESSABLE_ENTITY,
208
                        RestErrorMessageTransfer::DETAIL => 'newPassword => This value is too long. It should have 64 characters or less.',
209
                    ],
210
                    [
211
                        RestErrorMessageTransfer::CODE => RestRequestValidatorConfig::RESPONSE_CODE_REQUEST_INVALID,
212
                        RestErrorMessageTransfer::STATUS => Response::HTTP_UNPROCESSABLE_ENTITY,
213
                        RestErrorMessageTransfer::DETAIL => 'confirmPassword => This value is too long. It should have 64 characters or less.',
214
                    ],
215
                ],
216
            ],
217
            [
218
                'attributes' => [
219
                    RestCustomerPasswordAttributesTransfer::PASSWORD => 'change123',
220
                    RestCustomerPasswordAttributesTransfer::NEW_PASSWORD => 'qwe',
221
                    RestCustomerPasswordAttributesTransfer::CONFIRM_PASSWORD => 'qwe',
222
                ],
223
                RestErrorMessageTransfer::STATUS => Response::HTTP_UNPROCESSABLE_ENTITY,
224
                'errors' => [
225
                    [
226
                        RestErrorMessageTransfer::CODE => RestRequestValidatorConfig::RESPONSE_CODE_REQUEST_INVALID,
227
                        RestErrorMessageTransfer::STATUS => Response::HTTP_UNPROCESSABLE_ENTITY,
228
                        RestErrorMessageTransfer::DETAIL => 'newPassword => This value is too short. It should have 8 characters or more.',
229
                    ],
230
                    [
231
                        RestErrorMessageTransfer::CODE => RestRequestValidatorConfig::RESPONSE_CODE_REQUEST_INVALID,
232
                        RestErrorMessageTransfer::STATUS => Response::HTTP_UNPROCESSABLE_ENTITY,
233
                        RestErrorMessageTransfer::DETAIL => 'confirmPassword => This value is too short. It should have 8 characters or more.',
234
                    ],
235
                ],
236
            ],
237
            [
238
                'attributes' => [
239
                    RestCustomerPasswordAttributesTransfer::PASSWORD => 'change123',
240
                    RestCustomerPasswordAttributesTransfer::NEW_PASSWORD => 'qwertyui',
241
                    RestCustomerPasswordAttributesTransfer::CONFIRM_PASSWORD => 'qwertyui',
242
                ],
243
                RestErrorMessageTransfer::STATUS => Response::HTTP_BAD_REQUEST,
244
                'errors' => [
245
                    [
246
                        RestErrorMessageTransfer::CODE => CustomersRestApiConfig::RESPONSE_CODE_CUSTOMER_PASSWORD_INVALID_CHARACTER_SET,
247
                        RestErrorMessageTransfer::STATUS => Response::HTTP_BAD_REQUEST,
248
                        RestErrorMessageTransfer::DETAIL => CustomersRestApiConfig::RESPONSE_MESSAGE_CUSTOMER_PASSWORD_INVALID_CHARACTER_SET,
249
                    ],
250
                ],
251
            ],
252
            [
253
                'attributes' => [
254
                    RestCustomerPasswordAttributesTransfer::PASSWORD => 'change123',
255
                    RestCustomerPasswordAttributesTransfer::NEW_PASSWORD => 'qwertyuI1!eee',
256
                    RestCustomerPasswordAttributesTransfer::CONFIRM_PASSWORD => 'qwertyuI1!eee',
257
                ],
258
                RestErrorMessageTransfer::STATUS => Response::HTTP_BAD_REQUEST,
259
                'errors' => [
260
                    [
261
                        RestErrorMessageTransfer::CODE => CustomersRestApiConfig::RESPONSE_CODE_CUSTOMER_PASSWORD_SEQUENCE_NOT_ALLOWED,
262
                        RestErrorMessageTransfer::STATUS => Response::HTTP_BAD_REQUEST,
263
                        RestErrorMessageTransfer::DETAIL => CustomersRestApiConfig::RESPONSE_MESSAGE_CUSTOMER_PASSWORD_SEQUENCE_NOT_ALLOWED,
264
                    ],
265
                ],
266
            ],
267
        ];
268
    }
269
}
270