5
5
import should from 'should'
6
6
import 'isomorphic-fetch'
7
7
import { Readable } from 'stream'
8
- import { Cozy } from '../../src'
8
+ import { Client } from '../../src'
9
9
import { randomGenerator } from '../helpers'
10
10
11
11
const COZY_STACK_URL = process . env && process . env . COZY_STACK_URL || ''
@@ -14,13 +14,13 @@ const COZY_STACK_TOKEN = process.env && process.env.COZY_STACK_TOKEN
14
14
15
15
describe ( 'files API' , async function ( ) {
16
16
let random
17
- let cozy
17
+ const cozy = { }
18
18
19
19
beforeEach ( function ( ) {
20
20
if ( COZY_STACK_VERSION === '2' ) {
21
21
this . skip ( )
22
22
}
23
- cozy = new Cozy ( {
23
+ cozy . client = new Client ( {
24
24
cozyURL : COZY_STACK_URL ,
25
25
token : COZY_STACK_TOKEN
26
26
} )
@@ -31,7 +31,7 @@ describe('files API', async function () {
31
31
const filename = 'foo_' + random ( )
32
32
const date = new Date ( 'Wed, 01 Feb 2017 10:24:42 GMT' )
33
33
34
- const created = await cozy . files . create ( 'datastring1' , {
34
+ const created = await cozy . client . files . create ( 'datastring1' , {
35
35
name : filename ,
36
36
contentType : 'application/json' ,
37
37
lastModifiedDate : date
@@ -50,7 +50,7 @@ describe('files API', async function () {
50
50
stream . push ( 'datastring1' )
51
51
stream . push ( null )
52
52
53
- const created = await cozy . files . create ( stream , {
53
+ const created = await cozy . client . files . create ( stream , {
54
54
name : filename ,
55
55
contentType : 'application/json'
56
56
} )
@@ -62,34 +62,34 @@ describe('files API', async function () {
62
62
it ( 'updates a file' , async function ( ) {
63
63
const filename = 'foo_' + random ( )
64
64
65
- const created = await cozy . files . create ( 'datastring1' , { name : filename } )
65
+ const created = await cozy . client . files . create ( 'datastring1' , { name : filename } )
66
66
created . should . have . property ( 'attributes' )
67
67
68
68
const createdId = created . _id
69
69
70
- const updated = await cozy . files . updateById ( createdId , 'datastring2' )
70
+ const updated = await cozy . client . files . updateById ( createdId , 'datastring2' )
71
71
updated . should . have . property ( 'attributes' )
72
72
updated . attributes . md5sum . should . equal ( 'iWpp8tcTP/DWTJSLf0hoyQ==' )
73
73
} )
74
74
75
75
it ( 'updates attributes' , async function ( ) {
76
76
const filename = 'foo_' + random ( )
77
77
78
- const created = await cozy . files . create ( 'datastring1' , { name : filename } )
78
+ const created = await cozy . client . files . create ( 'datastring1' , { name : filename } )
79
79
created . should . have . property ( 'attributes' )
80
80
81
81
const createdId = created . _id
82
82
83
83
const newname1 = 'newname1_' + random ( )
84
84
const attrs1 = { tags : [ 'foo' , 'bar' ] , name : newname1 }
85
- const updated1 = await cozy . files . updateAttributesById ( createdId , attrs1 )
85
+ const updated1 = await cozy . client . files . updateAttributesById ( createdId , attrs1 )
86
86
updated1 . should . have . property ( 'attributes' )
87
87
updated1 . attributes . name . should . startWith ( 'newname1_' )
88
88
updated1 . attributes . tags . should . eql ( [ 'foo' , 'bar' ] )
89
89
90
90
const newname2 = 'newname2_' + random ( )
91
91
const attrs2 = { tags : [ 'foo' ] , name : newname2 }
92
- const updated2 = await cozy . files . updateAttributesByPath ( '/' + newname1 , attrs2 )
92
+ const updated2 = await cozy . client . files . updateAttributesByPath ( '/' + newname1 , attrs2 )
93
93
updated2 . should . have . property ( 'attributes' )
94
94
updated2 . attributes . name . should . startWith ( 'newname2_' )
95
95
updated2 . attributes . tags . should . eql ( [ 'foo' ] )
@@ -99,7 +99,7 @@ describe('files API', async function () {
99
99
const dirname = 'foo_' + random ( )
100
100
const date = new Date ( 'Wed, 01 Feb 2017 10:24:42 GMT' )
101
101
102
- const created = await cozy . files . createDirectory ( { name : dirname , lastModifiedDate : date } )
102
+ const created = await cozy . client . files . createDirectory ( { name : dirname , lastModifiedDate : date } )
103
103
created . should . have . property ( 'attributes' )
104
104
new Date ( created . attributes . created_at ) . should . eql ( date )
105
105
new Date ( created . attributes . updated_at ) . should . eql ( date )
@@ -108,66 +108,66 @@ describe('files API', async function () {
108
108
it ( 'gets directory info by ID' , async function ( ) {
109
109
const dirname = 'foo_' + random ( )
110
110
111
- const created = await cozy . files . createDirectory ( { name : dirname } )
111
+ const created = await cozy . client . files . createDirectory ( { name : dirname } )
112
112
let directoryID = created . _id
113
113
114
- const stats = await cozy . files . statById ( directoryID )
114
+ const stats = await cozy . client . files . statById ( directoryID )
115
115
stats . should . have . property ( 'attributes' )
116
116
} )
117
117
118
118
it ( 'gets directory info by Path' , async function ( ) {
119
119
const dirname = 'foo_' + random ( )
120
120
121
- const created = await cozy . files . createDirectory ( { name : dirname } )
121
+ const created = await cozy . client . files . createDirectory ( { name : dirname } )
122
122
123
- const stats = await cozy . files . statByPath ( '/' + dirname )
123
+ const stats = await cozy . client . files . statByPath ( '/' + dirname )
124
124
stats . should . have . property ( 'attributes' )
125
125
stats . should . have . property ( '_id' , created . _id )
126
126
} )
127
127
128
128
it ( 'trashes file or directory' , async function ( ) {
129
129
const dirname = 'foo_' + random ( )
130
130
131
- const created = await cozy . files . createDirectory ( { name : dirname } )
131
+ const created = await cozy . client . files . createDirectory ( { name : dirname } )
132
132
const createdId = created . _id
133
133
134
- await cozy . files . trashById ( createdId )
134
+ await cozy . client . files . trashById ( createdId )
135
135
} )
136
136
137
137
it ( 'downloads a file by path and id' , async function ( ) {
138
138
const filename = 'foo_' + random ( )
139
139
140
- const created = await cozy . files . create ( 'foo' , {
140
+ const created = await cozy . client . files . create ( 'foo' , {
141
141
name : filename ,
142
142
contentType : 'application/json'
143
143
} )
144
144
145
- const downloaded1 = await cozy . files . downloadById ( created . _id )
145
+ const downloaded1 = await cozy . client . files . downloadById ( created . _id )
146
146
const txt1 = await downloaded1 . text ( )
147
147
148
148
txt1 . should . equal ( 'foo' )
149
149
150
- const downloaded2 = await cozy . files . downloadByPath ( '/' + filename )
150
+ const downloaded2 = await cozy . client . files . downloadByPath ( '/' + filename )
151
151
const txt2 = await downloaded2 . text ( )
152
152
153
153
txt2 . should . equal ( 'foo' )
154
154
} )
155
155
156
156
it ( 'destroy all trashed files and directories' , async function ( ) {
157
- await createTrashedDirectory ( cozy , 'foo_' + random ( ) )
158
- await createTrashedDirectory ( cozy , 'foo_' + random ( ) )
159
- await cozy . files . clearTrash ( )
157
+ await createTrashedDirectory ( cozy . client , 'foo_' + random ( ) )
158
+ await createTrashedDirectory ( cozy . client , 'foo_' + random ( ) )
159
+ await cozy . client . files . clearTrash ( )
160
160
161
- let trashed = await cozy . files . listTrash ( )
161
+ let trashed = await cozy . client . files . listTrash ( )
162
162
trashed . should . be . an . Array ( )
163
163
trashed . should . have . length ( 0 )
164
164
} )
165
165
166
166
it ( 'list trashed files and directories' , async function ( ) {
167
- await cozy . files . clearTrash ( )
168
- const created1 = await createTrashedDirectory ( cozy , 'foo_' + random ( ) )
169
- const created2 = await createTrashedDirectory ( cozy , 'foo_' + random ( ) )
170
- let trashed = await cozy . files . listTrash ( )
167
+ await cozy . client . files . clearTrash ( )
168
+ const created1 = await createTrashedDirectory ( cozy . client , 'foo_' + random ( ) )
169
+ const created2 = await createTrashedDirectory ( cozy . client , 'foo_' + random ( ) )
170
+ let trashed = await cozy . client . files . listTrash ( )
171
171
trashed . should . be . an . Array ( )
172
172
trashed . should . have . length ( 2 )
173
173
let found1 = false
@@ -182,50 +182,50 @@ describe('files API', async function () {
182
182
} )
183
183
184
184
it ( 'restore a trashed file or directory' , async function ( ) {
185
- await cozy . files . clearTrash ( )
186
- const created = await createTrashedDirectory ( cozy , 'foo_' + random ( ) )
187
- await cozy . files . restoreById ( created . _id )
185
+ await cozy . client . files . clearTrash ( )
186
+ const created = await createTrashedDirectory ( cozy . client , 'foo_' + random ( ) )
187
+ await cozy . client . files . restoreById ( created . _id )
188
188
} )
189
189
190
190
it ( 'destroy a trashed file or directory' , async function ( ) {
191
- await cozy . files . clearTrash ( )
192
- const created = await createTrashedDirectory ( cozy , 'foo_' + random ( ) )
193
- await cozy . files . destroyById ( created . _id )
194
- let trashed = await cozy . files . listTrash ( )
191
+ await cozy . client . files . clearTrash ( )
192
+ const created = await createTrashedDirectory ( cozy . client , 'foo_' + random ( ) )
193
+ await cozy . client . files . destroyById ( created . _id )
194
+ let trashed = await cozy . client . files . listTrash ( )
195
195
trashed . should . be . an . Array ( )
196
196
trashed . should . have . length ( 0 )
197
197
} )
198
198
199
199
it ( 'creates download link for 1 file' , async function ( ) {
200
200
const filename = 'foo_' + random ( )
201
- const created = await cozy . files . create ( 'foo' , {
201
+ const created = await cozy . client . files . create ( 'foo' , {
202
202
name : filename ,
203
203
contentType : 'application/json'
204
204
} )
205
205
const path = '/' + created . attributes . name
206
- let link = await cozy . files . getDowloadLink ( path )
206
+ let link = await cozy . client . files . getDowloadLink ( path )
207
207
let downloaded = await fetch ( COZY_STACK_URL + link )
208
208
const txt1 = await downloaded . text ( )
209
209
txt1 . should . equal ( 'foo' )
210
210
} )
211
211
212
212
it ( 'creates download link for archive' , async function ( ) {
213
213
const filename = 'foo_' + random ( )
214
- const created = await cozy . files . create ( 'foo' , {
214
+ const created = await cozy . client . files . create ( 'foo' , {
215
215
name : filename ,
216
216
contentType : 'application/json'
217
217
} )
218
218
219
219
const filename2 = 'bar_' + random ( )
220
- const created2 = await cozy . files . create ( 'bar' , {
220
+ const created2 = await cozy . client . files . create ( 'bar' , {
221
221
name : filename2 ,
222
222
contentType : 'application/json'
223
223
} )
224
224
const toDownload = [
225
225
'/' + created . attributes . name ,
226
226
'/' + created2 . attributes . name
227
227
]
228
- let link = await cozy . files . getArchiveLink ( toDownload , 'foobar' )
228
+ let link = await cozy . client . files . getArchiveLink ( toDownload , 'foobar' )
229
229
let downloaded = await fetch ( COZY_STACK_URL + link )
230
230
downloaded . ok . should . be . true
231
231
downloaded . headers . get ( 'Content-Type' ) . should . equal ( 'application/zip' )
@@ -235,21 +235,21 @@ describe('files API', async function () {
235
235
236
236
describe ( 'offline' , async ( ) => {
237
237
beforeEach ( ( ) => {
238
- cozy = new Cozy ( {
238
+ cozy . client = new Client ( {
239
239
cozyURL : COZY_STACK_URL ,
240
240
token : COZY_STACK_TOKEN ,
241
241
offline : { doctypes : [ 'io.cozy.files' ] , options : { adapter : 'memory' } }
242
242
} )
243
243
} )
244
244
afterEach ( ( ) => {
245
- cozy . offline . destroyDatabase ( 'io.cozy.files' )
245
+ cozy . client . offline . destroyDatabase ( 'io.cozy.files' )
246
246
} )
247
247
248
248
it ( 'should be same document offline/online' , async ( ) => {
249
- const folder = await createRandomDirectory ( cozy )
250
- await cozy . offline . replicateFromCozy ( 'io.cozy.files' )
251
- const offline = await cozy . files . statById ( folder . _id )
252
- const online = await cozy . files . statById ( folder . _id , false )
249
+ const folder = await createRandomDirectory ( cozy . client )
250
+ await cozy . client . offline . replicateFromCozy ( 'io.cozy.files' )
251
+ const offline = await cozy . client . files . statById ( folder . _id )
252
+ const online = await cozy . client . files . statById ( folder . _id , false )
253
253
Object . keys ( online ) . forEach ( key => { key === 'links' || offline . should . have . keys ( key ) } )
254
254
Object . keys ( offline ) . forEach ( key => { online . should . have . keys ( key ) } )
255
255
Object . keys ( online . attributes ) . forEach ( key => { offline . attributes . should . have . keys ( key ) } )
@@ -258,14 +258,14 @@ describe('files API', async function () {
258
258
} )
259
259
} )
260
260
261
- async function createTrashedDirectory ( cozy , dirname ) {
262
- const created = await cozy . files . createDirectory ( { name : dirname } )
263
- await cozy . files . trashById ( created . _id )
261
+ async function createTrashedDirectory ( client , dirname ) {
262
+ const created = await client . files . createDirectory ( { name : dirname } )
263
+ await client . files . trashById ( created . _id )
264
264
return created
265
265
}
266
266
267
- async function createRandomDirectory ( cozy ) {
267
+ async function createRandomDirectory ( client ) {
268
268
const dirname = 'foo_' + randomGenerator ( ) ( )
269
- const created = await cozy . files . createDirectory ( { name : dirname } )
269
+ const created = await client . files . createDirectory ( { name : dirname } )
270
270
return created
271
271
}
0 commit comments