Completed
Push — master ( af4954...38e997 )
by Georg
27s
created

ProxyControllerTest   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 514
Duplicated Lines 32.68 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 4
dl 168
loc 514
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
B setUp() 0 56 2
A testProxy() 0 27 1
A testProxyClientException() 0 33 1
A testProxyConnectException() 30 30 1
A testProxyRequestExceptionHTTP() 30 30 1
A testProxyRequestExceptionHTTPS() 30 30 1
A testProxyRedirect() 39 39 1
A testProxyRedirectNonPermanent() 0 39 1
A testProxyMultipleRedirects() 0 53 1
A testProxyMultipleRedirectsNonPermanent() 39 39 1
B testProxyAtMostFiveRedirects() 0 105 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
38
	private $newClient;
39
	private $response0;
40
	private $response1;
41
	private $response2;
42
	private $response3;
43
	private $response4;
44
	private $response5;
45
	private $exceptionRequest;
46
	private $exceptionResponse;
47
48
	private $controller;
49
50
	public function setUp() {
51
		$this->appName = 'calendar';
52
		$this->request = $this->getMockBuilder('\OCP\IRequest')
53
			->disableOriginalConstructor()
54
			->getMock();
55
		$this->client = $this->getMockBuilder('\OCP\Http\Client\IClientService')
56
			->disableOriginalConstructor()
57
			->getMock();
58
		$this->l10n = $this->getMockBuilder('\OCP\IL10N')
59
			->disableOriginalConstructor()
60
			->getMock();
61
		$this->logger = $this->getMockBuilder('\OCP\ILogger')
62
			->disableOriginalConstructor()
63
			->getMock();
64
65
		$this->newClient = $this->getMockBuilder('\OCP\Http\Client\IClient')
66
			->disableOriginalConstructor()
67
			->getMock();
68
		$this->response0 = $this->getMockBuilder('\OCP\Http\Client\IResponse')
69
			->disableOriginalConstructor()
70
			->getMock();
71
		$this->response1 = $this->getMockBuilder('\OCP\Http\Client\IResponse')
72
			->disableOriginalConstructor()
73
			->getMock();
74
		$this->response2 = $this->getMockBuilder('\OCP\Http\Client\IResponse')
75
			->disableOriginalConstructor()
76
			->getMock();
77
		$this->response3 = $this->getMockBuilder('\OCP\Http\Client\IResponse')
78
			->disableOriginalConstructor()
79
			->getMock();
80
		$this->response4 = $this->getMockBuilder('\OCP\Http\Client\IResponse')
81
			->disableOriginalConstructor()
82
			->getMock();
83
		$this->response5 = $this->getMockBuilder('\OCP\Http\Client\IResponse')
84
			->disableOriginalConstructor()
85
			->getMock();
86
87
		if (version_compare(\OC::$server->getConfig()->getSystemValue('version'), '14', '>=')) {
88
			$this->exceptionRequest = $this->getMockBuilder('Psr\Http\Message\RequestInterface')
89
				->disableOriginalConstructor()
90
				->getMock();
91
			$this->exceptionResponse = $this->getMockBuilder('Psr\Http\Message\ResponseInterface')
92
				->disableOriginalConstructor()
93
				->getMock();
94
		} else {
95
			$this->exceptionRequest = $this->getMockBuilder('GuzzleHttp\Message\RequestInterface')
96
				->disableOriginalConstructor()
97
				->getMock();
98
			$this->exceptionResponse = $this->getMockBuilder('GuzzleHttp\Message\ResponseInterface')
99
				->disableOriginalConstructor()
100
				->getMock();
101
		}
102
103
		$this->controller = new ProxyController($this->appName, $this->request,
104
			$this->client, $this->l10n, $this->logger);
105
	}
106
107
	public function testProxy() {
108
		$testUrl = 'http://abc.def/foobar?123';
109
110
		$this->client->expects($this->once())
111
			->method('newClient')
112
			->will($this->returnValue($this->newClient));
113
		$this->newClient->expects($this->once())
114
			->method('get')
115
			->with($testUrl, [
116
				'allow_redirects' => false,
117
			])
118
			->will($this->returnValue($this->response0));
119
		$this->response0->expects($this->once())
120
			->method('getStatusCode')
121
			->with()
122
			->will($this->returnValue(200));
123
		$this->response0->expects($this->once())
124
			->method('getBody')
125
			->with()
126
			->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"));
127
128
		$actual = $this->controller->proxy($testUrl);
129
130
		$this->assertInstanceOf(DataDisplayResponse::class, $actual);
131
		$this->assertEquals('text/calendar', $actual->getHeaders()['Content-Type']);
132
		$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());
133
	}
134
135
	public function testProxyClientException() {
136
		$testUrl = 'http://abc.def/foobar?123';
137
138
		$this->client->expects($this->once())
139
			->method('newClient')
140
			->will($this->returnValue($this->newClient));
141
		$this->newClient->expects($this->once())
142
			->method('get')
143
			->with($testUrl, [
144
				'allow_redirects' => false,
145
			])
146
			->will($this->throwException(new ClientException('Exception Message foo bar 42',
147
				$this->exceptionRequest, $this->exceptionResponse)));
148
		$this->exceptionResponse->expects($this->once())
149
			->method('getStatusCode')
150
			->will($this->returnValue(403));
151
		$this->l10n->expects($this->once())
152
			->method('t')
153
			->with($this->equalTo('The remote server did not give us access to the calendar (HTTP {%s} error)', '403'))
154
			->will($this->returnValue('translated string 1337'));
155
		$this->logger->expects($this->once())
156
			->method('debug')
157
			->with($this->equalTo('Exception Message foo bar 42'));
158
159
		$actual = $this->controller->proxy($testUrl);
160
161
		$this->assertInstanceOf('OCP\AppFramework\Http\JSONResponse', $actual);
162
		$this->assertEquals('422', $actual->getStatus());
163
		$this->assertEquals([
164
			'message' => 'translated string 1337',
165
			'proxy_code' => 403
166
		], $actual->getData());
167
	}
168
169 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...
170
		$testUrl = 'http://abc.def/foobar?123';
171
172
		$this->client->expects($this->once())
173
			->method('newClient')
174
			->will($this->returnValue($this->newClient));
175
		$this->newClient->expects($this->once())
176
			->method('get')
177
			->with($testUrl, [
178
				'allow_redirects' => false,
179
			])
180
			->will($this->throwException(new ConnectException('Exception Message foo bar 42',
181
				$this->exceptionRequest)));
182
		$this->l10n->expects($this->once())
183
			->method('t')
184
			->with($this->equalTo('Error connecting to remote server'))
185
			->will($this->returnValue('translated string 1337'));
186
		$this->logger->expects($this->once())
187
			->method('debug')
188
			->with($this->equalTo('Exception Message foo bar 42'));
189
190
		$actual = $this->controller->proxy($testUrl);
191
192
		$this->assertInstanceOf('OCP\AppFramework\Http\JSONResponse', $actual);
193
		$this->assertEquals('422', $actual->getStatus());
194
		$this->assertEquals([
195
			'message' => 'translated string 1337',
196
			'proxy_code' => -1
197
		], $actual->getData());
198
	}
199
200 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...
201
		$testUrl = 'http://abc.def/foobar?123';
202
203
		$this->client->expects($this->once())
204
			->method('newClient')
205
			->will($this->returnValue($this->newClient));
206
		$this->newClient->expects($this->once())
207
			->method('get')
208
			->with($testUrl, [
209
				'allow_redirects' => false,
210
			])
211
			->will($this->throwException(new RequestException('Exception Message foo bar 42',
212
				$this->exceptionRequest, $this->exceptionResponse)));
213
		$this->l10n->expects($this->once())
214
			->method('t')
215
			->with($this->equalTo('Error requesting resource on remote server'))
216
			->will($this->returnValue('translated string 1337'));
217
		$this->logger->expects($this->once())
218
			->method('debug')
219
			->with($this->equalTo('Exception Message foo bar 42'));
220
221
		$actual = $this->controller->proxy($testUrl);
222
223
		$this->assertInstanceOf('OCP\AppFramework\Http\JSONResponse', $actual);
224
		$this->assertEquals('422', $actual->getStatus());
225
		$this->assertEquals([
226
			'message' => 'translated string 1337',
227
			'proxy_code' => -2
228
		], $actual->getData());
229
	}
230
231 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...
232
		$testUrl = 'https://abc.def/foobar?123';
233
234
		$this->client->expects($this->once())
235
			->method('newClient')
236
			->will($this->returnValue($this->newClient));
237
		$this->newClient->expects($this->once())
238
			->method('get')
239
			->with($testUrl, [
240
				'allow_redirects' => false,
241
			])
242
			->will($this->throwException(new RequestException('Exception Message foo bar 42',
243
				$this->exceptionRequest, $this->exceptionResponse)));
244
		$this->l10n->expects($this->once())
245
			->method('t')
246
			->with($this->equalTo('Error requesting resource on remote server. This could possible be related to a certificate mismatch'))
247
			->will($this->returnValue('translated string 1337'));
248
		$this->logger->expects($this->once())
249
			->method('debug')
250
			->with($this->equalTo('Exception Message foo bar 42'));
251
252
		$actual = $this->controller->proxy($testUrl);
253
254
		$this->assertInstanceOf('OCP\AppFramework\Http\JSONResponse', $actual);
255
		$this->assertEquals('422', $actual->getStatus());
256
		$this->assertEquals([
257
			'message' => 'translated string 1337',
258
			'proxy_code' => -2
259
		], $actual->getData());
260
	}
261
262 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...
263
		$testUrl = 'http://abc.def/foobar?123';
264
265
		$this->client->expects($this->once())
266
			->method('newClient')
267
			->will($this->returnValue($this->newClient));
268
		$this->newClient->expects($this->at(0))
269
			->method('get')
270
			->with($testUrl, [
271
				'allow_redirects' => false,
272
			])
273
			->will($this->returnValue($this->response0));
274
		$this->response0->expects($this->at(0))
275
			->method('getStatusCode')
276
			->with()
277
			->will($this->returnValue(301));
278
		$this->response0->expects($this->at(1))
279
			->method('getHeader')
280
			->with('Location')
281
			->will($this->returnValue('http://def.abc/foobar?456'));
282
		$this->newClient->expects($this->at(1))
283
			->method('get')
284
			->with('http://def.abc/foobar?456', [
285
				'allow_redirects' => false,
286
			])
287
			->will($this->returnValue($this->response0));
288
		$this->response0->expects($this->at(2))
289
			->method('getStatusCode')
290
			->with()
291
			->will($this->returnValue(200));
292
293
		$actual = $this->controller->proxy($testUrl);
294
295
		$this->assertInstanceOf('OCP\AppFramework\Http\JSONResponse', $actual);
296
		$this->assertEquals([
297
			'proxy_code' => -4,
298
			'new_url' => 'http://def.abc/foobar?456',
299
		], $actual->getData());
300
	}
301
302
	public function testProxyRedirectNonPermanent() {
303
		$testUrl = 'http://abc.def/foobar?123';
304
305
		$this->client->expects($this->once())
306
			->method('newClient')
307
			->will($this->returnValue($this->newClient));
308
		$this->newClient->expects($this->at(0))
309
			->method('get')
310
			->with($testUrl, [
311
				'allow_redirects' => false,
312
			])
313
			->will($this->returnValue($this->response0));
314
		$this->response0->expects($this->at(0))
315
			->method('getStatusCode')
316
			->with()
317
			->will($this->returnValue(307));
318
		$this->newClient->expects($this->at(1))
319
			->method('get')
320
			->with('http://abc.def/foobar?123' , [
321
				'allow_redirects' => [
322
					'max' => 5,
323
				],
324
			])
325
			->will($this->returnValue($this->response1));
326
		$this->response1->expects($this->at(0))
327
			->method('getStatusCode')
328
			->with()
329
			->will($this->returnValue(200));
330
		$this->response1->expects($this->once())
331
			->method('getBody')
332
			->with()
333
			->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"));
334
335
		$actual = $this->controller->proxy($testUrl);
336
337
		$this->assertInstanceOf(DataDisplayResponse::class, $actual);
338
		$this->assertEquals('text/calendar', $actual->getHeaders()['Content-Type']);
339
		$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());
340
	}
341
342
	public function testProxyMultipleRedirects() {
343
		$testUrl = 'http://abc.def/foobar?123';
344
345
		$this->client->expects($this->once())
346
			->method('newClient')
347
			->will($this->returnValue($this->newClient));
348
		$this->newClient->expects($this->at(0))
349
			->method('get')
350
			->with($testUrl, [
351
				'allow_redirects' => false,
352
			])
353
			->will($this->returnValue($this->response0));
354
		$this->newClient->expects($this->at(1))
355
			->method('get')
356
			->with('http://def.abc/foobar?456' , [
357
				'allow_redirects' => false,
358
			])
359
			->will($this->returnValue($this->response1));
360
		$this->newClient->expects($this->at(2))
361
			->method('get')
362
			->with('http://xyz.abc/foobar?789' , [
363
				'allow_redirects' => false,
364
			])
365
			->will($this->returnValue($this->response2));
366
		$this->response0->expects($this->at(0))
367
			->method('getStatusCode')
368
			->with()
369
			->will($this->returnValue(301));
370
		$this->response0->expects($this->at(1))
371
			->method('getHeader')
372
			->with('Location')
373
			->will($this->returnValue('http://def.abc/foobar?456'));
374
		$this->response1->expects($this->at(0))
375
			->method('getStatusCode')
376
			->with()
377
			->will($this->returnValue(301));
378
		$this->response1->expects($this->at(1))
379
			->method('getHeader')
380
			->with('Location')
381
			->will($this->returnValue('http://xyz.abc/foobar?789'));
382
		$this->response2->expects($this->at(0))
383
			->method('getStatusCode')
384
			->with()
385
			->will($this->returnValue(200));
386
387
		$actual = $this->controller->proxy($testUrl);
388
389
		$this->assertInstanceOf('OCP\AppFramework\Http\JSONResponse', $actual);
390
		$this->assertEquals([
391
			'proxy_code' => -4,
392
			'new_url' => 'http://xyz.abc/foobar?789',
393
		], $actual->getData());
394
	}
395
396 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...
397
		$testUrl = 'http://abc.def/foobar?123';
398
399
		$this->client->expects($this->once())
400
			->method('newClient')
401
			->will($this->returnValue($this->newClient));
402
		$this->newClient->expects($this->at(0))
403
			->method('get')
404
			->with($testUrl, [
405
				'allow_redirects' => false,
406
			])
407
			->will($this->returnValue($this->response0));
408
		$this->newClient->expects($this->at(1))
409
			->method('get')
410
			->with('http://def.abc/foobar?456' , [
411
				'allow_redirects' => false,
412
			])
413
			->will($this->returnValue($this->response1));
414
		$this->response0->expects($this->at(0))
415
			->method('getStatusCode')
416
			->with()
417
			->will($this->returnValue(301));
418
		$this->response0->expects($this->at(1))
419
			->method('getHeader')
420
			->with('Location')
421
			->will($this->returnValue('http://def.abc/foobar?456'));
422
		$this->response1->expects($this->at(0))
423
			->method('getStatusCode')
424
			->with()
425
			->will($this->returnValue(307));
426
427
		$actual = $this->controller->proxy($testUrl);
428
429
		$this->assertInstanceOf('OCP\AppFramework\Http\JSONResponse', $actual);
430
		$this->assertEquals([
431
			'proxy_code' => -4,
432
			'new_url' => 'http://def.abc/foobar?456',
433
		], $actual->getData());
434
	}
435
436
	public function testProxyAtMostFiveRedirects() {
437
		$testUrl = 'http://abc.def/foobar?123';
438
439
		$this->client->expects($this->once())
440
			->method('newClient')
441
			->will($this->returnValue($this->newClient));
442
		$this->newClient->expects($this->at(0))
443
			->method('get')
444
			->with($testUrl, [
445
				'allow_redirects' => false,
446
			])
447
			->will($this->returnValue($this->response0));
448
		$this->newClient->expects($this->at(1))
449
			->method('get')
450
			->with('http://def.abc/foobar?456-0', [
451
				'allow_redirects' => false,
452
			])
453
			->will($this->returnValue($this->response1));
454
		$this->newClient->expects($this->at(2))
455
			->method('get')
456
			->with('http://def.abc/foobar?456-1', [
457
				'allow_redirects' => false,
458
			])
459
			->will($this->returnValue($this->response2));
460
		$this->newClient->expects($this->at(3))
461
			->method('get')
462
			->with('http://def.abc/foobar?456-2', [
463
				'allow_redirects' => false,
464
			])
465
			->will($this->returnValue($this->response3));
466
		$this->newClient->expects($this->at(4))
467
			->method('get')
468
			->with('http://def.abc/foobar?456-3', [
469
				'allow_redirects' => false,
470
			])
471
			->will($this->returnValue($this->response4));
472
		$this->newClient->expects($this->at(5))
473
			->method('get')
474
			->with('http://def.abc/foobar?456-4', [
475
				'allow_redirects' => false,
476
			])
477
			->will($this->returnValue($this->response5));
478
		$this->response0->expects($this->at(0))
479
			->method('getStatusCode')
480
			->with()
481
			->will($this->returnValue(301));
482
		$this->response0->expects($this->at(1))
483
			->method('getHeader')
484
			->with('Location')
485
			->will($this->returnValue('http://def.abc/foobar?456-0'));
486
		$this->response1->expects($this->at(0))
487
			->method('getStatusCode')
488
			->with()
489
			->will($this->returnValue(301));
490
		$this->response1->expects($this->at(1))
491
			->method('getHeader')
492
			->with('Location')
493
			->will($this->returnValue('http://def.abc/foobar?456-1'));
494
		$this->response2->expects($this->at(0))
495
			->method('getStatusCode')
496
			->with()
497
			->will($this->returnValue(301));
498
		$this->response2->expects($this->at(1))
499
			->method('getHeader')
500
			->with('Location')
501
			->will($this->returnValue('http://def.abc/foobar?456-2'));
502
		$this->response3->expects($this->at(0))
503
			->method('getStatusCode')
504
			->with()
505
			->will($this->returnValue(301));
506
		$this->response3->expects($this->at(1))
507
			->method('getHeader')
508
			->with('Location')
509
			->will($this->returnValue('http://def.abc/foobar?456-3'));
510
		$this->response4->expects($this->at(0))
511
			->method('getStatusCode')
512
			->with()
513
			->will($this->returnValue(301));
514
		$this->response4->expects($this->at(1))
515
			->method('getHeader')
516
			->with('Location')
517
			->will($this->returnValue('http://def.abc/foobar?456-4'));
518
		$this->response5->expects($this->at(0))
519
			->method('getStatusCode')
520
			->with()
521
			->will($this->returnValue(301));
522
		$this->response5->expects($this->at(1))
523
			->method('getHeader')
524
			->with('Location')
525
			->will($this->returnValue('http://def.abc/foobar?456-5'));
526
		$this->l10n->expects($this->once())
527
			->method('t')
528
			->with($this->equalTo('Too many redirects. Aborting ...'))
529
			->will($this->returnValue('translated string 1337'));
530
		$this->newClient->expects($this->exactly(6))
531
			->method('get');
532
533
		$actual = $this->controller->proxy($testUrl);
534
535
		$this->assertInstanceOf('OCP\AppFramework\Http\JSONResponse', $actual);
536
		$this->assertEquals([
537
			'proxy_code' => -3,
538
			'message' => 'translated string 1337',
539
		], $actual->getData());
540
	}
541
542
543
}
544