You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

382 lines
10 KiB

  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016 Joas Schilling <coding@schilljs.com>
  4. *
  5. * @license GNU AGPL version 3 or any later version
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Affero General Public License as
  9. * published by the Free Software Foundation, either version 3 of the
  10. * License, or (at your option) any later version.
  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
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. */
  21. namespace OCA\Provisioning_API\Tests\Controller;
  22. use OCA\Provisioning_API\Controller\AppConfigController;
  23. use OCP\AppFramework\Http;
  24. use OCP\AppFramework\Http\DataResponse;
  25. use OCP\IAppConfig;
  26. use OCP\IConfig;
  27. use OCP\IRequest;
  28. use Test\TestCase;
  29. /**
  30. * Class AppConfigControllerTest
  31. *
  32. * @package OCA\Provisioning_API\Tests
  33. */
  34. class AppConfigControllerTest extends TestCase {
  35. /** @var IConfig|\PHPUnit_Framework_MockObject_MockObject */
  36. private $config;
  37. /** @var IAppConfig|\PHPUnit_Framework_MockObject_MockObject */
  38. private $appConfig;
  39. protected function setUp() {
  40. parent::setUp();
  41. $this->config = $this->createMock(IConfig::class);
  42. $this->appConfig = $this->createMock(IAppConfig::class);
  43. }
  44. /**
  45. * @param string[] $methods
  46. * @return AppConfigController|\PHPUnit_Framework_MockObject_MockObject
  47. */
  48. protected function getInstance(array $methods = []) {
  49. $request = $this->createMock(IRequest::class);
  50. if (empty($methods)) {
  51. return new AppConfigController(
  52. 'provisioning_api',
  53. $request,
  54. $this->config,
  55. $this->appConfig
  56. );
  57. } else {
  58. return $this->getMockBuilder(AppConfigController::class)
  59. ->setConstructorArgs([
  60. 'provisioning_api',
  61. $request,
  62. $this->config,
  63. $this->appConfig,
  64. ])
  65. ->setMethods($methods)
  66. ->getMock();
  67. }
  68. }
  69. public function testGetApps() {
  70. $this->appConfig->expects($this->once())
  71. ->method('getApps')
  72. ->willReturn(['apps']);
  73. $result = $this->getInstance()->getApps();
  74. $this->assertInstanceOf(DataResponse::class, $result);
  75. $this->assertSame(Http::STATUS_OK, $result->getStatus());
  76. $this->assertEquals(['data' => ['apps']], $result->getData());
  77. }
  78. public function dataGetKeys() {
  79. return [
  80. ['app1 ', null, new \InvalidArgumentException('error'), Http::STATUS_FORBIDDEN],
  81. ['app2', ['keys'], null, Http::STATUS_OK],
  82. ];
  83. }
  84. /**
  85. * @dataProvider dataGetKeys
  86. * @param string $app
  87. * @param array|null $keys
  88. * @param \Exception|null $throws
  89. * @param int $status
  90. */
  91. public function testGetKeys($app, $keys, $throws, $status) {
  92. $api = $this->getInstance(['verifyAppId']);
  93. if ($throws instanceof \Exception) {
  94. $api->expects($this->once())
  95. ->method('verifyAppId')
  96. ->with($app)
  97. ->willThrowException($throws);
  98. $this->config->expects($this->never())
  99. ->method('getAppKeys');
  100. } else {
  101. $api->expects($this->once())
  102. ->method('verifyAppId')
  103. ->with($app);
  104. $this->config->expects($this->once())
  105. ->method('getAppKeys')
  106. ->with($app)
  107. ->willReturn($keys);
  108. }
  109. $result = $api->getKeys($app);
  110. $this->assertInstanceOf(DataResponse::class, $result);
  111. $this->assertSame($status, $result->getStatus());
  112. if ($throws instanceof \Exception) {
  113. $this->assertEquals(['data' => ['message' => $throws->getMessage()]], $result->getData());
  114. } else {
  115. $this->assertEquals(['data' => $keys], $result->getData());
  116. }
  117. }
  118. public function dataGetValue() {
  119. return [
  120. ['app1 ', null, null, null, new \InvalidArgumentException('error'), Http::STATUS_FORBIDDEN],
  121. ['app2', 'key', 'default', 'return', null, Http::STATUS_OK],
  122. ];
  123. }
  124. /**
  125. * @dataProvider dataGetValue
  126. * @param string $app
  127. * @param string|null $key
  128. * @param string|null $default
  129. * @param string|null $return
  130. * @param \Exception|null $throws
  131. * @param int $status
  132. */
  133. public function testGetValue($app, $key, $default, $return, $throws, $status) {
  134. $api = $this->getInstance(['verifyAppId']);
  135. if ($throws instanceof \Exception) {
  136. $api->expects($this->once())
  137. ->method('verifyAppId')
  138. ->with($app)
  139. ->willThrowException($throws);
  140. $this->config->expects($this->never())
  141. ->method('getAppValue');
  142. } else {
  143. $api->expects($this->once())
  144. ->method('verifyAppId')
  145. ->with($app);
  146. $this->config->expects($this->once())
  147. ->method('getAppValue')
  148. ->with($app, $key, $default)
  149. ->willReturn($return);
  150. }
  151. $result = $api->getValue($app, $key, $default);
  152. $this->assertInstanceOf(DataResponse::class, $result);
  153. $this->assertSame($status, $result->getStatus());
  154. if ($throws instanceof \Exception) {
  155. $this->assertEquals(['data' => ['message' => $throws->getMessage()]], $result->getData());
  156. } else {
  157. $this->assertEquals(['data' => $return], $result->getData());
  158. }
  159. }
  160. public function dataSetValue() {
  161. return [
  162. ['app1 ', null, null, new \InvalidArgumentException('error1'), null, Http::STATUS_FORBIDDEN],
  163. ['app2', 'key', null, null, new \InvalidArgumentException('error2'), Http::STATUS_FORBIDDEN],
  164. ['app2', 'key', 'default', null, null, Http::STATUS_OK],
  165. ];
  166. }
  167. /**
  168. * @dataProvider dataSetValue
  169. * @param string $app
  170. * @param string|null $key
  171. * @param string|null $value
  172. * @param \Exception|null $appThrows
  173. * @param \Exception|null $keyThrows
  174. * @param int $status
  175. */
  176. public function testSetValue($app, $key, $value, $appThrows, $keyThrows, $status) {
  177. $api = $this->getInstance(['verifyAppId', 'verifyConfigKey']);
  178. if ($appThrows instanceof \Exception) {
  179. $api->expects($this->once())
  180. ->method('verifyAppId')
  181. ->with($app)
  182. ->willThrowException($appThrows);
  183. $api->expects($this->never())
  184. ->method('verifyConfigKey');
  185. $this->config->expects($this->never())
  186. ->method('setAppValue');
  187. } else if ($keyThrows instanceof \Exception) {
  188. $api->expects($this->once())
  189. ->method('verifyAppId')
  190. ->with($app);
  191. $api->expects($this->once())
  192. ->method('verifyConfigKey')
  193. ->with($app, $key)
  194. ->willThrowException($keyThrows);
  195. $this->config->expects($this->never())
  196. ->method('setAppValue');
  197. } else {
  198. $api->expects($this->once())
  199. ->method('verifyAppId')
  200. ->with($app);
  201. $api->expects($this->once())
  202. ->method('verifyConfigKey')
  203. ->with($app, $key);
  204. $this->config->expects($this->once())
  205. ->method('setAppValue')
  206. ->with($app, $key, $value);
  207. }
  208. $result = $api->setValue($app, $key, $value);
  209. $this->assertInstanceOf(DataResponse::class, $result);
  210. $this->assertSame($status, $result->getStatus());
  211. if ($appThrows instanceof \Exception) {
  212. $this->assertEquals(['data' => ['message' => $appThrows->getMessage()]], $result->getData());
  213. } else if ($keyThrows instanceof \Exception) {
  214. $this->assertEquals(['data' => ['message' => $keyThrows->getMessage()]], $result->getData());
  215. } else {
  216. $this->assertEquals([], $result->getData());
  217. }
  218. }
  219. public function dataDeleteValue() {
  220. return [
  221. ['app1 ', null, new \InvalidArgumentException('error1'), null, Http::STATUS_FORBIDDEN],
  222. ['app2', 'key', null, new \InvalidArgumentException('error2'), Http::STATUS_FORBIDDEN],
  223. ['app2', 'key', null, null, Http::STATUS_OK],
  224. ];
  225. }
  226. /**
  227. * @dataProvider dataDeleteValue
  228. * @param string $app
  229. * @param string|null $key
  230. * @param \Exception|null $appThrows
  231. * @param \Exception|null $keyThrows
  232. * @param int $status
  233. */
  234. public function testDeleteValue($app, $key, $appThrows, $keyThrows, $status) {
  235. $api = $this->getInstance(['verifyAppId', 'verifyConfigKey']);
  236. if ($appThrows instanceof \Exception) {
  237. $api->expects($this->once())
  238. ->method('verifyAppId')
  239. ->with($app)
  240. ->willThrowException($appThrows);
  241. $api->expects($this->never())
  242. ->method('verifyConfigKey');
  243. $this->config->expects($this->never())
  244. ->method('deleteAppValue');
  245. } else if ($keyThrows instanceof \Exception) {
  246. $api->expects($this->once())
  247. ->method('verifyAppId')
  248. ->with($app);
  249. $api->expects($this->once())
  250. ->method('verifyConfigKey')
  251. ->with($app, $key)
  252. ->willThrowException($keyThrows);
  253. $this->config->expects($this->never())
  254. ->method('deleteAppValue');
  255. } else {
  256. $api->expects($this->once())
  257. ->method('verifyAppId')
  258. ->with($app);
  259. $api->expects($this->once())
  260. ->method('verifyConfigKey')
  261. ->with($app, $key);
  262. $this->config->expects($this->once())
  263. ->method('deleteAppValue')
  264. ->with($app, $key);
  265. }
  266. $result = $api->deleteKey($app, $key);
  267. $this->assertInstanceOf(DataResponse::class, $result);
  268. $this->assertSame($status, $result->getStatus());
  269. if ($appThrows instanceof \Exception) {
  270. $this->assertEquals(['data' => ['message' => $appThrows->getMessage()]], $result->getData());
  271. } else if ($keyThrows instanceof \Exception) {
  272. $this->assertEquals(['data' => ['message' => $keyThrows->getMessage()]], $result->getData());
  273. } else {
  274. $this->assertEquals([], $result->getData());
  275. }
  276. }
  277. public function testVerifyAppId() {
  278. $api = $this->getInstance();
  279. $this->invokePrivate($api, 'verifyAppId', ['activity']);
  280. $this->assertTrue(true);
  281. }
  282. public function dataVerifyAppIdThrows() {
  283. return [
  284. ['activity..'],
  285. ['activity/'],
  286. ['activity\\'],
  287. ['activity\0'],
  288. ];
  289. }
  290. /**
  291. * @dataProvider dataVerifyAppIdThrows
  292. * @expectedException \InvalidArgumentException
  293. * @param string $app
  294. */
  295. public function testVerifyAppIdThrows($app) {
  296. $api = $this->getInstance();
  297. $this->invokePrivate($api, 'verifyAppId', [$app]);
  298. }
  299. public function dataVerifyConfigKey() {
  300. return [
  301. ['activity', 'abc'],
  302. ['dav', 'public_route'],
  303. ['files', 'remote_route'],
  304. ];
  305. }
  306. /**
  307. * @dataProvider dataVerifyConfigKey
  308. * @param string $app
  309. * @param string $key
  310. */
  311. public function testVerifyConfigKey($app, $key) {
  312. $api = $this->getInstance();
  313. $this->invokePrivate($api, 'verifyConfigKey', [$app, $key]);
  314. $this->assertTrue(true);
  315. }
  316. public function dataVerifyConfigKeyThrows() {
  317. return [
  318. ['activity', 'installed_version'],
  319. ['calendar', 'enabled'],
  320. ['contacts', 'types'],
  321. ['core', 'public_files'],
  322. ['core', 'public_dav'],
  323. ['core', 'remote_files'],
  324. ['core', 'remote_dav'],
  325. ];
  326. }
  327. /**
  328. * @dataProvider dataVerifyConfigKeyThrows
  329. * @expectedException \InvalidArgumentException
  330. * @param string $app
  331. * @param string $key
  332. */
  333. public function testVerifyConfigKeyThrows($app, $key) {
  334. $api = $this->getInstance();
  335. $this->invokePrivate($api, 'verifyConfigKey', [$app, $key]);
  336. }
  337. }