|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Tests\Eole\RestApi; |
|
4
|
|
|
|
|
5
|
|
|
use Tests\Eole\RestApi\AbstractApplicationTest; |
|
6
|
|
|
|
|
7
|
|
|
class OAuthTest extends AbstractApplicationTest |
|
8
|
|
|
{ |
|
9
|
|
|
public function testCreateAccessToken() |
|
10
|
|
|
{ |
|
11
|
|
|
$client = $this->createClient(); |
|
12
|
|
|
|
|
13
|
|
|
$client->request('POST', '/oauth/access-token', array( |
|
14
|
|
|
'grant_type' => 'password', |
|
15
|
|
|
'client_id' => 'client-id', |
|
16
|
|
|
'client_secret' => 'client-secret', |
|
17
|
|
|
'username' => 'existing-player', |
|
18
|
|
|
'password' => 'good-password', |
|
19
|
|
|
)); |
|
20
|
|
|
|
|
21
|
|
|
$this->assertTrue($client->getResponse()->isSuccessful()); |
|
22
|
|
|
|
|
23
|
|
|
$result = json_decode($client->getResponse()->getContent()); |
|
24
|
|
|
|
|
25
|
|
|
$this->assertObjectHasAttribute('access_token', $result); |
|
26
|
|
|
$this->assertNotEmpty($result->access_token); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
public function testCreateAccessTokenForbiddenIfPasswordWrong() |
|
30
|
|
|
{ |
|
31
|
|
|
$client = $this->createClient(); |
|
32
|
|
|
|
|
33
|
|
|
$client->request('POST', '/oauth/access-token', array( |
|
34
|
|
|
'grant_type' => 'password', |
|
35
|
|
|
'client_id' => 'client-id', |
|
36
|
|
|
'client_secret' => 'client-secret', |
|
37
|
|
|
'username' => 'existing-player', |
|
38
|
|
|
'password' => 'wrong-password', |
|
39
|
|
|
)); |
|
40
|
|
|
|
|
41
|
|
|
$this->assertEquals(401, $client->getResponse()->getStatusCode()); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function testRefreshToken() |
|
45
|
|
|
{ |
|
46
|
|
|
$client = $this->createClient(); |
|
47
|
|
|
|
|
48
|
|
|
$client->request('POST', '/oauth/access-token', array( |
|
49
|
|
|
'grant_type' => 'password', |
|
50
|
|
|
'client_id' => 'client-id', |
|
51
|
|
|
'client_secret' => 'client-secret', |
|
52
|
|
|
'username' => 'existing-player', |
|
53
|
|
|
'password' => 'good-password', |
|
54
|
|
|
)); |
|
55
|
|
|
|
|
56
|
|
|
$accessToken = json_decode($client->getResponse()->getContent()); |
|
57
|
|
|
|
|
58
|
|
|
$client->request('POST', '/oauth/access-token', array( |
|
59
|
|
|
'grant_type' => 'refresh_token', |
|
60
|
|
|
'client_id' => 'client-id', |
|
61
|
|
|
'client_secret' => 'client-secret', |
|
62
|
|
|
'refresh_token' => $accessToken->refresh_token, |
|
63
|
|
|
)); |
|
64
|
|
|
|
|
65
|
|
|
$this->assertTrue($client->getResponse()->isSuccessful()); |
|
66
|
|
|
|
|
67
|
|
|
$freshAccessToken = json_decode($client->getResponse()->getContent()); |
|
68
|
|
|
|
|
69
|
|
|
$this->assertObjectHasAttribute('access_token', $freshAccessToken); |
|
70
|
|
|
$this->assertNotEmpty($freshAccessToken->access_token); |
|
71
|
|
|
$this->assertObjectHasAttribute('refresh_token', $freshAccessToken); |
|
72
|
|
|
$this->assertNotEmpty($freshAccessToken->refresh_token); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
public function testRefreshTokenReturnsClientErrorOnInvalidRefreshToken() |
|
76
|
|
|
{ |
|
77
|
|
|
$client = $this->createClient(); |
|
78
|
|
|
|
|
79
|
|
|
$client->request('POST', '/oauth/access-token', array( |
|
80
|
|
|
'grant_type' => 'refresh_token', |
|
81
|
|
|
'client_id' => 'client-id', |
|
82
|
|
|
'client_secret' => 'client-secret', |
|
83
|
|
|
'refresh_token' => 'invalidrefreshtoken', |
|
84
|
|
|
)); |
|
85
|
|
|
|
|
86
|
|
|
$this->assertTrue($client->getResponse()->isClientError()); |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|