|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Firesphere\GraphQLJWT\Tests; |
|
4
|
|
|
|
|
5
|
|
|
use Firesphere\GraphQLJWT\Authentication\AnonymousUserAuthenticator; |
|
6
|
|
|
use Firesphere\GraphQLJWT\Mutations\CreateTokenMutationCreator; |
|
7
|
|
|
use GraphQL\Type\Definition\ResolveInfo; |
|
8
|
|
|
use SilverStripe\Core\Environment; |
|
9
|
|
|
use SilverStripe\Dev\SapphireTest; |
|
10
|
|
|
use SilverStripe\ORM\ValidationException; |
|
11
|
|
|
use SilverStripe\Security\Member; |
|
12
|
|
|
|
|
13
|
|
|
class CreateTokenMutationCreatorTest extends SapphireTest |
|
14
|
|
|
{ |
|
15
|
|
|
protected static $fixture_file = '../fixtures/JWTAuthenticatorTest.yml'; |
|
16
|
|
|
|
|
17
|
|
|
protected $member; |
|
18
|
|
|
|
|
19
|
|
|
public function setUp() |
|
20
|
|
|
{ |
|
21
|
|
|
Environment::putEnv('JWT_SIGNER_KEY=test_signer'); |
|
22
|
|
|
|
|
23
|
|
|
parent::setUp(); |
|
24
|
|
|
$this->member = $this->objFromFixture(Member::class, 'admin'); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @throws ValidationException |
|
29
|
|
|
*/ |
|
30
|
|
|
public function testResolveValid() |
|
31
|
|
|
{ |
|
32
|
|
|
$createToken = CreateTokenMutationCreator::singleton(); |
|
33
|
|
|
|
|
34
|
|
|
$response = $createToken->resolve( |
|
35
|
|
|
null, |
|
36
|
|
|
['Email' => '[email protected]', 'Password' => 'error'], |
|
37
|
|
|
[], |
|
38
|
|
|
new ResolveInfo([]) |
|
39
|
|
|
); |
|
40
|
|
|
|
|
41
|
|
|
$this->assertTrue($response['Member'] instanceof Member); |
|
42
|
|
|
$this->assertNotNull($response['Token']); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @throws ValidationException |
|
47
|
|
|
*/ |
|
48
|
|
|
public function testResolveInvalidWithAllowedAnonymous() |
|
49
|
|
|
{ |
|
50
|
|
|
$authenticator = CreateTokenMutationCreator::singleton(); |
|
51
|
|
|
|
|
52
|
|
|
// Inject custom authenticator |
|
53
|
|
|
$authenticator->setCustomAuthenticators([ |
|
54
|
|
|
AnonymousUserAuthenticator::singleton(), |
|
55
|
|
|
]); |
|
56
|
|
|
|
|
57
|
|
|
$response = $authenticator->resolve( |
|
58
|
|
|
null, |
|
59
|
|
|
['Email' => 'anonymous'], |
|
60
|
|
|
[], |
|
61
|
|
|
new ResolveInfo([]) |
|
62
|
|
|
); |
|
63
|
|
|
|
|
64
|
|
|
/** @var Member $member */ |
|
65
|
|
|
$member = $response['Member']; |
|
66
|
|
|
$this->assertTrue($member instanceof Member); |
|
67
|
|
|
$this->assertTrue($member->exists()); |
|
68
|
|
|
$this->assertEquals($member->Email, 'anonymous'); |
|
69
|
|
|
$this->assertNotNull($response['Token']); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @throws ValidationException |
|
74
|
|
|
*/ |
|
75
|
|
|
public function testResolveInvalidWithoutAllowedAnonymous() |
|
76
|
|
|
{ |
|
77
|
|
|
$authenticator = CreateTokenMutationCreator::singleton(); |
|
78
|
|
|
$response = $authenticator->resolve( |
|
79
|
|
|
null, |
|
80
|
|
|
['Email' => 'anonymous'], |
|
81
|
|
|
[], |
|
82
|
|
|
new ResolveInfo([]) |
|
83
|
|
|
); |
|
84
|
|
|
|
|
85
|
|
|
$this->assertNull($response['Member']); |
|
86
|
|
|
$this->assertNull($response['Token']); |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|