AppConfigControllerTest.php 11 KB

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