shareitemmodelSpec.js 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025
  1. /**
  2. * ownCloud
  3. *
  4. * @author Vincent Petry
  5. * @copyright 2015 Vincent Petry <pvince81@owncloud.com>
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  9. * License as published by the Free Software Foundation; either
  10. * version 3 of the License, or any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public
  18. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. */
  21. /* global oc_appconfig */
  22. describe('OC.Share.ShareItemModel', function() {
  23. var fetchSharesStub, fetchReshareStub;
  24. var fetchSharesDeferred, fetchReshareDeferred;
  25. var fileInfoModel, configModel, model;
  26. var oldCurrentUser;
  27. var capsSpec;
  28. beforeEach(function() {
  29. oldCurrentUser = OC.currentUser;
  30. fetchSharesDeferred = new $.Deferred();
  31. fetchSharesStub = sinon.stub(OC.Share.ShareItemModel.prototype, '_fetchShares')
  32. .returns(fetchSharesDeferred.promise());
  33. fetchReshareDeferred = new $.Deferred();
  34. fetchReshareStub = sinon.stub(OC.Share.ShareItemModel.prototype, '_fetchReshare')
  35. .returns(fetchReshareDeferred.promise());
  36. fileInfoModel = new OCA.Files.FileInfoModel({
  37. id: 123,
  38. name: 'shared_file_name.txt',
  39. path: '/subdir',
  40. size: 100,
  41. mimetype: 'text/plain',
  42. permissions: 31,
  43. sharePermissions: 31
  44. });
  45. var attributes = {
  46. itemType: fileInfoModel.isDirectory() ? 'folder' : 'file',
  47. itemSource: fileInfoModel.get('id'),
  48. possiblePermissions: fileInfoModel.get('sharePermissions')
  49. };
  50. configModel = new OC.Share.ShareConfigModel();
  51. model = new OC.Share.ShareItemModel(attributes, {
  52. configModel: configModel,
  53. fileInfoModel: fileInfoModel
  54. });
  55. capsSpec = sinon.stub(OC, 'getCapabilities');
  56. capsSpec.returns({
  57. 'files_sharing': {
  58. 'default_permissions': OC.PERMISSION_ALL
  59. }
  60. });
  61. });
  62. afterEach(function() {
  63. capsSpec.restore();
  64. if (fetchSharesStub) {
  65. fetchSharesStub.restore();
  66. }
  67. if (fetchReshareStub) {
  68. fetchReshareStub.restore();
  69. }
  70. OC.currentUser = oldCurrentUser;
  71. });
  72. function makeOcsResponse(data) {
  73. return [{
  74. ocs: {
  75. data: data
  76. }
  77. }];
  78. }
  79. describe('Fetching and parsing', function() {
  80. it('fetches both outgoing shares and the current incoming share', function() {
  81. model.fetch();
  82. expect(fetchSharesStub.calledOnce).toEqual(true);
  83. expect(fetchReshareStub.calledOnce).toEqual(true);
  84. });
  85. it('fetches shares for the current path', function() {
  86. fetchSharesStub.restore();
  87. model._fetchShares();
  88. expect(fakeServer.requests.length).toEqual(1);
  89. expect(fakeServer.requests[0].method).toEqual('GET');
  90. expect(fakeServer.requests[0].url).toEqual(
  91. OC.linkToOCS('apps/files_sharing/api/v1', 2) +
  92. 'shares?format=json&path=%2Fsubdir%2Fshared_file_name.txt&reshares=true'
  93. );
  94. fetchSharesStub = null;
  95. });
  96. it('fetches reshare for the current path', function() {
  97. fetchReshareStub.restore();
  98. model._fetchReshare();
  99. expect(fakeServer.requests.length).toEqual(1);
  100. expect(fakeServer.requests[0].method).toEqual('GET');
  101. expect(fakeServer.requests[0].url).toEqual(
  102. OC.linkToOCS('apps/files_sharing/api/v1', 2) +
  103. 'shares?format=json&path=%2Fsubdir%2Fshared_file_name.txt&shared_with_me=true'
  104. );
  105. fetchReshareStub = null;
  106. });
  107. it('populates attributes with parsed response', function() {
  108. /* jshint camelcase: false */
  109. fetchReshareDeferred.resolve(makeOcsResponse([
  110. {
  111. share_type: OC.Share.SHARE_TYPE_USER,
  112. uid_owner: 'owner',
  113. displayname_owner: 'Owner',
  114. permissions: 31
  115. }
  116. ]));
  117. fetchSharesDeferred.resolve(makeOcsResponse([
  118. {
  119. id: 100,
  120. item_source: 123,
  121. permissions: 31,
  122. share_type: OC.Share.SHARE_TYPE_USER,
  123. share_with: 'user1',
  124. share_with_displayname: 'User One'
  125. }, {
  126. id: 101,
  127. item_source: 123,
  128. permissions: 31,
  129. share_type: OC.Share.SHARE_TYPE_GROUP,
  130. share_with: 'group',
  131. share_with_displayname: 'group'
  132. }, {
  133. id: 102,
  134. item_source: 123,
  135. permissions: 31,
  136. share_type: OC.Share.SHARE_TYPE_REMOTE,
  137. share_with: 'foo@bar.com/baz',
  138. share_with_displayname: 'foo@bar.com/baz'
  139. }, {
  140. displayname_owner: 'root',
  141. expiration: null,
  142. file_source: 123,
  143. file_target: '/folder',
  144. id: 20,
  145. item_source: '123',
  146. item_type: 'folder',
  147. mail_send: '0',
  148. parent: null,
  149. path: '/folder',
  150. permissions: OC.PERMISSION_READ,
  151. share_type: OC.Share.SHARE_TYPE_LINK,
  152. share_with: null,
  153. stime: 1403884258,
  154. storage: 1,
  155. token: 'tehtoken',
  156. uid_owner: 'root',
  157. hide_download: 1,
  158. send_password_by_talk: true
  159. }
  160. ]));
  161. OC.currentUser = 'root';
  162. model.fetch();
  163. var shares = model.get('shares');
  164. expect(shares.length).toEqual(3);
  165. expect(shares[0].id).toEqual(100);
  166. expect(shares[0].permissions).toEqual(31);
  167. expect(shares[0].share_type).toEqual(OC.Share.SHARE_TYPE_USER);
  168. expect(shares[0].share_with).toEqual('user1');
  169. expect(shares[0].share_with_displayname).toEqual('User One');
  170. var linkShares = model.get('linkShares');
  171. expect(linkShares.length).toEqual(1);
  172. var linkShare = linkShares[0];
  173. expect(linkShare.hideDownload).toEqual(true);
  174. expect(linkShare.sendPasswordByTalk).toEqual(true);
  175. // TODO: check more attributes
  176. });
  177. it('groups reshare info into a single item', function() {
  178. /* jshint camelcase: false */
  179. fetchReshareDeferred.resolve(makeOcsResponse([
  180. {
  181. id: '1',
  182. share_type: OC.Share.SHARE_TYPE_USER,
  183. uid_owner: 'owner',
  184. displayname_owner: 'Owner',
  185. share_with: 'root',
  186. share_with_displayname: 'Wurzel',
  187. permissions: 1
  188. },
  189. {
  190. id: '2',
  191. share_type: OC.Share.SHARE_TYPE_GROUP,
  192. uid_owner: 'owner',
  193. displayname_owner: 'Owner',
  194. share_with: 'group1',
  195. permissions: 15
  196. },
  197. {
  198. id: '3',
  199. share_type: OC.Share.SHARE_TYPE_GROUP,
  200. uid_owner: 'owner',
  201. displayname_owner: 'Owner',
  202. share_with: 'group1',
  203. permissions: 17
  204. }
  205. ]));
  206. fetchSharesDeferred.resolve(makeOcsResponse([]));
  207. OC.currentUser = 'root';
  208. model.fetch();
  209. var reshare = model.get('reshare');
  210. // max permissions
  211. expect(reshare.permissions).toEqual(31);
  212. // user share has higher priority
  213. expect(reshare.share_type).toEqual(OC.Share.SHARE_TYPE_USER);
  214. expect(reshare.share_with).toEqual('root');
  215. expect(reshare.share_with_displayname).toEqual('Wurzel');
  216. expect(reshare.id).toEqual('1');
  217. expect(model.getReshareWith()).toEqual('root');
  218. expect(model.getReshareWithDisplayName()).toEqual('Wurzel');
  219. });
  220. it('does not parse link share when for a different file', function() {
  221. /* jshint camelcase: false */
  222. fetchReshareDeferred.resolve(makeOcsResponse([]));
  223. fetchSharesDeferred.resolve(makeOcsResponse([{
  224. displayname_owner: 'root',
  225. expiration: null,
  226. file_source: 456,
  227. file_target: '/folder',
  228. id: 20,
  229. item_source: '456',
  230. item_type: 'folder',
  231. mail_send: '0',
  232. parent: null,
  233. path: '/folder',
  234. permissions: OC.PERMISSION_READ,
  235. share_type: OC.Share.SHARE_TYPE_LINK,
  236. share_with: null,
  237. stime: 1403884258,
  238. storage: 1,
  239. token: 'tehtoken',
  240. uid_owner: 'root'
  241. }]
  242. ));
  243. model.fetch();
  244. var shares = model.get('shares');
  245. // remaining share appears in this list
  246. expect(shares.length).toEqual(1);
  247. var linkShares = model.get('linkShares');
  248. expect(linkShares.length).toEqual(0);
  249. });
  250. it('parses correct link share when a nested link share exists along with parent one', function() {
  251. /* jshint camelcase: false */
  252. fetchReshareDeferred.resolve(makeOcsResponse([]));
  253. fetchSharesDeferred.resolve(makeOcsResponse([{
  254. displayname_owner: 'root',
  255. expiration: '2015-10-12 00:00:00',
  256. file_source: 123,
  257. file_target: '/folder',
  258. id: 20,
  259. item_source: '123',
  260. item_type: 'file',
  261. mail_send: '0',
  262. parent: null,
  263. path: '/folder',
  264. permissions: OC.PERMISSION_READ,
  265. share_type: OC.Share.SHARE_TYPE_LINK,
  266. share_with: null,
  267. stime: 1403884258,
  268. storage: 1,
  269. token: 'tehtoken',
  270. uid_owner: 'root',
  271. hide_download: 0,
  272. send_password_by_talk: false
  273. }, {
  274. displayname_owner: 'root',
  275. expiration: '2015-10-15 00:00:00',
  276. file_source: 456,
  277. file_target: '/file_in_folder.txt',
  278. id: 21,
  279. item_source: '456',
  280. item_type: 'file',
  281. mail_send: '0',
  282. parent: null,
  283. path: '/folder/file_in_folder.txt',
  284. permissions: OC.PERMISSION_READ,
  285. share_type: OC.Share.SHARE_TYPE_LINK,
  286. share_with: null,
  287. stime: 1403884509,
  288. storage: 1,
  289. token: 'anothertoken',
  290. uid_owner: 'root',
  291. hide_download: 1,
  292. send_password_by_talk: true
  293. }]
  294. ));
  295. OC.currentUser = 'root';
  296. model.fetch();
  297. var shares = model.get('shares');
  298. // the parent share remains in the list
  299. expect(shares.length).toEqual(1);
  300. var linkShares = model.get('linkShares');
  301. expect(linkShares.length).toEqual(1);
  302. var linkShare = linkShares[0];
  303. expect(linkShare.token).toEqual('tehtoken');
  304. expect(linkShare.hideDownload).toEqual(false);
  305. expect(linkShare.sendPasswordByTalk).toEqual(false);
  306. // TODO: check child too
  307. });
  308. it('reduces reshare permissions to the ones from the original share', function() {
  309. /* jshint camelcase: false */
  310. fetchReshareDeferred.resolve(makeOcsResponse([{
  311. id: 123,
  312. permissions: OC.PERMISSION_READ,
  313. uid_owner: 'user1'
  314. }]));
  315. fetchSharesDeferred.resolve(makeOcsResponse([]));
  316. model.fetch();
  317. // no resharing allowed
  318. expect(model.get('permissions')).toEqual(OC.PERMISSION_READ);
  319. });
  320. it('reduces reshare permissions to possible permissions', function() {
  321. /* jshint camelcase: false */
  322. fetchReshareDeferred.resolve(makeOcsResponse([{
  323. id: 123,
  324. permissions: OC.PERMISSION_ALL,
  325. uid_owner: 'user1'
  326. }]));
  327. fetchSharesDeferred.resolve(makeOcsResponse([]));
  328. model.fileInfoModel.set('permissions', OC.PERMISSION_READ);
  329. model.fetch();
  330. // no resharing allowed
  331. expect(model.get('permissions')).toEqual(OC.PERMISSION_READ);
  332. });
  333. it('allows owner to share their own share when they are also the recipient', function() {
  334. OC.currentUser = 'user1';
  335. fetchReshareDeferred.resolve(makeOcsResponse([]));
  336. fetchSharesDeferred.resolve(makeOcsResponse([]));
  337. model.fetch();
  338. // sharing still allowed
  339. expect(model.get('permissions') & OC.PERMISSION_SHARE).toEqual(OC.PERMISSION_SHARE);
  340. });
  341. it('properly parses integer values when the server is in the mood of returning ints as string', function() {
  342. /* jshint camelcase: false */
  343. fetchReshareDeferred.resolve(makeOcsResponse([]));
  344. fetchSharesDeferred.resolve(makeOcsResponse([{
  345. displayname_owner: 'root',
  346. expiration: '2015-10-12 00:00:00',
  347. file_source: '123',
  348. file_target: '/folder',
  349. id: '20',
  350. item_source: '123',
  351. item_type: 'file',
  352. mail_send: '0',
  353. parent: '999',
  354. path: '/folder',
  355. permissions: '' + OC.PERMISSION_READ,
  356. share_type: '' + OC.Share.SHARE_TYPE_USER,
  357. share_with: 'user1',
  358. stime: '1403884258',
  359. storage: '1',
  360. token: 'tehtoken',
  361. uid_owner: 'root'
  362. }]
  363. ));
  364. model.fetch();
  365. var shares = model.get('shares');
  366. expect(shares.length).toEqual(1);
  367. var share = shares[0];
  368. expect(share.id).toEqual(20);
  369. expect(share.file_source).toEqual(123);
  370. expect(share.file_target).toEqual('/folder');
  371. expect(share.item_source).toEqual(123);
  372. expect(share.item_type).toEqual('file');
  373. expect(share.displayname_owner).toEqual('root');
  374. expect(share.mail_send).toEqual(0);
  375. expect(share.parent).toEqual(999);
  376. expect(share.path).toEqual('/folder');
  377. expect(share.permissions).toEqual(OC.PERMISSION_READ);
  378. expect(share.share_type).toEqual(OC.Share.SHARE_TYPE_USER);
  379. expect(share.share_with).toEqual('user1');
  380. expect(share.stime).toEqual(1403884258);
  381. expect(share.expiration).toEqual('2015-10-12 00:00:00');
  382. });
  383. });
  384. describe('hasUserShares', function() {
  385. it('returns false when no user shares exist', function() {
  386. fetchReshareDeferred.resolve(makeOcsResponse([]));
  387. fetchSharesDeferred.resolve(makeOcsResponse([]));
  388. model.fetch();
  389. expect(model.hasUserShares()).toEqual(false);
  390. });
  391. it('returns true when user shares exist on the current item', function() {
  392. /* jshint camelcase: false */
  393. fetchReshareDeferred.resolve(makeOcsResponse([]));
  394. fetchSharesDeferred.resolve(makeOcsResponse([{
  395. id: 1,
  396. share_type: OC.Share.SHARE_TYPE_USER,
  397. share_with: 'user1',
  398. item_source: '123'
  399. }]));
  400. model.fetch();
  401. expect(model.hasUserShares()).toEqual(true);
  402. });
  403. it('returns true when group shares exist on the current item', function() {
  404. /* jshint camelcase: false */
  405. fetchReshareDeferred.resolve(makeOcsResponse([]));
  406. fetchSharesDeferred.resolve(makeOcsResponse([{
  407. id: 1,
  408. share_type: OC.Share.SHARE_TYPE_GROUP,
  409. share_with: 'group1',
  410. item_source: '123'
  411. }]));
  412. model.fetch();
  413. expect(model.hasUserShares()).toEqual(true);
  414. });
  415. it('returns false when share exist on parent item', function() {
  416. /* jshint camelcase: false */
  417. fetchReshareDeferred.resolve(makeOcsResponse([]));
  418. fetchSharesDeferred.resolve(makeOcsResponse([{
  419. id: 1,
  420. share_type: OC.Share.SHARE_TYPE_GROUP,
  421. share_with: 'group1',
  422. item_source: '111'
  423. }]));
  424. model.fetch();
  425. expect(model.hasUserShares()).toEqual(false);
  426. });
  427. });
  428. describe('Util', function() {
  429. it('parseTime should properly parse strings', function() {
  430. _.each([
  431. [ '123456', 123456],
  432. [ 123456 , 123456],
  433. ['0123456', 123456],
  434. ['abcdefg', null],
  435. ['0x12345', null],
  436. [ '', null],
  437. ], function(value) {
  438. expect(OC.Share.ShareItemModel.prototype._parseTime(value[0])).toEqual(value[1]);
  439. });
  440. });
  441. });
  442. describe('share permissions', function() {
  443. beforeEach(function() {
  444. oc_appconfig.core.resharingAllowed = true;
  445. });
  446. /**
  447. * Tests sharing with the given possible permissions
  448. *
  449. * @param {int} possiblePermissions
  450. * @return {int} permissions sent to the server
  451. */
  452. function testWithPermissions(possiblePermissions) {
  453. model.set({
  454. permissions: possiblePermissions,
  455. possiblePermissions: possiblePermissions
  456. });
  457. model.addShare({
  458. shareType: OC.Share.SHARE_TYPE_USER,
  459. shareWith: 'user2'
  460. });
  461. var requestBody = OC.parseQueryString(_.last(fakeServer.requests).requestBody);
  462. return parseInt(requestBody.permissions, 10);
  463. }
  464. describe('regular sharing', function() {
  465. it('shares with given permissions with default config', function() {
  466. configModel.set('isResharingAllowed', true);
  467. model.set({
  468. reshare: {},
  469. shares: []
  470. });
  471. expect(
  472. testWithPermissions(OC.PERMISSION_READ | OC.PERMISSION_UPDATE | OC.PERMISSION_SHARE)
  473. ).toEqual(OC.PERMISSION_READ | OC.PERMISSION_UPDATE | OC.PERMISSION_SHARE);
  474. expect(
  475. testWithPermissions(OC.PERMISSION_READ | OC.PERMISSION_SHARE)
  476. ).toEqual(OC.PERMISSION_READ | OC.PERMISSION_SHARE);
  477. });
  478. it('removes share permission when not allowed', function() {
  479. configModel.set('isResharingAllowed', false);
  480. model.set({
  481. reshare: {},
  482. shares: []
  483. });
  484. expect(
  485. testWithPermissions(OC.PERMISSION_READ | OC.PERMISSION_UPDATE | OC.PERMISSION_SHARE)
  486. ).toEqual(OC.PERMISSION_READ | OC.PERMISSION_UPDATE);
  487. });
  488. it('automatically adds READ permission even when not specified', function() {
  489. configModel.set('isResharingAllowed', false);
  490. model.set({
  491. reshare: {},
  492. shares: []
  493. });
  494. expect(
  495. testWithPermissions(OC.PERMISSION_UPDATE | OC.PERMISSION_SHARE)
  496. ).toEqual(OC.PERMISSION_READ | OC.PERMISSION_UPDATE);
  497. });
  498. it('uses default permissions from capabilities', function() {
  499. capsSpec.returns({
  500. 'files_sharing': {
  501. 'default_permissions': OC.PERMISSION_READ | OC.PERMISSION_CREATE | OC.PERMISSION_SHARE
  502. }
  503. });
  504. configModel.set('isResharingAllowed', true);
  505. model.set({
  506. reshare: {},
  507. shares: []
  508. });
  509. expect(
  510. testWithPermissions(OC.PERMISSION_ALL)
  511. ).toEqual(OC.PERMISSION_READ | OC.PERMISSION_CREATE | OC.PERMISSION_SHARE);
  512. });
  513. });
  514. });
  515. describe('saveLinkShare', function() {
  516. var addShareStub;
  517. var updateShareStub;
  518. beforeEach(function() {
  519. addShareStub = sinon.stub(model, 'addShare');
  520. updateShareStub = sinon.stub(model, 'updateShare');
  521. });
  522. afterEach(function() {
  523. addShareStub.restore();
  524. updateShareStub.restore();
  525. });
  526. it('creates a new share if no link share exists', function() {
  527. model.set({
  528. linkShares: [
  529. ]
  530. });
  531. model.saveLinkShare();
  532. expect(addShareStub.calledOnce).toEqual(true);
  533. expect(addShareStub.firstCall.args[0]).toEqual({
  534. hideDownload: false,
  535. password: '',
  536. passwordChanged: false,
  537. sendPasswordByTalk: false,
  538. permissions: OC.PERMISSION_READ,
  539. expireDate: '',
  540. shareType: OC.Share.SHARE_TYPE_LINK
  541. });
  542. expect(updateShareStub.notCalled).toEqual(true);
  543. });
  544. it('creates a new share with default expiration date', function() {
  545. var clock = sinon.useFakeTimers(Date.UTC(2015, 6, 17, 1, 2, 0, 3));
  546. configModel.set({
  547. isDefaultExpireDateEnabled: true,
  548. defaultExpireDate: 7
  549. });
  550. model.set({
  551. linkShares: [
  552. ]
  553. });
  554. model.saveLinkShare();
  555. expect(addShareStub.calledOnce).toEqual(true);
  556. expect(addShareStub.firstCall.args[0]).toEqual({
  557. hideDownload: false,
  558. password: '',
  559. passwordChanged: false,
  560. sendPasswordByTalk: false,
  561. permissions: OC.PERMISSION_READ,
  562. expireDate: '2015-07-24 00:00:00',
  563. shareType: OC.Share.SHARE_TYPE_LINK
  564. });
  565. expect(updateShareStub.notCalled).toEqual(true);
  566. clock.restore();
  567. });
  568. it('updates link share if it exists', function() {
  569. model.set({
  570. linkShares: [{
  571. id: 123
  572. }]
  573. });
  574. model.saveLinkShare({
  575. cid: 123,
  576. password: 'test'
  577. });
  578. expect(addShareStub.notCalled).toEqual(true);
  579. expect(updateShareStub.calledOnce).toEqual(true);
  580. expect(updateShareStub.firstCall.args[0]).toEqual(123);
  581. expect(updateShareStub.firstCall.args[1]).toEqual({
  582. cid: 123,
  583. password: 'test'
  584. });
  585. });
  586. it('forwards error message on add', function() {
  587. var errorStub = sinon.stub();
  588. model.set({
  589. linkShares: [
  590. ]
  591. }, {
  592. });
  593. model.saveLinkShare({
  594. }, {
  595. error: errorStub
  596. });
  597. addShareStub.yieldTo('error', 'Some error message');
  598. expect(errorStub.calledOnce).toEqual(true);
  599. expect(errorStub.lastCall.args[0]).toEqual('Some error message');
  600. });
  601. it('forwards error message on update', function() {
  602. var errorStub = sinon.stub();
  603. model.set({
  604. linkShares: [{
  605. id: 123
  606. }]
  607. }, {
  608. });
  609. model.saveLinkShare({
  610. cid: 123,
  611. password: 'test'
  612. }, {
  613. error: errorStub
  614. });
  615. updateShareStub.yieldTo('error', 'Some error message');
  616. expect(errorStub.calledOnce).toEqual(true);
  617. expect(errorStub.lastCall.args[0]).toEqual('Some error message');
  618. });
  619. });
  620. describe('creating shares', function() {
  621. it('sends POST method to endpoint with passed values', function() {
  622. model.addShare({
  623. shareType: OC.Share.SHARE_TYPE_GROUP,
  624. shareWith: 'group1'
  625. });
  626. expect(fakeServer.requests.length).toEqual(1);
  627. expect(fakeServer.requests[0].method).toEqual('POST');
  628. expect(fakeServer.requests[0].url).toEqual(
  629. OC.linkToOCS('apps/files_sharing/api/v1', 2) +
  630. 'shares?format=json'
  631. );
  632. expect(OC.parseQueryString(fakeServer.requests[0].requestBody)).toEqual({
  633. path: '/subdir/shared_file_name.txt',
  634. permissions: '' + OC.PERMISSION_READ,
  635. shareType: '' + OC.Share.SHARE_TYPE_GROUP,
  636. shareWith: 'group1'
  637. });
  638. });
  639. it('calls complete handler before refreshing the model', function() {
  640. var completeStub = sinon.stub();
  641. model.addShare({
  642. shareType: OC.Share.SHARE_TYPE_GROUP,
  643. shareWith: 'group1'
  644. }, {
  645. complete: completeStub
  646. });
  647. expect(fakeServer.requests.length).toEqual(1);
  648. fakeServer.requests[0].respond(
  649. 200,
  650. { 'Content-Type': 'application/json' },
  651. JSON.stringify({ })
  652. );
  653. expect(completeStub.calledOnce).toEqual(true);
  654. expect(completeStub.lastCall.args[0]).toEqual(model);
  655. fetchReshareDeferred.resolve(makeOcsResponse([]));
  656. fetchSharesDeferred.resolve(makeOcsResponse([]));
  657. expect(completeStub.calledOnce).toEqual(true);
  658. });
  659. it('calls success handler after refreshing the model', function() {
  660. var successStub = sinon.stub();
  661. model.addShare({
  662. shareType: OC.Share.SHARE_TYPE_GROUP,
  663. shareWith: 'group1'
  664. }, {
  665. success: successStub
  666. });
  667. expect(fakeServer.requests.length).toEqual(1);
  668. fakeServer.requests[0].respond(
  669. 200,
  670. { 'Content-Type': 'application/json' },
  671. JSON.stringify({ })
  672. );
  673. expect(successStub.called).toEqual(false);
  674. fetchReshareDeferred.resolve(makeOcsResponse([]));
  675. fetchSharesDeferred.resolve(makeOcsResponse([]));
  676. expect(successStub.calledOnce).toEqual(true);
  677. expect(successStub.lastCall.args[0]).toEqual(model);
  678. });
  679. it('calls complete handler before error handler', function() {
  680. var completeStub = sinon.stub();
  681. var errorStub = sinon.stub();
  682. model.addShare({
  683. shareType: OC.Share.SHARE_TYPE_GROUP,
  684. shareWith: 'group1'
  685. }, {
  686. complete: completeStub,
  687. error: errorStub
  688. });
  689. expect(fakeServer.requests.length).toEqual(1);
  690. fakeServer.requests[0].respond(
  691. 400,
  692. { 'Content-Type': 'application/json' },
  693. JSON.stringify({
  694. ocs: {
  695. meta: {
  696. message: 'Some error message'
  697. }
  698. }
  699. })
  700. );
  701. expect(completeStub.calledOnce).toEqual(true);
  702. expect(completeStub.lastCall.args[0]).toEqual(model);
  703. expect(errorStub.calledOnce).toEqual(true);
  704. expect(completeStub.calledBefore(errorStub)).toEqual(true);
  705. });
  706. it('calls error handler with error message', function() {
  707. var errorStub = sinon.stub();
  708. model.addShare({
  709. shareType: OC.Share.SHARE_TYPE_GROUP,
  710. shareWith: 'group1'
  711. }, {
  712. error: errorStub
  713. });
  714. expect(fakeServer.requests.length).toEqual(1);
  715. fakeServer.requests[0].respond(
  716. 400,
  717. { 'Content-Type': 'application/json' },
  718. JSON.stringify({
  719. ocs: {
  720. meta: {
  721. message: 'Some error message'
  722. }
  723. }
  724. })
  725. );
  726. expect(errorStub.calledOnce).toEqual(true);
  727. expect(errorStub.lastCall.args[0]).toEqual(model);
  728. expect(errorStub.lastCall.args[1]).toEqual('Some error message');
  729. });
  730. });
  731. describe('updating shares', function() {
  732. it('sends PUT method to endpoint with passed values', function() {
  733. model.updateShare(123, {
  734. permissions: OC.PERMISSION_READ | OC.PERMISSION_SHARE
  735. });
  736. expect(fakeServer.requests.length).toEqual(1);
  737. expect(fakeServer.requests[0].method).toEqual('PUT');
  738. expect(fakeServer.requests[0].url).toEqual(
  739. OC.linkToOCS('apps/files_sharing/api/v1', 2) +
  740. 'shares/123?format=json'
  741. );
  742. expect(OC.parseQueryString(fakeServer.requests[0].requestBody)).toEqual({
  743. permissions: '' + (OC.PERMISSION_READ | OC.PERMISSION_SHARE)
  744. });
  745. });
  746. it('calls complete handler before refreshing the model', function() {
  747. var completeStub = sinon.stub();
  748. model.updateShare(123, {
  749. permissions: OC.PERMISSION_READ | OC.PERMISSION_SHARE
  750. }, {
  751. complete: completeStub
  752. });
  753. expect(fakeServer.requests.length).toEqual(1);
  754. fakeServer.requests[0].respond(
  755. 200,
  756. { 'Content-Type': 'application/json' },
  757. JSON.stringify({ })
  758. );
  759. expect(completeStub.calledOnce).toEqual(true);
  760. expect(completeStub.lastCall.args[0]).toEqual(model);
  761. fetchReshareDeferred.resolve(makeOcsResponse([]));
  762. fetchSharesDeferred.resolve(makeOcsResponse([]));
  763. expect(completeStub.calledOnce).toEqual(true);
  764. });
  765. it('calls success handler after refreshing the model', function() {
  766. var successStub = sinon.stub();
  767. model.updateShare(123, {
  768. permissions: OC.PERMISSION_READ | OC.PERMISSION_SHARE
  769. }, {
  770. success: successStub
  771. });
  772. expect(fakeServer.requests.length).toEqual(1);
  773. fakeServer.requests[0].respond(
  774. 200,
  775. { 'Content-Type': 'application/json' },
  776. JSON.stringify({ })
  777. );
  778. expect(successStub.called).toEqual(false);
  779. fetchReshareDeferred.resolve(makeOcsResponse([]));
  780. fetchSharesDeferred.resolve(makeOcsResponse([]));
  781. expect(successStub.calledOnce).toEqual(true);
  782. expect(successStub.lastCall.args[0]).toEqual(model);
  783. });
  784. it('calls complete handler before error handler', function() {
  785. var completeStub = sinon.stub();
  786. var errorStub = sinon.stub();
  787. model.updateShare(123, {
  788. permissions: OC.PERMISSION_READ | OC.PERMISSION_SHARE
  789. }, {
  790. complete: completeStub,
  791. error: errorStub
  792. });
  793. expect(fakeServer.requests.length).toEqual(1);
  794. fakeServer.requests[0].respond(
  795. 400,
  796. { 'Content-Type': 'application/json' },
  797. JSON.stringify({
  798. ocs: {
  799. meta: {
  800. message: 'Some error message'
  801. }
  802. }
  803. })
  804. );
  805. expect(completeStub.calledOnce).toEqual(true);
  806. expect(completeStub.lastCall.args[0]).toEqual(model);
  807. expect(errorStub.calledOnce).toEqual(true);
  808. expect(completeStub.calledBefore(errorStub)).toEqual(true);
  809. });
  810. it('calls error handler with error message', function() {
  811. var errorStub = sinon.stub();
  812. model.updateShare(123, {
  813. permissions: OC.PERMISSION_READ | OC.PERMISSION_SHARE
  814. }, {
  815. error: errorStub
  816. });
  817. expect(fakeServer.requests.length).toEqual(1);
  818. fakeServer.requests[0].respond(
  819. 400,
  820. { 'Content-Type': 'application/json' },
  821. JSON.stringify({
  822. ocs: {
  823. meta: {
  824. message: 'Some error message'
  825. }
  826. }
  827. })
  828. );
  829. expect(errorStub.calledOnce).toEqual(true);
  830. expect(errorStub.lastCall.args[0]).toEqual(model);
  831. expect(errorStub.lastCall.args[1]).toEqual('Some error message');
  832. });
  833. });
  834. describe('removing shares', function() {
  835. it('sends DELETE method to endpoint with share id', function() {
  836. model.removeShare(123);
  837. expect(fakeServer.requests.length).toEqual(1);
  838. expect(fakeServer.requests[0].method).toEqual('DELETE');
  839. expect(fakeServer.requests[0].url).toEqual(
  840. OC.linkToOCS('apps/files_sharing/api/v1', 2) +
  841. 'shares/123?format=json'
  842. );
  843. });
  844. it('calls error handler with error message', function() {
  845. var errorStub = sinon.stub();
  846. model.removeShare(123, {
  847. error: errorStub
  848. });
  849. expect(fakeServer.requests.length).toEqual(1);
  850. fakeServer.requests[0].respond(
  851. 400,
  852. { 'Content-Type': 'application/json' },
  853. JSON.stringify({
  854. ocs: {
  855. meta: {
  856. message: 'Some error message'
  857. }
  858. }
  859. })
  860. );
  861. expect(errorStub.calledOnce).toEqual(true);
  862. expect(errorStub.lastCall.args[1]).toEqual('Some error message');
  863. });
  864. });
  865. describe('getShareTypes', function() {
  866. var dataProvider = [
  867. [
  868. ],
  869. [
  870. OC.Share.SHARE_TYPE_USER,
  871. OC.Share.SHARE_TYPE_USER,
  872. ],
  873. [
  874. OC.Share.SHARE_TYPE_USER,
  875. OC.Share.SHARE_TYPE_GROUP,
  876. OC.Share.SHARE_TYPE_LINK,
  877. OC.Share.SHARE_TYPE_REMOTE
  878. ],
  879. [
  880. OC.Share.SHARE_TYPE_USER,
  881. OC.Share.SHARE_TYPE_GROUP,
  882. OC.Share.SHARE_TYPE_GROUP,
  883. OC.Share.SHARE_TYPE_LINK,
  884. OC.Share.SHARE_TYPE_LINK,
  885. OC.Share.SHARE_TYPE_REMOTE,
  886. OC.Share.SHARE_TYPE_REMOTE,
  887. OC.Share.SHARE_TYPE_REMOTE
  888. ],
  889. [
  890. OC.Share.SHARE_TYPE_LINK,
  891. OC.Share.SHARE_TYPE_LINK,
  892. OC.Share.SHARE_TYPE_USER
  893. ]
  894. ];
  895. _.each(dataProvider, function testCase(shareTypes, i) {
  896. it('returns set of share types for case ' + i, function() {
  897. /* jshint camelcase: false */
  898. fetchReshareDeferred.resolve(makeOcsResponse([]));
  899. var id = 100;
  900. var shares = _.map(shareTypes, function(shareType) {
  901. return {
  902. id: id++,
  903. item_source: 123,
  904. permissions: 31,
  905. share_type: shareType,
  906. uid_owner: 'root'
  907. };
  908. });
  909. var expectedResult = _.uniq(shareTypes).sort();
  910. fetchSharesDeferred.resolve(makeOcsResponse(shares));
  911. OC.currentUser = 'root';
  912. model.fetch();
  913. expect(model.getShareTypes().sort()).toEqual(expectedResult);
  914. });
  915. });
  916. });
  917. });