-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.sh
executable file
·207 lines (170 loc) · 6 KB
/
test.sh
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
#!/usr/bin/env bash
# output: body + status code, read cookies from file
cargo build
cargo run &
server=$(jobs -p)
sleep 1
base=localhost:8080
description=""
expected=""
result=""
assert () {
if [[ $result != *"$expected"* ]]
then
echo "❌ $description"
echo "Expected: '$expected'"
echo "Actually: '$result'"
kill $server
exit 1
else
echo "✅ $description"
fi
}
# clear database
rm -f cookies
sqlite3 storage/database.sqlite "delete from document"
sqlite3 storage/database.sqlite "delete from user"
sqlite3 storage/database.sqlite "delete from user_session"
# Index
description="server is up"
expected=' 200'
result=$(curl -s -w ' %{http_code}' $base)
assert
# static folder
echo -n "asdf" > client/public/static/file.txt
description="serves static files"
expected='asdf 200'
result=$(curl -s -w ' %{http_code}' $base/static/file.txt)
assert
rm client/public/static/file.txt
# Not logged in - all should fail
description="unauthorized get"
expected='unauthorized 401'
result=$(curl -s -w ' %{http_code}' -b cookies -c cookies $base/document/12345)
assert
description="unauthorized post"
result=$(curl -s -w ' %{http_code}' -b cookies -c cookies -X POST -d @test/data.json -H 'content-type: application/json' $base/document)
assert
description="unauthorized put"
result=$(curl -s -w ' %{http_code}' -b cookies -c cookies -X PUT -d @test/data_update.json -H 'content-type: application/json' $base/document/12345)
assert
description="unauthorized delete"
result=$(curl -s -w ' %{http_code}' -b cookies -c cookies -X DELETE $base/document/12345)
assert
# username and passwords not allowed
description="register with empty username"
expected="empty username 400"
result=$(curl -s -w ' %{http_code}' -b cookies -c cookies -d 'username=&password=1' $base/user/register)
assert
description="register with empty password"
expected="empty password 400"
result=$(curl -s -w ' %{http_code}' -b cookies -c cookies -d 'username=a&password=' $base/user/register)
assert
# Registered and logged in
description="register user"
expected="user registered 303"
result=$(curl -s -w ' %{http_code}' -b cookies -c cookies -d 'username=adsf&password=1234' $base/user/register)
assert
description="logout"
expected="logged out 303"
result=$(curl -s -w ' %{http_code}' -b cookies -c cookies $base/user/logout)
assert
description="login"
expected="logged in 303"
result=$(curl -s -w ' %{http_code}' -b cookies -c cookies -d 'username=adsf&password=1234' $base/user/login)
assert
description="get non-existent data"
expected="not found 404"
result=$(curl -s -w ' %{http_code}' -b cookies -c cookies $base/document/12345)
assert
description="insert data"
expected=" 201"
result=$(curl -s -w ' %{http_code}' -b cookies -c cookies -X POST -d @test/data.json -H 'content-type: application/json' $base/document)
assert
id_not_deleted=$(echo $result | sed -e 's/{.*"id":"//' | sed -e 's/".*//')
description="insert data again"
expected=" 201"
result=$(curl -s -w ' %{http_code}' -b cookies -c cookies -X POST -d @test/data.json -H 'content-type: application/json' $base/document)
assert
id=$(echo $result | sed -e 's/.*"id":"//' | sed -e 's/".*//')
description="get inserted data"
expected='{"pk":2,"id":"'
expected+=$id
expected+='","created":'
result=$(curl -s -w ' %{http_code}' -b cookies -c cookies $base/document/$id)
# before created
assert
# after created
expected=',"data":{"b" : 111}} 200'
assert
description="update inserted data"
expected="updated 200"
result=$(curl -s -w ' %{http_code}' -b cookies -c cookies -X PUT -d @test/data_update.json -H 'content-type: application/json' $base/document/$id)
assert
description="get updated data"
expected='{"pk":2,"id":"'
expected+=$id
expected+='","created":'
result=$(curl -s -w ' %{http_code}' -b cookies -c cookies $base/document/$id)
assert
expected=',"data":{"c": 444}} 200'
assert
description="delete data"
expected="deleted 200"
result=$(curl -s -w ' %{http_code}' -b cookies -c cookies -X DELETE $base/document/$id)
assert
description="delete missing data"
expected="not found 404"
result=$(curl -s -w ' %{http_code}' -b cookies -c cookies -X DELETE $base/document/$id)
assert
# insert data for cross-user access
result=$(curl -s -w ' %{http_code}' -b cookies -c cookies -X POST -d @test/data.json -H 'content-type: application/json' $base)
description="logout"
expected="logged out 303"
result=$(curl -s -w ' %{http_code}' -b cookies -c cookies $base/user/logout)
assert
# Registered another user
description="register thief user"
expected="user registered 303"
result=$(curl -s -w ' %{http_code}' -b cookies -c cookies -d 'username=thief&password=1234' $base/user/register)
assert
description="get asdf's data logged in as thief"
expected="not found 404"
result=$(curl -s -w ' %{http_code}' -b cookies -c cookies $base/document/$id_not_deleted)
assert
description="insert thief data"
expected=" 201"
result=$(curl -s -w ' %{http_code}' -b cookies -c cookies -X POST -d @test/thief_data.json -H 'content-type: application/json' $base/document)
assert
id=$(echo $result | sed -e 's/{.*"id":"//' | sed -e 's/".*//')
description="get thief's inserted data"
expected='{"pk":2,"id":"'
expected+=$id
expected+='","created":'
result=$(curl -s -w ' %{http_code}' -b cookies -c cookies $base/document/$id)
assert
expected=',"data":{"thief": true}} 200'
assert
description="update inserted data"
expected="updated 200"
result=$(curl -s -w ' %{http_code}' -b cookies -c cookies -X PUT -d @test/data_update.json -H 'content-type: application/json' $base/document/$id)
assert
description="get updated data"
expected='{"pk":2,"id":"'
expected+=$id
expected+='","created":'
result=$(curl -s -w ' %{http_code}' -b cookies -c cookies $base/document/$id)
assert
expected=',"data":{"c": 444}} 200'
assert
description="delete data"
expected="deleted 200"
result=$(curl -s -w ' %{http_code}' -b cookies -c cookies -X DELETE $base/document/$id)
assert
description="logout"
expected="logged out 303"
result=$(curl -s -w ' %{http_code}' -b cookies -c cookies $base/user/logout)
assert
echo "All curl tests OK, running mocha tests:"
npm -C test test
kill $server