1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Firesphere\GraphQLJWT\Tests; |
4
|
|
|
|
5
|
|
|
use Firesphere\GraphQLJWT\CreateTokenMutationCreator; |
6
|
|
|
use Firesphere\GraphQLJWT\JWTAuthenticator; |
7
|
|
|
use Firesphere\GraphQLJWT\RefreshTokenMutationCreator; |
8
|
|
|
use GraphQL\Type\Definition\ResolveInfo; |
|
|
|
|
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 RefreshTokenMutationCreatorTest extends SapphireTest |
20
|
|
|
{ |
21
|
|
|
protected static $fixture_file = '../fixtures/JWTAuthenticatorTest.yml'; |
22
|
|
|
|
23
|
|
|
protected $member; |
24
|
|
|
|
25
|
|
|
protected $token; |
26
|
|
|
|
27
|
|
|
protected $anonymousToken; |
28
|
|
|
|
29
|
|
|
public function setUp() |
30
|
|
|
{ |
31
|
|
|
Environment::putEnv('JWT_SIGNER_KEY=test_signer'); |
32
|
|
|
|
33
|
|
|
parent::setUp(); |
34
|
|
|
$this->member = $this->objFromFixture(Member::class, 'admin'); |
35
|
|
|
$createToken = Injector::inst()->get(CreateTokenMutationCreator::class); |
36
|
|
|
// Requires to be an expired token |
37
|
|
|
Config::modify()->set(JWTAuthenticator::class, 'nbf_expiration', -5); |
38
|
|
|
|
39
|
|
|
$response = $createToken->resolve( |
40
|
|
|
null, |
41
|
|
|
['Email' => '[email protected]', 'Password' => 'error'], |
42
|
|
|
[], |
43
|
|
|
new ResolveInfo([]) |
44
|
|
|
); |
45
|
|
|
|
46
|
|
|
$this->token = $response->Token; |
47
|
|
|
$response = $createToken->resolve( |
48
|
|
|
null, |
49
|
|
|
['Email' => '[email protected]', 'Password' => 'notCorrect'], |
50
|
|
|
[], |
51
|
|
|
new ResolveInfo([]) |
52
|
|
|
); |
53
|
|
|
|
54
|
|
|
$this->anonymousToken = $response->Token; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function tearDown() |
58
|
|
|
{ |
59
|
|
|
parent::tearDown(); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
private function buildRequest($anonymous = false) |
63
|
|
|
{ |
64
|
|
|
$token = $this->token; |
65
|
|
|
if ($anonymous) { |
66
|
|
|
$token = $this->anonymousToken; |
67
|
|
|
} |
68
|
|
|
$request = new HTTPRequest('POST', Director::absoluteBaseURL() . '/graphql'); |
69
|
|
|
$request->addHeader('Authorization', 'Bearer ' . $token); |
70
|
|
|
|
71
|
|
|
$request->setSession(new Session(['hello' => 'bye'])); // We need a session |
72
|
|
|
Controller::curr()->setRequest($request); |
73
|
|
|
|
74
|
|
|
return $request; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function testRefreshToken() |
78
|
|
|
{ |
79
|
|
|
$this->buildRequest(); |
80
|
|
|
|
81
|
|
|
$queryCreator = Injector::inst()->get(RefreshTokenMutationCreator::class); |
82
|
|
|
$response = $queryCreator->resolve(null, [], [], new ResolveInfo([])); |
83
|
|
|
|
84
|
|
|
$this->assertNotNull($response->Token); |
85
|
|
|
$this->assertInstanceOf(Member::class, $response); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function testAnonRefreshToken() |
89
|
|
|
{ |
90
|
|
|
$this->buildRequest(true); |
91
|
|
|
Config::modify()->set(JWTAuthenticator::class, 'anonymous_allowed', true); |
92
|
|
|
|
93
|
|
|
$queryCreator = Injector::inst()->get(RefreshTokenMutationCreator::class); |
94
|
|
|
$response = $queryCreator->resolve(null, [], [], new ResolveInfo([])); |
95
|
|
|
|
96
|
|
|
$this->assertNotNull($response->Token); |
97
|
|
|
$this->assertInstanceOf(Member::class, $response); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
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:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths