ProxyControllerTest::testProxy()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 27
rs 9.488
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
/**
3
 * Calendar App
4
 *
5
 * @author Georg Ehrke
6
 * @copyright 2016 Georg Ehrke <[email protected]>
7
 *
8
 * This library is free software; you can redistribute it and/or
9
 * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
10
 * License as published by the Free Software Foundation; either
11
 * version 3 of the License, or any later version.
12
 *
13
 * This library is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
17
 *
18
 * You should have received a copy of the GNU Affero General Public
19
 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
20
 *
21
 */
22
namespace OCA\Calendar\Controller;
23
24
use OCP\AppFramework\Http\DataDisplayResponse;
25
use PHPUnit\Framework\TestCase;
26
use GuzzleHttp\Exception\RequestException;
27
use GuzzleHttp\Exception\ClientException;
28
use GuzzleHttp\Exception\ConnectException;
29
30
class ProxyControllerTest extends TestCase {
31
32
	private $appName;
33
	private $request;
34
	private $client;
35
	private $l10n;
36
	private $logger;
37
	private $config;
38
39
	private $newClient;
40
	private $response0;
41
	private $response1;
42
	private $response2;
43
	private $response3;
44
	private $response4;
45
	private $response5;
46
	private $exceptionRequest;
47
	private $exceptionResponse;
48
49
	private $controller;
50
51
	public function setUp() {
52
		$this->appName = 'calendar';
53
		$this->request = $this->getMockBuilder('\OCP\IRequest')
54
			->disableOriginalConstructor()
55
			->getMock();
56
		$this->client = $this->getMockBuilder('\OCP\Http\Client\IClientService')
57
			->disableOriginalConstructor()
58
			->getMock();
59
		$this->l10n = $this->getMockBuilder('\OCP\IL10N')
60
			->disableOriginalConstructor()
61
			->getMock();
62
		$this->logger = $this->getMockBuilder('\OCP\ILogger')
63
			->disableOriginalConstructor()
64
			->getMock();
65
		$this->config = $this->getMockBuilder('\OCP\IConfig')
66
			->disableOriginalConstructor()
67
			->getMock();
68
69
		$this->newClient = $this->getMockBuilder('\OCP\Http\Client\IClient')
70
			->disableOriginalConstructor()
71
			->getMock();
72
		$this->response0 = $this->getMockBuilder('\OCP\Http\Client\IResponse')
73
			->disableOriginalConstructor()
74
			->getMock();
75
		$this->response1 = $this->getMockBuilder('\OCP\Http\Client\IResponse')
76
			->disableOriginalConstructor()
77
			->getMock();
78
		$this->response2 = $this->getMockBuilder('\OCP\Http\Client\IResponse')
79
			->disableOriginalConstructor()
80
			->getMock();
81
		$this->response3 = $this->getMockBuilder('\OCP\Http\Client\IResponse')
82
			->disableOriginalConstructor()
83
			->getMock();
84
		$this->response4 = $this->getMockBuilder('\OCP\Http\Client\IResponse')
85
			->disableOriginalConstructor()
86
			->getMock();
87
		$this->response5 = $this->getMockBuilder('\OCP\Http\Client\IResponse')
88
			->disableOriginalConstructor()
89
			->getMock();
90
91
		if (version_compare(\OC::$server->getConfig()->getSystemValue('version'), '14', '>=')) {
92
			$this->exceptionRequest = $this->getMockBuilder('Psr\Http\Message\RequestInterface')
93
				->disableOriginalConstructor()
94
				->getMock();
95
			$this->exceptionResponse = $this->getMockBuilder('Psr\Http\Message\ResponseInterface')
96
				->disableOriginalConstructor()
97
				->getMock();
98
		} else {
99
			$this->exceptionRequest = $this->getMockBuilder('GuzzleHttp\Message\RequestInterface')
100
				->disableOriginalConstructor()
101
				->getMock();
102
			$this->exceptionResponse = $this->getMockBuilder('GuzzleHttp\Message\ResponseInterface')
103
				->disableOriginalConstructor()
104
				->getMock();
105
		}
106
107
		$this->config->method('getAppValue')
108
			->with('dav', 'webcalAllowLocalAccess', 'no')
109
			->willReturn('no');
110
111
		$this->controller = new ProxyController($this->appName, $this->request,
112
			$this->client, $this->l10n, $this->logger, $this->config);
113
	}
114
115
	public function testProxy() {
116
		$testUrl = 'http://abc.def/foobar?123';
117
118
		$this->client->expects($this->once())
119
			->method('newClient')
120
			->will($this->returnValue($this->newClient));
121
		$this->newClient->expects($this->once())
122
			->method('get')
123
			->with($testUrl, [
124
				'allow_redirects' => false,
125
			])
126
			->will($this->returnValue($this->response0));
127
		$this->response0->expects($this->once())
128
			->method('getStatusCode')
129
			->with()
130
			->will($this->returnValue(200));
131
		$this->response0->expects($this->once())
132
			->method('getBody')
133
			->with()
134
			->will($this->returnValue("BEGIN:VCALENDAR\r\nVERSION:2.0\r\nPRODID:-//Sabre//Sabre VObject 4.1.6//EN\r\nCALSCALE:GREGORIAN\r\nEND:VCALENDAR\r\n"));
135
136
		$actual = $this->controller->proxy($testUrl);
137
138
		$this->assertInstanceOf(DataDisplayResponse::class, $actual);
139
		$this->assertEquals('text/calendar', $actual->getHeaders()['Content-Type']);
140
		$this->assertEquals("BEGIN:VCALENDAR\r\nVERSION:2.0\r\nPRODID:-//Sabre//Sabre VObject 4.1.6//EN\r\nCALSCALE:GREGORIAN\r\nEND:VCALENDAR\r\n", $actual->getData());
141
	}
142
143
	public function testProxyClientException() {
144
		$testUrl = 'http://abc.def/foobar?123';
145
146
		$this->client->expects($this->once())
147
			->method('newClient')
148
			->will($this->returnValue($this->newClient));
149
		$this->newClient->expects($this->once())
150
			->method('get')
151
			->with($testUrl, [
152
				'allow_redirects' => false,
153
			])
154
			->will($this->throwException(new ClientException('Exception Message foo bar 42',
155
				$this->exceptionRequest, $this->exceptionResponse)));
156
		$this->exceptionResponse->expects($this->once())
157
			->method('getStatusCode')
158
			->will($this->returnValue(403));
159
		$this->l10n->expects($this->once())
160
			->method('t')
161
			->with($this->equalTo('The remote server did not give us access to the calendar (HTTP {%s} error)', '403'))
162
			->will($this->returnValue('translated string 1337'));
163
		$this->logger->expects($this->once())
164
			->method('debug')
165
			->with($this->equalTo('Exception Message foo bar 42'));
166
167
		$actual = $this->controller->proxy($testUrl);
168
169
		$this->assertInstanceOf('OCP\AppFramework\Http\JSONResponse', $actual);
170
		$this->assertEquals('422', $actual->getStatus());
171
		$this->assertEquals([
172
			'message' => 'translated string 1337',
173
			'proxy_code' => 403
174
		], $actual->getData());
175
	}
176
177 View Code Duplication
	public function testProxyConnectException() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
178
		$testUrl = 'http://abc.def/foobar?123';
179
180
		$this->client->expects($this->once())
181
			->method('newClient')
182
			->will($this->returnValue($this->newClient));
183
		$this->newClient->expects($this->once())
184
			->method('get')
185
			->with($testUrl, [
186
				'allow_redirects' => false,
187
			])
188
			->will($this->throwException(new ConnectException('Exception Message foo bar 42',
189
				$this->exceptionRequest)));
190
		$this->l10n->expects($this->once())
191
			->method('t')
192
			->with($this->equalTo('Error connecting to remote server'))
193
			->will($this->returnValue('translated string 1337'));
194
		$this->logger->expects($this->once())
195
			->method('debug')
196
			->with($this->equalTo('Exception Message foo bar 42'));
197
198
		$actual = $this->controller->proxy($testUrl);
199
200
		$this->assertInstanceOf('OCP\AppFramework\Http\JSONResponse', $actual);
201
		$this->assertEquals('422', $actual->getStatus());
202
		$this->assertEquals([
203
			'message' => 'translated string 1337',
204
			'proxy_code' => -1
205
		], $actual->getData());
206
	}
207
208 View Code Duplication
	public function testProxyRequestExceptionHTTP() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
209
		$testUrl = 'http://abc.def/foobar?123';
210
211
		$this->client->expects($this->once())
212
			->method('newClient')
213
			->will($this->returnValue($this->newClient));
214
		$this->newClient->expects($this->once())
215
			->method('get')
216
			->with($testUrl, [
217
				'allow_redirects' => false,
218
			])
219
			->will($this->throwException(new RequestException('Exception Message foo bar 42',
220
				$this->exceptionRequest, $this->exceptionResponse)));
221
		$this->l10n->expects($this->once())
222
			->method('t')
223
			->with($this->equalTo('Error requesting resource on remote server'))
224
			->will($this->returnValue('translated string 1337'));
225
		$this->logger->expects($this->once())
226
			->method('debug')
227
			->with($this->equalTo('Exception Message foo bar 42'));
228
229
		$actual = $this->controller->proxy($testUrl);
230
231
		$this->assertInstanceOf('OCP\AppFramework\Http\JSONResponse', $actual);
232
		$this->assertEquals('422', $actual->getStatus());
233
		$this->assertEquals([
234
			'message' => 'translated string 1337',
235
			'proxy_code' => -2
236
		], $actual->getData());
237
	}
238
239 View Code Duplication
	public function testProxyRequestExceptionHTTPS() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
240
		$testUrl = 'https://abc.def/foobar?123';
241
242
		$this->client->expects($this->once())
243
			->method('newClient')
244
			->will($this->returnValue($this->newClient));
245
		$this->newClient->expects($this->once())
246
			->method('get')
247
			->with($testUrl, [
248
				'allow_redirects' => false,
249
			])
250
			->will($this->throwException(new RequestException('Exception Message foo bar 42',
251
				$this->exceptionRequest, $this->exceptionResponse)));
252
		$this->l10n->expects($this->once())
253
			->method('t')
254
			->with($this->equalTo('Error requesting resource on remote server. This could possible be related to a certificate mismatch'))
255
			->will($this->returnValue('translated string 1337'));
256
		$this->logger->expects($this->once())
257
			->method('debug')
258
			->with($this->equalTo('Exception Message foo bar 42'));
259
260
		$actual = $this->controller->proxy($testUrl);
261
262
		$this->assertInstanceOf('OCP\AppFramework\Http\JSONResponse', $actual);
263
		$this->assertEquals('422', $actual->getStatus());
264
		$this->assertEquals([
265
			'message' => 'translated string 1337',
266
			'proxy_code' => -2
267
		], $actual->getData());
268
	}
269
270 View Code Duplication
	public function testProxyRedirect() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
271
		$testUrl = 'http://abc.def/foobar?123';
272
273
		$this->client->expects($this->once())
274
			->method('newClient')
275
			->will($this->returnValue($this->newClient));
276
		$this->newClient->expects($this->at(0))
277
			->method('get')
278
			->with($testUrl, [
279
				'allow_redirects' => false,
280
			])
281
			->will($this->returnValue($this->response0));
282
		$this->response0->expects($this->at(0))
283
			->method('getStatusCode')
284
			->with()
285
			->will($this->returnValue(301));
286
		$this->response0->expects($this->at(1))
287
			->method('getHeader')
288
			->with('Location')
289
			->will($this->returnValue('http://def.abc/foobar?456'));
290
		$this->newClient->expects($this->at(1))
291
			->method('get')
292
			->with('http://def.abc/foobar?456', [
293
				'allow_redirects' => false,
294
			])
295
			->will($this->returnValue($this->response0));
296
		$this->response0->expects($this->at(2))
297
			->method('getStatusCode')
298
			->with()
299
			->will($this->returnValue(200));
300
301
		$actual = $this->controller->proxy($testUrl);
302
303
		$this->assertInstanceOf('OCP\AppFramework\Http\JSONResponse', $actual);
304
		$this->assertEquals([
305
			'proxy_code' => -4,
306
			'new_url' => 'http://def.abc/foobar?456',
307
		], $actual->getData());
308
	}
309
310
	public function testProxyRedirectNonPermanent() {
311
		$testUrl = 'http://abc.def/foobar?123';
312
313
		$this->client->expects($this->once())
314
			->method('newClient')
315
			->will($this->returnValue($this->newClient));
316
		$this->newClient->expects($this->at(0))
317
			->method('get')
318
			->with($testUrl, [
319
				'allow_redirects' => false,
320
			])
321
			->will($this->returnValue($this->response0));
322
		$this->response0->expects($this->at(0))
323
			->method('getStatusCode')
324
			->with()
325
			->will($this->returnValue(307));
326
		$this->newClient->expects($this->at(1))
327
			->method('get')
328
			->with('http://abc.def/foobar?123' , [
329
				'allow_redirects' => [
330
					'max' => 5,
331
				],
332
			])
333
			->will($this->returnValue($this->response1));
334
		$this->response1->expects($this->at(0))
335
			->method('getStatusCode')
336
			->with()
337
			->will($this->returnValue(200));
338
		$this->response1->expects($this->once())
339
			->method('getBody')
340
			->with()
341
			->will($this->returnValue("BEGIN:VCALENDAR\r\nVERSION:2.0\r\nPRODID:-//Sabre//Sabre VObject 4.1.6//EN\r\nCALSCALE:GREGORIAN\r\nEND:VCALENDAR\r\n"));
342
343
		$actual = $this->controller->proxy($testUrl);
344
345
		$this->assertInstanceOf(DataDisplayResponse::class, $actual);
346
		$this->assertEquals('text/calendar', $actual->getHeaders()['Content-Type']);
347
		$this->assertEquals("BEGIN:VCALENDAR\r\nVERSION:2.0\r\nPRODID:-//Sabre//Sabre VObject 4.1.6//EN\r\nCALSCALE:GREGORIAN\r\nEND:VCALENDAR\r\n", $actual->getData());
348
	}
349
350
	public function testProxyMultipleRedirects() {
351
		$testUrl = 'http://abc.def/foobar?123';
352
353
		$this->client->expects($this->once())
354
			->method('newClient')
355
			->will($this->returnValue($this->newClient));
356
		$this->newClient->expects($this->at(0))
357
			->method('get')
358
			->with($testUrl, [
359
				'allow_redirects' => false,
360
			])
361
			->will($this->returnValue($this->response0));
362
		$this->newClient->expects($this->at(1))
363
			->method('get')
364
			->with('http://def.abc/foobar?456' , [
365
				'allow_redirects' => false,
366
			])
367
			->will($this->returnValue($this->response1));
368
		$this->newClient->expects($this->at(2))
369
			->method('get')
370
			->with('http://xyz.abc/foobar?789' , [
371
				'allow_redirects' => false,
372
			])
373
			->will($this->returnValue($this->response2));
374
		$this->response0->expects($this->at(0))
375
			->method('getStatusCode')
376
			->with()
377
			->will($this->returnValue(301));
378
		$this->response0->expects($this->at(1))
379
			->method('getHeader')
380
			->with('Location')
381
			->will($this->returnValue('http://def.abc/foobar?456'));
382
		$this->response1->expects($this->at(0))
383
			->method('getStatusCode')
384
			->with()
385
			->will($this->returnValue(301));
386
		$this->response1->expects($this->at(1))
387
			->method('getHeader')
388
			->with('Location')
389
			->will($this->returnValue('http://xyz.abc/foobar?789'));
390
		$this->response2->expects($this->at(0))
391
			->method('getStatusCode')
392
			->with()
393
			->will($this->returnValue(200));
394
395
		$actual = $this->controller->proxy($testUrl);
396
397
		$this->assertInstanceOf('OCP\AppFramework\Http\JSONResponse', $actual);
398
		$this->assertEquals([
399
			'proxy_code' => -4,
400
			'new_url' => 'http://xyz.abc/foobar?789',
401
		], $actual->getData());
402
	}
403
404 View Code Duplication
	public function testProxyMultipleRedirectsNonPermanent() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
405
		$testUrl = 'http://abc.def/foobar?123';
406
407
		$this->client->expects($this->once())
408
			->method('newClient')
409
			->will($this->returnValue($this->newClient));
410
		$this->newClient->expects($this->at(0))
411
			->method('get')
412
			->with($testUrl, [
413
				'allow_redirects' => false,
414
			])
415
			->will($this->returnValue($this->response0));
416
		$this->newClient->expects($this->at(1))
417
			->method('get')
418
			->with('http://def.abc/foobar?456' , [
419
				'allow_redirects' => false,
420
			])
421
			->will($this->returnValue($this->response1));
422
		$this->response0->expects($this->at(0))
423
			->method('getStatusCode')
424
			->with()
425
			->will($this->returnValue(301));
426
		$this->response0->expects($this->at(1))
427
			->method('getHeader')
428
			->with('Location')
429
			->will($this->returnValue('http://def.abc/foobar?456'));
430
		$this->response1->expects($this->at(0))
431
			->method('getStatusCode')
432
			->with()
433
			->will($this->returnValue(307));
434
435
		$actual = $this->controller->proxy($testUrl);
436
437
		$this->assertInstanceOf('OCP\AppFramework\Http\JSONResponse', $actual);
438
		$this->assertEquals([
439
			'proxy_code' => -4,
440
			'new_url' => 'http://def.abc/foobar?456',
441
		], $actual->getData());
442
	}
443
444
	public function testProxyAtMostFiveRedirects() {
445
		$testUrl = 'http://abc.def/foobar?123';
446
447
		$this->client->expects($this->once())
448
			->method('newClient')
449
			->will($this->returnValue($this->newClient));
450
		$this->newClient->expects($this->at(0))
451
			->method('get')
452
			->with($testUrl, [
453
				'allow_redirects' => false,
454
			])
455
			->will($this->returnValue($this->response0));
456
		$this->newClient->expects($this->at(1))
457
			->method('get')
458
			->with('http://def.abc/foobar?456-0', [
459
				'allow_redirects' => false,
460
			])
461
			->will($this->returnValue($this->response1));
462
		$this->newClient->expects($this->at(2))
463
			->method('get')
464
			->with('http://def.abc/foobar?456-1', [
465
				'allow_redirects' => false,
466
			])
467
			->will($this->returnValue($this->response2));
468
		$this->newClient->expects($this->at(3))
469
			->method('get')
470
			->with('http://def.abc/foobar?456-2', [
471
				'allow_redirects' => false,
472
			])
473
			->will($this->returnValue($this->response3));
474
		$this->newClient->expects($this->at(4))
475
			->method('get')
476
			->with('http://def.abc/foobar?456-3', [
477
				'allow_redirects' => false,
478
			])
479
			->will($this->returnValue($this->response4));
480
		$this->newClient->expects($this->at(5))
481
			->method('get')
482
			->with('http://def.abc/foobar?456-4', [
483
				'allow_redirects' => false,
484
			])
485
			->will($this->returnValue($this->response5));
486
		$this->response0->expects($this->at(0))
487
			->method('getStatusCode')
488
			->with()
489
			->will($this->returnValue(301));
490
		$this->response0->expects($this->at(1))
491
			->method('getHeader')
492
			->with('Location')
493
			->will($this->returnValue('http://def.abc/foobar?456-0'));
494
		$this->response1->expects($this->at(0))
495
			->method('getStatusCode')
496
			->with()
497
			->will($this->returnValue(301));
498
		$this->response1->expects($this->at(1))
499
			->method('getHeader')
500
			->with('Location')
501
			->will($this->returnValue('http://def.abc/foobar?456-1'));
502
		$this->response2->expects($this->at(0))
503
			->method('getStatusCode')
504
			->with()
505
			->will($this->returnValue(301));
506
		$this->response2->expects($this->at(1))
507
			->method('getHeader')
508
			->with('Location')
509
			->will($this->returnValue('http://def.abc/foobar?456-2'));
510
		$this->response3->expects($this->at(0))
511
			->method('getStatusCode')
512
			->with()
513
			->will($this->returnValue(301));
514
		$this->response3->expects($this->at(1))
515
			->method('getHeader')
516
			->with('Location')
517
			->will($this->returnValue('http://def.abc/foobar?456-3'));
518
		$this->response4->expects($this->at(0))
519
			->method('getStatusCode')
520
			->with()
521
			->will($this->returnValue(301));
522
		$this->response4->expects($this->at(1))
523
			->method('getHeader')
524
			->with('Location')
525
			->will($this->returnValue('http://def.abc/foobar?456-4'));
526
		$this->response5->expects($this->at(0))
527
			->method('getStatusCode')
528
			->with()
529
			->will($this->returnValue(301));
530
		$this->response5->expects($this->at(1))
531
			->method('getHeader')
532
			->with('Location')
533
			->will($this->returnValue('http://def.abc/foobar?456-5'));
534
		$this->l10n->expects($this->once())
535
			->method('t')
536
			->with($this->equalTo('Too many redirects. Aborting ...'))
537
			->will($this->returnValue('translated string 1337'));
538
		$this->newClient->expects($this->exactly(6))
539
			->method('get');
540
541
		$actual = $this->controller->proxy($testUrl);
542
543
		$this->assertInstanceOf('OCP\AppFramework\Http\JSONResponse', $actual);
544
		$this->assertEquals([
545
			'proxy_code' => -3,
546
			'message' => 'translated string 1337',
547
		], $actual->getData());
548
	}
549
550
551
}
552