shareitemmodelSpec.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828
  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('does not parse link share when for a different file', function() {
  165. /* jshint camelcase: false */
  166. fetchReshareDeferred.resolve(makeOcsResponse([]));
  167. fetchSharesDeferred.resolve(makeOcsResponse([{
  168. displayname_owner: 'root',
  169. expiration: null,
  170. file_source: 456,
  171. file_target: '/folder',
  172. id: 20,
  173. item_source: '456',
  174. item_type: 'folder',
  175. mail_send: '0',
  176. parent: null,
  177. path: '/folder',
  178. permissions: OC.PERMISSION_READ,
  179. share_type: OC.Share.SHARE_TYPE_LINK,
  180. share_with: null,
  181. stime: 1403884258,
  182. storage: 1,
  183. token: 'tehtoken',
  184. uid_owner: 'root'
  185. }]
  186. ));
  187. model.fetch();
  188. var shares = model.get('shares');
  189. // remaining share appears in this list
  190. expect(shares.length).toEqual(1);
  191. var linkShare = model.get('linkShare');
  192. expect(linkShare.isLinkShare).toEqual(false);
  193. });
  194. it('parses correct link share when a nested link share exists along with parent one', function() {
  195. /* jshint camelcase: false */
  196. fetchReshareDeferred.resolve(makeOcsResponse([]));
  197. fetchSharesDeferred.resolve(makeOcsResponse([{
  198. displayname_owner: 'root',
  199. expiration: '2015-10-12 00:00:00',
  200. file_source: 123,
  201. file_target: '/folder',
  202. id: 20,
  203. item_source: '123',
  204. item_type: 'file',
  205. mail_send: '0',
  206. parent: null,
  207. path: '/folder',
  208. permissions: OC.PERMISSION_READ,
  209. share_type: OC.Share.SHARE_TYPE_LINK,
  210. share_with: null,
  211. stime: 1403884258,
  212. storage: 1,
  213. token: 'tehtoken',
  214. uid_owner: 'root'
  215. }, {
  216. displayname_owner: 'root',
  217. expiration: '2015-10-15 00:00:00',
  218. file_source: 456,
  219. file_target: '/file_in_folder.txt',
  220. id: 21,
  221. item_source: '456',
  222. item_type: 'file',
  223. mail_send: '0',
  224. parent: null,
  225. path: '/folder/file_in_folder.txt',
  226. permissions: OC.PERMISSION_READ,
  227. share_type: OC.Share.SHARE_TYPE_LINK,
  228. share_with: null,
  229. stime: 1403884509,
  230. storage: 1,
  231. token: 'anothertoken',
  232. uid_owner: 'root'
  233. }]
  234. ));
  235. OC.currentUser = 'root';
  236. model.fetch();
  237. var shares = model.get('shares');
  238. // the parent share remains in the list
  239. expect(shares.length).toEqual(1);
  240. var linkShare = model.get('linkShare');
  241. expect(linkShare.isLinkShare).toEqual(true);
  242. expect(linkShare.token).toEqual('tehtoken');
  243. // TODO: check child too
  244. });
  245. it('reduces reshare permissions to the ones from the original share', function() {
  246. /* jshint camelcase: false */
  247. fetchReshareDeferred.resolve(makeOcsResponse([{
  248. id: 123,
  249. permissions: OC.PERMISSION_READ,
  250. uid_owner: 'user1'
  251. }]));
  252. fetchSharesDeferred.resolve(makeOcsResponse([]));
  253. model.fetch();
  254. // no resharing allowed
  255. expect(model.get('permissions')).toEqual(OC.PERMISSION_READ);
  256. });
  257. it('reduces reshare permissions to possible permissions', function() {
  258. /* jshint camelcase: false */
  259. fetchReshareDeferred.resolve(makeOcsResponse([{
  260. id: 123,
  261. permissions: OC.PERMISSION_ALL,
  262. uid_owner: 'user1'
  263. }]));
  264. fetchSharesDeferred.resolve(makeOcsResponse([]));
  265. model.set('possiblePermissions', OC.PERMISSION_READ);
  266. model.fetch();
  267. // no resharing allowed
  268. expect(model.get('permissions')).toEqual(OC.PERMISSION_READ);
  269. });
  270. it('allows owner to share their own share when they are also the recipient', function() {
  271. OC.currentUser = 'user1';
  272. fetchReshareDeferred.resolve(makeOcsResponse([]));
  273. fetchSharesDeferred.resolve(makeOcsResponse([]));
  274. model.fetch();
  275. // sharing still allowed
  276. expect(model.get('permissions') & OC.PERMISSION_SHARE).toEqual(OC.PERMISSION_SHARE);
  277. });
  278. it('properly parses integer values when the server is in the mood of returning ints as string', function() {
  279. /* jshint camelcase: false */
  280. fetchReshareDeferred.resolve(makeOcsResponse([]));
  281. fetchSharesDeferred.resolve(makeOcsResponse([{
  282. displayname_owner: 'root',
  283. expiration: '2015-10-12 00:00:00',
  284. file_source: '123',
  285. file_target: '/folder',
  286. id: '20',
  287. item_source: '123',
  288. item_type: 'file',
  289. mail_send: '0',
  290. parent: '999',
  291. path: '/folder',
  292. permissions: '' + OC.PERMISSION_READ,
  293. share_type: '' + OC.Share.SHARE_TYPE_USER,
  294. share_with: 'user1',
  295. stime: '1403884258',
  296. storage: '1',
  297. token: 'tehtoken',
  298. uid_owner: 'root'
  299. }]
  300. ));
  301. model.fetch();
  302. var shares = model.get('shares');
  303. expect(shares.length).toEqual(1);
  304. var share = shares[0];
  305. expect(share.id).toEqual(20);
  306. expect(share.file_source).toEqual(123);
  307. expect(share.file_target).toEqual('/folder');
  308. expect(share.item_source).toEqual(123);
  309. expect(share.item_type).toEqual('file');
  310. expect(share.displayname_owner).toEqual('root');
  311. expect(share.mail_send).toEqual(0);
  312. expect(share.parent).toEqual(999);
  313. expect(share.path).toEqual('/folder');
  314. expect(share.permissions).toEqual(OC.PERMISSION_READ);
  315. expect(share.share_type).toEqual(OC.Share.SHARE_TYPE_USER);
  316. expect(share.share_with).toEqual('user1');
  317. expect(share.stime).toEqual(1403884258);
  318. expect(share.expiration).toEqual('2015-10-12 00:00:00');
  319. });
  320. });
  321. describe('hasUserShares', function() {
  322. it('returns false when no user shares exist', function() {
  323. fetchReshareDeferred.resolve(makeOcsResponse([]));
  324. fetchSharesDeferred.resolve(makeOcsResponse([]));
  325. model.fetch();
  326. expect(model.hasUserShares()).toEqual(false);
  327. });
  328. it('returns true when user shares exist on the current item', function() {
  329. /* jshint camelcase: false */
  330. fetchReshareDeferred.resolve(makeOcsResponse([]));
  331. fetchSharesDeferred.resolve(makeOcsResponse([{
  332. id: 1,
  333. share_type: OC.Share.SHARE_TYPE_USER,
  334. share_with: 'user1',
  335. item_source: '123'
  336. }]));
  337. model.fetch();
  338. expect(model.hasUserShares()).toEqual(true);
  339. });
  340. it('returns true when group shares exist on the current item', function() {
  341. /* jshint camelcase: false */
  342. fetchReshareDeferred.resolve(makeOcsResponse([]));
  343. fetchSharesDeferred.resolve(makeOcsResponse([{
  344. id: 1,
  345. share_type: OC.Share.SHARE_TYPE_GROUP,
  346. share_with: 'group1',
  347. item_source: '123'
  348. }]));
  349. model.fetch();
  350. expect(model.hasUserShares()).toEqual(true);
  351. });
  352. it('returns false when share exist on parent item', function() {
  353. /* jshint camelcase: false */
  354. fetchReshareDeferred.resolve(makeOcsResponse([]));
  355. fetchSharesDeferred.resolve(makeOcsResponse([{
  356. id: 1,
  357. share_type: OC.Share.SHARE_TYPE_GROUP,
  358. share_with: 'group1',
  359. item_source: '111'
  360. }]));
  361. model.fetch();
  362. expect(model.hasUserShares()).toEqual(false);
  363. });
  364. });
  365. describe('Util', function() {
  366. it('parseTime should properly parse strings', function() {
  367. _.each([
  368. [ '123456', 123456],
  369. [ 123456 , 123456],
  370. ['0123456', 123456],
  371. ['abcdefg', null],
  372. ['0x12345', null],
  373. [ '', null],
  374. ], function(value) {
  375. expect(OC.Share.ShareItemModel.prototype._parseTime(value[0])).toEqual(value[1]);
  376. });
  377. });
  378. });
  379. describe('sendEmailPrivateLink', function() {
  380. it('succeeds', function() {
  381. /* jshint camelcase: false */
  382. fetchReshareDeferred.resolve(makeOcsResponse([]));
  383. fetchSharesDeferred.resolve(makeOcsResponse([{
  384. displayname_owner: 'root',
  385. expiration: null,
  386. file_source: 123,
  387. file_target: '/folder',
  388. id: 20,
  389. item_source: '123',
  390. item_type: 'folder',
  391. mail_send: '0',
  392. parent: null,
  393. path: '/folder',
  394. permissions: OC.PERMISSION_READ,
  395. share_type: OC.Share.SHARE_TYPE_LINK,
  396. share_with: null,
  397. stime: 1403884258,
  398. storage: 1,
  399. token: 'tehtoken',
  400. uid_owner: 'root'
  401. }]));
  402. OC.currentUser = 'root';
  403. model.fetch();
  404. var res = model.sendEmailPrivateLink('foo@bar.com');
  405. expect(res.state()).toEqual('pending');
  406. expect(fakeServer.requests.length).toEqual(1);
  407. expect(fakeServer.requests[0].url).toEqual(OC.generateUrl('core/ajax/share.php'));
  408. expect(OC.parseQueryString(fakeServer.requests[0].requestBody)).toEqual(
  409. {
  410. action: 'email',
  411. toaddress: 'foo@bar.com',
  412. link: model.get('linkShare').link,
  413. itemType: 'file',
  414. itemSource: '123',
  415. file: 'shared_file_name.txt',
  416. expiration: ''
  417. }
  418. )
  419. fakeServer.requests[0].respond(
  420. 200,
  421. { 'Content-Type': 'application/json' },
  422. JSON.stringify({status: 'success'})
  423. );
  424. expect(res.state()).toEqual('resolved');
  425. });
  426. it('fails', function() {
  427. /* jshint camelcase: false */
  428. fetchReshareDeferred.resolve(makeOcsResponse([]));
  429. fetchSharesDeferred.resolve(makeOcsResponse([{
  430. displayname_owner: 'root',
  431. expiration: null,
  432. file_source: 123,
  433. file_target: '/folder',
  434. id: 20,
  435. item_source: '123',
  436. item_type: 'folder',
  437. mail_send: '0',
  438. parent: null,
  439. path: '/folder',
  440. permissions: OC.PERMISSION_READ,
  441. share_type: OC.Share.SHARE_TYPE_LINK,
  442. share_with: null,
  443. stime: 1403884258,
  444. storage: 1,
  445. token: 'tehtoken',
  446. uid_owner: 'root'
  447. }]));
  448. OC.currentUser = 'root';
  449. model.fetch();
  450. var res = model.sendEmailPrivateLink('foo@bar.com');
  451. expect(res.state()).toEqual('pending');
  452. expect(fakeServer.requests.length).toEqual(1);
  453. expect(fakeServer.requests[0].url).toEqual(OC.generateUrl('core/ajax/share.php'));
  454. expect(OC.parseQueryString(fakeServer.requests[0].requestBody)).toEqual(
  455. {
  456. action: 'email',
  457. toaddress: 'foo@bar.com',
  458. link: model.get('linkShare').link,
  459. itemType: 'file',
  460. itemSource: '123',
  461. file: 'shared_file_name.txt',
  462. expiration: ''
  463. }
  464. )
  465. fakeServer.requests[0].respond(
  466. 200,
  467. { 'Content-Type': 'application/json' },
  468. JSON.stringify({data: {message: 'fail'}, status: 'error'})
  469. );
  470. expect(res.state()).toEqual('rejected');
  471. });
  472. });
  473. describe('share permissions', function() {
  474. beforeEach(function() {
  475. oc_appconfig.core.resharingAllowed = true;
  476. });
  477. /**
  478. * Tests sharing with the given possible permissions
  479. *
  480. * @param {int} possiblePermissions
  481. * @return {int} permissions sent to the server
  482. */
  483. function testWithPermissions(possiblePermissions) {
  484. model.set({
  485. permissions: possiblePermissions,
  486. possiblePermissions: possiblePermissions
  487. });
  488. model.addShare({
  489. shareType: OC.Share.SHARE_TYPE_USER,
  490. shareWith: 'user2'
  491. });
  492. var requestBody = OC.parseQueryString(_.last(fakeServer.requests).requestBody);
  493. return parseInt(requestBody.permissions, 10);
  494. }
  495. describe('regular sharing', function() {
  496. it('shares with given permissions with default config', function() {
  497. configModel.set('isResharingAllowed', true);
  498. model.set({
  499. reshare: {},
  500. shares: []
  501. });
  502. expect(
  503. testWithPermissions(OC.PERMISSION_READ | OC.PERMISSION_UPDATE | OC.PERMISSION_SHARE)
  504. ).toEqual(OC.PERMISSION_READ | OC.PERMISSION_UPDATE | OC.PERMISSION_SHARE);
  505. expect(
  506. testWithPermissions(OC.PERMISSION_READ | OC.PERMISSION_SHARE)
  507. ).toEqual(OC.PERMISSION_READ | OC.PERMISSION_SHARE);
  508. });
  509. it('removes share permission when not allowed', function() {
  510. configModel.set('isResharingAllowed', false);
  511. model.set({
  512. reshare: {},
  513. shares: []
  514. });
  515. expect(
  516. testWithPermissions(OC.PERMISSION_READ | OC.PERMISSION_UPDATE | OC.PERMISSION_SHARE)
  517. ).toEqual(OC.PERMISSION_READ | OC.PERMISSION_UPDATE);
  518. });
  519. it('automatically adds READ permission even when not specified', function() {
  520. configModel.set('isResharingAllowed', false);
  521. model.set({
  522. reshare: {},
  523. shares: []
  524. });
  525. expect(
  526. testWithPermissions(OC.PERMISSION_UPDATE | OC.PERMISSION_SHARE)
  527. ).toEqual(OC.PERMISSION_READ | OC.PERMISSION_UPDATE | OC.PERMISSION_UPDATE);
  528. });
  529. });
  530. });
  531. describe('saveLinkShare', function() {
  532. var addShareStub;
  533. var updateShareStub;
  534. beforeEach(function() {
  535. addShareStub = sinon.stub(model, 'addShare');
  536. updateShareStub = sinon.stub(model, 'updateShare');
  537. });
  538. afterEach(function() {
  539. addShareStub.restore();
  540. updateShareStub.restore();
  541. });
  542. it('creates a new share if no link share exists', function() {
  543. model.set({
  544. linkShare: {
  545. isLinkShare: false
  546. }
  547. });
  548. model.saveLinkShare();
  549. expect(addShareStub.calledOnce).toEqual(true);
  550. expect(addShareStub.firstCall.args[0]).toEqual({
  551. password: '',
  552. passwordChanged: false,
  553. permissions: OC.PERMISSION_READ,
  554. expireDate: '',
  555. shareType: OC.Share.SHARE_TYPE_LINK
  556. });
  557. expect(updateShareStub.notCalled).toEqual(true);
  558. });
  559. it('creates a new share with default expiration date', function() {
  560. var clock = sinon.useFakeTimers(Date.UTC(2015, 6, 17, 1, 2, 0, 3));
  561. configModel.set({
  562. isDefaultExpireDateEnabled: true,
  563. defaultExpireDate: 7
  564. });
  565. model.set({
  566. linkShare: {
  567. isLinkShare: false
  568. }
  569. });
  570. model.saveLinkShare();
  571. expect(addShareStub.calledOnce).toEqual(true);
  572. expect(addShareStub.firstCall.args[0]).toEqual({
  573. password: '',
  574. passwordChanged: false,
  575. permissions: OC.PERMISSION_READ,
  576. expireDate: '2015-07-24 00:00:00',
  577. shareType: OC.Share.SHARE_TYPE_LINK
  578. });
  579. expect(updateShareStub.notCalled).toEqual(true);
  580. clock.restore();
  581. });
  582. it('updates link share if it exists', function() {
  583. model.set({
  584. linkShare: {
  585. isLinkShare: true,
  586. id: 123
  587. }
  588. });
  589. model.saveLinkShare({
  590. password: 'test'
  591. });
  592. expect(addShareStub.notCalled).toEqual(true);
  593. expect(updateShareStub.calledOnce).toEqual(true);
  594. expect(updateShareStub.firstCall.args[0]).toEqual(123);
  595. expect(updateShareStub.firstCall.args[1]).toEqual({
  596. password: 'test'
  597. });
  598. });
  599. it('forwards error message on add', function() {
  600. var errorStub = sinon.stub();
  601. model.set({
  602. linkShare: {
  603. isLinkShare: false
  604. }
  605. }, {
  606. });
  607. model.saveLinkShare({
  608. password: 'test'
  609. }, {
  610. error: errorStub
  611. });
  612. addShareStub.yieldTo('error', 'Some error message');
  613. expect(errorStub.calledOnce).toEqual(true);
  614. expect(errorStub.lastCall.args[0]).toEqual('Some error message');
  615. });
  616. it('forwards error message on update', function() {
  617. var errorStub = sinon.stub();
  618. model.set({
  619. linkShare: {
  620. isLinkShare: true,
  621. id: '123'
  622. }
  623. }, {
  624. });
  625. model.saveLinkShare({
  626. password: 'test'
  627. }, {
  628. error: errorStub
  629. });
  630. updateShareStub.yieldTo('error', 'Some error message');
  631. expect(errorStub.calledOnce).toEqual(true);
  632. expect(errorStub.lastCall.args[0]).toEqual('Some error message');
  633. });
  634. });
  635. describe('creating shares', function() {
  636. it('sends POST method to endpoint with passed values', function() {
  637. model.addShare({
  638. shareType: OC.Share.SHARE_TYPE_GROUP,
  639. shareWith: 'group1'
  640. });
  641. expect(fakeServer.requests.length).toEqual(1);
  642. expect(fakeServer.requests[0].method).toEqual('POST');
  643. expect(fakeServer.requests[0].url).toEqual(
  644. OC.linkToOCS('apps/files_sharing/api/v1', 2) +
  645. 'shares?format=json'
  646. );
  647. expect(OC.parseQueryString(fakeServer.requests[0].requestBody)).toEqual({
  648. path: '/subdir/shared_file_name.txt',
  649. permissions: '' + OC.PERMISSION_READ,
  650. shareType: '' + OC.Share.SHARE_TYPE_GROUP,
  651. shareWith: 'group1'
  652. });
  653. });
  654. it('calls error handler with error message', function() {
  655. var errorStub = sinon.stub();
  656. model.addShare({
  657. shareType: OC.Share.SHARE_TYPE_GROUP,
  658. shareWith: 'group1'
  659. }, {
  660. error: errorStub
  661. });
  662. expect(fakeServer.requests.length).toEqual(1);
  663. fakeServer.requests[0].respond(
  664. 400,
  665. { 'Content-Type': 'application/json' },
  666. JSON.stringify({
  667. ocs: {
  668. meta: {
  669. message: 'Some error message'
  670. }
  671. }
  672. })
  673. );
  674. expect(errorStub.calledOnce).toEqual(true);
  675. expect(errorStub.lastCall.args[1]).toEqual('Some error message');
  676. });
  677. });
  678. describe('updating shares', function() {
  679. it('sends PUT method to endpoint with passed values', function() {
  680. model.updateShare(123, {
  681. permissions: OC.PERMISSION_READ | OC.PERMISSION_SHARE
  682. });
  683. expect(fakeServer.requests.length).toEqual(1);
  684. expect(fakeServer.requests[0].method).toEqual('PUT');
  685. expect(fakeServer.requests[0].url).toEqual(
  686. OC.linkToOCS('apps/files_sharing/api/v1', 2) +
  687. 'shares/123?format=json'
  688. );
  689. expect(OC.parseQueryString(fakeServer.requests[0].requestBody)).toEqual({
  690. permissions: '' + (OC.PERMISSION_READ | OC.PERMISSION_SHARE)
  691. });
  692. });
  693. it('calls error handler with error message', function() {
  694. var errorStub = sinon.stub();
  695. model.updateShare(123, {
  696. permissions: OC.PERMISSION_READ | OC.PERMISSION_SHARE
  697. }, {
  698. error: errorStub
  699. });
  700. expect(fakeServer.requests.length).toEqual(1);
  701. fakeServer.requests[0].respond(
  702. 400,
  703. { 'Content-Type': 'application/json' },
  704. JSON.stringify({
  705. ocs: {
  706. meta: {
  707. message: 'Some error message'
  708. }
  709. }
  710. })
  711. );
  712. expect(errorStub.calledOnce).toEqual(true);
  713. expect(errorStub.lastCall.args[1]).toEqual('Some error message');
  714. });
  715. });
  716. describe('removing shares', function() {
  717. it('sends DELETE method to endpoint with share id', function() {
  718. model.removeShare(123);
  719. expect(fakeServer.requests.length).toEqual(1);
  720. expect(fakeServer.requests[0].method).toEqual('DELETE');
  721. expect(fakeServer.requests[0].url).toEqual(
  722. OC.linkToOCS('apps/files_sharing/api/v1', 2) +
  723. 'shares/123?format=json'
  724. );
  725. });
  726. it('calls error handler with error message', function() {
  727. var errorStub = sinon.stub();
  728. model.removeShare(123, {
  729. error: errorStub
  730. });
  731. expect(fakeServer.requests.length).toEqual(1);
  732. fakeServer.requests[0].respond(
  733. 400,
  734. { 'Content-Type': 'application/json' },
  735. JSON.stringify({
  736. ocs: {
  737. meta: {
  738. message: 'Some error message'
  739. }
  740. }
  741. })
  742. );
  743. expect(errorStub.calledOnce).toEqual(true);
  744. expect(errorStub.lastCall.args[1]).toEqual('Some error message');
  745. });
  746. });
  747. });