Welcome to mirror list, hosted at ThFree Co, Russian Federation.

SharingCheckMiddlewareTest.php « Middleware « unit « tests - github.com/nextcloud/gallery.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b4a687000c72cb90dbd37398d610532ab685ed1d (plain)
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
<?php
/**
 * Nextcloud - Gallery
 *
 *
 * This file is licensed under the Affero General Public License version 3 or
 * later. See the COPYING file.
 *
 * @author Olivier Paroz <galleryapps@oparoz.com>
 * @author Lukas Reschke
 * @author Bernhard Posselt
 *
 * @copyright Olivier Paroz 2017
 * @copyright ownCloud Inc. 2015
 */

namespace OCA\Gallery\Tests\Middleware;

use OC\AppFramework\Utility\ControllerMethodReflector;

use OCP\IConfig;
use OCP\IRequest;
use OCP\ILogger;
use OCP\IURLGenerator;

use OCP\AppFramework\Controller;
use OCP\AppFramework\Http;
use OCP\AppFramework\Utility\IControllerMethodReflector;

use OCA\Gallery\Middleware\SharingCheckMiddleware;


/**
 * Class SharingCheckMiddlewareTest
 *
 * @todo It's not possible to test if beforeController still works as expected when sharing is
 *     disabled without creating a full working environment or by refactoring isSharingEnabled
 *
 * @package OCA\Gallery\Tests\Middleware
 */
class SharingCheckMiddlewareTest extends \Test\TestCase {

	/** @var string */
	private $appName = 'gallery';
	/** @var IRequest */
	private $request;
	/** @var IConfig */
	private $config;
	/** @var IControllerMethodReflector */
	protected $reflector;
	/** @var IURLGenerator */
	private $urlGenerator;
	/** @var ILogger */
	protected $logger;
	/** @var Controller */
	private $controller;
	/** @var SharingCheckMiddleware */
	private $middleware;

	/**
	 * Test set up
	 */
	protected function setUp() {
		parent::setUp();

		$this->request = $this->getMockBuilder('\OCP\IRequest')
							  ->disableOriginalConstructor()
							  ->getMock();
		$this->config = $this->getMockBuilder('\OCP\IConfig')
							 ->disableOriginalConstructor()
							 ->getMock();
		// We need to use a real reflector to be able to test our custom notation
		$this->reflector = new ControllerMethodReflector();
		$this->urlGenerator = $this->getMockBuilder('\OCP\IURLGenerator')
								   ->disableOriginalConstructor()
								   ->getMock();
		$this->logger = $this->getMockBuilder('\OCP\ILogger')
							 ->disableOriginalConstructor()
							 ->getMock();
		$this->controller = $this->getMockBuilder('OCP\AppFramework\Controller')
								 ->disableOriginalConstructor()
								 ->getMock();

		$this->middleware = new SharingCheckMiddleware(
			$this->appName,
			$this->request,
			$this->config,
			$this->reflector,
			$this->urlGenerator,
			$this->logger
		);
	}

	public function testIsSharingEnabledWithSharingEnabled() {
		$this->mockSharingConfigTo('yes');

		$this->assertTrue(self::invokePrivate($this->middleware, 'isSharingEnabled'));
	}

	public function testIsSharingEnabledWithSharingDisabled() {
		$this->mockSharingConfigTo('no');

		$this->assertFalse(self::invokePrivate($this->middleware, 'isSharingEnabled'));
	}

	/**
	 * @Guest
	 * @PublicPage
	 *
	 * Guest pages don't need to be checked
	 */
	public function testBeforeControllerWithGuestNotation() {
		$this->reflector->reflect(__CLASS__, __FUNCTION__);
		$this->middleware->beforeController(__CLASS__, __FUNCTION__);
	}

	/**
	 * @PublicPage
	 *
	 * Contains PublicPage notation, does not contain Guest notation and sharing is enabled, so
	 *     should not throw any exception
	 */
	public function testBeforeControllerWithAllRequirements() {
		$this->mockSharingConfigTo('yes');
		$this->reflector->reflect(__CLASS__, __FUNCTION__);
		$this->middleware->beforeController(__CLASS__, __FUNCTION__);
	}

	/**
	 * Non-public pages don't need to be checked
	 */
	public function testBeforeControllerWithoutPublicPageNotation() {
		$this->mockSharingConfigTo('yes');

		$this->reflector->reflect(__CLASS__, __FUNCTION__);
		$this->middleware->beforeController(__CLASS__, __FUNCTION__);
	}

	/**
	 * @PublicPage
	 *
	 * Sharing needs to be enabled on public pages
	 *
	 * @expectedException \OCA\Gallery\Middleware\CheckException
	 */
	public function testBeforeControllerWithSharingDisabled() {
		$this->mockSharingConfigTo('no');
		$this->reflector->reflect(__CLASS__, __FUNCTION__);
		$this->middleware->beforeController(__CLASS__, __FUNCTION__);
	}

	/**
	 * Mocks IConfig->getAppValue
	 *
	 * @param $state
	 */
	private function mockSharingConfigTo($state) {
		$this->config->expects($this->once())
					 ->method('getAppValue')
					 ->with(
						 'core',
						 'shareapi_allow_links',
						 'yes'
					 )
					 ->willReturn($state);
	}

}