-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLeagueOAuth2ProviderTest.php
110 lines (75 loc) · 3.94 KB
/
LeagueOAuth2ProviderTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<?php
namespace Dingo\Api\Tests\Auth;
use Mockery;
use Illuminate\Http\Request;
use Dingo\Api\Routing\Route;
use PHPUnit_Framework_TestCase;
use Dingo\Api\Auth\LeagueOAuth2Provider;
class LeagueOAuth2ProviderTest extends PHPUnit_Framework_TestCase
{
public function setUp()
{
$this->server = Mockery::mock('League\OAuth2\Server\ResourceServer');
$this->provider = new LeagueOAuth2Provider($this->server, false);
}
public function tearDown()
{
Mockery::close();
}
/**
* @expectedException \Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException
*/
public function testExceptionThrownWhenNoScopesProvided()
{
$request = Request::create('GET', '/', [], [], [], ['HTTP_AUTHORIZATION' => 'Bearer 12345']);
$this->server->shouldReceive('isValidRequest')->once()->andReturn(true);
$token = Mockery::mock('League\OAuth2\Server\Entity\AccessTokenEntity');
$token->shouldReceive('hasScope')->once()->with('foo')->andReturn(false);
$this->server->shouldReceive('getAccessToken')->once()->andReturn($token);
$this->provider->authenticate($request, new Route('GET', '/', ['scopes' => 'foo']));
}
public function testOnlyOneScopeRequiredToValidateCorrectly()
{
$request = Request::create('GET', '/', [], [], [], ['HTTP_AUTHORIZATION' => 'Bearer 12345']);
$this->server->shouldReceive('isValidRequest')->once()->andReturn(true);
$token = Mockery::mock('League\OAuth2\Server\Entity\AccessTokenEntity');
$token->shouldReceive('hasScope')->once()->with('foo')->andReturn(true);
$this->server->shouldReceive('getAccessToken')->once()->andReturn($token);
$session = Mockery::mock('League\OAuth2\Server\Entity\SessionEntity');
$token->shouldReceive('getSession')->once()->andReturn($session);
$session->shouldReceive('getOwnerType')->once()->andReturn('client');
$session->shouldReceive('getOwnerId')->once()->andReturn(1);
$this->provider->setClientResolver(function ($id) {});
$this->assertNull($this->provider->authenticate($request, new Route('GET', '/', ['scopes' => 'foo|bar'])));
}
public function testClientIsResolved()
{
$request = Request::create('GET', '/', [], [], [], ['HTTP_AUTHORIZATION' => 'Bearer 12345']);
$this->server->shouldReceive('isValidRequest')->once()->andReturn(true);
$token = Mockery::mock('League\OAuth2\Server\Entity\AccessTokenEntity');
$this->server->shouldReceive('getAccessToken')->once()->andReturn($token);
$session = Mockery::mock('League\OAuth2\Server\Entity\SessionEntity');
$token->shouldReceive('getSession')->once()->andReturn($session);
$session->shouldReceive('getOwnerType')->once()->andReturn('client');
$session->shouldReceive('getOwnerId')->once()->andReturn(1);
$this->provider->setClientResolver(function ($id) {
return 'foo';
});
$this->assertEquals('foo', $this->provider->authenticate($request, new Route('GET', '/', [])));
}
public function testUserIsResolved()
{
$request = Request::create('GET', '/', [], [], [], ['HTTP_AUTHORIZATION' => 'Bearer 12345']);
$this->server->shouldReceive('isValidRequest')->once()->andReturn(true);
$token = Mockery::mock('League\OAuth2\Server\Entity\AccessTokenEntity');
$this->server->shouldReceive('getAccessToken')->once()->andReturn($token);
$session = Mockery::mock('League\OAuth2\Server\Entity\SessionEntity');
$token->shouldReceive('getSession')->once()->andReturn($session);
$session->shouldReceive('getOwnerType')->once()->andReturn('user');
$session->shouldReceive('getOwnerId')->once()->andReturn(1);
$this->provider->setUserResolver(function ($id) {
return 'foo';
});
$this->assertEquals('foo', $this->provider->authenticate($request, new Route('GET', '/', [])));
}
}