cozy-client 48.24.1 → 49.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.
@@ -0,0 +1,128 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+
5
+ Object.defineProperty(exports, "__esModule", {
6
+ value: true
7
+ });
8
+ exports.default = void 0;
9
+
10
+ var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
11
+
12
+ var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
13
+
14
+ var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
15
+
16
+ var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
17
+
18
+ var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));
19
+
20
+ var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));
21
+
22
+ var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));
23
+
24
+ var _CozyLink2 = _interopRequireDefault(require("./CozyLink"));
25
+
26
+ function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = (0, _getPrototypeOf2.default)(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = (0, _getPrototypeOf2.default)(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return (0, _possibleConstructorReturn2.default)(this, result); }; }
27
+
28
+ function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } }
29
+
30
+ var WebFlagshipLink = /*#__PURE__*/function (_CozyLink) {
31
+ (0, _inherits2.default)(WebFlagshipLink, _CozyLink);
32
+
33
+ var _super = _createSuper(WebFlagshipLink);
34
+
35
+ /**
36
+ * @param {object} [options] - Options
37
+ * @param {import('cozy-intent').WebviewService} [options.webviewIntent] - The webview's intent reference
38
+ */
39
+ function WebFlagshipLink() {
40
+ var _this;
41
+
42
+ var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
43
+ webviewIntent = _ref.webviewIntent;
44
+
45
+ (0, _classCallCheck2.default)(this, WebFlagshipLink);
46
+ _this = _super.call(this);
47
+ _this.webviewIntent = webviewIntent;
48
+ return _this;
49
+ }
50
+
51
+ (0, _createClass2.default)(WebFlagshipLink, [{
52
+ key: "registerClient",
53
+ value: function registerClient(client) {// does nothing, we don't need any client for this kind of link
54
+ }
55
+ }, {
56
+ key: "reset",
57
+ value: function () {
58
+ var _reset = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee() {
59
+ return _regenerator.default.wrap(function _callee$(_context) {
60
+ while (1) {
61
+ switch (_context.prev = _context.next) {
62
+ case 0:
63
+ case "end":
64
+ return _context.stop();
65
+ }
66
+ }
67
+ }, _callee);
68
+ }));
69
+
70
+ function reset() {
71
+ return _reset.apply(this, arguments);
72
+ }
73
+
74
+ return reset;
75
+ }()
76
+ }, {
77
+ key: "request",
78
+ value: function () {
79
+ var _request = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee2(operation, result, forward) {
80
+ return _regenerator.default.wrap(function _callee2$(_context2) {
81
+ while (1) {
82
+ switch (_context2.prev = _context2.next) {
83
+ case 0:
84
+ return _context2.abrupt("return", this.webviewIntent.call('flagshipLinkRequest', operation));
85
+
86
+ case 1:
87
+ case "end":
88
+ return _context2.stop();
89
+ }
90
+ }
91
+ }, _callee2, this);
92
+ }));
93
+
94
+ function request(_x, _x2, _x3) {
95
+ return _request.apply(this, arguments);
96
+ }
97
+
98
+ return request;
99
+ }()
100
+ }, {
101
+ key: "persistCozyData",
102
+ value: function () {
103
+ var _persistCozyData = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee3(data, forward) {
104
+ return _regenerator.default.wrap(function _callee3$(_context3) {
105
+ while (1) {
106
+ switch (_context3.prev = _context3.next) {
107
+ case 0:
108
+ return _context3.abrupt("return");
109
+
110
+ case 1:
111
+ case "end":
112
+ return _context3.stop();
113
+ }
114
+ }
115
+ }, _callee3);
116
+ }));
117
+
118
+ function persistCozyData(_x4, _x5) {
119
+ return _persistCozyData.apply(this, arguments);
120
+ }
121
+
122
+ return persistCozyData;
123
+ }()
124
+ }]);
125
+ return WebFlagshipLink;
126
+ }(_CozyLink2.default);
127
+
128
+ exports.default = WebFlagshipLink;
@@ -129,7 +129,7 @@ var HasManyFiles = /*#__PURE__*/function (_HasMany) {
129
129
  switch (_context.prev = _context.next) {
130
130
  case 0:
131
131
  lastRelDoc = (0, _store.getDocumentFromState)(getState(), lastRelationship._type, lastRelationship._id);
132
- lastDatetime = getFileDatetime(lastRelDoc.attributes); // cursor-based pagination
132
+ lastDatetime = getFileDatetime(lastRelDoc); // cursor-based pagination
133
133
 
134
134
  cursor = newCursor([_this.target._type, _this.target._id, lastDatetime], relationships[relationships.length - 1]._id);
135
135
  _context.next = 5;
@@ -7,7 +7,8 @@ import { SCHEMA, TODO_1, TODO_2 } from './__tests__/fixtures'
7
7
 
8
8
  describe('Associations', () => {
9
9
  const requestHandler = jest.fn()
10
- const link = new CozyLink(requestHandler)
10
+ const persistHandler = jest.fn()
11
+ const link = new CozyLink(requestHandler, persistHandler)
11
12
  const client = new CozyClient({ links: [link], schema: SCHEMA })
12
13
 
13
14
  const getTodo = id =>
@@ -30,9 +30,7 @@ describe('useAppLinkWithStoreFallback', () => {
30
30
  mockClient.query.mockResolvedValue({
31
31
  data: [
32
32
  {
33
- attributes: {
34
- slug: testAppSlug
35
- },
33
+ slug: testAppSlug,
36
34
  links: { related: 'http://testapp.cozy.io' }
37
35
  }
38
36
  ]
@@ -52,9 +50,7 @@ describe('useAppLinkWithStoreFallback', () => {
52
50
  mockClient.query.mockResolvedValue({
53
51
  data: [
54
52
  {
55
- attributes: {
56
- slug: 'store'
57
- },
53
+ slug: 'store',
58
54
  links: { related: 'http://store.cozy.io' }
59
55
  }
60
56
  ]
@@ -45,7 +45,7 @@ var useCapabilities = function useCapabilities(client) {
45
45
 
46
46
  case 4:
47
47
  capabilitiesResult = _context.sent;
48
- setCapabilities((0, _get.default)(capabilitiesResult, 'data.attributes', {}));
48
+ setCapabilities((0, _get.default)(capabilitiesResult, 'data', {}));
49
49
  setFetchStatus('loaded');
50
50
  _context.next = 12;
51
51
  break;
@@ -33,7 +33,8 @@ describe('useCapabilities', () => {
33
33
  data: {
34
34
  type: 'io.cozy.settings',
35
35
  id: 'io.cozy.settings.capabilities',
36
- attributes: { file_versioning: true, flat_subdomains: true },
36
+ file_versioning: true,
37
+ flat_subdomains: true,
37
38
  meta: {},
38
39
  links: { self: '/settings/capabilities' }
39
40
  }
@@ -44,8 +45,12 @@ describe('useCapabilities', () => {
44
45
 
45
46
  await waitForNextUpdate()
46
47
  expect(result.current.capabilities).toEqual({
48
+ type: 'io.cozy.settings',
49
+ id: 'io.cozy.settings.capabilities',
47
50
  file_versioning: true,
48
- flat_subdomains: true
51
+ flat_subdomains: true,
52
+ meta: {},
53
+ links: { self: '/settings/capabilities' }
49
54
  })
50
55
  })
51
56
 
@@ -40,7 +40,7 @@ var useFetchShortcut = function useFetchShortcut(client, id) {
40
40
  (0, _react.useEffect)(function () {
41
41
  var fetchData = /*#__PURE__*/function () {
42
42
  var _ref = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee() {
43
- var _shortcutInfosResult$, _shortcutInfosResult$2, _shortcutInfosResult$3, _shortcutInfosResult$4, shortcutInfosResult, targetApp, targetAppIconUrl, shortcutRemoteUrl, imgUrl;
43
+ var _shortcutInfosResult$, _shortcutInfosResult$2, _shortcutInfosResult$3, shortcutInfosResult, targetApp, targetAppIconUrl, shortcutRemoteUrl, imgUrl;
44
44
 
45
45
  return _regenerator.default.wrap(function _callee$(_context) {
46
46
  while (1) {
@@ -60,7 +60,7 @@ var useFetchShortcut = function useFetchShortcut(client, id) {
60
60
 
61
61
  case 4:
62
62
  shortcutInfosResult = _context.sent;
63
- targetApp = shortcutInfosResult === null || shortcutInfosResult === void 0 ? void 0 : (_shortcutInfosResult$ = shortcutInfosResult.data) === null || _shortcutInfosResult$ === void 0 ? void 0 : (_shortcutInfosResult$2 = _shortcutInfosResult$.attributes) === null || _shortcutInfosResult$2 === void 0 ? void 0 : (_shortcutInfosResult$3 = _shortcutInfosResult$2.metadata) === null || _shortcutInfosResult$3 === void 0 ? void 0 : (_shortcutInfosResult$4 = _shortcutInfosResult$3.target) === null || _shortcutInfosResult$4 === void 0 ? void 0 : _shortcutInfosResult$4.app;
63
+ targetApp = shortcutInfosResult === null || shortcutInfosResult === void 0 ? void 0 : (_shortcutInfosResult$ = shortcutInfosResult.data) === null || _shortcutInfosResult$ === void 0 ? void 0 : (_shortcutInfosResult$2 = _shortcutInfosResult$.metadata) === null || _shortcutInfosResult$2 === void 0 ? void 0 : (_shortcutInfosResult$3 = _shortcutInfosResult$2.target) === null || _shortcutInfosResult$3 === void 0 ? void 0 : _shortcutInfosResult$3.app;
64
64
 
65
65
  if (!targetApp) {
66
66
  _context.next = 13;
@@ -81,7 +81,7 @@ var useFetchShortcut = function useFetchShortcut(client, id) {
81
81
  break;
82
82
 
83
83
  case 13:
84
- shortcutRemoteUrl = new URL(shortcutInfosResult.data.attributes.url);
84
+ shortcutRemoteUrl = new URL(shortcutInfosResult.data.url);
85
85
  imgUrl = "".concat(client.getStackClient().uri, "/bitwarden/icons/").concat(shortcutRemoteUrl.host, "/icon.png");
86
86
  setShortcutImg(imgUrl);
87
87
 
@@ -16,13 +16,11 @@ describe('useFetchShortcut', () => {
16
16
  {
17
17
  type: 'io.cozy.files.shortcuts',
18
18
  id: 'b7470059d40c88e4bd30031d5e0109d3',
19
- attributes: {
20
- _id: '',
21
- name: 'cozy.url',
22
- dir_id: '8034db0016d0548ded99b9627e003270',
23
- url: 'https://cozy.io',
24
- metadata: { extractor_version: 2 }
25
- },
19
+ _id: 'b7470059d40c88e4bd30031d5e0109d3',
20
+ name: 'cozy.url',
21
+ dir_id: '8034db0016d0548ded99b9627e003270',
22
+ url: 'https://cozy.io',
23
+ metadata: { extractor_version: 2 },
26
24
  meta: { rev: '1-60e1359e63fa7fa9fa000a2726d5d4c7' }
27
25
  }
28
26
  ]
@@ -37,16 +35,14 @@ describe('useFetchShortcut', () => {
37
35
  {
38
36
  type: 'io.cozy.files.shortcuts',
39
37
  id: 'linkToCozyApp',
40
- attributes: {
41
- _id: '',
42
- name: 'cozy.url',
43
- dir_id: '8034db0016d0548ded99b9627e003270',
44
- url: 'https://cozy.io',
45
- metadata: {
46
- extractor_version: 2,
47
- target: {
48
- app: 'notes'
49
- }
38
+ _id: '',
39
+ name: 'cozy.url',
40
+ dir_id: '8034db0016d0548ded99b9627e003270',
41
+ url: 'https://cozy.io',
42
+ metadata: {
43
+ extractor_version: 2,
44
+ target: {
45
+ app: 'notes'
50
46
  }
51
47
  },
52
48
  meta: { rev: '1-60e1359e63fa7fa9fa000a2726d5d4c7' }
@@ -93,15 +89,12 @@ describe('useFetchShortcut', () => {
93
89
  data: {
94
90
  _id: 'b7470059d40c88e4bd30031d5e0109d3',
95
91
  _type: 'io.cozy.files.shortcuts',
96
- type: 'io.cozy.files.shortcuts',
97
92
  id: 'b7470059d40c88e4bd30031d5e0109d3',
98
- attributes: {
99
- _id: '',
100
- name: 'cozy.url',
101
- dir_id: '8034db0016d0548ded99b9627e003270',
102
- url: 'https://cozy.io',
103
- metadata: { extractor_version: 2 }
104
- },
93
+ type: 'io.cozy.files.shortcuts',
94
+ name: 'cozy.url',
95
+ dir_id: '8034db0016d0548ded99b9627e003270',
96
+ url: 'https://cozy.io',
97
+ metadata: { extractor_version: 2 },
105
98
  meta: { rev: '1-60e1359e63fa7fa9fa000a2726d5d4c7' }
106
99
  }
107
100
  })
package/dist/index.js CHANGED
@@ -10,6 +10,7 @@ Object.defineProperty(exports, "__esModule", {
10
10
  var _exportNames = {
11
11
  CozyLink: true,
12
12
  StackLink: true,
13
+ WebFlagshipLink: true,
13
14
  compose: true,
14
15
  QueryDefinition: true,
15
16
  Q: true,
@@ -77,6 +78,12 @@ Object.defineProperty(exports, "StackLink", {
77
78
  return _StackLink.default;
78
79
  }
79
80
  });
81
+ Object.defineProperty(exports, "WebFlagshipLink", {
82
+ enumerable: true,
83
+ get: function get() {
84
+ return _WebFlagshipLink.default;
85
+ }
86
+ });
80
87
  Object.defineProperty(exports, "compose", {
81
88
  enumerable: true,
82
89
  get: function get() {
@@ -361,6 +368,8 @@ var _CozyLink = _interopRequireDefault(require("./CozyLink"));
361
368
 
362
369
  var _StackLink = _interopRequireDefault(require("./StackLink"));
363
370
 
371
+ var _WebFlagshipLink = _interopRequireDefault(require("./WebFlagshipLink"));
372
+
364
373
  var _flow = _interopRequireDefault(require("lodash/flow"));
365
374
 
366
375
  var _dsl = require("./queries/dsl");
@@ -71,7 +71,7 @@ var isInstalled = function isInstalled() {
71
71
  var apps = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
72
72
  var wantedApp = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
73
73
  return apps.find(function (app) {
74
- return app.attributes && app.attributes.slug === wantedApp.slug;
74
+ return app.slug === wantedApp.slug;
75
75
  });
76
76
  };
77
77
  /**
@@ -94,9 +94,7 @@ describe('applications model', () => {
94
94
  describe('when the store app is installed', () => {
95
95
  it('should return the store url for the given app', () => {
96
96
  const storeApp = {
97
- attributes: {
98
- slug: 'store'
99
- },
97
+ slug: 'store',
100
98
  links: {
101
99
  related: 'http://store.cozy.tools:8080/'
102
100
  }
@@ -120,9 +118,7 @@ describe('applications model', () => {
120
118
  describe('when the store app is installed', () => {
121
119
  it('should return the store installation url for the given app', () => {
122
120
  const storeApp = {
123
- attributes: {
124
- slug: 'store'
125
- },
121
+ slug: 'store',
126
122
  links: {
127
123
  related: 'http://store.cozy.tools:8080/'
128
124
  }
@@ -8,7 +8,7 @@ Object.defineProperty(exports, "__esModule", {
8
8
  exports.normalize = normalize;
9
9
  exports.ensureFilePath = ensureFilePath;
10
10
  exports.getParentFolderId = getParentFolderId;
11
- exports.copy = exports.fetchBlobFileById = exports.isFromKonnector = exports.hasCertifications = exports.hasQualifications = exports.isPlainText = exports.doMobileUpload = exports.readMobileFile = exports.uploadFileWithConflictStrategy = exports.generateFileNameForRevision = exports.generateNewFileNameOnConflict = exports.overrideFileForPath = exports.move = exports.getFullpath = exports.hasMetadataAttribute = exports.fetchFilesByQualificationRules = exports.saveFileQualification = exports.isSharingShorcutNew = exports.isSharingShortcutNew = exports.isSharingShorcut = exports.isSharingShortcut = exports.getSharingShortcutTargetDoctype = exports.getSharingShortcutTargetMime = exports.getSharingShortcutStatus = exports.isShortcut = exports.shouldBeOpenedByOnlyOffice = exports.isOnlyOfficeFile = exports.isEncrypted = exports.isNote = exports.isDirectory = exports.isFile = exports.splitFilename = exports.ALBUMS_DOCTYPE = void 0;
11
+ exports.downloadFile = exports.copy = exports.fetchBlobFileById = exports.isFromKonnector = exports.hasCertifications = exports.hasQualifications = exports.isPlainText = exports.doMobileUpload = exports.readMobileFile = exports.uploadFileWithConflictStrategy = exports.generateFileNameForRevision = exports.generateNewFileNameOnConflict = exports.overrideFileForPath = exports.move = exports.getFullpath = exports.hasMetadataAttribute = exports.fetchFilesByQualificationRules = exports.saveFileQualification = exports.isSharingShorcutNew = exports.isSharingShortcutNew = exports.isSharingShorcut = exports.isSharingShortcut = exports.getSharingShortcutTargetDoctype = exports.getSharingShortcutTargetMime = exports.getSharingShortcutStatus = exports.isShortcut = exports.shouldBeOpenedByOnlyOffice = exports.isOnlyOfficeFile = exports.isEncrypted = exports.isNote = exports.isDirectory = exports.isFile = exports.splitFilename = exports.ALBUMS_DOCTYPE = void 0;
12
12
 
13
13
  var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
14
14
 
@@ -16,6 +16,8 @@ var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/
16
16
 
17
17
  var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
18
18
 
19
+ var _cozyDeviceHelper = require("cozy-device-helper");
20
+
19
21
  var _get = _interopRequireDefault(require("lodash/get"));
20
22
 
21
23
  var _isString = _interopRequireDefault(require("lodash/isString"));
@@ -1173,5 +1175,105 @@ var copy = /*#__PURE__*/function () {
1173
1175
  return _ref15.apply(this, arguments);
1174
1176
  };
1175
1177
  }();
1178
+ /**
1179
+ * Download the requested file
1180
+ *
1181
+ * This method can be used in a web page context or in a WebView hosted by a Flagship app
1182
+ *
1183
+ * When used in a FlagshipApp WebView context, then the action is redirected to the host app
1184
+ * that will process the download
1185
+ *
1186
+ * @param {object} params - The download parameters
1187
+ * @param {CozyClient} params.client - Instance of CozyClient
1188
+ * @param {import("../types").IOCozyFile} params.file - io.cozy.files metadata of the document to downloaded
1189
+ * @param {string} [params.url] - Blob url that should be used to download encrypted files
1190
+ * @param {import('cozy-intent').WebviewService} [params.webviewIntent] - webviewIntent that can be used to redirect the download to host Flagship app
1191
+ *
1192
+ * @returns {Promise<any>}
1193
+ */
1194
+
1195
+
1196
+ exports.copy = copy;
1197
+
1198
+ var downloadFile = /*#__PURE__*/function () {
1199
+ var _ref17 = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee14(_ref16) {
1200
+ var client, file, url, webviewIntent, filesCollection, _yield$webviewIntent$, isFlagshipDownloadAvailable;
1201
+
1202
+ return _regenerator.default.wrap(function _callee14$(_context14) {
1203
+ while (1) {
1204
+ switch (_context14.prev = _context14.next) {
1205
+ case 0:
1206
+ client = _ref16.client, file = _ref16.file, url = _ref16.url, webviewIntent = _ref16.webviewIntent;
1207
+ filesCollection = client.collection(_const.DOCTYPE_FILES);
1208
+
1209
+ if (!((0, _cozyDeviceHelper.isFlagshipApp)() && webviewIntent && !isEncrypted(file))) {
1210
+ _context14.next = 19;
1211
+ break;
1212
+ }
1213
+
1214
+ _context14.next = 5;
1215
+ return webviewIntent === null || webviewIntent === void 0 ? void 0 : webviewIntent.call('isAvailable', 'downloadFile');
1216
+
1217
+ case 5:
1218
+ _context14.t1 = _yield$webviewIntent$ = _context14.sent;
1219
+ _context14.t0 = _context14.t1 !== null;
1220
+
1221
+ if (!_context14.t0) {
1222
+ _context14.next = 9;
1223
+ break;
1224
+ }
1225
+
1226
+ _context14.t0 = _yield$webviewIntent$ !== void 0;
1227
+
1228
+ case 9:
1229
+ if (!_context14.t0) {
1230
+ _context14.next = 13;
1231
+ break;
1232
+ }
1233
+
1234
+ _context14.t2 = _yield$webviewIntent$;
1235
+ _context14.next = 14;
1236
+ break;
1237
+
1238
+ case 13:
1239
+ _context14.t2 = false;
1240
+
1241
+ case 14:
1242
+ isFlagshipDownloadAvailable = _context14.t2;
1243
+
1244
+ if (!isFlagshipDownloadAvailable) {
1245
+ _context14.next = 19;
1246
+ break;
1247
+ }
1248
+
1249
+ _context14.next = 18;
1250
+ return webviewIntent.call('downloadFile', file);
1251
+
1252
+ case 18:
1253
+ return _context14.abrupt("return", _context14.sent);
1254
+
1255
+ case 19:
1256
+ if (!isEncrypted(file)) {
1257
+ _context14.next = 21;
1258
+ break;
1259
+ }
1260
+
1261
+ return _context14.abrupt("return", filesCollection.forceFileDownload(url, file.name));
1262
+
1263
+ case 21:
1264
+ return _context14.abrupt("return", filesCollection.download(file));
1265
+
1266
+ case 22:
1267
+ case "end":
1268
+ return _context14.stop();
1269
+ }
1270
+ }
1271
+ }, _callee14);
1272
+ }));
1273
+
1274
+ return function downloadFile(_x30) {
1275
+ return _ref17.apply(this, arguments);
1276
+ };
1277
+ }();
1176
1278
 
1177
- exports.copy = copy;
1279
+ exports.downloadFile = downloadFile;
@@ -1,3 +1,5 @@
1
+ import { isFlagshipApp } from 'cozy-device-helper'
2
+
1
3
  import * as fileModel from './file'
2
4
  import { Qualification } from './document/qualification'
3
5
  import { QueryDefinition } from '../queries/dsl'
@@ -5,6 +7,9 @@ const CozyClient = require('cozy-client/dist/CozyClient').default
5
7
  const CozyStackClient = require('cozy-stack-client').default
6
8
 
7
9
  jest.mock('cozy-stack-client')
10
+ jest.mock('cozy-device-helper', () => ({
11
+ isFlagshipApp: jest.fn()
12
+ }))
8
13
 
9
14
  const cozyClient = new CozyClient({
10
15
  stackClient: new CozyStackClient()
@@ -19,6 +24,8 @@ const fetchFileContentByIdSpy = jest.fn().mockName('fetchFileContentById')
19
24
  const moveSpy = jest.fn().mockName('move')
20
25
  const moveToCozySpy = jest.fn().mockName('moveToCozy')
21
26
  const moveFromCozySpy = jest.fn().mockName('moveFromCozy')
27
+ const downloadFromCozySpy = jest.fn().mockName('downloadFromCozy')
28
+ const forceFileDownloadFromCozySpy = jest.fn().mockName('forceFileDownload')
22
29
 
23
30
  beforeAll(() => {
24
31
  cozyClient.stackClient.collection.mockReturnValue({
@@ -31,10 +38,16 @@ beforeAll(() => {
31
38
  fetchFileContentById: fetchFileContentByIdSpy,
32
39
  move: moveSpy,
33
40
  moveToCozy: moveToCozySpy,
34
- moveFromCozy: moveFromCozySpy
41
+ moveFromCozy: moveFromCozySpy,
42
+ download: downloadFromCozySpy,
43
+ forceFileDownload: forceFileDownloadFromCozySpy
35
44
  })
36
45
  })
37
46
 
47
+ beforeEach(() => {
48
+ jest.clearAllMocks()
49
+ })
50
+
38
51
  describe('File Model', () => {
39
52
  it('should test if a file is a note or not', () => {
40
53
  const fileDocument = {
@@ -856,3 +869,111 @@ describe('File qualification', () => {
856
869
  })
857
870
  })
858
871
  })
872
+
873
+ describe('downloadFile', () => {
874
+ it('should handle download in web page', async () => {
875
+ const file = {
876
+ _id: 'SOME_FILE_ID',
877
+ _type: 'io.cozy.file',
878
+ name: 'SOME_FILE_NAME'
879
+ }
880
+
881
+ await fileModel.downloadFile({
882
+ // @ts-ignore
883
+ client: cozyClient,
884
+ // @ts-ignore
885
+ file,
886
+ webviewIntent: null
887
+ })
888
+
889
+ expect(downloadFromCozySpy).toHaveBeenCalledWith(file)
890
+ })
891
+
892
+ it('should handle download in Flagship app', async () => {
893
+ isFlagshipApp.mockReturnValue(true)
894
+ const webviewIntent = {
895
+ call: jest.fn().mockResolvedValue(true)
896
+ }
897
+
898
+ const file = {
899
+ _id: 'SOME_FILE_ID',
900
+ _type: 'io.cozy.file',
901
+ name: 'SOME_FILE_NAME'
902
+ }
903
+
904
+ await fileModel.downloadFile({
905
+ // @ts-ignore
906
+ client: cozyClient,
907
+ // @ts-ignore
908
+ file,
909
+ // @ts-ignore
910
+ webviewIntent
911
+ })
912
+
913
+ expect(downloadFromCozySpy).not.toHaveBeenCalled()
914
+ expect(webviewIntent.call).toHaveBeenCalledWith(
915
+ 'isAvailable',
916
+ 'downloadFile'
917
+ )
918
+ expect(webviewIntent.call).toHaveBeenCalledWith('downloadFile', file)
919
+ })
920
+
921
+ it('should download files from web page in old Flagship app versions', async () => {
922
+ isFlagshipApp.mockReturnValue(true)
923
+ const webviewIntent = {
924
+ call: jest.fn().mockResolvedValue(false) // `isAvailable` returns `false` when not implemented
925
+ }
926
+
927
+ const file = {
928
+ _id: 'SOME_FILE_ID',
929
+ _type: 'io.cozy.file',
930
+ name: 'SOME_FILE_NAME'
931
+ }
932
+
933
+ await fileModel.downloadFile({
934
+ // @ts-ignore
935
+ client: cozyClient,
936
+ // @ts-ignore
937
+ file,
938
+ // @ts-ignore
939
+ webviewIntent
940
+ })
941
+
942
+ expect(downloadFromCozySpy).toHaveBeenCalled()
943
+ expect(webviewIntent.call).toHaveBeenCalledWith(
944
+ 'isAvailable',
945
+ 'downloadFile'
946
+ )
947
+ expect(webviewIntent.call).not.toHaveBeenCalledWith('downloadFile', file)
948
+ })
949
+
950
+ it('should download encrypted files from web page as this is not supported yet by Flagship app', async () => {
951
+ isFlagshipApp.mockReturnValue(true)
952
+ const webviewIntent = {
953
+ call: jest.fn()
954
+ }
955
+
956
+ const file = {
957
+ _id: 'SOME_FILE_ID',
958
+ _type: 'io.cozy.file',
959
+ name: 'SOME_FILE_NAME',
960
+ encrypted: true
961
+ }
962
+
963
+ await fileModel.downloadFile({
964
+ // @ts-ignore
965
+ client: cozyClient,
966
+ // @ts-ignore
967
+ file,
968
+ url: 'SOME_URL',
969
+ // @ts-ignore
970
+ webviewIntent
971
+ })
972
+
973
+ expect(forceFileDownloadFromCozySpy).toHaveBeenCalledWith(
974
+ 'SOME_URL',
975
+ 'SOME_FILE_NAME'
976
+ )
977
+ expect(webviewIntent.call).not.toHaveBeenCalled()
978
+ })
979
+ })