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

JWTAuthenticatorTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 103
Duplicated Lines 13.59 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
c 1
b 0
f 0
dl 14
loc 103
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
B testRSAKey() 0 29 1
A testValidToken() 0 10 1
A tearDown() 0 3 1
A testInvalidToken() 13 13 1
A testInvalidUniqueID() 0 14 1
A setUp() 0 16 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Firesphere\GraphQLJWT\tests;
4
5
use Firesphere\GraphQLJWT\CreateTokenMutationCreator;
6
use Firesphere\GraphQLJWT\JWTAuthenticator;
7
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...
8
use JWTException;
9
use SilverStripe\Control\Director;
10
use SilverStripe\Control\HTTPRequest;
11
use SilverStripe\Core\Environment;
12
use SilverStripe\Core\Injector\Injector;
13
use SilverStripe\Dev\SapphireTest;
14
use SilverStripe\Security\Member;
15
16
class JWTAuthenticatorTest extends SapphireTest
17
{
18
    protected static $fixture_file = '../fixtures/JWTAuthenticatorTest.yml';
19
20
    protected $member;
21
22
    protected $token;
23
24
    public function setUp()
25
    {
26
        Environment::putEnv('JWT_SIGNER_KEY=test_signer');
27
28
        parent::setUp();
29
        $this->member = $this->objFromFixture(Member::class, 'admin');
30
        $createToken = Injector::inst()->get(CreateTokenMutationCreator::class);
31
32
        $response = $createToken->resolve(
33
            null,
34
            ['Email' => '[email protected]', 'Password' => 'error'],
35
            [],
36
            new ResolveInfo([])
37
        );
38
39
        $this->token = $response->Token;
40
    }
41
42
    public function tearDown()
43
    {
44
        parent::tearDown();
45
    }
46
47
    public function testValidToken()
48
    {
49
        $authenticator = Injector::inst()->get(JWTAuthenticator::class);
50
        $request = new HTTPRequest('POST', Director::absoluteBaseURL() . '/graphql');
51
        $request->addHeader('Authorization', 'Bearer ' . $this->token);
52
53
        $result = $authenticator->authenticate(['token' => $this->token], $request);
54
55
        $this->assertInstanceOf(Member::class, $result);
56
        $this->assertEquals($this->member->ID, $result->ID);
57
    }
58
59 View Code Duplication
    public function testInvalidToken()
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...
60
    {
61
        Environment::putEnv('JWT_SIGNER_KEY=string');
62
63
        $authenticator = Injector::inst()->get(JWTAuthenticator::class);
64
        $request = new HTTPRequest('POST', Director::absoluteBaseURL() . '/graphql');
65
        $request->addHeader('Authorization', 'Bearer ' . $this->token);
66
67
        $result = $authenticator->authenticate(['token' => $this->token], $request);
68
69
        $this->assertNotInstanceOf(Member::class, $result);
70
71
        Environment::putEnv('JWT_SIGNER_KEY=test_signer');
72
    }
73
74
    public function testInvalidUniqueID()
75
    {
76
        $authenticator = Injector::inst()->get(JWTAuthenticator::class);
77
        $request = new HTTPRequest('POST', Director::absoluteBaseURL() . '/graphql');
78
        $request->addHeader('Authorization', 'Bearer ' . $this->token);
79
80
        // Invalidate the Unique ID by making it something arbitrarily wrong
81
        $member = Member::get()->filter(['Email' => '[email protected]'])->first();
82
        $member->JWTUniqueID = 'make_error';
83
        $member->write();
84
85
        $result = $authenticator->authenticate(['token' => $this->token], $request);
86
87
        $this->assertNull($result);
88
    }
89
90
    public function testRSAKey()
91
    {
92
        Environment::putEnv('JWT_SIGNER_KEY=graphql-jwt/tests/keys/private.key');
93
        Environment::putEnv('JWT_PUBLIC_KEY=graphql-jwt/tests/keys/public.pub');
94
95
        $createToken = Injector::inst()->get(CreateTokenMutationCreator::class);
96
97
        $response = $createToken->resolve(
98
            null,
99
            ['Email' => '[email protected]', 'Password' => 'error'],
100
            [],
101
            new ResolveInfo([])
102
        );
103
104
        $token = $response->Token;
105
106
        $authenticator = Injector::inst()->get(JWTAuthenticator::class);
107
        $request = new HTTPRequest('POST', Director::absoluteBaseURL() . '/graphql');
108
        $request->addHeader('Authorization', 'Bearer ' . $token);
109
110
        $result = $authenticator->authenticate(['token' => $token], $request);
111
112
        $this->assertInstanceOf(Member::class, $result);
113
        $this->assertEquals($this->member->ID, $result->ID);
114
115
        Environment::putEnv('JWT_SIGNER_KEY=test_signer');
116
        // After changing the key to a string, the token should be invalid
117
        $result = $authenticator->authenticate(['token' => $token], $request);
118
        $this->assertNull($result);
119
    }
120
}
121