cozy-client 60.40.0 → 60.42.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.
@@ -3127,7 +3127,7 @@ var CozyClient = /*#__PURE__*/function () {
3127
3127
  CozyClient.hooks = CozyClient.hooks || {};
3128
3128
  CozyClient.fetchPolicies = _policies.default; //COZY_CLIENT_VERSION_PACKAGE in replaced by babel. See babel config
3129
3129
 
3130
- CozyClient.version = "60.40.0";
3130
+ CozyClient.version = "60.42.0";
3131
3131
 
3132
3132
  _microee.default.mixin(CozyClient);
3133
3133
 
@@ -2223,6 +2223,121 @@ describe('file creation', () => {
2223
2223
  })
2224
2224
  )
2225
2225
  })
2226
+
2227
+ it('should create a file from sourceURL via real StackLink/FileCollection HTTP boundary and update store cache', async () => {
2228
+ const { client } = setup()
2229
+ jest.spyOn(client, 'dispatch')
2230
+ client.stackClient.fetchJSON = jest.fn().mockResolvedValue({
2231
+ data: {
2232
+ id: 'file-42',
2233
+ _id: 'file-42',
2234
+ type: 'io.cozy.files',
2235
+ attributes: {
2236
+ name: 'remote.pdf',
2237
+ dir_id: 'folder-123'
2238
+ },
2239
+ meta: {
2240
+ rev: '1-xyz'
2241
+ }
2242
+ }
2243
+ })
2244
+
2245
+ const { data: doc } = await client.create('io.cozy.files', {
2246
+ type: 'file',
2247
+ sourceURL:
2248
+ 'https://example.com/remote.pdf?token=abc+123&secret=xyz%20foo',
2249
+ dirId: 'folder-123',
2250
+ name: 'remote.pdf'
2251
+ })
2252
+
2253
+ expect(doc._id).toEqual('file-42')
2254
+ expect(doc.name).toEqual('remote.pdf')
2255
+ expect(doc.dir_id).toEqual('folder-123')
2256
+ expect(client.stackClient.fetchJSON).toHaveBeenCalledWith(
2257
+ 'POST',
2258
+ '/files/folder-123?Name=remote.pdf&Type=file&Executable=false&Encrypted=false&MetadataID=&Size=&SourceAccount=&SourceAccountIdentifier=&SourceURL=https%3A%2F%2Fexample.com%2Fremote.pdf%3Ftoken%3Dabc%2B123%26secret%3Dxyz%2520foo',
2259
+ '',
2260
+ {
2261
+ headers: {
2262
+ 'Content-Type': 'application/octet-stream'
2263
+ }
2264
+ }
2265
+ )
2266
+ expect(client.dispatch.mock.calls.map(x => x[0].type)).toEqual([
2267
+ 'INIT_MUTATION',
2268
+ 'RECEIVE_MUTATION_RESULT'
2269
+ ])
2270
+
2271
+ const cachedDoc = client.getDocumentFromState('io.cozy.files', 'file-42')
2272
+ expect(cachedDoc).toBeDefined()
2273
+ expect(cachedDoc._id).toBe('file-42')
2274
+ expect(cachedDoc.name).toBe('remote.pdf')
2275
+ expect(cachedDoc.dir_id).toBe('folder-123')
2276
+ })
2277
+
2278
+ it('should reject file creation when both binary data and sourceURL are provided', async () => {
2279
+ const { client } = setup()
2280
+ jest.spyOn(client, 'dispatch')
2281
+ await expect(
2282
+ client.create('io.cozy.files', {
2283
+ type: 'file',
2284
+ data: 'content',
2285
+ sourceURL: 'https://example.com/remote.pdf',
2286
+ name: 'remote.pdf',
2287
+ dirId: 'folder-123'
2288
+ })
2289
+ ).rejects.toThrow('Cannot pass both data and sourceURL')
2290
+
2291
+ expect(client.dispatch.mock.calls.map(x => x[0].type)).toEqual([
2292
+ 'INIT_MUTATION',
2293
+ 'RECEIVE_MUTATION_ERROR'
2294
+ ])
2295
+ })
2296
+
2297
+ it('should reject file creation when sourceURL is empty', async () => {
2298
+ const { client } = setup()
2299
+ await expect(
2300
+ client.create('io.cozy.files', {
2301
+ type: 'file',
2302
+ sourceURL: '',
2303
+ name: 'remote.pdf',
2304
+ dirId: 'folder-123'
2305
+ })
2306
+ ).rejects.toThrow('sourceURL cannot be empty')
2307
+ })
2308
+
2309
+ it('should reject directory creation when sourceURL is provided', async () => {
2310
+ const { client } = setup()
2311
+ await expect(
2312
+ client.create('io.cozy.files', {
2313
+ type: 'directory',
2314
+ sourceURL: '',
2315
+ name: 'my-folder'
2316
+ })
2317
+ ).rejects.toThrow('You cannot pass a sourceURL for a directory')
2318
+ })
2319
+
2320
+ it('should dispatch mutation error and throw when stack HTTP call fails', async () => {
2321
+ const { client } = setup()
2322
+ jest.spyOn(client, 'dispatch')
2323
+ client.stackClient.fetchJSON.mockRejectedValueOnce(
2324
+ new Error('502 could not download SourceURL')
2325
+ )
2326
+
2327
+ await expect(
2328
+ client.create('io.cozy.files', {
2329
+ type: 'file',
2330
+ sourceURL: 'https://example.com/remote.pdf',
2331
+ name: 'remote.pdf',
2332
+ dirId: 'folder-123'
2333
+ })
2334
+ ).rejects.toThrow('502 could not download SourceURL')
2335
+
2336
+ expect(client.dispatch.mock.calls.map(x => x[0].type)).toEqual([
2337
+ 'INIT_MUTATION',
2338
+ 'RECEIVE_MUTATION_ERROR'
2339
+ ])
2340
+ })
2226
2341
  })
2227
2342
 
2228
2343
  describe('file update', () => {
@@ -1,4 +1,4 @@
1
- import { Q } from '../queries/dsl'
1
+ import { Q, MutationTypes } from '../queries/dsl'
2
2
  import CozyClient from '../CozyClient'
3
3
  import StackLink, { transformBulkDocsResponse } from './StackLink'
4
4
  import { SCHEMA } from '../__tests__/fixtures'
@@ -283,4 +283,29 @@ describe('StackLink', () => {
283
283
  })
284
284
  })
285
285
  })
286
+
287
+ describe('mutations', () => {
288
+ it('should delegate CREATE_DOCUMENT with sourceURL to collection.create without modifying or dropping sourceURL', async () => {
289
+ const doc = {
290
+ _type: 'io.cozy.files',
291
+ type: 'file',
292
+ name: 'remote.pdf',
293
+ dirId: '123',
294
+ sourceURL: 'https://example.com/file.pdf'
295
+ }
296
+ const fakeCollection = {
297
+ create: jest.fn().mockResolvedValue({ data: { id: 'created-id' } })
298
+ }
299
+ stackClient.collection = jest.fn().mockReturnValue(fakeCollection)
300
+
301
+ const result = await link.executeMutation({
302
+ mutationType: MutationTypes.CREATE_DOCUMENT,
303
+ document: doc
304
+ })
305
+
306
+ expect(stackClient.collection).toHaveBeenCalledWith('io.cozy.files')
307
+ expect(fakeCollection.create).toHaveBeenCalledWith(doc)
308
+ expect(result).toEqual({ data: { id: 'created-id' } })
309
+ })
310
+ })
286
311
  })
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cozy-client",
3
- "version": "60.40.0",
3
+ "version": "60.42.0",
4
4
  "license": "MIT",
5
5
  "main": "dist/index.node.js",
6
6
  "browser": "dist/index.js",
@@ -19,7 +19,7 @@
19
19
  "@types/jest": "^26.0.20",
20
20
  "@types/lodash": "^4.14.170",
21
21
  "btoa": "^1.2.1",
22
- "cozy-stack-client": "^60.37.0",
22
+ "cozy-stack-client": "^60.42.0",
23
23
  "date-fns": "^2.30.0",
24
24
  "fast-deep-equal": "^3.1.3",
25
25
  "json-stable-stringify": "^1.0.1",
@@ -74,5 +74,5 @@
74
74
  "react-native-ios11-devicecheck": "^0.0.3"
75
75
  },
76
76
  "sideEffects": false,
77
- "gitHead": "3735e1a5f655886b47d13e7ceb6bc9c180cdfddc"
77
+ "gitHead": "49d3d0389e275bff916ca8ffa510458aac3f866c"
78
78
  }