1
0

CapabilitiesTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Bjoern Schiessle <bjoern@schiessle.org>
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author Morris Jobke <hey@morrisjobke.de>
  9. * @author Robin Appelman <robin@icewind.nl>
  10. * @author Roeland Jago Douma <roeland@famdouma.nl>
  11. * @author Thomas Müller <thomas.mueller@tmit.eu>
  12. * @author Vincent Petry <vincent@nextcloud.com>
  13. *
  14. * @license AGPL-3.0
  15. *
  16. * This code is free software: you can redistribute it and/or modify
  17. * it under the terms of the GNU Affero General Public License, version 3,
  18. * as published by the Free Software Foundation.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU Affero General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU Affero General Public License, version 3,
  26. * along with this program. If not, see <http://www.gnu.org/licenses/>
  27. *
  28. */
  29. namespace OCA\Files_Sharing\Tests;
  30. use OC\KnownUser\KnownUserService;
  31. use OC\Share20\Manager;
  32. use OCA\Files_Sharing\Capabilities;
  33. use OCP\EventDispatcher\IEventDispatcher;
  34. use OCP\Files\IRootFolder;
  35. use OCP\Files\Mount\IMountManager;
  36. use OCP\IConfig;
  37. use OCP\IGroupManager;
  38. use OCP\IL10N;
  39. use OCP\IURLGenerator;
  40. use OCP\IUserManager;
  41. use OCP\IUserSession;
  42. use OCP\L10N\IFactory;
  43. use OCP\Mail\IMailer;
  44. use OCP\Security\IHasher;
  45. use OCP\Security\ISecureRandom;
  46. use OCP\Share\IProviderFactory;
  47. use Psr\Log\LoggerInterface;
  48. /**
  49. * Class CapabilitiesTest
  50. *
  51. * @group DB
  52. */
  53. class CapabilitiesTest extends \Test\TestCase {
  54. /**
  55. * Test for the general part in each return statement and assert.
  56. * Strip of the general part on the way.
  57. *
  58. * @param string[] $data Capabilities
  59. * @return string[]
  60. */
  61. private function getFilesSharingPart(array $data) {
  62. $this->assertArrayHasKey('files_sharing', $data);
  63. return $data['files_sharing'];
  64. }
  65. /**
  66. * Create a mock config object and insert the values in $map to the getAppValue
  67. * function. Then obtain the capabilities and extract the first few
  68. * levels in the array
  69. *
  70. * @param (string[])[] $map Map of arguments to return types for the getAppValue function in the mock
  71. * @return string[]
  72. */
  73. private function getResults(array $map) {
  74. $config = $this->getMockBuilder(IConfig::class)->disableOriginalConstructor()->getMock();
  75. $config->method('getAppValue')->willReturnMap($map);
  76. $shareManager = new Manager(
  77. $this->createMock(LoggerInterface::class),
  78. $config,
  79. $this->createMock(ISecureRandom::class),
  80. $this->createMock(IHasher::class),
  81. $this->createMock(IMountManager::class),
  82. $this->createMock(IGroupManager::class),
  83. $this->createMock(IL10N::class),
  84. $this->createMock(IFactory::class),
  85. $this->createMock(IProviderFactory::class),
  86. $this->createMock(IUserManager::class),
  87. $this->createMock(IRootFolder::class),
  88. $this->createMock(IMailer::class),
  89. $this->createMock(IURLGenerator::class),
  90. $this->createMock(\OC_Defaults::class),
  91. $this->createMock(IEventDispatcher::class),
  92. $this->createMock(IUserSession::class),
  93. $this->createMock(KnownUserService::class)
  94. );
  95. $cap = new Capabilities($config, $shareManager);
  96. $result = $this->getFilesSharingPart($cap->getCapabilities());
  97. return $result;
  98. }
  99. public function testEnabledSharingAPI() {
  100. $map = [
  101. ['core', 'shareapi_enabled', 'yes', 'yes'],
  102. ];
  103. $result = $this->getResults($map);
  104. $this->assertTrue($result['api_enabled']);
  105. $this->assertArrayHasKey('public', $result);
  106. $this->assertArrayHasKey('user', $result);
  107. $this->assertArrayHasKey('resharing', $result);
  108. }
  109. public function testDisabledSharingAPI() {
  110. $map = [
  111. ['core', 'shareapi_enabled', 'yes', 'no'],
  112. ];
  113. $result = $this->getResults($map);
  114. $this->assertFalse($result['api_enabled']);
  115. $this->assertFalse($result['public']['enabled']);
  116. $this->assertFalse($result['user']['send_mail']);
  117. $this->assertFalse($result['resharing']);
  118. }
  119. public function testNoLinkSharing() {
  120. $map = [
  121. ['core', 'shareapi_enabled', 'yes', 'yes'],
  122. ['core', 'shareapi_allow_links', 'yes', 'no'],
  123. ];
  124. $result = $this->getResults($map);
  125. $this->assertIsArray($result['public']);
  126. $this->assertFalse($result['public']['enabled']);
  127. }
  128. public function testOnlyLinkSharing() {
  129. $map = [
  130. ['core', 'shareapi_enabled', 'yes', 'yes'],
  131. ['core', 'shareapi_allow_links', 'yes', 'yes'],
  132. ['core', 'shareapi_enforce_links_password_excluded_groups', '', ''],
  133. ];
  134. $result = $this->getResults($map);
  135. $this->assertIsArray($result['public']);
  136. $this->assertTrue($result['public']['enabled']);
  137. }
  138. public function testLinkPassword() {
  139. $map = [
  140. ['core', 'shareapi_enabled', 'yes', 'yes'],
  141. ['core', 'shareapi_allow_links', 'yes', 'yes'],
  142. ['core', 'shareapi_enforce_links_password_excluded_groups', '', ''],
  143. ['core', 'shareapi_enforce_links_password', 'no', 'yes'],
  144. ];
  145. $result = $this->getResults($map);
  146. $this->assertArrayHasKey('password', $result['public']);
  147. $this->assertArrayHasKey('enforced', $result['public']['password']);
  148. $this->assertTrue($result['public']['password']['enforced']);
  149. }
  150. public function testLinkNoPassword() {
  151. $map = [
  152. ['core', 'shareapi_enabled', 'yes', 'yes'],
  153. ['core', 'shareapi_allow_links', 'yes', 'yes'],
  154. ['core', 'shareapi_enforce_links_password_excluded_groups', '', ''],
  155. ['core', 'shareapi_enforce_links_password', 'no', 'no'],
  156. ];
  157. $result = $this->getResults($map);
  158. $this->assertArrayHasKey('password', $result['public']);
  159. $this->assertArrayHasKey('enforced', $result['public']['password']);
  160. $this->assertFalse($result['public']['password']['enforced']);
  161. }
  162. public function testLinkNoExpireDate() {
  163. $map = [
  164. ['core', 'shareapi_enabled', 'yes', 'yes'],
  165. ['core', 'shareapi_allow_links', 'yes', 'yes'],
  166. ['core', 'shareapi_default_expire_date', 'no', 'no'],
  167. ['core', 'shareapi_enforce_links_password_excluded_groups', '', ''],
  168. ];
  169. $result = $this->getResults($map);
  170. $this->assertArrayHasKey('expire_date', $result['public']);
  171. $this->assertIsArray($result['public']['expire_date']);
  172. $this->assertFalse($result['public']['expire_date']['enabled']);
  173. }
  174. public function testLinkExpireDate() {
  175. $map = [
  176. ['core', 'shareapi_enabled', 'yes', 'yes'],
  177. ['core', 'shareapi_allow_links', 'yes', 'yes'],
  178. ['core', 'shareapi_default_expire_date', 'no', 'yes'],
  179. ['core', 'shareapi_expire_after_n_days', '7', '7'],
  180. ['core', 'shareapi_enforce_expire_date', 'no', 'no'],
  181. ['core', 'shareapi_enforce_links_password_excluded_groups', '', ''],
  182. ];
  183. $result = $this->getResults($map);
  184. $this->assertArrayHasKey('expire_date', $result['public']);
  185. $this->assertIsArray($result['public']['expire_date']);
  186. $this->assertTrue($result['public']['expire_date']['enabled']);
  187. $this->assertArrayHasKey('days', $result['public']['expire_date']);
  188. $this->assertFalse($result['public']['expire_date']['enforced']);
  189. }
  190. public function testLinkExpireDateEnforced() {
  191. $map = [
  192. ['core', 'shareapi_enabled', 'yes', 'yes'],
  193. ['core', 'shareapi_allow_links', 'yes', 'yes'],
  194. ['core', 'shareapi_default_expire_date', 'no', 'yes'],
  195. ['core', 'shareapi_enforce_expire_date', 'no', 'yes'],
  196. ['core', 'shareapi_enforce_links_password_excluded_groups', '', ''],
  197. ];
  198. $result = $this->getResults($map);
  199. $this->assertArrayHasKey('expire_date', $result['public']);
  200. $this->assertIsArray($result['public']['expire_date']);
  201. $this->assertTrue($result['public']['expire_date']['enforced']);
  202. }
  203. public function testLinkSendMail() {
  204. $map = [
  205. ['core', 'shareapi_enabled', 'yes', 'yes'],
  206. ['core', 'shareapi_allow_links', 'yes', 'yes'],
  207. ['core', 'shareapi_allow_public_notification', 'no', 'yes'],
  208. ['core', 'shareapi_enforce_links_password_excluded_groups', '', ''],
  209. ];
  210. $result = $this->getResults($map);
  211. $this->assertTrue($result['public']['send_mail']);
  212. }
  213. public function testLinkNoSendMail() {
  214. $map = [
  215. ['core', 'shareapi_enabled', 'yes', 'yes'],
  216. ['core', 'shareapi_allow_links', 'yes', 'yes'],
  217. ['core', 'shareapi_allow_public_notification', 'no', 'no'],
  218. ['core', 'shareapi_enforce_links_password_excluded_groups', '', ''],
  219. ];
  220. $result = $this->getResults($map);
  221. $this->assertFalse($result['public']['send_mail']);
  222. }
  223. public function testResharing() {
  224. $map = [
  225. ['core', 'shareapi_enabled', 'yes', 'yes'],
  226. ['core', 'shareapi_allow_resharing', 'yes', 'yes'],
  227. ['core', 'shareapi_enforce_links_password_excluded_groups', '', ''],
  228. ];
  229. $result = $this->getResults($map);
  230. $this->assertTrue($result['resharing']);
  231. }
  232. public function testNoResharing() {
  233. $map = [
  234. ['core', 'shareapi_enabled', 'yes', 'yes'],
  235. ['core', 'shareapi_allow_resharing', 'yes', 'no'],
  236. ['core', 'shareapi_enforce_links_password_excluded_groups', '', ''],
  237. ];
  238. $result = $this->getResults($map);
  239. $this->assertFalse($result['resharing']);
  240. }
  241. public function testLinkPublicUpload() {
  242. $map = [
  243. ['core', 'shareapi_enabled', 'yes', 'yes'],
  244. ['core', 'shareapi_allow_links', 'yes', 'yes'],
  245. ['core', 'shareapi_allow_public_upload', 'yes', 'yes'],
  246. ['core', 'shareapi_enforce_links_password_excluded_groups', '', ''],
  247. ];
  248. $result = $this->getResults($map);
  249. $this->assertTrue($result['public']['upload']);
  250. $this->assertTrue($result['public']['upload_files_drop']);
  251. }
  252. public function testLinkNoPublicUpload() {
  253. $map = [
  254. ['core', 'shareapi_enabled', 'yes', 'yes'],
  255. ['core', 'shareapi_allow_links', 'yes', 'yes'],
  256. ['core', 'shareapi_allow_public_upload', 'yes', 'no'],
  257. ['core', 'shareapi_enforce_links_password_excluded_groups', '', ''],
  258. ];
  259. $result = $this->getResults($map);
  260. $this->assertFalse($result['public']['upload']);
  261. $this->assertFalse($result['public']['upload_files_drop']);
  262. }
  263. public function testNoGroupSharing() {
  264. $map = [
  265. ['core', 'shareapi_enabled', 'yes', 'yes'],
  266. ['core', 'shareapi_allow_group_sharing', 'yes', 'no'],
  267. ];
  268. $result = $this->getResults($map);
  269. $this->assertFalse($result['group_sharing']);
  270. }
  271. public function testGroupSharing() {
  272. $map = [
  273. ['core', 'shareapi_enabled', 'yes', 'yes'],
  274. ['core', 'shareapi_allow_group_sharing', 'yes', 'yes'],
  275. ];
  276. $result = $this->getResults($map);
  277. $this->assertTrue($result['group_sharing']);
  278. }
  279. public function testFederatedSharingIncoming() {
  280. $map = [
  281. ['files_sharing', 'incoming_server2server_share_enabled', 'yes', 'yes'],
  282. ];
  283. $result = $this->getResults($map);
  284. $this->assertArrayHasKey('federation', $result);
  285. $this->assertTrue($result['federation']['incoming']);
  286. }
  287. public function testFederatedSharingNoIncoming() {
  288. $map = [
  289. ['files_sharing', 'incoming_server2server_share_enabled', 'yes', 'no'],
  290. ];
  291. $result = $this->getResults($map);
  292. $this->assertArrayHasKey('federation', $result);
  293. $this->assertFalse($result['federation']['incoming']);
  294. }
  295. public function testFederatedSharingOutgoing() {
  296. $map = [
  297. ['files_sharing', 'outgoing_server2server_share_enabled', 'yes', 'yes'],
  298. ];
  299. $result = $this->getResults($map);
  300. $this->assertArrayHasKey('federation', $result);
  301. $this->assertTrue($result['federation']['outgoing']);
  302. }
  303. public function testFederatedSharingNoOutgoing() {
  304. $map = [
  305. ['files_sharing', 'outgoing_server2server_share_enabled', 'yes', 'no'],
  306. ];
  307. $result = $this->getResults($map);
  308. $this->assertArrayHasKey('federation', $result);
  309. $this->assertFalse($result['federation']['outgoing']);
  310. }
  311. public function testFederatedSharingExpirationDate() {
  312. $result = $this->getResults([]);
  313. $this->assertArrayHasKey('federation', $result);
  314. $this->assertEquals(['enabled' => true], $result['federation']['expire_date']);
  315. $this->assertEquals(['enabled' => true], $result['federation']['expire_date_supported']);
  316. }
  317. }