Passed
Push — master ( 42aa8f...8137e8 )
by Simon
02:14
created

ValidateTokenQueryCreatorTest::testExpiredToken()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 21
rs 9.3142
c 1
b 0
f 0
cc 1
eloc 12
nc 1
nop 0
1
<?php
2
3
namespace Firesphere\GraphQLJWT\tests;
4
5
use Firesphere\GraphQLJWT\CreateTokenMutationCreator;
6
use Firesphere\GraphQLJWT\JWTAuthenticator;
7
use Firesphere\GraphQLJWT\ValidateTokenQueryCreator;
8
use GraphQL\Type\Definition\ResolveInfo;
0 ignored issues
show
Bug introduced by
The type GraphQL\Type\Definition\ResolveInfo 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...
9
use SilverStripe\Control\Controller;
10
use SilverStripe\Control\Director;
11
use SilverStripe\Control\HTTPRequest;
12
use SilverStripe\Control\Session;
13
use SilverStripe\Core\Config\Config;
14
use SilverStripe\Core\Environment;
15
use SilverStripe\Core\Injector\Injector;
16
use SilverStripe\Dev\SapphireTest;
17
use SilverStripe\Security\Member;
18
19
class ValidateTokenQueryCreatorTest extends SapphireTest
20
{
21
    protected static $fixture_file = '../fixtures/JWTAuthenticatorTest.yml';
22
23
    protected $member;
24
25
    protected $token;
26
27 View Code Duplication
    public function setUp()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
28
    {
29
        Environment::putEnv('JWT_SIGNER_KEY=test_signer');
30
31
        parent::setUp();
32
        $this->member = $this->objFromFixture(Member::class, 'admin');
33
        $createToken = Injector::inst()->get(CreateTokenMutationCreator::class);
34
35
        $response = $createToken->resolve(
36
            null,
37
            ['Email' => '[email protected]', 'Password' => 'error'],
38
            [],
39
            new ResolveInfo([])
40
        );
41
42
        $this->token = $response->Token;
43
    }
44
45
    public function tearDown()
46
    {
47
        parent::tearDown();
48
    }
49
50
    private function buildRequest()
51
    {
52
        $request = new HTTPRequest('POST', Director::absoluteBaseURL() . '/graphql');
53
        $request->addHeader('Authorization', 'Bearer ' . $this->token);
54
55
        $request->setSession(new Session(['hello' => 'bye'])); // We need a session
56
        Controller::curr()->setRequest($request);
57
58
        return $request;
59
    }
60
61
    public function testValidateToken()
62
    {
63
        $this->buildRequest();
64
65
        $queryCreator = Injector::inst()->get(ValidateTokenQueryCreator::class);
66
        $response = $queryCreator->resolve(null, [], [], new ResolveInfo([]));
67
68
        $this->assertTrue($response['Valid']);
69
    }
70
71
    public function testExpiredToken()
72
    {
73
        Config::modify()->set(JWTAuthenticator::class, 'nbf_expiration', -5);
74
75
        $createToken = Injector::inst()->get(CreateTokenMutationCreator::class);
76
77
        $response = $createToken->resolve(
78
            null,
79
            ['Email' => '[email protected]', 'Password' => 'error'],
80
            [],
81
            new ResolveInfo([])
82
        );
83
        $this->token = $response->Token;
84
85
        $this->buildRequest();
86
87
        $queryCreator = Injector::inst()->get(ValidateTokenQueryCreator::class);
88
        $response = $queryCreator->resolve(null, [], [], new ResolveInfo([]));
89
90
        $this->assertFalse($response['Valid']);
91
        $this->assertContains('Token is expired', $response['Message']);
92
    }
93
}
94