1
0

AppConfigControllerTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OCA\Provisioning_API\Tests\Controller;
  7. use OC\AppConfig;
  8. use OCA\Provisioning_API\Controller\AppConfigController;
  9. use OCP\AppFramework\Http;
  10. use OCP\AppFramework\Http\DataResponse;
  11. use OCP\IAppConfig;
  12. use OCP\IGroupManager;
  13. use OCP\IL10N;
  14. use OCP\IRequest;
  15. use OCP\IUser;
  16. use OCP\IUserSession;
  17. use OCP\Settings\IManager;
  18. use Test\TestCase;
  19. /**
  20. * Class AppConfigControllerTest
  21. *
  22. * @package OCA\Provisioning_API\Tests
  23. */
  24. class AppConfigControllerTest extends TestCase {
  25. /** @var IAppConfig|\PHPUnit\Framework\MockObject\MockObject */
  26. private $appConfig;
  27. /** @var IUserSession|\PHPUnit\Framework\MockObject\MockObject */
  28. private $userSession;
  29. /** @var IL10N|\PHPUnit\Framework\MockObject\MockObject */
  30. private $l10n;
  31. /** @var IManager|\PHPUnit\Framework\MockObject\MockObject */
  32. private $settingManager;
  33. /** @var IGroupManager|\PHPUnit\Framework\MockObject\MockObject */
  34. private $groupManager;
  35. protected function setUp(): void {
  36. parent::setUp();
  37. $this->appConfig = $this->createMock(AppConfig::class);
  38. $this->userSession = $this->createMock(IUserSession::class);
  39. $this->l10n = $this->createMock(IL10N::class);
  40. $this->groupManager = $this->createMock(IGroupManager::class);
  41. $this->settingManager = $this->createMock(IManager::class);
  42. }
  43. /**
  44. * @param string[] $methods
  45. * @return AppConfigController|\PHPUnit\Framework\MockObject\MockObject
  46. */
  47. protected function getInstance(array $methods = []) {
  48. $request = $this->createMock(IRequest::class);
  49. if (empty($methods)) {
  50. return new AppConfigController(
  51. 'provisioning_api',
  52. $request,
  53. $this->appConfig,
  54. $this->userSession,
  55. $this->l10n,
  56. $this->groupManager,
  57. $this->settingManager
  58. );
  59. } else {
  60. return $this->getMockBuilder(AppConfigController::class)
  61. ->setConstructorArgs([
  62. 'provisioning_api',
  63. $request,
  64. $this->appConfig,
  65. $this->userSession,
  66. $this->l10n,
  67. $this->groupManager,
  68. $this->settingManager
  69. ])
  70. ->setMethods($methods)
  71. ->getMock();
  72. }
  73. }
  74. public function testGetApps() {
  75. $this->appConfig->expects($this->once())
  76. ->method('getApps')
  77. ->willReturn(['apps']);
  78. $result = $this->getInstance()->getApps();
  79. $this->assertInstanceOf(DataResponse::class, $result);
  80. $this->assertSame(Http::STATUS_OK, $result->getStatus());
  81. $this->assertEquals(['data' => ['apps']], $result->getData());
  82. }
  83. public function dataGetKeys() {
  84. return [
  85. ['app1 ', null, new \InvalidArgumentException('error'), Http::STATUS_FORBIDDEN],
  86. ['app2', ['keys'], null, Http::STATUS_OK],
  87. ];
  88. }
  89. /**
  90. * @dataProvider dataGetKeys
  91. * @param string $app
  92. * @param array|null $keys
  93. * @param \Exception|null $throws
  94. * @param int $status
  95. */
  96. public function testGetKeys($app, $keys, $throws, $status) {
  97. $api = $this->getInstance(['verifyAppId']);
  98. if ($throws instanceof \Exception) {
  99. $api->expects($this->once())
  100. ->method('verifyAppId')
  101. ->with($app)
  102. ->willThrowException($throws);
  103. $this->appConfig->expects($this->never())
  104. ->method('getKeys');
  105. } else {
  106. $api->expects($this->once())
  107. ->method('verifyAppId')
  108. ->with($app);
  109. $this->appConfig->expects($this->once())
  110. ->method('getKeys')
  111. ->with($app)
  112. ->willReturn($keys);
  113. }
  114. $result = $api->getKeys($app);
  115. $this->assertInstanceOf(DataResponse::class, $result);
  116. $this->assertSame($status, $result->getStatus());
  117. if ($throws instanceof \Exception) {
  118. $this->assertEquals(['data' => ['message' => $throws->getMessage()]], $result->getData());
  119. } else {
  120. $this->assertEquals(['data' => $keys], $result->getData());
  121. }
  122. }
  123. public function dataGetValue() {
  124. return [
  125. ['app1', 'key', 'default', null, new \InvalidArgumentException('error'), Http::STATUS_FORBIDDEN],
  126. ['app2', 'key', 'default', 'return', null, Http::STATUS_OK],
  127. ];
  128. }
  129. /**
  130. * @dataProvider dataGetValue
  131. * @param string $app
  132. * @param string|null $key
  133. * @param string|null $default
  134. * @param string|null $return
  135. * @param \Exception|null $throws
  136. * @param int $status
  137. */
  138. public function testGetValue($app, $key, $default, $return, $throws, $status) {
  139. $api = $this->getInstance(['verifyAppId']);
  140. if ($throws instanceof \Exception) {
  141. $api->expects($this->once())
  142. ->method('verifyAppId')
  143. ->with($app)
  144. ->willThrowException($throws);
  145. } else {
  146. $api->expects($this->once())
  147. ->method('verifyAppId')
  148. ->with($app);
  149. $this->appConfig->expects($this->once())
  150. ->method('getValueMixed')
  151. ->with($app, $key, $default)
  152. ->willReturn($return);
  153. }
  154. $result = $api->getValue($app, $key, $default);
  155. $this->assertInstanceOf(DataResponse::class, $result);
  156. $this->assertSame($status, $result->getStatus());
  157. if ($throws instanceof \Exception) {
  158. $this->assertEquals(['data' => ['message' => $throws->getMessage()]], $result->getData());
  159. } else {
  160. $this->assertEquals(['data' => $return], $result->getData());
  161. }
  162. }
  163. public function dataSetValue() {
  164. return [
  165. ['app1', 'key', 'default', new \InvalidArgumentException('error1'), null, Http::STATUS_FORBIDDEN],
  166. ['app2', 'key', 'default', null, new \InvalidArgumentException('error2'), Http::STATUS_FORBIDDEN],
  167. ['app2', 'key', 'default', null, null, Http::STATUS_OK],
  168. ];
  169. }
  170. /**
  171. * @dataProvider dataSetValue
  172. * @param string $app
  173. * @param string|null $key
  174. * @param string|null $value
  175. * @param \Exception|null $appThrows
  176. * @param \Exception|null $keyThrows
  177. * @param int $status
  178. */
  179. public function testSetValue($app, $key, $value, $appThrows, $keyThrows, $status) {
  180. $adminUser = $this->createMock(IUser::class);
  181. $adminUser->expects($this->once())
  182. ->method('getUid')
  183. ->willReturn('admin');
  184. $this->userSession->expects($this->once())
  185. ->method('getUser')
  186. ->willReturn($adminUser);
  187. $this->groupManager->expects($this->once())
  188. ->method('isAdmin')
  189. ->with('admin')
  190. ->willReturn(true);
  191. $api = $this->getInstance(['verifyAppId', 'verifyConfigKey']);
  192. if ($appThrows instanceof \Exception) {
  193. $api->expects($this->once())
  194. ->method('verifyAppId')
  195. ->with($app)
  196. ->willThrowException($appThrows);
  197. $api->expects($this->never())
  198. ->method('verifyConfigKey');
  199. $this->appConfig->expects($this->never())
  200. ->method('setValueMixed');
  201. } elseif ($keyThrows instanceof \Exception) {
  202. $api->expects($this->once())
  203. ->method('verifyAppId')
  204. ->with($app);
  205. $api->expects($this->once())
  206. ->method('verifyConfigKey')
  207. ->with($app, $key)
  208. ->willThrowException($keyThrows);
  209. $this->appConfig->expects($this->never())
  210. ->method('setValueMixed');
  211. } else {
  212. $api->expects($this->once())
  213. ->method('verifyAppId')
  214. ->with($app);
  215. $api->expects($this->once())
  216. ->method('verifyConfigKey')
  217. ->with($app, $key);
  218. $this->appConfig->expects($this->once())
  219. ->method('setValueMixed')
  220. ->with($app, $key, $value);
  221. }
  222. $result = $api->setValue($app, $key, $value);
  223. $this->assertInstanceOf(DataResponse::class, $result);
  224. $this->assertSame($status, $result->getStatus());
  225. if ($appThrows instanceof \Exception) {
  226. $this->assertEquals(['data' => ['message' => $appThrows->getMessage()]], $result->getData());
  227. } elseif ($keyThrows instanceof \Exception) {
  228. $this->assertEquals(['data' => ['message' => $keyThrows->getMessage()]], $result->getData());
  229. } else {
  230. $this->assertEquals([], $result->getData());
  231. }
  232. }
  233. public function dataDeleteValue() {
  234. return [
  235. ['app1', 'key', new \InvalidArgumentException('error1'), null, Http::STATUS_FORBIDDEN],
  236. ['app2', 'key', null, new \InvalidArgumentException('error2'), Http::STATUS_FORBIDDEN],
  237. ['app2', 'key', null, null, Http::STATUS_OK],
  238. ];
  239. }
  240. /**
  241. * @dataProvider dataDeleteValue
  242. * @param string $app
  243. * @param string|null $key
  244. * @param \Exception|null $appThrows
  245. * @param \Exception|null $keyThrows
  246. * @param int $status
  247. */
  248. public function testDeleteValue($app, $key, $appThrows, $keyThrows, $status) {
  249. $api = $this->getInstance(['verifyAppId', 'verifyConfigKey']);
  250. if ($appThrows instanceof \Exception) {
  251. $api->expects($this->once())
  252. ->method('verifyAppId')
  253. ->with($app)
  254. ->willThrowException($appThrows);
  255. $api->expects($this->never())
  256. ->method('verifyConfigKey');
  257. $this->appConfig->expects($this->never())
  258. ->method('deleteKey');
  259. } elseif ($keyThrows instanceof \Exception) {
  260. $api->expects($this->once())
  261. ->method('verifyAppId')
  262. ->with($app);
  263. $api->expects($this->once())
  264. ->method('verifyConfigKey')
  265. ->with($app, $key)
  266. ->willThrowException($keyThrows);
  267. $this->appConfig->expects($this->never())
  268. ->method('deleteKey');
  269. } else {
  270. $api->expects($this->once())
  271. ->method('verifyAppId')
  272. ->with($app);
  273. $api->expects($this->once())
  274. ->method('verifyConfigKey')
  275. ->with($app, $key);
  276. $this->appConfig->expects($this->once())
  277. ->method('deleteKey')
  278. ->with($app, $key);
  279. }
  280. $result = $api->deleteKey($app, $key);
  281. $this->assertInstanceOf(DataResponse::class, $result);
  282. $this->assertSame($status, $result->getStatus());
  283. if ($appThrows instanceof \Exception) {
  284. $this->assertEquals(['data' => ['message' => $appThrows->getMessage()]], $result->getData());
  285. } elseif ($keyThrows instanceof \Exception) {
  286. $this->assertEquals(['data' => ['message' => $keyThrows->getMessage()]], $result->getData());
  287. } else {
  288. $this->assertEquals([], $result->getData());
  289. }
  290. }
  291. public function testVerifyAppId() {
  292. $api = $this->getInstance();
  293. $this->invokePrivate($api, 'verifyAppId', ['activity']);
  294. $this->addToAssertionCount(1);
  295. }
  296. public function dataVerifyAppIdThrows() {
  297. return [
  298. ['activity..'],
  299. ['activity/'],
  300. ['activity\\'],
  301. ['activity\0'],
  302. ];
  303. }
  304. /**
  305. * @dataProvider dataVerifyAppIdThrows
  306. * @param string $app
  307. */
  308. public function testVerifyAppIdThrows($app) {
  309. $this->expectException(\InvalidArgumentException::class);
  310. $api = $this->getInstance();
  311. $this->invokePrivate($api, 'verifyAppId', [$app]);
  312. }
  313. public function dataVerifyConfigKey() {
  314. return [
  315. ['activity', 'abc', ''],
  316. ['dav', 'public_route', ''],
  317. ['files', 'remote_route', ''],
  318. ['core', 'encryption_enabled', 'yes'],
  319. ];
  320. }
  321. /**
  322. * @dataProvider dataVerifyConfigKey
  323. * @param string $app
  324. * @param string $key
  325. * @param string $value
  326. */
  327. public function testVerifyConfigKey($app, $key, $value) {
  328. $api = $this->getInstance();
  329. $this->invokePrivate($api, 'verifyConfigKey', [$app, $key, $value]);
  330. $this->addToAssertionCount(1);
  331. }
  332. public function dataVerifyConfigKeyThrows() {
  333. return [
  334. ['activity', 'installed_version', ''],
  335. ['calendar', 'enabled', ''],
  336. ['contacts', 'types', ''],
  337. ['core', 'encryption_enabled', 'no'],
  338. ['core', 'encryption_enabled', ''],
  339. ['core', 'public_files', ''],
  340. ['core', 'public_dav', ''],
  341. ['core', 'remote_files', ''],
  342. ['core', 'remote_dav', ''],
  343. ];
  344. }
  345. /**
  346. * @dataProvider dataVerifyConfigKeyThrows
  347. * @param string $app
  348. * @param string $key
  349. * @param string $value
  350. */
  351. public function testVerifyConfigKeyThrows($app, $key, $value) {
  352. $this->expectException(\InvalidArgumentException::class);
  353. $api = $this->getInstance();
  354. $this->invokePrivate($api, 'verifyConfigKey', [$app, $key, $value]);
  355. }
  356. }