AppConfigControllerTest.php 11 KB

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