-
Notifications
You must be signed in to change notification settings - Fork 783
/
Copy pathBridgeClientRepositoryTest.php
156 lines (120 loc) · 4.93 KB
/
BridgeClientRepositoryTest.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<?php
namespace Laravel\Passport\Tests;
use Mockery as m;
use PHPUnit\Framework\TestCase;
use Laravel\Passport\Bridge\Client;
use Laravel\Passport\ClientRepository;
use Laravel\Passport\Bridge\ClientRepository as BridgeClientRepository;
class BridgeClientRepositoryTest extends TestCase
{
/**
* @var \Laravel\Passport\ClientRepository
*/
private $clientModelRepository;
/**
* @var \Laravel\Passport\Bridge\ClientRepository
*/
private $repository;
public function setUp()
{
$clientModelRepository = m::mock(ClientRepository::class);
$clientModelRepository->shouldReceive('findActive')
->with(1)
->andReturn(new BridgeClientRepositoryTestClientStub);
$this->clientModelRepository = $clientModelRepository;
$this->repository = new BridgeClientRepository($clientModelRepository);
}
public function tearDown()
{
m::close();
unset($this->clientModelRepository, $this->repository);
}
public function test_can_get_client()
{
$client = $this->repository->getClientEntity(1);
$this->assertInstanceOf(Client::class, $client);
$this->assertEquals('1', $client->getIdentifier());
$this->assertEquals('Client', $client->getName());
$this->assertEquals(['http://localhost'], $client->getRedirectUri());
$this->assertTrue($client->isConfidential());
}
public function test_can_validate_client_for_auth_code_grant()
{
$this->assertTrue($this->repository->validateClient(1, 'secret', 'authorization_code'));
$this->assertFalse($this->repository->validateClient(1, 'wrong-secret', 'authorization_code'));
$this->assertFalse($this->repository->validateClient(1, 'wrong-secret', 'client_credentials'));
}
public function test_can_validate_client_for_client_credentials_grant()
{
$client = $this->clientModelRepository->findActive(1);
$client->personal_access_client = true;
$this->assertTrue($this->repository->validateClient(1, 'secret', 'client_credentials'));
$this->assertFalse($this->repository->validateClient(1, 'wrong-secret', 'client_credentials'));
$this->assertFalse($this->repository->validateClient(1, 'secret', 'authorization_code'));
}
public function test_password_grant_is_permitted()
{
$client = $this->clientModelRepository->findActive(1);
$client->password_client = true;
$this->assertTrue($this->repository->validateClient(1, 'secret', 'password'));
}
public function test_password_grant_is_prevented()
{
$this->assertFalse($this->repository->validateClient(1, 'secret', 'password'));
}
public function test_authorization_code_grant_is_permitted()
{
$this->assertTrue($this->repository->validateClient(1, 'secret', 'authorization_code'));
}
public function test_authorization_code_grant_is_prevented()
{
$client = $this->clientModelRepository->findActive(1);
$client->password_client = true;
$this->assertFalse($this->repository->validateClient(1, 'secret', 'authorization_code'));
}
public function test_personal_access_grant_is_permitted()
{
$client = $this->clientModelRepository->findActive(1);
$client->personal_access_client = true;
$this->assertTrue($this->repository->validateClient(1, 'secret', 'personal_access'));
}
public function test_personal_access_grant_is_prevented()
{
$this->assertFalse($this->repository->validateClient(1, 'secret', 'personal_access'));
}
public function test_client_credentials_grant_is_permitted()
{
$this->assertTrue($this->repository->validateClient(1, 'secret', 'client_credentials'));
}
public function test_client_credentials_grant_is_prevented()
{
$client = $this->clientModelRepository->findActive(1);
$client->secret = null;
$this->assertFalse($this->repository->validateClient(1, 'secret', 'client_credentials'));
}
public function test_grant_types_allows_request()
{
$client = $this->clientModelRepository->findActive(1);
$client->grant_types = ['client_credentials'];
$this->assertTrue($this->repository->validateClient(1, 'secret', 'client_credentials'));
}
public function test_grant_types_disallows_request()
{
$client = $this->clientModelRepository->findActive(1);
$client->grant_types = ['client_credentials'];
$this->assertFalse($this->repository->validateClient(1, 'secret', 'authorization_code'));
}
}
class BridgeClientRepositoryTestClientStub
{
public $name = 'Client';
public $redirect = 'http://localhost';
public $secret = 'secret';
public $personal_access_client = false;
public $password_client = false;
public $grant_types;
public function firstParty()
{
return $this->personal_access_client || $this->password_client;
}
}