CapabilitiesTest.php 12 KB

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