CapabilitiesTest.php 12 KB

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