-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfigurationTest.php
More file actions
204 lines (172 loc) · 7.56 KB
/
Copy pathConfigurationTest.php
File metadata and controls
204 lines (172 loc) · 7.56 KB
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
<?php
namespace ItkDev\OpenIdConnectBundle\Tests\DependencyInjection;
use ItkDev\OpenIdConnectBundle\DependencyInjection\Configuration;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
use Symfony\Component\Config\Definition\Processor;
class ConfigurationTest extends TestCase
{
private Processor $processor;
private Configuration $configuration;
protected function setUp(): void
{
$this->processor = new Processor();
$this->configuration = new Configuration();
}
private function getMinimalConfig(): array
{
return [
'cache_options' => [
'cache_pool' => 'cache.app',
],
'cli_login_options' => [
'route' => 'my_route',
],
'openid_providers' => [
'provider1' => [
'options' => [
'metadata_url' => 'https://example.com/.well-known/openid-configuration',
'client_id' => 'my_id',
'client_secret' => 'my_secret',
],
],
],
];
}
public function testMinimalConfig(): void
{
$config = $this->processor->processConfiguration(
$this->configuration,
[$this->getMinimalConfig()]
);
$this->assertSame('cache.app', $config['cache_options']['cache_pool']);
$this->assertSame('my_route', $config['cli_login_options']['route']);
$this->assertNull($config['user_provider']);
$this->assertArrayHasKey('provider1', $config['openid_providers']);
$provider = $config['openid_providers']['provider1']['options'];
$this->assertSame('https://example.com/.well-known/openid-configuration', $provider['metadata_url']);
$this->assertSame('my_id', $provider['client_id']);
$this->assertSame('my_secret', $provider['client_secret']);
$this->assertSame(10, $provider['leeway']);
$this->assertSame(86400, $provider['cache_duration']);
$this->assertFalse($provider['allow_http']);
}
public function testFullConfig(): void
{
$input = $this->getMinimalConfig();
$input['user_provider'] = 'my_user_provider';
$input['openid_providers']['provider1']['options']['leeway'] = 30;
$input['openid_providers']['provider1']['options']['cache_duration'] = 3600;
$input['openid_providers']['provider1']['options']['redirect_uri'] = 'https://app.com/callback';
$input['openid_providers']['provider1']['options']['allow_http'] = true;
$config = $this->processor->processConfiguration(
$this->configuration,
[$input]
);
$this->assertSame('my_user_provider', $config['user_provider']);
$provider = $config['openid_providers']['provider1']['options'];
$this->assertSame(30, $provider['leeway']);
$this->assertSame(3600, $provider['cache_duration']);
$this->assertSame('https://app.com/callback', $provider['redirect_uri']);
$this->assertTrue($provider['allow_http']);
}
public function testRedirectRouteConfig(): void
{
$input = $this->getMinimalConfig();
$input['openid_providers']['provider1']['options']['redirect_route'] = 'my_redirect_route';
$config = $this->processor->processConfiguration(
$this->configuration,
[$input]
);
$provider = $config['openid_providers']['provider1']['options'];
$this->assertSame('my_redirect_route', $provider['redirect_route']);
}
public function testBothRedirectUriAndRouteThrows(): void
{
$input = $this->getMinimalConfig();
$input['openid_providers']['provider1']['options']['redirect_uri'] = 'https://app.com/callback';
$input['openid_providers']['provider1']['options']['redirect_route'] = 'my_route';
$this->expectException(InvalidConfigurationException::class);
$this->expectExceptionMessage('Only one of redirect_uri or redirect_route must be set.');
$this->processor->processConfiguration(
$this->configuration,
[$input]
);
}
public function testHttpClientOptionsAccepted(): void
{
$input = $this->getMinimalConfig();
$input['openid_providers']['provider1']['options']['http_client_options'] = [
'timeout' => 2.5,
'proxy' => 'http://proxy:8080',
'verify' => true,
];
$config = $this->processor->processConfiguration(
$this->configuration,
[$input]
);
$httpClientOptions = $config['openid_providers']['provider1']['options']['http_client_options'];
$this->assertSame(2.5, $httpClientOptions['timeout']);
$this->assertSame('http://proxy:8080', $httpClientOptions['proxy']);
$this->assertTrue($httpClientOptions['verify']);
}
public function testHttpClientOptionsDefaultsApplied(): void
{
$config = $this->processor->processConfiguration(
$this->configuration,
[$this->getMinimalConfig()]
);
$providerOptions = $config['openid_providers']['provider1']['options'];
// The block carries a sensible default timeout so an omitted input still
// protects workers from a hung IdP. proxy/verify have no default and so
// stay absent (Guzzle's own defaults apply).
$this->assertSame(30.0, $providerOptions['http_client_options']['timeout']);
$this->assertArrayNotHasKey('proxy', $providerOptions['http_client_options']);
$this->assertArrayNotHasKey('verify', $providerOptions['http_client_options']);
}
public function testHttpClientOptionsRejectsUnknownKey(): void
{
$input = $this->getMinimalConfig();
$input['openid_providers']['provider1']['options']['http_client_options'] = [
'foo' => 1,
];
$this->expectException(InvalidConfigurationException::class);
$this->processor->processConfiguration(
$this->configuration,
[$input]
);
}
public function testProviderKeysAreNotNormalized(): void
{
$input = $this->getMinimalConfig();
$input['openid_providers']['my-provider'] = $input['openid_providers']['provider1'];
unset($input['openid_providers']['provider1']);
$config = $this->processor->processConfiguration(
$this->configuration,
[$input]
);
// Provider keys are part of the public contract ('my-provider' and
// 'my_provider' are distinct providers), so dashes must survive
// config processing instead of being normalized to underscores.
$this->assertArrayHasKey('my-provider', $config['openid_providers']);
$this->assertArrayNotHasKey('my_provider', $config['openid_providers']);
}
public function testMultipleProviders(): void
{
$input = $this->getMinimalConfig();
$input['openid_providers']['provider2'] = [
'options' => [
'metadata_url' => 'https://other.com/.well-known/openid-configuration',
'client_id' => 'other_id',
'client_secret' => 'other_secret',
],
];
$config = $this->processor->processConfiguration(
$this->configuration,
[$input]
);
$this->assertCount(2, $config['openid_providers']);
$this->assertArrayHasKey('provider1', $config['openid_providers']);
$this->assertArrayHasKey('provider2', $config['openid_providers']);
}
}