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

InvalidateTokensCest::_before()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 27
c 0
b 0
f 0
rs 9.488
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 Codeception\Util\HttpCode;
8
use SlayerBirden\DataFlowServer\Authentication\Entities\Grant;
9
use SlayerBirden\DataFlowServer\Authentication\Entities\Token;
10
use SlayerBirden\DataFlowServer\Domain\Entities\User;
11
12
class InvalidateTokensCest
13
{
14
    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...
15
    {
16
        $userId = $I->haveInRepository(User::class, [
17
            'first' => 'Tester2',
18
            'last' => 'Tester2',
19
            'email' => '[email protected]',
20
        ]);
21
22
        $user = $I->grabEntityFromRepository(User::class, ['id' => $userId]);
23
24
        $tokenId = $I->haveInRepository(Token::class, [
25
            'owner' => $user,
26
            'active' => true,
27
            'token' => 'yyy',
28
            'due' => new \DateTime('+1 year'),
29
            'createdAt' => new \DateTime(),
30
        ]);
31
32
        $token = $I->grabEntityFromRepository(Token::class, ['id' => $tokenId]);
33
34
        $I->haveInRepository(Grant::class, [
35
            'token' => $token,
36
            'resource' => 'invalidate_tokens',
37
        ]);
38
39
        $I->amBearerAuthenticated('yyy');
40
    }
41
42
    public function invalidateTokensSuccess(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...
43
    {
44
        $I->wantTo('invalidate other user\'s token');
45
46
        $I->haveHttpHeader('Content-Type', 'application/json');
47
        $I->sendPost('/invalidatetokens', [
48
            'users' => [1]
49
        ]);
50
        $I->seeResponseCodeIs(HttpCode::OK);
51
        $I->seeResponseContainsJson([
52
            'success' => true,
53
            'data' => [
54
                'tokens' => [
55
                    [
56
                        'owner' => [
57
                            'email' => '[email protected]',
58
                        ],
59
                        'active' => 0,
60
                    ]
61
                ],
62
                'count' => 1
63
            ],
64
        ]);
65
    }
66
67
    public function invalidateAllTokensSuccess(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...
68
    {
69
        $I->wantTo('invalidate all tokens');
70
71
        $I->haveHttpHeader('Content-Type', 'application/json');
72
        $I->sendPost('/invalidatetokens');
73
        $I->seeResponseCodeIs(HttpCode::OK);
74
        $I->seeResponseContainsJson([
75
            'success' => true,
76
            'data' => [
77
                'count' => 2
78
            ],
79
        ]);
80
    }
81
82
    public function invalidateTokesWrongUsers(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...
83
    {
84
        $I->wantTo('invalidate wrong user\'s token');
85
86
        $I->haveHttpHeader('Content-Type', 'application/json');
87
        $I->sendPost('/invalidatetokens', [
88
            'users' => [10]
89
        ]);
90
        $I->seeResponseCodeIs(HttpCode::BAD_REQUEST);
91
        $I->seeResponseContainsJson([
92
            'success' => false,
93
            'data' => [
94
                'tokens' => [],
95
                'count' => 0
96
            ],
97
        ]);
98
    }
99
}
100