|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* eduVPN - End-user friendly VPN. |
|
5
|
|
|
* |
|
6
|
|
|
* Copyright: 2016-2017, The Commons Conservancy eduVPN Programme |
|
7
|
|
|
* SPDX-License-Identifier: AGPL-3.0+ |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace SURFnet\VPN\Server\Tests\Acl\Provider; |
|
11
|
|
|
|
|
12
|
|
|
use DateTime; |
|
13
|
|
|
use fkooman\OAuth\Client\AccessToken; |
|
14
|
|
|
use fkooman\OAuth\Client\Http\Response; |
|
15
|
|
|
use fkooman\OAuth\Client\OAuthClient; |
|
16
|
|
|
use fkooman\OAuth\Client\Provider; |
|
17
|
|
|
use PDO; |
|
18
|
|
|
use PHPUnit_Framework_TestCase; |
|
19
|
|
|
use SURFnet\VPN\Server\Acl\Provider\VootProvider; |
|
20
|
|
|
use SURFnet\VPN\Server\Storage; |
|
21
|
|
|
use SURFnet\VPN\Server\Tests\TestOAuthClientRandom; |
|
22
|
|
|
use SURFnet\VPN\Server\Tests\TestOAuthClientSession; |
|
23
|
|
|
|
|
24
|
|
|
class VootProviderTest extends PHPUnit_Framework_TestCase |
|
25
|
|
|
{ |
|
26
|
|
|
/** @var VootProvider */ |
|
27
|
|
|
private $vootProvider; |
|
28
|
|
|
|
|
29
|
|
|
public function setUp() |
|
|
|
|
|
|
30
|
|
|
{ |
|
31
|
|
|
$vootClient = $this->getMockBuilder('\fkooman\OAuth\Client\Http\HttpClientInterface')->getMock(); |
|
32
|
|
|
$vootClient->method('send')->will( |
|
33
|
|
|
$this->onConsecutiveCalls( |
|
34
|
|
|
new Response(200, file_get_contents(sprintf('%s/data/response.json', __DIR__)), ['Content-Type' => 'application/json']), |
|
35
|
|
|
new Response(401, file_get_contents(sprintf('%s/data/response_invalid_token.json', __DIR__)), ['Content-Type' => 'application/json']) |
|
36
|
|
|
) |
|
37
|
|
|
); |
|
38
|
|
|
|
|
39
|
|
|
$storage = new Storage( |
|
40
|
|
|
new PDO( |
|
41
|
|
|
$GLOBALS['DB_DSN'], |
|
42
|
|
|
$GLOBALS['DB_USER'], |
|
43
|
|
|
$GLOBALS['DB_PASSWD'] |
|
44
|
|
|
), |
|
45
|
|
|
new DateTime() |
|
46
|
|
|
); |
|
47
|
|
|
$storage->init(); |
|
48
|
|
|
// $storage->setAccessToken('foo', 'voot', new AccessToken('AT', 'bearer', 'groups', 'RT', new DateTime('2016-01-02'))); |
|
|
|
|
|
|
49
|
|
|
$storage->storeAccessToken( |
|
50
|
|
|
'foo', |
|
51
|
|
|
AccessToken::fromJson( |
|
52
|
|
|
json_encode([ |
|
53
|
|
|
'provider_id' => 'c|a', |
|
54
|
|
|
'user_id' => 'foo', |
|
55
|
|
|
'access_token' => 'AT', |
|
56
|
|
|
'token_type' => 'bearer', |
|
57
|
|
|
'scope' => 'groups', |
|
58
|
|
|
'refresh_token' => 'RT', |
|
59
|
|
|
'expires_in' => 3600, |
|
60
|
|
|
'issued_at' => '2016-01-02 00:00:00', |
|
61
|
|
|
]) |
|
62
|
|
|
) |
|
63
|
|
|
); |
|
64
|
|
|
|
|
65
|
|
|
$oauthClient = new OAuthClient( |
|
66
|
|
|
$storage, |
|
67
|
|
|
$vootClient, |
|
68
|
|
|
new TestOAuthClientSession(), |
|
69
|
|
|
new TestOAuthClientRandom(), |
|
70
|
|
|
new DateTime('2016-01-01') |
|
71
|
|
|
); |
|
72
|
|
|
$oauthClient->setProvider(new Provider('a', 'b', 'c', 'd')); |
|
73
|
|
|
$this->vootProvider = new VootProvider( |
|
74
|
|
|
$oauthClient, |
|
75
|
|
|
'https://voot.surfconext.nl/me/groups' |
|
76
|
|
|
); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
public function testVootCall() |
|
80
|
|
|
{ |
|
81
|
|
|
$this->assertSame( |
|
82
|
|
|
[ |
|
83
|
|
|
[ |
|
84
|
|
|
'id' => 'urn:collab:group:surfteams.nl:nl:surfnet:diensten:eduvpn', |
|
85
|
|
|
'displayName' => 'EduVPN', |
|
86
|
|
|
], |
|
87
|
|
|
[ |
|
88
|
|
|
'id' => 'urn:collab:group:surfteams.nl:nl:surfnet:diensten:eduvpn-test', |
|
89
|
|
|
'displayName' => 'eduVPN-test', |
|
90
|
|
|
], |
|
91
|
|
|
[ |
|
92
|
|
|
'id' => 'urn:collab:group:surfteams.nl:nl:surfnet:diensten:enabling_dynamic_services_2015', |
|
93
|
|
|
'displayName' => 'Enabling Dynamic Services 2015', |
|
94
|
|
|
], |
|
95
|
|
|
[ |
|
96
|
|
|
'id' => 'urn:collab:group:surfteams.nl:nl:surfnet:diensten:surfcloud_utrecht_users', |
|
97
|
|
|
'displayName' => 'SURFcloud Utrecht users', |
|
98
|
|
|
], |
|
99
|
|
|
], |
|
100
|
|
|
$this->vootProvider->getGroups('foo') |
|
101
|
|
|
); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
public function testVootCallNoToken() |
|
105
|
|
|
{ |
|
106
|
|
|
$this->assertSame( |
|
107
|
|
|
[ |
|
108
|
|
|
], |
|
109
|
|
|
$this->vootProvider->getGroups('bar') |
|
110
|
|
|
); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
public function testVootCallInvalidToken() |
|
114
|
|
|
{ |
|
115
|
|
|
// first call succeeds, second call is invalid token response |
|
116
|
|
|
$this->vootProvider->getGroups('foo'); |
|
117
|
|
|
$this->assertSame( |
|
118
|
|
|
[ |
|
119
|
|
|
], |
|
120
|
|
|
$this->vootProvider->getGroups('foo') |
|
121
|
|
|
); |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: