InvalidateTokenCest::_before()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 27
rs 9.488
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 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 InvalidateTokenCest
13
{
14
    /**
15
     * @var int
16
     */
17
    private $tokenId;
18
19
    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...
20
    {
21
        $userId = $I->haveInRepository(User::class, [
22
            'first' => 'Tester2',
23
            'last' => 'Tester2',
24
            'email' => '[email protected]',
25
        ]);
26
27
        $user = $I->grabEntityFromRepository(User::class, ['id' => $userId]);
28
29
        $this->tokenId = $I->haveInRepository(Token::class, [
30
            'owner' => $user,
31
            'active' => true,
32
            'token' => 'yyy',
33
            'due' => new \DateTime('+1 year'),
34
            'createdAt' => new \DateTime(),
35
        ]);
36
37
        $token = $I->grabEntityFromRepository(Token::class, ['id' => $this->tokenId]);
38
39
        $I->haveInRepository(Grant::class, [
40
            'token' => $token,
41
            'resource' => 'invalidate_token',
42
        ]);
43
44
        $I->amBearerAuthenticated('yyy');
45
    }
46
47
    public function invalidateTokenSuccess(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...
48
    {
49
        $I->wantTo('invalidate my token');
50
51
        $I->haveHttpHeader('Content-Type', 'application/json');
52
        $I->sendPut('/invalidatetoken/' . (string)$this->tokenId);
53
        $I->seeResponseCodeIs(HttpCode::OK);
54
        $I->seeResponseContainsJson([
55
            'data' => [
56
                'token' => [
57
                    'owner' => [
58
                        'email' => '[email protected]',
59
                    ],
60
                    'active' => 0,
61
                ],
62
            ],
63
        ]);
64
    }
65
66
    public function invalidateTokenNoPermissions(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...
67
    {
68
        $I->wantTo('invalidate someone elses token');
69
70
        $I->haveHttpHeader('Content-Type', 'application/json');
71
        $I->sendPut('/invalidatetoken/1');
72
        $I->seeResponseCodeIs(HttpCode::FORBIDDEN);
73
        $I->seeResponseContainsJson([
74
            'data' => [],
75
        ]);
76
    }
77
78
    public function invalidateTokenCanNotFindToken(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...
79
    {
80
        $I->wantTo('invalidate token that doesn\'t exist');
81
82
        $I->haveHttpHeader('Content-Type', 'application/json');
83
        $I->sendPut('/invalidatetoken/100');
84
        $I->seeResponseCodeIs(HttpCode::NOT_FOUND);
85
        $I->seeResponseContainsJson([
86
            'data' => [
87
                'token' => null,
88
            ],
89
        ]);
90
    }
91
}
92