CapabilitiesTest.php 12 KB

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