Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Avatar for KuCalc : devops.
Download
50660 views
1
###
2
Testing API functions relating to text files
3
###
4
5
api = require('./apitest')
6
{setup, teardown} = api
7
misc = require('smc-util/misc')
8
expect = require('expect')
9
10
describe 'testing text file operations -- ', ->
11
before(setup)
12
after(teardown)
13
14
project_id = undefined
15
content = 'hello\nworld'
16
17
it "creates target project", (done) ->
18
api.call
19
event : 'create_project'
20
body :
21
title : 'TFTEST'
22
description : 'Testing text file operations'
23
cb : (err, resp) ->
24
expect(resp?.event).toBe('project_created')
25
project_id = resp.project_id
26
done(err)
27
28
it "creates a text file in a project", (done) ->
29
@timeout(15000)
30
api.call
31
event : 'write_text_file_to_project'
32
body :
33
project_id: project_id
34
content : content
35
path : 'A1/doc1.txt'
36
cb : (err, resp) ->
37
expect(err).toEqual(null)
38
expect(resp?.event).toBe('file_written_to_project')
39
done(err)
40
41
it "reads a text file in a project", (done) ->
42
@timeout(15000)
43
api.call
44
event : 'read_text_file_from_project'
45
body :
46
project_id: project_id
47
path : 'A1/doc1.txt'
48
cb : (err, resp) ->
49
expect(err).toEqual(null)
50
expect(resp?.event).toBe('text_file_read_from_project')
51
expect(resp?.content).toBe(content)
52
done(err)
53
54
it "tries to read a private file as public and fails", (done) ->
55
api.call
56
event : 'public_get_text_file'
57
body :
58
project_id: project_id
59
path : 'A1/doc1.txt'
60
cb : (err, resp) ->
61
expect(resp?.event).toBe('error')
62
expect(resp?.error).toInclude('is not public')
63
done(err)
64
65
66
it "tries to read a nonexistent public file and fails", (done) ->
67
api.call
68
event : 'public_get_text_file'
69
body :
70
project_id: project_id
71
path : 'nonexistent.txt'
72
cb : (err, resp) ->
73
expect(resp?.event).toBe('error')
74
expect(resp?.error).toInclude('is not public')
75
done(err)
76
77
it "tries to list public files in a private project and fails", (done) ->
78
api.call
79
event : 'public_get_directory_listing'
80
body :
81
project_id: project_id
82
path : 'A1'
83
cb : (err, resp) ->
84
expect(resp?.event).toBe('error')
85
expect(resp?.error).toBe('not_public')
86
done(err)
87
88
it "uses API query to make a file public", (done) ->
89
api.call
90
event : 'query'
91
body :
92
query : {public_paths:{project_id:project_id, path:'A1/doc1.txt', description:'Handout #1'}}
93
cb : (err, resp) ->
94
expect(resp?.event).toBe('query')
95
done(err)
96
97
it "reads a public text file in a project", (done) ->
98
@timeout(15000)
99
api.call
100
event : 'public_get_text_file'
101
body :
102
project_id: project_id
103
path : 'A1/doc1.txt'
104
cb : (err, resp) ->
105
expect(resp?.event).toBe('public_text_file_contents')
106
expect(resp?.data).toBe(content)
107
done(err)
108
109
it "lists public files in a public project", (done) ->
110
api.call
111
event : 'public_get_directory_listing'
112
body :
113
project_id: project_id
114
path : 'A1'
115
cb : (err, resp) ->
116
expect(resp?.event).toBe('public_directory_listing')
117
expect(resp?.result?.files?.length).toBe(1)
118
expect(resp?.result?.files?[0]).toIncludeKeys(['mtime','name','size'])
119
done(err)
120
121
it "uses API query to make a folder public", (done) ->
122
api.call
123
event : 'query'
124
body :
125
query : {public_paths:{project_id:project_id, path:'A1/', description:'public folder A1'}}
126
cb : (err, resp) ->
127
expect(resp?.event).toBe('query')
128
done(err)
129
130
it "reads a folder as public text file and gets zip archive", (done) ->
131
api.call
132
event : 'public_get_text_file'
133
body :
134
project_id: project_id
135
path : 'A1/'
136
cb : (err, resp) ->
137
expect(resp?.data[0..1]).toEqual("PK")
138
done(err)
139
140
it "tries to list public files on nonexistent path in a public project and gets empty list", (done) ->
141
api.call
142
event : 'public_get_directory_listing'
143
body :
144
project_id: project_id
145
path : 'nonexistent'
146
cb : (err, resp) ->
147
expect(resp?.event).toBe('public_directory_listing')
148
expect(resp?.result).toEqual(files:[])
149
done(err)
150
151