CapabilitiesTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\Files_Sharing\Tests;
  8. use OC\KnownUser\KnownUserService;
  9. use OC\Share20\Manager;
  10. use OC\Share20\ShareDisableChecker;
  11. use OCA\Files_Sharing\Capabilities;
  12. use OCP\EventDispatcher\IEventDispatcher;
  13. use OCP\Files\IRootFolder;
  14. use OCP\Files\Mount\IMountManager;
  15. use OCP\IConfig;
  16. use OCP\IDateTimeZone;
  17. use OCP\IGroupManager;
  18. use OCP\IURLGenerator;
  19. use OCP\IUserManager;
  20. use OCP\IUserSession;
  21. use OCP\L10N\IFactory;
  22. use OCP\Mail\IMailer;
  23. use OCP\Security\IHasher;
  24. use OCP\Security\ISecureRandom;
  25. use OCP\Share\IProviderFactory;
  26. use Psr\Log\LoggerInterface;
  27. /**
  28. * Class CapabilitiesTest
  29. *
  30. * @group DB
  31. */
  32. class CapabilitiesTest extends \Test\TestCase {
  33. /**
  34. * Test for the general part in each return statement and assert.
  35. * Strip of the general part on the way.
  36. *
  37. * @param string[] $data Capabilities
  38. * @return string[]
  39. */
  40. private function getFilesSharingPart(array $data) {
  41. $this->assertArrayHasKey('files_sharing', $data);
  42. return $data['files_sharing'];
  43. }
  44. /**
  45. * Create a mock config object and insert the values in $map to the getAppValue
  46. * function. Then obtain the capabilities and extract the first few
  47. * levels in the array
  48. *
  49. * @param (string[])[] $map Map of arguments to return types for the getAppValue function in the mock
  50. * @return string[]
  51. */
  52. private function getResults(array $map) {
  53. $config = $this->getMockBuilder(IConfig::class)->disableOriginalConstructor()->getMock();
  54. $config->method('getAppValue')->willReturnMap($map);
  55. $shareManager = new Manager(
  56. $this->createMock(LoggerInterface::class),
  57. $config,
  58. $this->createMock(ISecureRandom::class),
  59. $this->createMock(IHasher::class),
  60. $this->createMock(IMountManager::class),
  61. $this->createMock(IGroupManager::class),
  62. $this->createMock(IFactory::class),
  63. $this->createMock(IProviderFactory::class),
  64. $this->createMock(IUserManager::class),
  65. $this->createMock(IRootFolder::class),
  66. $this->createMock(IMailer::class),
  67. $this->createMock(IURLGenerator::class),
  68. $this->createMock(\OC_Defaults::class),
  69. $this->createMock(IEventDispatcher::class),
  70. $this->createMock(IUserSession::class),
  71. $this->createMock(KnownUserService::class),
  72. $this->createMock(ShareDisableChecker::class),
  73. $this->createMock(IDateTimeZone::class),
  74. );
  75. $cap = new Capabilities($config, $shareManager);
  76. $result = $this->getFilesSharingPart($cap->getCapabilities());
  77. return $result;
  78. }
  79. public function testEnabledSharingAPI() {
  80. $map = [
  81. ['core', 'shareapi_enabled', 'yes', 'yes'],
  82. ];
  83. $result = $this->getResults($map);
  84. $this->assertTrue($result['api_enabled']);
  85. $this->assertArrayHasKey('public', $result);
  86. $this->assertArrayHasKey('user', $result);
  87. $this->assertArrayHasKey('resharing', $result);
  88. }
  89. public function testDisabledSharingAPI() {
  90. $map = [
  91. ['core', 'shareapi_enabled', 'yes', 'no'],
  92. ];
  93. $result = $this->getResults($map);
  94. $this->assertFalse($result['api_enabled']);
  95. $this->assertFalse($result['public']['enabled']);
  96. $this->assertFalse($result['user']['send_mail']);
  97. $this->assertFalse($result['resharing']);
  98. }
  99. public function testNoLinkSharing() {
  100. $map = [
  101. ['core', 'shareapi_enabled', 'yes', 'yes'],
  102. ['core', 'shareapi_allow_links', 'yes', 'no'],
  103. ];
  104. $result = $this->getResults($map);
  105. $this->assertIsArray($result['public']);
  106. $this->assertFalse($result['public']['enabled']);
  107. }
  108. public function testOnlyLinkSharing() {
  109. $map = [
  110. ['core', 'shareapi_enabled', 'yes', 'yes'],
  111. ['core', 'shareapi_allow_links', 'yes', 'yes'],
  112. ['core', 'shareapi_enforce_links_password_excluded_groups', '', ''],
  113. ];
  114. $result = $this->getResults($map);
  115. $this->assertIsArray($result['public']);
  116. $this->assertTrue($result['public']['enabled']);
  117. }
  118. public function testLinkPassword() {
  119. $map = [
  120. ['core', 'shareapi_enabled', 'yes', 'yes'],
  121. ['core', 'shareapi_allow_links', 'yes', 'yes'],
  122. ['core', 'shareapi_enforce_links_password_excluded_groups', '', ''],
  123. ['core', 'shareapi_enforce_links_password', 'no', 'yes'],
  124. ];
  125. $result = $this->getResults($map);
  126. $this->assertArrayHasKey('password', $result['public']);
  127. $this->assertArrayHasKey('enforced', $result['public']['password']);
  128. $this->assertTrue($result['public']['password']['enforced']);
  129. }
  130. public function testLinkNoPassword() {
  131. $map = [
  132. ['core', 'shareapi_enabled', 'yes', 'yes'],
  133. ['core', 'shareapi_allow_links', 'yes', 'yes'],
  134. ['core', 'shareapi_enforce_links_password_excluded_groups', '', ''],
  135. ['core', 'shareapi_enforce_links_password', 'no', 'no'],
  136. ];
  137. $result = $this->getResults($map);
  138. $this->assertArrayHasKey('password', $result['public']);
  139. $this->assertArrayHasKey('enforced', $result['public']['password']);
  140. $this->assertFalse($result['public']['password']['enforced']);
  141. }
  142. public function testLinkNoExpireDate() {
  143. $map = [
  144. ['core', 'shareapi_enabled', 'yes', 'yes'],
  145. ['core', 'shareapi_allow_links', 'yes', 'yes'],
  146. ['core', 'shareapi_default_expire_date', 'no', 'no'],
  147. ['core', 'shareapi_enforce_links_password_excluded_groups', '', ''],
  148. ];
  149. $result = $this->getResults($map);
  150. $this->assertArrayHasKey('expire_date', $result['public']);
  151. $this->assertIsArray($result['public']['expire_date']);
  152. $this->assertFalse($result['public']['expire_date']['enabled']);
  153. }
  154. public function testLinkExpireDate() {
  155. $map = [
  156. ['core', 'shareapi_enabled', 'yes', 'yes'],
  157. ['core', 'shareapi_allow_links', 'yes', 'yes'],
  158. ['core', 'shareapi_default_expire_date', 'no', 'yes'],
  159. ['core', 'shareapi_expire_after_n_days', '7', '7'],
  160. ['core', 'shareapi_enforce_expire_date', 'no', 'no'],
  161. ['core', 'shareapi_enforce_links_password_excluded_groups', '', ''],
  162. ];
  163. $result = $this->getResults($map);
  164. $this->assertArrayHasKey('expire_date', $result['public']);
  165. $this->assertIsArray($result['public']['expire_date']);
  166. $this->assertTrue($result['public']['expire_date']['enabled']);
  167. $this->assertArrayHasKey('days', $result['public']['expire_date']);
  168. $this->assertFalse($result['public']['expire_date']['enforced']);
  169. }
  170. public function testLinkExpireDateEnforced() {
  171. $map = [
  172. ['core', 'shareapi_enabled', 'yes', 'yes'],
  173. ['core', 'shareapi_allow_links', 'yes', 'yes'],
  174. ['core', 'shareapi_default_expire_date', 'no', 'yes'],
  175. ['core', 'shareapi_enforce_expire_date', 'no', 'yes'],
  176. ['core', 'shareapi_enforce_links_password_excluded_groups', '', ''],
  177. ];
  178. $result = $this->getResults($map);
  179. $this->assertArrayHasKey('expire_date', $result['public']);
  180. $this->assertIsArray($result['public']['expire_date']);
  181. $this->assertTrue($result['public']['expire_date']['enforced']);
  182. }
  183. public function testLinkSendMail() {
  184. $map = [
  185. ['core', 'shareapi_enabled', 'yes', 'yes'],
  186. ['core', 'shareapi_allow_links', 'yes', 'yes'],
  187. ['core', 'shareapi_allow_public_notification', 'no', 'yes'],
  188. ['core', 'shareapi_enforce_links_password_excluded_groups', '', ''],
  189. ];
  190. $result = $this->getResults($map);
  191. $this->assertTrue($result['public']['send_mail']);
  192. }
  193. public function testLinkNoSendMail() {
  194. $map = [
  195. ['core', 'shareapi_enabled', 'yes', 'yes'],
  196. ['core', 'shareapi_allow_links', 'yes', 'yes'],
  197. ['core', 'shareapi_allow_public_notification', 'no', 'no'],
  198. ['core', 'shareapi_enforce_links_password_excluded_groups', '', ''],
  199. ];
  200. $result = $this->getResults($map);
  201. $this->assertFalse($result['public']['send_mail']);
  202. }
  203. public function testResharing() {
  204. $map = [
  205. ['core', 'shareapi_enabled', 'yes', 'yes'],
  206. ['core', 'shareapi_allow_resharing', 'yes', 'yes'],
  207. ['core', 'shareapi_enforce_links_password_excluded_groups', '', ''],
  208. ];
  209. $result = $this->getResults($map);
  210. $this->assertTrue($result['resharing']);
  211. }
  212. public function testNoResharing() {
  213. $map = [
  214. ['core', 'shareapi_enabled', 'yes', 'yes'],
  215. ['core', 'shareapi_allow_resharing', 'yes', 'no'],
  216. ['core', 'shareapi_enforce_links_password_excluded_groups', '', ''],
  217. ];
  218. $result = $this->getResults($map);
  219. $this->assertFalse($result['resharing']);
  220. }
  221. public function testLinkPublicUpload() {
  222. $map = [
  223. ['core', 'shareapi_enabled', 'yes', 'yes'],
  224. ['core', 'shareapi_allow_links', 'yes', 'yes'],
  225. ['core', 'shareapi_allow_public_upload', 'yes', 'yes'],
  226. ['core', 'shareapi_enforce_links_password_excluded_groups', '', ''],
  227. ];
  228. $result = $this->getResults($map);
  229. $this->assertTrue($result['public']['upload']);
  230. $this->assertTrue($result['public']['upload_files_drop']);
  231. }
  232. public function testLinkNoPublicUpload() {
  233. $map = [
  234. ['core', 'shareapi_enabled', 'yes', 'yes'],
  235. ['core', 'shareapi_allow_links', 'yes', 'yes'],
  236. ['core', 'shareapi_allow_public_upload', 'yes', 'no'],
  237. ['core', 'shareapi_enforce_links_password_excluded_groups', '', ''],
  238. ];
  239. $result = $this->getResults($map);
  240. $this->assertFalse($result['public']['upload']);
  241. $this->assertFalse($result['public']['upload_files_drop']);
  242. }
  243. public function testNoGroupSharing() {
  244. $map = [
  245. ['core', 'shareapi_enabled', 'yes', 'yes'],
  246. ['core', 'shareapi_allow_group_sharing', 'yes', 'no'],
  247. ];
  248. $result = $this->getResults($map);
  249. $this->assertFalse($result['group_sharing']);
  250. }
  251. public function testGroupSharing() {
  252. $map = [
  253. ['core', 'shareapi_enabled', 'yes', 'yes'],
  254. ['core', 'shareapi_allow_group_sharing', 'yes', 'yes'],
  255. ];
  256. $result = $this->getResults($map);
  257. $this->assertTrue($result['group_sharing']);
  258. }
  259. public function testFederatedSharingIncoming() {
  260. $map = [
  261. ['files_sharing', 'incoming_server2server_share_enabled', 'yes', 'yes'],
  262. ];
  263. $result = $this->getResults($map);
  264. $this->assertArrayHasKey('federation', $result);
  265. $this->assertTrue($result['federation']['incoming']);
  266. }
  267. public function testFederatedSharingNoIncoming() {
  268. $map = [
  269. ['files_sharing', 'incoming_server2server_share_enabled', 'yes', 'no'],
  270. ];
  271. $result = $this->getResults($map);
  272. $this->assertArrayHasKey('federation', $result);
  273. $this->assertFalse($result['federation']['incoming']);
  274. }
  275. public function testFederatedSharingOutgoing() {
  276. $map = [
  277. ['files_sharing', 'outgoing_server2server_share_enabled', 'yes', 'yes'],
  278. ];
  279. $result = $this->getResults($map);
  280. $this->assertArrayHasKey('federation', $result);
  281. $this->assertTrue($result['federation']['outgoing']);
  282. }
  283. public function testFederatedSharingNoOutgoing() {
  284. $map = [
  285. ['files_sharing', 'outgoing_server2server_share_enabled', 'yes', 'no'],
  286. ];
  287. $result = $this->getResults($map);
  288. $this->assertArrayHasKey('federation', $result);
  289. $this->assertFalse($result['federation']['outgoing']);
  290. }
  291. public function testFederatedSharingExpirationDate() {
  292. $result = $this->getResults([]);
  293. $this->assertArrayHasKey('federation', $result);
  294. $this->assertEquals(['enabled' => true], $result['federation']['expire_date']);
  295. $this->assertEquals(['enabled' => true], $result['federation']['expire_date_supported']);
  296. }
  297. }