shareitemmodelSpec.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775
  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. beforeEach(function() {
  28. oldCurrentUser = OC.currentUser;
  29. fetchSharesDeferred = new $.Deferred();
  30. fetchSharesStub = sinon.stub(OC.Share.ShareItemModel.prototype, '_fetchShares')
  31. .returns(fetchSharesDeferred.promise());
  32. fetchReshareDeferred = new $.Deferred();
  33. fetchReshareStub = sinon.stub(OC.Share.ShareItemModel.prototype, '_fetchReshare')
  34. .returns(fetchReshareDeferred.promise());
  35. fileInfoModel = new OCA.Files.FileInfoModel({
  36. id: 123,
  37. name: 'shared_file_name.txt',
  38. path: '/subdir',
  39. size: 100,
  40. mimetype: 'text/plain',
  41. permissions: 31,
  42. sharePermissions: 31
  43. });
  44. var attributes = {
  45. itemType: fileInfoModel.isDirectory() ? 'folder' : 'file',
  46. itemSource: fileInfoModel.get('id'),
  47. possiblePermissions: fileInfoModel.get('sharePermissions')
  48. };
  49. configModel = new OC.Share.ShareConfigModel();
  50. model = new OC.Share.ShareItemModel(attributes, {
  51. configModel: configModel,
  52. fileInfoModel: fileInfoModel
  53. });
  54. });
  55. afterEach(function() {
  56. if (fetchSharesStub) {
  57. fetchSharesStub.restore();
  58. }
  59. if (fetchReshareStub) {
  60. fetchReshareStub.restore();
  61. }
  62. OC.currentUser = oldCurrentUser;
  63. });
  64. function makeOcsResponse(data) {
  65. return [{
  66. ocs: {
  67. data: data
  68. }
  69. }];
  70. }
  71. describe('Fetching and parsing', function() {
  72. it('fetches both outgoing shares and the current incoming share', function() {
  73. model.fetch();
  74. expect(fetchSharesStub.calledOnce).toEqual(true);
  75. expect(fetchReshareStub.calledOnce).toEqual(true);
  76. });
  77. it('fetches shares for the current path', function() {
  78. fetchSharesStub.restore();
  79. model._fetchShares();
  80. expect(fakeServer.requests.length).toEqual(1);
  81. expect(fakeServer.requests[0].method).toEqual('GET');
  82. expect(fakeServer.requests[0].url).toEqual(
  83. OC.linkToOCS('apps/files_sharing/api/v1', 2) +
  84. 'shares?format=json&path=%2Fsubdir%2Fshared_file_name.txt&reshares=true'
  85. );
  86. fetchSharesStub = null;
  87. });
  88. it('fetches reshare for the current path', function() {
  89. fetchReshareStub.restore();
  90. model._fetchReshare();
  91. expect(fakeServer.requests.length).toEqual(1);
  92. expect(fakeServer.requests[0].method).toEqual('GET');
  93. expect(fakeServer.requests[0].url).toEqual(
  94. OC.linkToOCS('apps/files_sharing/api/v1', 2) +
  95. 'shares?format=json&path=%2Fsubdir%2Fshared_file_name.txt&shared_with_me=true'
  96. );
  97. fetchReshareStub = null;
  98. });
  99. it('populates attributes with parsed response', function() {
  100. /* jshint camelcase: false */
  101. fetchReshareDeferred.resolve(makeOcsResponse([
  102. {
  103. share_type: OC.Share.SHARE_TYPE_USER,
  104. uid_owner: 'owner',
  105. displayname_owner: 'Owner',
  106. permissions: 31
  107. }
  108. ]));
  109. fetchSharesDeferred.resolve(makeOcsResponse([
  110. {
  111. id: 100,
  112. item_source: 123,
  113. permissions: 31,
  114. share_type: OC.Share.SHARE_TYPE_USER,
  115. share_with: 'user1',
  116. share_with_displayname: 'User One'
  117. }, {
  118. id: 101,
  119. item_source: 123,
  120. permissions: 31,
  121. share_type: OC.Share.SHARE_TYPE_GROUP,
  122. share_with: 'group',
  123. share_with_displayname: 'group'
  124. }, {
  125. id: 102,
  126. item_source: 123,
  127. permissions: 31,
  128. share_type: OC.Share.SHARE_TYPE_REMOTE,
  129. share_with: 'foo@bar.com/baz',
  130. share_with_displayname: 'foo@bar.com/baz'
  131. }, {
  132. displayname_owner: 'root',
  133. expiration: null,
  134. file_source: 123,
  135. file_target: '/folder',
  136. id: 20,
  137. item_source: '123',
  138. item_type: 'folder',
  139. mail_send: '0',
  140. parent: null,
  141. path: '/folder',
  142. permissions: OC.PERMISSION_READ,
  143. share_type: OC.Share.SHARE_TYPE_LINK,
  144. share_with: null,
  145. stime: 1403884258,
  146. storage: 1,
  147. token: 'tehtoken',
  148. uid_owner: 'root'
  149. }
  150. ]));
  151. OC.currentUser = 'root';
  152. model.fetch();
  153. var shares = model.get('shares');
  154. expect(shares.length).toEqual(3);
  155. expect(shares[0].id).toEqual(100);
  156. expect(shares[0].permissions).toEqual(31);
  157. expect(shares[0].share_type).toEqual(OC.Share.SHARE_TYPE_USER);
  158. expect(shares[0].share_with).toEqual('user1');
  159. expect(shares[0].share_with_displayname).toEqual('User One');
  160. var linkShare = model.get('linkShare');
  161. expect(linkShare.isLinkShare).toEqual(true);
  162. // TODO: check more attributes
  163. });
  164. it('groups reshare info into a single item', function() {
  165. /* jshint camelcase: false */
  166. fetchReshareDeferred.resolve(makeOcsResponse([
  167. {
  168. id: '1',
  169. share_type: OC.Share.SHARE_TYPE_USER,
  170. uid_owner: 'owner',
  171. displayname_owner: 'Owner',
  172. share_with: 'root',
  173. share_with_displayname: 'Wurzel',
  174. permissions: 1
  175. },
  176. {
  177. id: '2',
  178. share_type: OC.Share.SHARE_TYPE_GROUP,
  179. uid_owner: 'owner',
  180. displayname_owner: 'Owner',
  181. share_with: 'group1',
  182. permissions: 15
  183. },
  184. {
  185. id: '3',
  186. share_type: OC.Share.SHARE_TYPE_GROUP,
  187. uid_owner: 'owner',
  188. displayname_owner: 'Owner',
  189. share_with: 'group1',
  190. permissions: 17
  191. }
  192. ]));
  193. fetchSharesDeferred.resolve(makeOcsResponse([]));
  194. OC.currentUser = 'root';
  195. model.fetch();
  196. var reshare = model.get('reshare');
  197. // max permissions
  198. expect(reshare.permissions).toEqual(31);
  199. // user share has higher priority
  200. expect(reshare.share_type).toEqual(OC.Share.SHARE_TYPE_USER);
  201. expect(reshare.share_with).toEqual('root');
  202. expect(reshare.share_with_displayname).toEqual('Wurzel');
  203. expect(reshare.id).toEqual('1');
  204. expect(model.getReshareWith()).toEqual('root');
  205. expect(model.getReshareWithDisplayName()).toEqual('Wurzel');
  206. });
  207. it('does not parse link share when for a different file', function() {
  208. /* jshint camelcase: false */
  209. fetchReshareDeferred.resolve(makeOcsResponse([]));
  210. fetchSharesDeferred.resolve(makeOcsResponse([{
  211. displayname_owner: 'root',
  212. expiration: null,
  213. file_source: 456,
  214. file_target: '/folder',
  215. id: 20,
  216. item_source: '456',
  217. item_type: 'folder',
  218. mail_send: '0',
  219. parent: null,
  220. path: '/folder',
  221. permissions: OC.PERMISSION_READ,
  222. share_type: OC.Share.SHARE_TYPE_LINK,
  223. share_with: null,
  224. stime: 1403884258,
  225. storage: 1,
  226. token: 'tehtoken',
  227. uid_owner: 'root'
  228. }]
  229. ));
  230. model.fetch();
  231. var shares = model.get('shares');
  232. // remaining share appears in this list
  233. expect(shares.length).toEqual(1);
  234. var linkShare = model.get('linkShare');
  235. expect(linkShare.isLinkShare).toEqual(false);
  236. });
  237. it('parses correct link share when a nested link share exists along with parent one', function() {
  238. /* jshint camelcase: false */
  239. fetchReshareDeferred.resolve(makeOcsResponse([]));
  240. fetchSharesDeferred.resolve(makeOcsResponse([{
  241. displayname_owner: 'root',
  242. expiration: '2015-10-12 00:00:00',
  243. file_source: 123,
  244. file_target: '/folder',
  245. id: 20,
  246. item_source: '123',
  247. item_type: 'file',
  248. mail_send: '0',
  249. parent: null,
  250. path: '/folder',
  251. permissions: OC.PERMISSION_READ,
  252. share_type: OC.Share.SHARE_TYPE_LINK,
  253. share_with: null,
  254. stime: 1403884258,
  255. storage: 1,
  256. token: 'tehtoken',
  257. uid_owner: 'root'
  258. }, {
  259. displayname_owner: 'root',
  260. expiration: '2015-10-15 00:00:00',
  261. file_source: 456,
  262. file_target: '/file_in_folder.txt',
  263. id: 21,
  264. item_source: '456',
  265. item_type: 'file',
  266. mail_send: '0',
  267. parent: null,
  268. path: '/folder/file_in_folder.txt',
  269. permissions: OC.PERMISSION_READ,
  270. share_type: OC.Share.SHARE_TYPE_LINK,
  271. share_with: null,
  272. stime: 1403884509,
  273. storage: 1,
  274. token: 'anothertoken',
  275. uid_owner: 'root'
  276. }]
  277. ));
  278. OC.currentUser = 'root';
  279. model.fetch();
  280. var shares = model.get('shares');
  281. // the parent share remains in the list
  282. expect(shares.length).toEqual(1);
  283. var linkShare = model.get('linkShare');
  284. expect(linkShare.isLinkShare).toEqual(true);
  285. expect(linkShare.token).toEqual('tehtoken');
  286. // TODO: check child too
  287. });
  288. it('reduces reshare permissions to the ones from the original share', function() {
  289. /* jshint camelcase: false */
  290. fetchReshareDeferred.resolve(makeOcsResponse([{
  291. id: 123,
  292. permissions: OC.PERMISSION_READ,
  293. uid_owner: 'user1'
  294. }]));
  295. fetchSharesDeferred.resolve(makeOcsResponse([]));
  296. model.fetch();
  297. // no resharing allowed
  298. expect(model.get('permissions')).toEqual(OC.PERMISSION_READ);
  299. });
  300. it('reduces reshare permissions to possible permissions', function() {
  301. /* jshint camelcase: false */
  302. fetchReshareDeferred.resolve(makeOcsResponse([{
  303. id: 123,
  304. permissions: OC.PERMISSION_ALL,
  305. uid_owner: 'user1'
  306. }]));
  307. fetchSharesDeferred.resolve(makeOcsResponse([]));
  308. model.set('possiblePermissions', OC.PERMISSION_READ);
  309. model.fetch();
  310. // no resharing allowed
  311. expect(model.get('permissions')).toEqual(OC.PERMISSION_READ);
  312. });
  313. it('allows owner to share their own share when they are also the recipient', function() {
  314. OC.currentUser = 'user1';
  315. fetchReshareDeferred.resolve(makeOcsResponse([]));
  316. fetchSharesDeferred.resolve(makeOcsResponse([]));
  317. model.fetch();
  318. // sharing still allowed
  319. expect(model.get('permissions') & OC.PERMISSION_SHARE).toEqual(OC.PERMISSION_SHARE);
  320. });
  321. it('properly parses integer values when the server is in the mood of returning ints as string', function() {
  322. /* jshint camelcase: false */
  323. fetchReshareDeferred.resolve(makeOcsResponse([]));
  324. fetchSharesDeferred.resolve(makeOcsResponse([{
  325. displayname_owner: 'root',
  326. expiration: '2015-10-12 00:00:00',
  327. file_source: '123',
  328. file_target: '/folder',
  329. id: '20',
  330. item_source: '123',
  331. item_type: 'file',
  332. mail_send: '0',
  333. parent: '999',
  334. path: '/folder',
  335. permissions: '' + OC.PERMISSION_READ,
  336. share_type: '' + OC.Share.SHARE_TYPE_USER,
  337. share_with: 'user1',
  338. stime: '1403884258',
  339. storage: '1',
  340. token: 'tehtoken',
  341. uid_owner: 'root'
  342. }]
  343. ));
  344. model.fetch();
  345. var shares = model.get('shares');
  346. expect(shares.length).toEqual(1);
  347. var share = shares[0];
  348. expect(share.id).toEqual(20);
  349. expect(share.file_source).toEqual(123);
  350. expect(share.file_target).toEqual('/folder');
  351. expect(share.item_source).toEqual(123);
  352. expect(share.item_type).toEqual('file');
  353. expect(share.displayname_owner).toEqual('root');
  354. expect(share.mail_send).toEqual(0);
  355. expect(share.parent).toEqual(999);
  356. expect(share.path).toEqual('/folder');
  357. expect(share.permissions).toEqual(OC.PERMISSION_READ);
  358. expect(share.share_type).toEqual(OC.Share.SHARE_TYPE_USER);
  359. expect(share.share_with).toEqual('user1');
  360. expect(share.stime).toEqual(1403884258);
  361. expect(share.expiration).toEqual('2015-10-12 00:00:00');
  362. });
  363. });
  364. describe('hasUserShares', function() {
  365. it('returns false when no user shares exist', function() {
  366. fetchReshareDeferred.resolve(makeOcsResponse([]));
  367. fetchSharesDeferred.resolve(makeOcsResponse([]));
  368. model.fetch();
  369. expect(model.hasUserShares()).toEqual(false);
  370. });
  371. it('returns true when user shares exist on the current item', function() {
  372. /* jshint camelcase: false */
  373. fetchReshareDeferred.resolve(makeOcsResponse([]));
  374. fetchSharesDeferred.resolve(makeOcsResponse([{
  375. id: 1,
  376. share_type: OC.Share.SHARE_TYPE_USER,
  377. share_with: 'user1',
  378. item_source: '123'
  379. }]));
  380. model.fetch();
  381. expect(model.hasUserShares()).toEqual(true);
  382. });
  383. it('returns true when group shares exist on the current item', function() {
  384. /* jshint camelcase: false */
  385. fetchReshareDeferred.resolve(makeOcsResponse([]));
  386. fetchSharesDeferred.resolve(makeOcsResponse([{
  387. id: 1,
  388. share_type: OC.Share.SHARE_TYPE_GROUP,
  389. share_with: 'group1',
  390. item_source: '123'
  391. }]));
  392. model.fetch();
  393. expect(model.hasUserShares()).toEqual(true);
  394. });
  395. it('returns false when share exist on parent item', function() {
  396. /* jshint camelcase: false */
  397. fetchReshareDeferred.resolve(makeOcsResponse([]));
  398. fetchSharesDeferred.resolve(makeOcsResponse([{
  399. id: 1,
  400. share_type: OC.Share.SHARE_TYPE_GROUP,
  401. share_with: 'group1',
  402. item_source: '111'
  403. }]));
  404. model.fetch();
  405. expect(model.hasUserShares()).toEqual(false);
  406. });
  407. });
  408. describe('Util', function() {
  409. it('parseTime should properly parse strings', function() {
  410. _.each([
  411. [ '123456', 123456],
  412. [ 123456 , 123456],
  413. ['0123456', 123456],
  414. ['abcdefg', null],
  415. ['0x12345', null],
  416. [ '', null],
  417. ], function(value) {
  418. expect(OC.Share.ShareItemModel.prototype._parseTime(value[0])).toEqual(value[1]);
  419. });
  420. });
  421. });
  422. describe('share permissions', function() {
  423. beforeEach(function() {
  424. oc_appconfig.core.resharingAllowed = true;
  425. });
  426. /**
  427. * Tests sharing with the given possible permissions
  428. *
  429. * @param {int} possiblePermissions
  430. * @return {int} permissions sent to the server
  431. */
  432. function testWithPermissions(possiblePermissions) {
  433. model.set({
  434. permissions: possiblePermissions,
  435. possiblePermissions: possiblePermissions
  436. });
  437. model.addShare({
  438. shareType: OC.Share.SHARE_TYPE_USER,
  439. shareWith: 'user2'
  440. });
  441. var requestBody = OC.parseQueryString(_.last(fakeServer.requests).requestBody);
  442. return parseInt(requestBody.permissions, 10);
  443. }
  444. describe('regular sharing', function() {
  445. it('shares with given permissions with default config', function() {
  446. configModel.set('isResharingAllowed', true);
  447. model.set({
  448. reshare: {},
  449. shares: []
  450. });
  451. expect(
  452. testWithPermissions(OC.PERMISSION_READ | OC.PERMISSION_UPDATE | OC.PERMISSION_SHARE)
  453. ).toEqual(OC.PERMISSION_READ | OC.PERMISSION_UPDATE | OC.PERMISSION_SHARE);
  454. expect(
  455. testWithPermissions(OC.PERMISSION_READ | OC.PERMISSION_SHARE)
  456. ).toEqual(OC.PERMISSION_READ | OC.PERMISSION_SHARE);
  457. });
  458. it('removes share permission when not allowed', function() {
  459. configModel.set('isResharingAllowed', false);
  460. model.set({
  461. reshare: {},
  462. shares: []
  463. });
  464. expect(
  465. testWithPermissions(OC.PERMISSION_READ | OC.PERMISSION_UPDATE | OC.PERMISSION_SHARE)
  466. ).toEqual(OC.PERMISSION_READ | OC.PERMISSION_UPDATE);
  467. });
  468. it('automatically adds READ permission even when not specified', function() {
  469. configModel.set('isResharingAllowed', false);
  470. model.set({
  471. reshare: {},
  472. shares: []
  473. });
  474. expect(
  475. testWithPermissions(OC.PERMISSION_UPDATE | OC.PERMISSION_SHARE)
  476. ).toEqual(OC.PERMISSION_READ | OC.PERMISSION_UPDATE | OC.PERMISSION_UPDATE);
  477. });
  478. });
  479. });
  480. describe('saveLinkShare', function() {
  481. var addShareStub;
  482. var updateShareStub;
  483. beforeEach(function() {
  484. addShareStub = sinon.stub(model, 'addShare');
  485. updateShareStub = sinon.stub(model, 'updateShare');
  486. });
  487. afterEach(function() {
  488. addShareStub.restore();
  489. updateShareStub.restore();
  490. });
  491. it('creates a new share if no link share exists', function() {
  492. model.set({
  493. linkShare: {
  494. isLinkShare: false
  495. }
  496. });
  497. model.saveLinkShare();
  498. expect(addShareStub.calledOnce).toEqual(true);
  499. expect(addShareStub.firstCall.args[0]).toEqual({
  500. password: '',
  501. passwordChanged: false,
  502. permissions: OC.PERMISSION_READ,
  503. expireDate: '',
  504. shareType: OC.Share.SHARE_TYPE_LINK
  505. });
  506. expect(updateShareStub.notCalled).toEqual(true);
  507. });
  508. it('creates a new share with default expiration date', function() {
  509. var clock = sinon.useFakeTimers(Date.UTC(2015, 6, 17, 1, 2, 0, 3));
  510. configModel.set({
  511. isDefaultExpireDateEnabled: true,
  512. defaultExpireDate: 7
  513. });
  514. model.set({
  515. linkShare: {
  516. isLinkShare: false
  517. }
  518. });
  519. model.saveLinkShare();
  520. expect(addShareStub.calledOnce).toEqual(true);
  521. expect(addShareStub.firstCall.args[0]).toEqual({
  522. password: '',
  523. passwordChanged: false,
  524. permissions: OC.PERMISSION_READ,
  525. expireDate: '2015-07-24 00:00:00',
  526. shareType: OC.Share.SHARE_TYPE_LINK
  527. });
  528. expect(updateShareStub.notCalled).toEqual(true);
  529. clock.restore();
  530. });
  531. it('updates link share if it exists', function() {
  532. model.set({
  533. linkShare: {
  534. isLinkShare: true,
  535. id: 123
  536. }
  537. });
  538. model.saveLinkShare({
  539. password: 'test'
  540. });
  541. expect(addShareStub.notCalled).toEqual(true);
  542. expect(updateShareStub.calledOnce).toEqual(true);
  543. expect(updateShareStub.firstCall.args[0]).toEqual(123);
  544. expect(updateShareStub.firstCall.args[1]).toEqual({
  545. password: 'test'
  546. });
  547. });
  548. it('forwards error message on add', function() {
  549. var errorStub = sinon.stub();
  550. model.set({
  551. linkShare: {
  552. isLinkShare: false
  553. }
  554. }, {
  555. });
  556. model.saveLinkShare({
  557. password: 'test'
  558. }, {
  559. error: errorStub
  560. });
  561. addShareStub.yieldTo('error', 'Some error message');
  562. expect(errorStub.calledOnce).toEqual(true);
  563. expect(errorStub.lastCall.args[0]).toEqual('Some error message');
  564. });
  565. it('forwards error message on update', function() {
  566. var errorStub = sinon.stub();
  567. model.set({
  568. linkShare: {
  569. isLinkShare: true,
  570. id: '123'
  571. }
  572. }, {
  573. });
  574. model.saveLinkShare({
  575. password: 'test'
  576. }, {
  577. error: errorStub
  578. });
  579. updateShareStub.yieldTo('error', 'Some error message');
  580. expect(errorStub.calledOnce).toEqual(true);
  581. expect(errorStub.lastCall.args[0]).toEqual('Some error message');
  582. });
  583. });
  584. describe('creating shares', function() {
  585. it('sends POST method to endpoint with passed values', function() {
  586. model.addShare({
  587. shareType: OC.Share.SHARE_TYPE_GROUP,
  588. shareWith: 'group1'
  589. });
  590. expect(fakeServer.requests.length).toEqual(1);
  591. expect(fakeServer.requests[0].method).toEqual('POST');
  592. expect(fakeServer.requests[0].url).toEqual(
  593. OC.linkToOCS('apps/files_sharing/api/v1', 2) +
  594. 'shares?format=json'
  595. );
  596. expect(OC.parseQueryString(fakeServer.requests[0].requestBody)).toEqual({
  597. path: '/subdir/shared_file_name.txt',
  598. permissions: '' + OC.PERMISSION_READ,
  599. shareType: '' + OC.Share.SHARE_TYPE_GROUP,
  600. shareWith: 'group1'
  601. });
  602. });
  603. it('calls error handler with error message', function() {
  604. var errorStub = sinon.stub();
  605. model.addShare({
  606. shareType: OC.Share.SHARE_TYPE_GROUP,
  607. shareWith: 'group1'
  608. }, {
  609. error: errorStub
  610. });
  611. expect(fakeServer.requests.length).toEqual(1);
  612. fakeServer.requests[0].respond(
  613. 400,
  614. { 'Content-Type': 'application/json' },
  615. JSON.stringify({
  616. ocs: {
  617. meta: {
  618. message: 'Some error message'
  619. }
  620. }
  621. })
  622. );
  623. expect(errorStub.calledOnce).toEqual(true);
  624. expect(errorStub.lastCall.args[1]).toEqual('Some error message');
  625. });
  626. });
  627. describe('updating shares', function() {
  628. it('sends PUT method to endpoint with passed values', function() {
  629. model.updateShare(123, {
  630. permissions: OC.PERMISSION_READ | OC.PERMISSION_SHARE
  631. });
  632. expect(fakeServer.requests.length).toEqual(1);
  633. expect(fakeServer.requests[0].method).toEqual('PUT');
  634. expect(fakeServer.requests[0].url).toEqual(
  635. OC.linkToOCS('apps/files_sharing/api/v1', 2) +
  636. 'shares/123?format=json'
  637. );
  638. expect(OC.parseQueryString(fakeServer.requests[0].requestBody)).toEqual({
  639. permissions: '' + (OC.PERMISSION_READ | OC.PERMISSION_SHARE)
  640. });
  641. });
  642. it('calls error handler with error message', function() {
  643. var errorStub = sinon.stub();
  644. model.updateShare(123, {
  645. permissions: OC.PERMISSION_READ | OC.PERMISSION_SHARE
  646. }, {
  647. error: errorStub
  648. });
  649. expect(fakeServer.requests.length).toEqual(1);
  650. fakeServer.requests[0].respond(
  651. 400,
  652. { 'Content-Type': 'application/json' },
  653. JSON.stringify({
  654. ocs: {
  655. meta: {
  656. message: 'Some error message'
  657. }
  658. }
  659. })
  660. );
  661. expect(errorStub.calledOnce).toEqual(true);
  662. expect(errorStub.lastCall.args[1]).toEqual('Some error message');
  663. });
  664. });
  665. describe('removing shares', function() {
  666. it('sends DELETE method to endpoint with share id', function() {
  667. model.removeShare(123);
  668. expect(fakeServer.requests.length).toEqual(1);
  669. expect(fakeServer.requests[0].method).toEqual('DELETE');
  670. expect(fakeServer.requests[0].url).toEqual(
  671. OC.linkToOCS('apps/files_sharing/api/v1', 2) +
  672. 'shares/123?format=json'
  673. );
  674. });
  675. it('calls error handler with error message', function() {
  676. var errorStub = sinon.stub();
  677. model.removeShare(123, {
  678. error: errorStub
  679. });
  680. expect(fakeServer.requests.length).toEqual(1);
  681. fakeServer.requests[0].respond(
  682. 400,
  683. { 'Content-Type': 'application/json' },
  684. JSON.stringify({
  685. ocs: {
  686. meta: {
  687. message: 'Some error message'
  688. }
  689. }
  690. })
  691. );
  692. expect(errorStub.calledOnce).toEqual(true);
  693. expect(errorStub.lastCall.args[1]).toEqual('Some error message');
  694. });
  695. });
  696. });