Completed
Pull Request — master (#1638)
by Thomas
06:26
created

ProxyControllerTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 5
c 2
b 1
f 0
lcom 1
cbo 2
dl 0
loc 108
rs 10
1
<?php
2
3
/**
4
 * @author Christoph Wurst <[email protected]>
5
 *
6
 * Mail
7
 *
8
 * This code is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Affero General Public License, version 3,
10
 * as published by the Free Software Foundation.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
 * GNU Affero General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License, version 3,
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
19
 *
20
 */
21
use OCP\AppFramework\Http\TemplateResponse;
22
use Test\TestCase;
23
use OCA\Mail\Controller\ProxyController;
24
use OCA\Mail\Http\ProxyDownloadResponse;
25
26
class ProxyControllerTest extends TestCase {
27
28
	private $appName;
29
	private $request;
30
	private $urlGenerator;
31
	private $session;
32
	private $controller;
33
	private $hostname;
34
	private $clientService;
35
	private $client;
36
37
	protected function setUp() {
38
		parent::setUp();
39
40
		$this->appName = 'mail';
41
		$this->request = $this->getMockBuilder('\OCP\IRequest')
42
			->disableOriginalConstructor()
43
			->getMock();
44
		$this->urlGenerator = $this->getMockBuilder('\OCP\IURLGenerator')
45
			->disableOriginalConstructor()
46
			->getMock();
47
		$this->session = $this->getMockBuilder('\OCP\ISession')
48
			->disableOriginalConstructor()
49
			->getMock();
50
		$this->clientService = $this->getMock('\OCP\Http\Client\IClientService');
51
		$this->client = $this->getMock('\OCP\Http\Client\IClient');
52
		$this->clientService->expects($this->any())
53
			->method('getClient')
54
			->will($this->returnValue($this->client));
55
		$this->hostname = 'example.com';
56
	}
57
58
	public function redirectDataProvider() {
59
		return [
60
			[
61
				'http://owncloud.org',
62
				false
63
			],
64
			[
65
				'https://owncloud.org',
66
				false
67
			],
68
			[
69
				'http://example.com',
70
				true
71
			],
72
			[
73
				'https://example.com',
74
				true
75
			]
76
		];
77
	}
78
79
	/**
80
	 * @dataProvider redirectDataProvider
81
	 */
82
	public function testRedirect($url, $authorized) {
83
		$this->urlGenerator->expects($this->once())
84
			->method('linkToRoute')
85
			->with('mail.page.index')
86
			->will($this->returnValue('mail-route'));
87
		$this->controller = new ProxyController($this->appName, $this->request,
88
			$this->urlGenerator, $this->session, $this->clientService, $url, 'example.com');
89
90
		$expected = new TemplateResponse($this->appName, 'redirect',
91
			[
92
			'authorizedRedirect' => $authorized,
93
			'url' => $url,
94
			'urlHost' => parse_url($url, PHP_URL_HOST),
95
			'mailURL' => 'mail-route'
96
			], 'guest');
97
		$response = $this->controller->redirect($url);
98
99
		$this->assertEquals($expected, $response);
100
	}
101
102
	/**
103
	 * @expectedException \Exception
104
	 */
105
	public function testRedirectInvalidUrl() {
106
		$this->controller = new ProxyController($this->appName, $this->request,
107
			$this->urlGenerator, $this->session, $this->clientService, '', '');
108
		$this->controller->redirect('ftp://example.com');
109
	}
110
111
	public function testProxy() {
112
		throw new PHPUnit_Framework_SkippedTestError("Test skipped because version hack in ProxyController::getUrlContents is not mockable");
113
114
		$src = 'http://example.com';
115
		$content = '🐵🐵🐵';
116
117
		$this->session->expects($this->once())
118
			->method('close');
119
		$this->helper->expects($this->once())
120
			->method('getUrlContent')
121
			->with($src)
122
			->will($this->returnValue($content));
123
124
		$expected = new ProxyDownloadResponse($content, $src,
125
			'application/octet-stream');
126
		$this->controller = new ProxyController($this->appName, $this->request,
127
			$this->urlGenerator, $this->session, $this->clientService, '', '');
128
		$response = $this->controller->proxy($src);
129
130
		$this->assertEquals($expected, $response);
131
	}
132
133
}
134