Passed
Push — master ( b0573e...b2a2f5 )
by Olha
05:18
created

MultiFactorAuthRestApiCest   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 402
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 18
eloc 129
dl 0
loc 402
rs 10
c 1
b 0
f 0

17 Methods

Rating   Name   Duplication   Size   Complexity  
A activateMultiFactorAuth() 0 4 1
A requestVerifyMultiFactorAuthTypeWithoutActivation() 0 13 1
A requestVerifyMultiFactorAuthType() 0 15 1
A requestDeactivateMultiFactorAuthTypeWithoutVerification() 0 19 1
A requestDeactivateMultiFactorAuthTypeWithInvalidCode() 0 21 1
A requestActivateMultiFactorAuthTypeWithInvalidType() 0 21 1
A requestActivateMultiFactorAuthType() 0 12 1
A verifyMultiFactorAuth() 0 12 1
A loadFixtures() 0 5 1
A requestCreateCartWithActivatedMultiFactorAuth() 0 22 1
A requestDeactivateMultiFactorAuthTypeWithoutActivation() 0 11 1
A requestCreateCartWithActivatedMultiFactorAuthWithoutCode() 0 23 1
A requestCreateCartWithActivatedMultiFactorAuthButInvalidCode() 0 22 1
A requestDeactivateMultiFactorAuthType() 0 15 1
A requestVerifyMultiFactorAuthTypeWithInvalidCode() 0 18 1
A requestGetMultiFactorAuthTypes() 0 12 1
A deactivateMultiFactorAuth() 0 13 2
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\MultiFactorAuth\RestApi;
11
12
use Codeception\Util\HttpCode;
13
use PyzTest\Glue\MultiFactorAuth\MultiFactorAuthRestApiTester;
14
use PyzTest\Glue\MultiFactorAuth\RestApi\Fixtures\MultiFactorAuthRestApiFixtures;
15
use Spryker\Glue\MultiFactorAuth\MultiFactorAuthConfig;
16
17
/**
18
 * Auto-generated group annotations
19
 *
20
 * @group PyzTest
21
 * @group Glue
22
 * @group MultiFactorAuth
23
 * @group RestApi
24
 * @group MultiFactorAuthRestApiCest
25
 * Add your own group annotations below this line
26
 */
27
class MultiFactorAuthRestApiCest
28
{
29
    /**
30
     * @var string
31
     */
32
    protected const RESOURCE_MULTI_FACTOR_AUTH_TYPES = 'multi-factor-auth-types';
33
34
    /**
35
     * @var string|null
36
     */
37
    protected ?string $mfaCode = null;
38
39
    /**
40
     * @var string
41
     */
42
    protected const INVALID_MFA_CODE = '000000';
43
44
    /**
45
     * @var string
46
     */
47
    protected const INVALID_MFA_TYPE = 'invalid-type';
48
49
    /**
50
     * @var \PyzTest\Glue\MultiFactorAuth\RestApi\Fixtures\MultiFactorAuthRestApiFixtures|null
51
     */
52
    protected ?MultiFactorAuthRestApiFixtures $fixtures = null;
53
54
    /**
55
     * @param \PyzTest\Glue\MultiFactorAuth\MultiFactorAuthRestApiTester $I
56
     *
57
     * @return void
58
     */
59
    public function loadFixtures(MultiFactorAuthRestApiTester $I): void
60
    {
61
        /** @var \PyzTest\Glue\MultiFactorAuth\RestApi\Fixtures\MultiFactorAuthRestApiFixtures $fixtures */
62
        $fixtures = $I->loadFixtures(MultiFactorAuthRestApiFixtures::class);
63
        $this->fixtures = $fixtures;
64
    }
65
66
    /**
67
     * @param \PyzTest\Glue\MultiFactorAuth\MultiFactorAuthRestApiTester $I
68
     *
69
     * @return void
70
     */
71
    public function requestGetMultiFactorAuthTypes(MultiFactorAuthRestApiTester $I): void
72
    {
73
        // Arrange
74
        $I->authorizeCustomerToGlue($this->fixtures->getCustomerTransfer());
0 ignored issues
show
Bug introduced by
The method getCustomerTransfer() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

74
        $I->authorizeCustomerToGlue($this->fixtures->/** @scrutinizer ignore-call */ getCustomerTransfer());

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
75
76
        // Act
77
        $I->sendGet(MultiFactorAuthConfig::RESOURCE_MULTI_FACTOR_AUTH_TYPES);
78
79
        // Assert
80
        $I->seeResponseCodeIs(HttpCode::OK);
81
        $I->seeResponseIsJson();
82
        $I->seeResponseDataContainsResourceCollectionOfType(static::RESOURCE_MULTI_FACTOR_AUTH_TYPES);
83
    }
84
85
    /**
86
     * @param \PyzTest\Glue\MultiFactorAuth\MultiFactorAuthRestApiTester $I
87
     *
88
     * @return void
89
     */
90
    public function requestActivateMultiFactorAuthType(MultiFactorAuthRestApiTester $I): void
91
    {
92
        // Arrange
93
        $I->authorizeCustomerToGlue($this->fixtures->getCustomerTransfer());
94
95
        // Act
96
        $this->activateMultiFactorAuth($I);
97
98
        // Assert
99
        $I->seeResponseCodeIs(HttpCode::NO_CONTENT);
100
101
        $this->deactivateMultiFactorAuth($I);
102
    }
103
104
    /**
105
     * @param \PyzTest\Glue\MultiFactorAuth\MultiFactorAuthRestApiTester $I
106
     *
107
     * @return void
108
     */
109
    public function requestActivateMultiFactorAuthTypeWithInvalidType(MultiFactorAuthRestApiTester $I): void
110
    {
111
        // Arrange
112
        $I->authorizeCustomerToGlue($this->fixtures->getCustomerTransfer());
113
114
        // Act
115
        $requestPayload = [
116
            'data' => [
117
                'type' => MultiFactorAuthConfig::RESOURCE_MULTI_FACTOR_AUTH_TYPE_ACTIVATE,
118
                'attributes' => [
119
                    'type' => static::INVALID_MFA_TYPE,
120
                ],
121
            ],
122
        ];
123
        $I->sendPost($this->fixtures->generateUrl(MultiFactorAuthConfig::RESOURCE_MULTI_FACTOR_AUTH_TYPE_ACTIVATE), $requestPayload);
124
125
        // Assert
126
        $I->seeResponseCodeIs(HttpCode::BAD_REQUEST);
127
        $I->seeResponseIsJson();
128
129
        $this->deactivateMultiFactorAuth($I);
130
    }
131
132
    /**
133
     * @param \PyzTest\Glue\MultiFactorAuth\MultiFactorAuthRestApiTester $I
134
     *
135
     * @return void
136
     */
137
    public function requestVerifyMultiFactorAuthType(MultiFactorAuthRestApiTester $I): void
138
    {
139
        // Arrange
140
        $I->authorizeCustomerToGlue($this->fixtures->getCustomerTransfer());
141
142
        // Act
143
        $this->activateMultiFactorAuth($I);
144
        $I->seeResponseCodeIs(HttpCode::NO_CONTENT);
145
146
        $this->verifyMultiFactorAuth($I);
147
148
        // Assert
149
        $I->seeResponseCodeIs(HttpCode::NO_CONTENT);
150
151
        $this->deactivateMultiFactorAuth($I);
152
    }
153
154
    /**
155
     * @param \PyzTest\Glue\MultiFactorAuth\MultiFactorAuthRestApiTester $I
156
     *
157
     * @return void
158
     */
159
    public function requestVerifyMultiFactorAuthTypeWithoutActivation(MultiFactorAuthRestApiTester $I): void
160
    {
161
        // Arrange
162
        $I->authorizeCustomerToGlue($this->fixtures->getCustomerTransfer());
163
164
        // Act
165
        $this->verifyMultiFactorAuth($I);
166
167
        // Assert
168
        $I->seeResponseCodeIs(HttpCode::BAD_REQUEST);
169
        $I->seeResponseIsJson();
170
171
        $this->deactivateMultiFactorAuth($I);
172
    }
173
174
    /**
175
     * @param \PyzTest\Glue\MultiFactorAuth\MultiFactorAuthRestApiTester $I
176
     *
177
     * @return void
178
     */
179
    public function requestVerifyMultiFactorAuthTypeWithInvalidCode(MultiFactorAuthRestApiTester $I): void
180
    {
181
        // Arrange
182
        $I->authorizeCustomerToGlue($this->fixtures->getCustomerTransfer());
183
184
        $this->activateMultiFactorAuth($I);
185
        $I->seeResponseCodeIs(HttpCode::NO_CONTENT);
186
187
        // Act
188
        $I->haveHttpHeader(MultiFactorAuthConfig::HEADER_MULTI_FACTOR_AUTH_CODE, static::INVALID_MFA_CODE);
189
        $requestPayload = $this->fixtures->createRequestPayload(MultiFactorAuthConfig::RESOURCE_MULTI_FACTOR_AUTH_TYPE_VERIFY);
190
        $I->sendPost($this->fixtures->generateUrl(MultiFactorAuthConfig::RESOURCE_MULTI_FACTOR_AUTH_TYPE_VERIFY), $requestPayload);
191
192
        // Assert
193
        $I->seeResponseCodeIs(HttpCode::FORBIDDEN);
194
        $I->seeResponseIsJson();
195
196
        $this->deactivateMultiFactorAuth($I);
197
    }
198
199
    /**
200
     * @param \PyzTest\Glue\MultiFactorAuth\MultiFactorAuthRestApiTester $I
201
     *
202
     * @return void
203
     */
204
    public function requestDeactivateMultiFactorAuthType(MultiFactorAuthRestApiTester $I): void
205
    {
206
        // Arrange
207
        $I->authorizeCustomerToGlue($this->fixtures->getCustomerTransfer());
208
209
        $this->activateMultiFactorAuth($I);
210
        $I->seeResponseCodeIs(HttpCode::NO_CONTENT);
211
212
        $this->verifyMultiFactorAuth($I);
213
        $I->seeResponseCodeIs(HttpCode::NO_CONTENT);
214
215
        $this->deactivateMultiFactorAuth($I);
216
217
        // Assert
218
        $I->seeResponseCodeIs(HttpCode::NO_CONTENT);
219
    }
220
221
    /**
222
     * @param \PyzTest\Glue\MultiFactorAuth\MultiFactorAuthRestApiTester $I
223
     *
224
     * @return void
225
     */
226
    public function requestDeactivateMultiFactorAuthTypeWithoutActivation(MultiFactorAuthRestApiTester $I): void
227
    {
228
        // Arrange
229
        $I->authorizeCustomerToGlue($this->fixtures->getCustomerTransfer());
230
231
        // Act
232
        $this->deactivateMultiFactorAuth($I);
233
234
        // Assert
235
        $I->seeResponseCodeIs(HttpCode::BAD_REQUEST);
236
        $I->seeResponseIsJson();
237
    }
238
239
    /**
240
     * @param \PyzTest\Glue\MultiFactorAuth\MultiFactorAuthRestApiTester $I
241
     *
242
     * @return void
243
     */
244
    public function requestDeactivateMultiFactorAuthTypeWithInvalidCode(MultiFactorAuthRestApiTester $I): void
245
    {
246
        // Arrange
247
        $I->authorizeCustomerToGlue($this->fixtures->getCustomerTransfer());
248
249
        $this->activateMultiFactorAuth($I);
250
        $I->seeResponseCodeIs(HttpCode::NO_CONTENT);
251
252
        $this->verifyMultiFactorAuth($I);
253
        $I->seeResponseCodeIs(HttpCode::NO_CONTENT);
254
255
        // Act
256
        $I->haveHttpHeader(MultiFactorAuthConfig::HEADER_MULTI_FACTOR_AUTH_CODE, static::INVALID_MFA_CODE);
257
        $requestPayload = $this->fixtures->createRequestPayload(MultiFactorAuthConfig::RESOURCE_MULTI_FACTOR_AUTH_TYPE_DEACTIVATE);
258
        $I->sendPost($this->fixtures->generateUrl(MultiFactorAuthConfig::RESOURCE_MULTI_FACTOR_AUTH_TYPE_DEACTIVATE), $requestPayload);
259
260
        // Assert
261
        $I->seeResponseCodeIs(HttpCode::FORBIDDEN);
262
        $I->seeResponseIsJson();
263
264
        $this->deactivateMultiFactorAuth($I);
265
    }
266
267
    /**
268
     * @param \PyzTest\Glue\MultiFactorAuth\MultiFactorAuthRestApiTester $I
269
     *
270
     * @return void
271
     */
272
    public function requestDeactivateMultiFactorAuthTypeWithoutVerification(MultiFactorAuthRestApiTester $I): void
273
    {
274
        // Arrange
275
        $I->authorizeCustomerToGlue($this->fixtures->getCustomerTransfer());
276
277
        $this->activateMultiFactorAuth($I);
278
        $I->seeResponseCodeIs(HttpCode::NO_CONTENT);
279
280
        // Act
281
        $this->deactivateMultiFactorAuth($I);
282
283
        // Assert
284
        $I->seeResponseCodeIs(HttpCode::BAD_REQUEST);
285
        $I->seeResponseIsJson();
286
287
        $I->authorizeCustomerToGlue($this->fixtures->getCustomerTransfer());
288
        $this->verifyMultiFactorAuth($I);
289
290
        $this->deactivateMultiFactorAuth($I);
291
    }
292
293
    /**
294
     * @param \PyzTest\Glue\MultiFactorAuth\MultiFactorAuthRestApiTester $I
295
     *
296
     * @return void
297
     */
298
    public function requestCreateCartWithActivatedMultiFactorAuth(MultiFactorAuthRestApiTester $I): void
299
    {
300
        // Arrange
301
        $I->authorizeCustomerToGlue($this->fixtures->getCustomerTransfer());
302
303
        $this->activateMultiFactorAuth($I);
304
        $I->seeResponseCodeIs(HttpCode::NO_CONTENT);
305
306
        $this->verifyMultiFactorAuth($I);
307
        $I->seeResponseCodeIs(HttpCode::NO_CONTENT);
308
309
        $I->haveHttpHeader(MultiFactorAuthConfig::HEADER_MULTI_FACTOR_AUTH_CODE, $this->mfaCode);
310
        $requestPayload = $this->fixtures->createCartRequestPayload();
311
312
        // Act
313
        $I->sendPost($this->fixtures->generateCartUrl(), $requestPayload);
314
315
        // Assert
316
        $I->seeResponseCodeIs(HttpCode::CREATED);
317
        $I->seeResponseIsJson();
318
319
        $this->deactivateMultiFactorAuth($I);
320
    }
321
322
    /**
323
     * @param \PyzTest\Glue\MultiFactorAuth\MultiFactorAuthRestApiTester $I
324
     *
325
     * @return void
326
     */
327
    public function requestCreateCartWithActivatedMultiFactorAuthButInvalidCode(MultiFactorAuthRestApiTester $I): void
328
    {
329
        // Arrange
330
        $I->authorizeCustomerToGlue($this->fixtures->getCustomerTransfer());
331
332
        $this->activateMultiFactorAuth($I);
333
        $I->seeResponseCodeIs(HttpCode::NO_CONTENT);
334
335
        $this->verifyMultiFactorAuth($I);
336
        $I->seeResponseCodeIs(HttpCode::NO_CONTENT);
337
338
        $I->haveHttpHeader(MultiFactorAuthConfig::HEADER_MULTI_FACTOR_AUTH_CODE, static::INVALID_MFA_CODE);
339
        $requestPayload = $this->fixtures->createCartRequestPayload();
340
341
        // Act
342
        $I->sendPost($this->fixtures->generateCartUrl(), $requestPayload);
343
344
        // Assert
345
        $I->seeResponseCodeIs(HttpCode::FORBIDDEN);
346
        $I->seeResponseIsJson();
347
348
        $this->deactivateMultiFactorAuth($I);
349
    }
350
351
    /**
352
     * @param \PyzTest\Glue\MultiFactorAuth\MultiFactorAuthRestApiTester $I
353
     *
354
     * @return void
355
     */
356
    public function requestCreateCartWithActivatedMultiFactorAuthWithoutCode(MultiFactorAuthRestApiTester $I): void
357
    {
358
        // Arrange
359
        $I->authorizeCustomerToGlue($this->fixtures->getCustomerTransfer());
360
361
        $this->activateMultiFactorAuth($I);
362
        $I->seeResponseCodeIs(HttpCode::NO_CONTENT);
363
364
        $this->verifyMultiFactorAuth($I);
365
        $I->seeResponseCodeIs(HttpCode::NO_CONTENT);
366
367
        $I->unsetHttpHeader(MultiFactorAuthConfig::HEADER_MULTI_FACTOR_AUTH_CODE);
368
        $requestPayload = $this->fixtures->createCartRequestPayload();
369
370
        // Act
371
        $I->sendPost($this->fixtures->generateCartUrl(), $requestPayload);
372
373
        // Assert
374
        $I->seeResponseCodeIs(HttpCode::FORBIDDEN);
375
        $I->seeResponseIsJson();
376
        $I->seeResponseContains(MultiFactorAuthConfig::ERROR_MESSAGE_MULTI_FACTOR_AUTH_CODE_MISSING);
377
378
        $this->deactivateMultiFactorAuth($I);
379
    }
380
381
    /**
382
     * @param \PyzTest\Glue\MultiFactorAuth\MultiFactorAuthRestApiTester $I
383
     *
384
     * @return void
385
     */
386
    protected function activateMultiFactorAuth(MultiFactorAuthRestApiTester $I): void
387
    {
388
        $requestPayload = $this->fixtures->createRequestPayload(MultiFactorAuthConfig::RESOURCE_MULTI_FACTOR_AUTH_TYPE_ACTIVATE);
389
        $I->sendPost($this->fixtures->generateUrl(MultiFactorAuthConfig::RESOURCE_MULTI_FACTOR_AUTH_TYPE_ACTIVATE), $requestPayload);
390
    }
391
392
    /**
393
     * @param \PyzTest\Glue\MultiFactorAuth\MultiFactorAuthRestApiTester $I
394
     *
395
     * @return void
396
     */
397
    protected function verifyMultiFactorAuth(MultiFactorAuthRestApiTester $I): void
398
    {
399
        $this->mfaCode = $I->getCustomerMultiFactorAuthCodeFromDatabase(
400
            $this->fixtures->getCustomerTransfer(),
401
            'email',
402
        );
403
404
        $I->comment(sprintf('Using MFA code from database: %s', $this->mfaCode));
405
406
        $requestPayload = $this->fixtures->createRequestPayload(MultiFactorAuthConfig::RESOURCE_MULTI_FACTOR_AUTH_TYPE_VERIFY);
407
        $I->haveHttpHeader(MultiFactorAuthConfig::HEADER_MULTI_FACTOR_AUTH_CODE, $this->mfaCode);
408
        $I->sendPost($this->fixtures->generateUrl(MultiFactorAuthConfig::RESOURCE_MULTI_FACTOR_AUTH_TYPE_VERIFY), $requestPayload);
409
    }
410
411
    /**
412
     * @param \PyzTest\Glue\MultiFactorAuth\MultiFactorAuthRestApiTester $I
413
     *
414
     * @return void
415
     */
416
    protected function deactivateMultiFactorAuth(MultiFactorAuthRestApiTester $I): void
417
    {
418
        if ($this->mfaCode === null) {
419
            $this->mfaCode = $I->getCustomerMultiFactorAuthCodeFromDatabase(
420
                $this->fixtures->getCustomerTransfer(),
421
                'email',
422
            );
423
            $I->comment(sprintf('Using MFA code from database for deactivation: %s', $this->mfaCode));
424
        }
425
426
        $I->haveHttpHeader(MultiFactorAuthConfig::HEADER_MULTI_FACTOR_AUTH_CODE, $this->mfaCode);
427
        $requestPayload = $this->fixtures->createRequestPayload(MultiFactorAuthConfig::RESOURCE_MULTI_FACTOR_AUTH_TYPE_DEACTIVATE);
428
        $I->sendPost($this->fixtures->generateUrl(MultiFactorAuthConfig::RESOURCE_MULTI_FACTOR_AUTH_TYPE_DEACTIVATE), $requestPayload);
429
    }
430
}
431