cozy-sharing 6.0.6 → 7.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -3,6 +3,22 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
5
 
6
+ # [7.0.0](https://github.com/cozy/cozy-libs/compare/cozy-sharing@6.0.6...cozy-sharing@7.0.0) (2023-03-16)
7
+
8
+
9
+ ### Features
10
+
11
+ * Return also readOnly status from getSharedDocument ([c68ba61](https://github.com/cozy/cozy-libs/commit/c68ba61b69dfd2b152e47252005f294fee818110))
12
+
13
+
14
+ ### BREAKING CHANGES
15
+
16
+ * The function now returns the id encapsulated in an object. You can extract the id like this `const { id } = getSharedDocument(client)`
17
+
18
+
19
+
20
+
21
+
6
22
  ## 6.0.6 (2023-03-16)
7
23
 
8
24
  **Note:** Version bump only for package cozy-sharing
@@ -1,10 +1,16 @@
1
1
  import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
2
2
  import _regeneratorRuntime from "@babel/runtime/regenerator";
3
- import get from 'lodash/get';
3
+ import { isReadOnly } from 'cozy-client/dist/models/permission';
4
+ /**
5
+ * Get the first shared document for the current shared token
6
+ *
7
+ * @param {CozyClient} client
8
+ * @returns {{id, isReadOnly}} id of the document and the readOnly status
9
+ */
4
10
 
5
11
  var getSharedDocument = /*#__PURE__*/function () {
6
12
  var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(client) {
7
- var _yield$client$collect, permissionsData, permissions, sharedDocumentId;
13
+ var _yield$client$collect, permissionsData, permissions, perm;
8
14
 
9
15
  return _regeneratorRuntime.wrap(function _callee$(_context) {
10
16
  while (1) switch (_context.prev = _context.next) {
@@ -15,10 +21,13 @@ var getSharedDocument = /*#__PURE__*/function () {
15
21
  case 2:
16
22
  _yield$client$collect = _context.sent;
17
23
  permissionsData = _yield$client$collect.data;
18
- permissions = permissionsData.attributes.permissions; // permissions contains several named keys, but the one to use depends on the situation. Using the first one is what we want in all known cases.
24
+ permissions = Object.values(permissionsData.attributes.permissions); // permissions contains several named keys, but the one to use depends on the situation. Using the first one is what we want in all known cases.
19
25
 
20
- sharedDocumentId = get(Object.values(permissions), '0.values.0');
21
- return _context.abrupt("return", sharedDocumentId);
26
+ perm = permissions[0];
27
+ return _context.abrupt("return", {
28
+ id: perm.values[0],
29
+ isReadOnly: isReadOnly(perm)
30
+ });
22
31
 
23
32
  case 7:
24
33
  case "end":
@@ -1,8 +1,10 @@
1
1
  import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
2
2
  import _regeneratorRuntime from "@babel/runtime/regenerator";
3
3
  import getSharedDocument from './getSharedDocument';
4
- describe('Getting the shared document id', function () {
5
- var mockClient = {
4
+
5
+ function setupClient() {
6
+ var verbs = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
7
+ return {
6
8
  collection: function collection() {
7
9
  return {
8
10
  getOwnPermissions: jest.fn().mockReturnValue({
@@ -11,14 +13,14 @@ describe('Getting the shared document id', function () {
11
13
  permissions: {
12
14
  collection: {
13
15
  type: 'io.cozy.photos.albums',
14
- verbs: ['GET'],
16
+ verbs: verbs,
15
17
  values: ['myshareid']
16
18
  },
17
19
  files: {
18
20
  selector: 'referenced_by',
19
21
  type: 'io.cozy.files',
20
22
  values: ['io.cozy.photos.albums/myshareid'],
21
- verbs: ['GET']
23
+ verbs: verbs
22
24
  }
23
25
  }
24
26
  }
@@ -27,22 +29,91 @@ describe('Getting the shared document id', function () {
27
29
  };
28
30
  }
29
31
  };
32
+ }
33
+
34
+ describe('Getting the shared document', function () {
30
35
  it('should get the id', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
31
- var id;
36
+ var client, _yield$getSharedDocum, id;
37
+
32
38
  return _regeneratorRuntime.wrap(function _callee$(_context) {
33
39
  while (1) switch (_context.prev = _context.next) {
34
40
  case 0:
35
- _context.next = 2;
36
- return getSharedDocument(mockClient);
41
+ client = setupClient(['GET']);
42
+ _context.next = 3;
43
+ return getSharedDocument(client);
37
44
 
38
- case 2:
39
- id = _context.sent;
45
+ case 3:
46
+ _yield$getSharedDocum = _context.sent;
47
+ id = _yield$getSharedDocum.id;
40
48
  expect(id).toEqual('myshareid');
41
49
 
42
- case 4:
50
+ case 6:
43
51
  case "end":
44
52
  return _context.stop();
45
53
  }
46
54
  }, _callee);
47
55
  })));
56
+ it('should return isReadOnly is true when we have only a GET permission', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2() {
57
+ var client, _yield$getSharedDocum2, isReadOnly;
58
+
59
+ return _regeneratorRuntime.wrap(function _callee2$(_context2) {
60
+ while (1) switch (_context2.prev = _context2.next) {
61
+ case 0:
62
+ client = setupClient(['GET']);
63
+ _context2.next = 3;
64
+ return getSharedDocument(client);
65
+
66
+ case 3:
67
+ _yield$getSharedDocum2 = _context2.sent;
68
+ isReadOnly = _yield$getSharedDocum2.isReadOnly;
69
+ expect(isReadOnly).toBeTruthy();
70
+
71
+ case 6:
72
+ case "end":
73
+ return _context2.stop();
74
+ }
75
+ }, _callee2);
76
+ })));
77
+ it('should return isReadOnly is false when we have an ALL permission', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee3() {
78
+ var client, _yield$getSharedDocum3, isReadOnly;
79
+
80
+ return _regeneratorRuntime.wrap(function _callee3$(_context3) {
81
+ while (1) switch (_context3.prev = _context3.next) {
82
+ case 0:
83
+ client = setupClient(['ALL']);
84
+ _context3.next = 3;
85
+ return getSharedDocument(client);
86
+
87
+ case 3:
88
+ _yield$getSharedDocum3 = _context3.sent;
89
+ isReadOnly = _yield$getSharedDocum3.isReadOnly;
90
+ expect(isReadOnly).toBeFalsy();
91
+
92
+ case 6:
93
+ case "end":
94
+ return _context3.stop();
95
+ }
96
+ }, _callee3);
97
+ })));
98
+ it('should return isReadOnly is false when we have a PATCH permission', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee4() {
99
+ var client, _yield$getSharedDocum4, isReadOnly;
100
+
101
+ return _regeneratorRuntime.wrap(function _callee4$(_context4) {
102
+ while (1) switch (_context4.prev = _context4.next) {
103
+ case 0:
104
+ client = setupClient(['GET', 'PATCH', 'PUT']);
105
+ _context4.next = 3;
106
+ return getSharedDocument(client);
107
+
108
+ case 3:
109
+ _yield$getSharedDocum4 = _context4.sent;
110
+ isReadOnly = _yield$getSharedDocum4.isReadOnly;
111
+ expect(isReadOnly).toBeFalsy();
112
+
113
+ case 6:
114
+ case "end":
115
+ return _context4.stop();
116
+ }
117
+ }, _callee4);
118
+ })));
48
119
  });
package/dist/state.js CHANGED
@@ -461,9 +461,7 @@ var getApps = function getApps(state) {
461
461
 
462
462
  export var hasSharedParent = function hasSharedParent(state, documentPath) {
463
463
  if (!state.sharedPaths) {
464
- // eslint-disable-next-line
465
- console.log('hasSharedParent should not occur', state, documentPath);
466
- return false;
464
+ return false; // hasSharedParent should not occur
467
465
  }
468
466
 
469
467
  return state.sharedPaths.some(function (path) {
@@ -472,9 +470,7 @@ export var hasSharedParent = function hasSharedParent(state, documentPath) {
472
470
  };
473
471
  export var hasSharedChild = function hasSharedChild(state, documentPath) {
474
472
  if (!state.sharedPaths) {
475
- // eslint-disable-next-line
476
- console.log('hasSharedChild should not occur', state, documentPath);
477
- return false;
473
+ return false; // hasSharedChild should not occur
478
474
  }
479
475
 
480
476
  var ret = state.sharedPaths.some(function (path) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cozy-sharing",
3
- "version": "6.0.6",
3
+ "version": "7.0.0",
4
4
  "description": "Provides sharing login for React applications.",
5
5
  "main": "dist/index.js",
6
6
  "author": "Cozy",
@@ -61,5 +61,5 @@
61
61
  "sideEffects": [
62
62
  "*.css"
63
63
  ],
64
- "gitHead": "94b2b5a6a105edfc6fc7a20204291d2191ba2ba1"
64
+ "gitHead": "b623a595cc50731ff2bf2db93f0533cf1553530a"
65
65
  }
@@ -1,15 +1,23 @@
1
- import get from 'lodash/get'
1
+ import { isReadOnly } from 'cozy-client/dist/models/permission'
2
2
 
3
+ /**
4
+ * Get the first shared document for the current shared token
5
+ *
6
+ * @param {CozyClient} client
7
+ * @returns {{id, isReadOnly}} id of the document and the readOnly status
8
+ */
3
9
  const getSharedDocument = async client => {
4
10
  const { data: permissionsData } = await client
5
11
  .collection('io.cozy.permissions')
6
12
  .getOwnPermissions()
7
13
 
8
- const permissions = permissionsData.attributes.permissions
14
+ const permissions = Object.values(permissionsData.attributes.permissions)
9
15
  // permissions contains several named keys, but the one to use depends on the situation. Using the first one is what we want in all known cases.
10
- const sharedDocumentId = get(Object.values(permissions), '0.values.0')
11
-
12
- return sharedDocumentId
16
+ const perm = permissions[0]
17
+ return {
18
+ id: perm.values[0],
19
+ isReadOnly: isReadOnly(perm)
20
+ }
13
21
  }
14
22
 
15
23
  export default getSharedDocument
@@ -1,7 +1,7 @@
1
1
  import getSharedDocument from './getSharedDocument'
2
2
 
3
- describe('Getting the shared document id', () => {
4
- const mockClient = {
3
+ function setupClient(verbs = []) {
4
+ return {
5
5
  collection: () => ({
6
6
  getOwnPermissions: jest.fn().mockReturnValue({
7
7
  data: {
@@ -9,14 +9,14 @@ describe('Getting the shared document id', () => {
9
9
  permissions: {
10
10
  collection: {
11
11
  type: 'io.cozy.photos.albums',
12
- verbs: ['GET'],
12
+ verbs,
13
13
  values: ['myshareid']
14
14
  },
15
15
  files: {
16
16
  selector: 'referenced_by',
17
17
  type: 'io.cozy.files',
18
18
  values: ['io.cozy.photos.albums/myshareid'],
19
- verbs: ['GET']
19
+ verbs
20
20
  }
21
21
  }
22
22
  }
@@ -24,9 +24,30 @@ describe('Getting the shared document id', () => {
24
24
  })
25
25
  })
26
26
  }
27
+ }
27
28
 
29
+ describe('Getting the shared document', () => {
28
30
  it('should get the id', async () => {
29
- const id = await getSharedDocument(mockClient)
31
+ const client = setupClient(['GET'])
32
+ const { id } = await getSharedDocument(client)
30
33
  expect(id).toEqual('myshareid')
31
34
  })
35
+
36
+ it('should return isReadOnly is true when we have only a GET permission', async () => {
37
+ const client = setupClient(['GET'])
38
+ const { isReadOnly } = await getSharedDocument(client)
39
+ expect(isReadOnly).toBeTruthy()
40
+ })
41
+
42
+ it('should return isReadOnly is false when we have an ALL permission', async () => {
43
+ const client = setupClient(['ALL'])
44
+ const { isReadOnly } = await getSharedDocument(client)
45
+ expect(isReadOnly).toBeFalsy()
46
+ })
47
+
48
+ it('should return isReadOnly is false when we have a PATCH permission', async () => {
49
+ const client = setupClient(['GET', 'PATCH', 'PUT'])
50
+ const { isReadOnly } = await getSharedDocument(client)
51
+ expect(isReadOnly).toBeFalsy()
52
+ })
32
53
  })
package/src/state.js CHANGED
@@ -349,18 +349,14 @@ const getApps = state => state.apps
349
349
 
350
350
  export const hasSharedParent = (state, documentPath) => {
351
351
  if (!state.sharedPaths) {
352
- // eslint-disable-next-line
353
- console.log('hasSharedParent should not occur', state, documentPath)
354
- return false
352
+ return false // hasSharedParent should not occur
355
353
  }
356
354
  return state.sharedPaths.some(path => documentPath.indexOf(`${path}/`) === 0)
357
355
  }
358
356
 
359
357
  export const hasSharedChild = (state, documentPath) => {
360
358
  if (!state.sharedPaths) {
361
- // eslint-disable-next-line
362
- console.log('hasSharedChild should not occur', state, documentPath)
363
- return false
359
+ return false // hasSharedChild should not occur
364
360
  }
365
361
  const ret = state.sharedPaths.some(
366
362
  path => path.indexOf(`${documentPath}/`) === 0