This repository has been archived by the owner on Oct 28, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
tests.coffee
922 lines (708 loc) · 27.5 KB
/
tests.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
fs = require "fs"
should = require 'should'
async = require 'async'
http = require 'http'
Client = require("request-json").JsonClient
Schema = require('jugglingdb').Schema
CozyAdapter = require('./src/cozy_data_system')
client = new Client "http://localhost:9101/"
schema = new Schema 'memory'
schema.settings = {}
process.env.NAME = "test"
process.env.TOKEN = "token"
indexerPort = if process.env.NODE_ENV is 'test' then 9092 else 9102
require("./src/cozy_data_system").initialize(schema)
client.setBasicAuth "test", "token"
Note = schema.define 'Note',
title:
type: String
content:
type: Schema.Text
author:
type: String
MailBox = schema.define 'MailBox',
name:
type: String
describe "Create application with all permissions", ->
it "When I create application", (done) ->
data =
name: "test"
slug: "test"
docType: "Application"
password: "token"
permissions:
"All":
description: "..."
client.setBasicAuth "home", "token"
client.post 'data/', data, (error, response, body) =>
@response = response
@error = error
done()
it "Then no error should be returned", ->
should.not.exist @error
it "And 201 should be return as response code", ->
@response.statusCode.should.equal 201
describe "Existence", ->
before (done) ->
client.del "data/321/", (error, response, body) ->
client.setBasicAuth "test", "token"
data =
value: "created value"
docType: "Note"
client.post 'data/321/', data, (error, response, body) ->
done()
after (done) ->
client.del "data/321/", (error, response, body) ->
done()
describe "Check Existence of a Document that does not exist in database", ->
it "When I check existence of Document with id 123", \
(done) ->
Note.exists 123, (err, isExist) =>
should.not.exist err
@isExist = isExist
done()
it "Then false should be returned", ->
@isExist.should.not.be.ok
describe "Check Existence of a Document that does exist in database", ->
it "When I check existence of Document with id 321", \
(done) ->
Note.exists 321, (err, isExist) =>
should.not.exist err
@isExist = isExist
done()
it "Then true should be returned", ->
@isExist.should.be.ok
describe "Find", ->
before (done) ->
client.post 'data/321/',
title: "my note"
content: "my content"
docType: "Note"
, (error, response, body) ->
done()
after (done) ->
client.del "data/321/", (error, response, body) ->
done()
describe "Find a note that does not exist in database", ->
it "When I claim note with id 123", (done) ->
Note.find 123, (err, note) =>
@note = note
done()
it "Then null should be returned", ->
should.not.exist @note
describe "Find a note that does exist in database", ->
it "When I claim note with id 321", (done) ->
Note.find 321, (err, note) =>
@note = note
done()
it "Then I should retrieve my note ", ->
should.exist @note
@note.title.should.equal "my note"
@note.content.should.equal "my content"
describe "Create", ->
before (done) ->
client.post 'data/321/', {
title: "my note"
content: "my content"
docType: "Note"
} , (error, response, body) ->
done()
after (done) ->
client.del "data/321/", (error, response, body) ->
client.del "data/987/", (error, response, body) ->
done()
describe "Try to create a Document existing in Database", ->
after ->
@err = null
@note = null
it "When create a document with id 321", (done) ->
Note.create { id: "321", "content":"created value"}, \
(err, note) =>
@err = err
@note = note
done()
it "Then an error is returned", ->
should.exist @err
describe "Create a new Document with a given id", ->
before ->
@id = "987"
after ->
@err = null
@note = null
it "When I create a document with id 987", (done) ->
Note.create { id: @id, "content": "new note" }, (err, note) =>
@err = err
@note = note
done()
it "Then this should be set on document", ->
should.not.exist @err
should.exist @note
@note.id.should.equal @id
it "And the Document with id 987 should exist in Database", (done) ->
Note.exists @id, (err, isExist) =>
should.not.exist err
isExist.should.be.ok
done()
it "And the Document in DB should equal the sent Document", (done) ->
Note.find @id, (err, note) =>
should.not.exist err
note.id.should.equal @id
note.content.should.equal "new note"
done()
describe "Create a new Document without an id", ->
before ->
@id = null
after (done) ->
@note.destroy =>
@err = null
@note = null
done()
it "When I create a document without an id", (done) ->
Note.create { "title": "cool note", "content": "new note" }, \
(err, note) =>
@err = err if err
@note = note
done()
it "Then the id of the new Document should be returned", ->
should.not.exist @err
should.exist @note.id
@id = @note.id
it "And the Document should exist in Database", (done) ->
Note.exists @id, (err, isExist) =>
should.not.exist err
isExist.should.be.ok
done()
it "And the Document in DB should equal the sent Document", (done) ->
Note.find @id, (err, note) =>
should.not.exist err
note.id.should.equal @id
note.content.should.equal "new note"
done()
describe "Create a many documents without an id", ->
before ->
@ids = []
after (done) ->
@note1.destroy =>
@note2.destroy =>
@note3.destroy =>
done()
it "When I create a document without an id", (done) ->
dataList = [
{ "title": "cool note many 01", "content": "new note 01" }
{ "title": "cool note many 02", "content": "new note 02" }
{ "title": "cool note many 03", "content": "new note 03" }
]
Note.createMany dataList, (err, ids) =>
@err = err if err
@ids = ids
done()
it "Then the ids of the new documents should be returned", ->
should.not.exist @err
should.exist @ids
@ids.length.should.equal 3
it "And the Document should exist in Database", (done) ->
Note.exists @ids[0], (err, isExist) =>
should.not.exist err
isExist.should.be.ok
Note.exists @ids[1], (err, isExist) =>
should.not.exist err
isExist.should.be.ok
Note.exists @ids[2], (err, isExist) =>
should.not.exist err
isExist.should.be.ok
done()
it "And the documents in DB should equal the sent document", (done) ->
Note.find @ids[0], (err, note) =>
should.not.exist err
note.id.should.equal @ids[0]
note.content.should.equal "new note 01"
@note1 = note
Note.find @ids[1], (err, note) =>
should.not.exist err
note.id.should.equal @ids[1]
note.content.should.equal "new note 02"
@note2 = note
Note.find @ids[2], (err, note) =>
should.not.exist err
note.id.should.equal @ids[2]
note.content.should.equal "new note 03"
@note3 = note
done()
describe "Update", ->
before (done) ->
data =
title: "my note"
content: "my content"
docType: "Note"
client.post 'data/321/', data, (error, response, body) ->
done()
@note = new Note data
after (done) ->
client.del "data/321/", (error, response, body) ->
done()
describe "Try to Update a Document that doesn't exist", ->
after ->
@err = null
it "When I update a document with id 123", (done) ->
@note.id = "123"
@note.save (err) =>
@err = err
done()
it "Then an error is returned", ->
should.exist @err
describe "Update a Document", ->
it "When I update document with id 321", (done) ->
@note.id = "321"
@note.title = "my new title"
@note.save (err) =>
@err = err
done()
it "Then no error is returned", ->
should.not.exist @err
it "And the old document must have been replaced in DB", (done) ->
Note.find @note.id, (err, updatedNote) =>
should.not.exist err
updatedNote.id.should.equal "321"
updatedNote.title.should.equal "my new title"
done()
describe "Update attributes", ->
before (done) ->
data =
title: "my note"
content: "my content"
docType: "Note"
client.post 'data/321/', data, (error, response, body) ->
done()
@note = new Note data
after (done) ->
client.del "data/321/", (error, response, body) ->
done()
describe "Try to update attributes of a document that doesn't exist", ->
after ->
@err = null
it "When I update a document with id 123", (done) ->
@note.updateAttributes title: "my new title", (err) =>
@err = err
done()
it "Then an error is returned", ->
should.exist @err
describe "Update a Document", ->
it "When I update document with id 321", (done) ->
@note.id = "321"
@note.updateAttributes title: "my new title", (err) =>
@err = err
done()
it "Then no error is returned", ->
should.not.exist @err
it "And the old document must have been replaced in DB", (done) ->
Note.find @note.id, (err, updatedNote) =>
should.not.exist err
updatedNote.id.should.equal "321"
updatedNote.title.should.equal "my new title"
done()
describe "Upsert attributes", ->
after (done) ->
client.del "data/654/", (error, response, body) ->
done()
describe "Upsert a non existing document", ->
it "When I upsert document with id 654", (done) ->
@data =
id: "654"
title: "my note"
content: "my content"
Note.updateOrCreate @data, (err) =>
@err = err
done()
it "Then no error should be returned.", ->
should.not.exist @err
it "And the document with id 654 should exist in Database", (done) ->
Note.find @data.id, (err, updatedNote) =>
should.not.exist err
updatedNote.id.should.equal "654"
updatedNote.title.should.equal "my note"
done()
describe "Upsert an existing Document", ->
it "When I upsert document with id 654", (done) ->
@data =
id: "654"
title: "my new title"
Note.updateOrCreate @data, (err, note) =>
should.not.exist note
@err = err
done()
it "Then no data should be returned", ->
should.not.exist @err
it "And the document with id 654 should be updated", (done) ->
Note.find @data.id, (err, updatedNote) =>
should.not.exist err
updatedNote.id.should.equal "654"
updatedNote.title.should.equal "my new title"
done()
describe "Delete", ->
before (done) ->
client.post 'data/321/', {
title: "my note"
content: "my content"
docType: "Note"
} , (error, response, body) ->
done()
after (done) ->
client.del "data/321/", (error, response, body) ->
client.del "data/987/", (error, response, body) ->
done()
describe "Deletes a document that is not in Database", ->
it "When I delete Document with id 123", (done) ->
note = new Note id:123
note.destroy (err) =>
@err = err
done()
it "Then an error should be returned", ->
should.exist @err
describe "Deletes a document from database", ->
it "When I delete document with id 321", (done) ->
note = new Note id:321
note.destroy (err) =>
@err = err
done()
it "Then no error is returned", ->
should.not.exist @err
it "And Document with id 321 shouldn't exist in Database", (done) ->
Note.exists 321, (err, isExist) =>
isExist.should.not.be.ok
done()
### Indexation ###
ids = []
dragonNoteId = "0"
createNoteFunction = (title, content, author) ->
(callback) ->
if author? then authorId = author.id else authorId = null
data =
title: title
content: content
author: authorId
Note.create data, (err, note) ->
ids.push note.id
dragonNoteId = note.id if title is "Note 02"
note.index ["title", "content"], (err) ->
callback()
createTaskFunction = (description) ->
(callback) ->
client.post 'data/', {
description: "description"
docType: "task"
} , (error, response, body) ->
callback()
fakeServer = (json, code=200, callback=null) ->
http.createServer (req, res) ->
body = ""
req.on 'data', (chunk) ->
body += chunk
req.on 'end', ->
if callback?
data = JSON.parse body if body? and body.length > 0
code = callback req.url, data
res.writeHead code, 'Content-Type': 'application/json'
res.end(JSON.stringify json)
deleteNoteFunction = (id) ->
(callback) ->
client.del "data/#{id}/", (err) -> callback()
describe "Search features", ->
before (done) ->
client.post 'data/321/', {
title: "my note"
content: "my content"
docType: "Note"
} , (error, response, body) ->
done()
after (done) ->
funcs = []
for id in ids
funcs.push deleteNoteFunction(id)
async.series funcs, ->
ids = []
done()
describe "index", ->
before (done) ->
client.del "data/index/clear-all/", (err, response) ->
done()
it "When given I index four notes", (done) ->
async.series [
createNoteFunction "Note 01", "little stories begin"
createNoteFunction "Note 02", "great dragons are coming"
createNoteFunction "Note 03", "small hobbits are afraid"
createNoteFunction "Note 04", "such as humans"
], =>
data = ids: [dragonNoteId]
@indexer = fakeServer data, 200, (url, body) ->
if url is '/index/'
should.exist body.fields
should.exist body.doc
should.exist body.doc.docType
200
else if url is '/search/'
should.exist body.query
body.query.should.equal "dragons"
200
else 204
@indexer.listen indexerPort
setTimeout done, 500
it "And I send a request to search the notes containing dragons", \
(done) ->
Note.search "dragons", (err, notes) =>
@notes = notes
@indexer.close()
done()
it "Then result is the second note I created", ->
@notes.length.should.equal 1
@notes[0].title.should.equal "Note 02"
@notes[0].content.should.equal "great dragons are coming"
describe "retrieve notes with result numbers", ->
before (done) ->
client.del "data/index/clear-all/", (err, response) ->
done()
it "When given I index four notes", (done) ->
data = ids: [dragonNoteId], numResults: 1
@indexer = fakeServer data, 200, (url, body) ->
if url is '/index/'
should.exist body.fields
should.exist body.doc
should.exist body.doc.docType
200
else if url is '/search/'
should.exist body.query
body.query.should.equal "dragons"
200
else 204
@indexer.listen indexerPort
setTimeout done, 500
it "When I search again with the options", (done) ->
options =
query: "dragons"
numPage: 1
numByPage: 10
Note.search options, (err, notes) =>
@notes = notes
@indexer.close()
done()
it "Then result should be made of the results themselves and the total number of results", ->
{results, numResults} = @notes
should.exist results
should.exist numResults
results.length.should.equal 1
results[0].title.should.equal "Note 02"
results[0].content.should.equal "great dragons are coming"
numResults.should.equal 1
### Attachments ###
describe "Attachments", ->
before (done) ->
@note = new Note id: 321
data =
title: "my note"
content: "my content"
docType: "Note"
client.post 'data/321/', data, (error, response, body) ->
done()
after (done) ->
client.del "data/321/", (error, response, body) ->
done()
describe "Add an attachment", ->
it "When I add an attachment", (done) ->
@note.attachFile "./test.png", (err) =>
@err = err
done()
it "Then no error is returned", ->
should.not.exist @err
describe "Retrieve an attachment", ->
it "When I claim this attachment", (done) ->
stream = @note.getFile "test.png", -> done()
stream.pipe fs.createWriteStream('./test-get.png')
it "Then I got the same file I attached before", ->
fileStats = fs.statSync('./test.png')
resultStats = fs.statSync('./test-get.png')
resultStats.size.should.equal fileStats.size
describe "Remove an attachment", ->
it "When I remove this attachment", (done) ->
@note.removeFile "test.png", (err) =>
@err = err
done()
it "Then no error is returned", ->
should.not.exist @err
it "When I claim this attachment", (done) ->
stream = @note.getFile "test.png", (err) =>
@err = err
done()
stream.pipe fs.createWriteStream('./test-get.png')
it "Then I got an error", ->
should.exist @err
checkNoError = ->
it "Then no error should be returned", ->
should.not.exist @err
checkError = ->
it "Then error should be returned", ->
should.exist @err
describe "Requests", ->
before (done) ->
async.series [
createNoteFunction "Note 01", "little stories begin"
createNoteFunction "Note 02", "great dragons are coming"
createNoteFunction "Note 03", "small hobbits are afraid"
createNoteFunction "Note 04", "such as humans"
createTaskFunction "Task 01"
createTaskFunction "Task 02"
createTaskFunction "Task 03"
], ->
done()
after (done) ->
funcs = []
for id in ids
funcs.push deleteNoteFunction(id)
async.series funcs, ->
ids = []
done()
describe "index", ->
describe "View creation", ->
describe "Creation of the first view + design document creation", ->
it "When I send a request to create view every_docs", (done) ->
delete @err
@map = (doc) ->
emit doc._id, doc
return
Note.defineRequest "every_notes", @map, (err) ->
should.not.exist err
done()
checkNoError()
describe "Access to a view without option", ->
describe "Access to a non existing view", ->
it "When I send a request to access view dont-exist", (done) ->
delete @err
Note.request "dont-exist", (err, notes) =>
@err = err
should.exist err
done()
checkError()
describe "Access to an existing view : every_notes", (done) ->
it "When I send a request to access view every_docs", (done) ->
delete @err
Note.request "every_notes", (err, notes) =>
@notes = notes
done()
it "Then I should have 4 documents returned", ->
@notes.should.have.length 4
describe "Access to a doc from a view : every_notes", (done) ->
it "When I send a request to access doc 3 from every_docs", \
(done) ->
delete @err
Note.request "every_notes", {key: ids[3]}, (err, notes) =>
@notes = notes
done()
it "Then I should have 1 documents returned", ->
@notes.should.have.length 1
@notes[0].id.should.equal ids[3]
describe "Deletion of docs through requests", ->
describe "Delete a doc from a view : every_notes", (done) ->
it "When I send a request to delete a doc from every_docs", \
(done) ->
Note.requestDestroy "every_notes", {key: ids[3]}, (err) ->
should.not.exist err
done()
it "And I send a request to access view every_docs", (done) ->
delete @err
delete @notes
Note.request "every_notes", {key: ids[3]}, (err, notes) =>
@notes = notes
done()
it "Then I should have 0 documents returned", ->
@notes.should.have.length 0
it "And other documents are still there", (done) ->
Note.request "every_notes", (err, notes) =>
should.not.exist err
notes.should.have.length 3
done()
describe "Delete all doc from a view : every_notes", (done) ->
it "When I delete all docs from every_docs", (done) ->
Note.requestDestroy "every_notes", (err) ->
should.not.exist err
done()
it "And I send a request to grab all docs from every_docs", \
(done) ->
delete @err
delete @notes
Note.request "every_notes", (err, notes) =>
@notes = notes
done()
it "Then I should have 0 documents returned", ->
@notes.should.have.length 0
describe "Deletion of an existing view", ->
it "When I send a request to delete view every_notes", (done) ->
Note.removeRequest "every_notes", (err) ->
should.not.exist err
done()
# Following DS commit 8e43fc66
# the request will be kept in case another app use it
#### Relations ###
#describe "Relations", ->
#before (done) ->
#Author.create name: "John", (err, author) =>
#@author = author
#async.series [
#createNoteFunction "Note 01", "little stories begin", author
#createNoteFunction "Note 02", "great dragons are coming", author
#createNoteFunction "Note 03", "small hobbits are afraid", author
#createNoteFunction "Note 04", "such as humans", null
#], ->
#done()
#after (done) ->
#funcs = []
#for id in ids
#funcs.push deleteNoteFunction(id)
#async.series funcs, ->
#ids = []
#if author?
#@author.destroy ->
#done()
#else
#done()
#describe "Has many relation", ->
#it "When I require all notes related to given author", (done) ->
#@author.notes (err, notes) =>
#should.not.exist err
#@notes = notes
#done()
#it "Then I have three notes", ->
#should.exist @notes
#@notes.length.should.equal 3
###describe "Send mail", ->
describe "Send common mail", ->
it "When I send the mail", (done) ->
data =
to: "test@cozycloud.cc"
from: "Cozy-test <test@cozycloud.cc>"
subject: "Test jugglingdb"
content: "Content of mail"
CozyAdapter.sendMail data, (err) =>
@err = err
done()
it "Then no error is returned", ->
should.not.exist @err
describe "Send mail to user", ->
it "When I send the mail", (done) ->
data =
from: "Cozy-test <test@cozycloud.cc>"
subject: "Test jugglingdb"
content: "Content of mail"
CozyAdapter.sendMailToUser data, (err) =>
@err = err
done()
it "Then no error is returned", ->
should.not.exist @err
describe "Send mail from user", ->
it "When I send the mail", (done) ->
data =
to: "test@cozycloud.cc"
subject: "Test jugglingdb"
content: "Content of mail"
CozyAdapter.sendMailFromUser data, (err) =>
@err = err
done()
it "Then no error is returned", ->
should.not.exist @err###