|
1 | 1 | <?php |
2 | 2 |
|
3 | | -namespace Unit; |
| 3 | +namespace OCA\Solid; |
4 | 4 |
|
5 | 5 | use OCA\Solid\AppInfo\Application; |
6 | 6 | use OCA\Solid\BaseServerConfig; |
7 | 7 | use OCP\IConfig; |
8 | 8 | use PHPUnit\Framework\TestCase; |
9 | 9 | use TypeError; |
10 | 10 |
|
| 11 | +function random_bytes() |
| 12 | +{ |
| 13 | + return BaseServerConfigTest::MOCK_RANDOM_BYTES; |
| 14 | +} |
| 15 | + |
11 | 16 | /** |
12 | 17 | * @coversDefaultClass \OCA\Solid\BaseServerConfig |
| 18 | + * @uses \OCA\Solid\BaseServerConfig |
13 | 19 | * @covers ::__construct |
14 | 20 | */ |
15 | 21 | class BaseServerConfigTest extends TestCase |
16 | 22 | { |
17 | 23 | /////////////////////////////////// TESTS \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ |
18 | 24 |
|
| 25 | + public const MOCK_RANDOM_BYTES = 'mock random bytes'; |
| 26 | + const MOCK_REDIRECT_URI = 'mock redirect uri'; |
19 | 27 | private const MOCK_CLIENT_ID = 'mock-client-id'; |
| 28 | + private const MOCK_ORIGIN = 'mock origin'; |
20 | 29 |
|
21 | 30 | /** |
22 | 31 | * @testdox BaseServerConfig should complain when called before given a Configuration |
@@ -139,6 +148,196 @@ public function testGetClientRegistrationForNonExistingClient() |
139 | 148 | $this->assertEquals($expected, $actual); |
140 | 149 | } |
141 | 150 |
|
| 151 | + /** |
| 152 | + * @testdox BaseServerConfig should complain when asked to save ClientRegistration without origin |
| 153 | + * @covers ::saveClientRegistration |
| 154 | + */ |
| 155 | + public function testSaveClientRegistrationWithoutOrigin() |
| 156 | + { |
| 157 | + $this->expectException(TypeError::class); |
| 158 | + $this->expectExceptionMessage('Too few arguments to function'); |
| 159 | + |
| 160 | + $configMock = $this->createMock(IConfig::class); |
| 161 | + $baseServerConfig = new BaseServerConfig($configMock); |
| 162 | + |
| 163 | + $baseServerConfig->saveClientRegistration(); |
| 164 | + } |
| 165 | + |
| 166 | + /** |
| 167 | + * @testdox BaseServerConfig should complain when asked to save ClientRegistration without client data |
| 168 | + * @covers ::saveClientRegistration |
| 169 | + */ |
| 170 | + public function testSaveClientRegistrationWithoutClientData() |
| 171 | + { |
| 172 | + $this->expectException(TypeError::class); |
| 173 | + $this->expectExceptionMessage('Too few arguments to function'); |
| 174 | + |
| 175 | + $configMock = $this->createMock(IConfig::class); |
| 176 | + $baseServerConfig = new BaseServerConfig($configMock); |
| 177 | + |
| 178 | + $baseServerConfig->saveClientRegistration(self::MOCK_ORIGIN); |
| 179 | + } |
| 180 | + |
| 181 | + /** |
| 182 | + * @testdox BaseServerConfig should save ClientRegistration when asked to save ClientRegistration for new client |
| 183 | + * @covers ::saveClientRegistration |
| 184 | + */ |
| 185 | + public function testSaveClientRegistrationForNewClient() |
| 186 | + { |
| 187 | + $configMock = $this->createMock(IConfig::class); |
| 188 | + |
| 189 | + $configMock->expects($this->once()) |
| 190 | + ->method('getAppValue') |
| 191 | + ->with(Application::APP_ID, 'client-' . md5(self::MOCK_ORIGIN)) |
| 192 | + ->willReturnArgument(2); |
| 193 | + |
| 194 | + $expected = [ |
| 195 | + 'client_id' => md5(self::MOCK_ORIGIN), |
| 196 | + 'client_name' => self::MOCK_ORIGIN, |
| 197 | + 'client_secret' => md5(self::MOCK_RANDOM_BYTES), |
| 198 | + ]; |
| 199 | + |
| 200 | + $configMock->expects($this->exactly(2)) |
| 201 | + ->method('setAppValue') |
| 202 | + ->willReturnMap([ |
| 203 | + // Using willReturnMap as withConsecutive is removed since PHPUnit 10 |
| 204 | + [Application::APP_ID, 'client-' . md5(self::MOCK_ORIGIN), json_encode($expected)], |
| 205 | + [Application::APP_ID, 'client-' . self::MOCK_ORIGIN, json_encode($expected)] |
| 206 | + ]); |
| 207 | + |
| 208 | + $baseServerConfig = new BaseServerConfig($configMock); |
| 209 | + |
| 210 | + $actual = $baseServerConfig->saveClientRegistration(self::MOCK_ORIGIN, []); |
| 211 | + |
| 212 | + $this->assertEquals($expected, $actual); |
| 213 | + } |
| 214 | + |
| 215 | + /** |
| 216 | + * @testdox BaseServerConfig should save ClientRegistration when asked to save ClientRegistration for existing client |
| 217 | + * @covers ::saveClientRegistration |
| 218 | + */ |
| 219 | + public function testSaveClientRegistrationForExistingClient() |
| 220 | + { |
| 221 | + $configMock = $this->createMock(IConfig::class); |
| 222 | + |
| 223 | + $expected = [ |
| 224 | + 'client_id' => md5(self::MOCK_ORIGIN), |
| 225 | + 'client_name' => self::MOCK_ORIGIN, |
| 226 | + 'client_secret' => md5(self::MOCK_RANDOM_BYTES), |
| 227 | + 'redirect_uris' => [self::MOCK_REDIRECT_URI], |
| 228 | + ]; |
| 229 | + |
| 230 | + $configMock->expects($this->once()) |
| 231 | + ->method('getAppValue') |
| 232 | + ->with(Application::APP_ID, 'client-' . md5(self::MOCK_ORIGIN)) |
| 233 | + ->willReturn(json_encode($expected)); |
| 234 | + |
| 235 | + $configMock->expects($this->exactly(2)) |
| 236 | + ->method('setAppValue') |
| 237 | + ->willReturnMap([ |
| 238 | + // Using willReturnMap as withConsecutive is deprecated since PHPUnit 10 |
| 239 | + [Application::APP_ID, 'client-' . md5(self::MOCK_ORIGIN), json_encode($expected)], |
| 240 | + [Application::APP_ID, 'client-' . self::MOCK_ORIGIN, json_encode($expected)] |
| 241 | + ]); |
| 242 | + |
| 243 | + $baseServerConfig = new BaseServerConfig($configMock); |
| 244 | + |
| 245 | + $actual = $baseServerConfig->saveClientRegistration(self::MOCK_ORIGIN, []); |
| 246 | + |
| 247 | + $this->assertEquals($expected, $actual); |
| 248 | + } |
| 249 | + |
| 250 | + /** |
| 251 | + * @testdox BaseServerConfig should save ClientRegistration when asked to save ClientRegistration for blocked client |
| 252 | + * @covers ::saveClientRegistration |
| 253 | + */ |
| 254 | + public function testSaveClientRegistrationForBlockedClient() |
| 255 | + { |
| 256 | + $configMock = $this->createMock(IConfig::class); |
| 257 | + |
| 258 | + $expected = [ |
| 259 | + 'client_id' => md5(self::MOCK_ORIGIN), |
| 260 | + 'client_name' => self::MOCK_ORIGIN, |
| 261 | + 'client_secret' => md5(self::MOCK_RANDOM_BYTES), |
| 262 | + 'redirect_uris' => [self::MOCK_REDIRECT_URI], |
| 263 | + 'blocked' => true, |
| 264 | + ]; |
| 265 | + |
| 266 | + $configMock->expects($this->once()) |
| 267 | + ->method('getAppValue') |
| 268 | + ->with(Application::APP_ID, 'client-' . md5(self::MOCK_ORIGIN)) |
| 269 | + ->willReturn(json_encode($expected)); |
| 270 | + |
| 271 | + $configMock->expects($this->exactly(2)) |
| 272 | + ->method('setAppValue') |
| 273 | + ->willReturnMap([ |
| 274 | + // Using willReturnMap as withConsecutive is deprecated since PHPUnit 10 |
| 275 | + [Application::APP_ID, 'client-' . md5(self::MOCK_ORIGIN), json_encode($expected)], |
| 276 | + [Application::APP_ID, 'client-' . self::MOCK_ORIGIN, json_encode($expected)] |
| 277 | + ]); |
| 278 | + |
| 279 | + $baseServerConfig = new BaseServerConfig($configMock); |
| 280 | + |
| 281 | + $actual = $baseServerConfig->saveClientRegistration(self::MOCK_ORIGIN, $expected); |
| 282 | + |
| 283 | + $this->assertEquals($expected, $actual); |
| 284 | + } |
| 285 | + |
| 286 | + /** |
| 287 | + * @testdox BaseServerConfig should always "blocked" to existing value when asked to save ClientRegistration for blocked client |
| 288 | + * @covers ::saveClientRegistration |
| 289 | + */ |
| 290 | + public function testSaveClientRegistrationSetsBlocked() |
| 291 | + { |
| 292 | + $configMock = $this->createMock(IConfig::class); |
| 293 | + |
| 294 | + $expected = [ |
| 295 | + 'client_id' => md5(self::MOCK_ORIGIN), |
| 296 | + 'client_name' => self::MOCK_ORIGIN, |
| 297 | + 'client_secret' => md5(self::MOCK_RANDOM_BYTES), |
| 298 | + 'redirect_uris' => [self::MOCK_REDIRECT_URI], |
| 299 | + 'blocked' => true, |
| 300 | + ]; |
| 301 | + |
| 302 | + $configMock->expects($this->once()) |
| 303 | + ->method('getAppValue') |
| 304 | + ->with(Application::APP_ID, 'client-' . md5(self::MOCK_ORIGIN)) |
| 305 | + ->willReturn(json_encode($expected)); |
| 306 | + |
| 307 | + $clientData = $expected; |
| 308 | + $clientData['blocked'] = false; |
| 309 | + |
| 310 | + $configMock->expects($this->exactly(2)) |
| 311 | + ->method('setAppValue') |
| 312 | + ->willReturnMap([ |
| 313 | + // Using willReturnMap as withConsecutive is deprecated since PHPUnit 10 |
| 314 | + [Application::APP_ID, 'client-' . md5(self::MOCK_ORIGIN), json_encode($expected)], |
| 315 | + [Application::APP_ID, 'client-' . self::MOCK_ORIGIN, json_encode($expected)] |
| 316 | + ]); |
| 317 | + |
| 318 | + $baseServerConfig = new BaseServerConfig($configMock); |
| 319 | + |
| 320 | + $actual = $baseServerConfig->saveClientRegistration(self::MOCK_ORIGIN, $clientData); |
| 321 | + |
| 322 | + $this->assertEquals($expected, $actual); |
| 323 | + } |
| 324 | + |
| 325 | + /** |
| 326 | + * @testdox BaseServerConfig should remove ClientRegistration when asked to remove ClientRegistration |
| 327 | + * @covers ::removeClientRegistration |
| 328 | + */ |
| 329 | + public function testRemoveClientRegistration() |
| 330 | + { |
| 331 | + $configMock = $this->createMock(IConfig::class); |
| 332 | + $baseServerConfig = new BaseServerConfig($configMock); |
| 333 | + |
| 334 | + $configMock->expects($this->once()) |
| 335 | + ->method('deleteAppValue') |
| 336 | + ->with(Application::APP_ID, 'client-' . self::MOCK_CLIENT_ID); |
| 337 | + |
| 338 | + $baseServerConfig->removeClientRegistration(self::MOCK_CLIENT_ID); |
| 339 | + } |
| 340 | + |
142 | 341 | /////////////////////////////// DATAPROVIDERS \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ |
143 | 342 |
|
144 | 343 | public function provideBooleans() |
|
0 commit comments