-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtests.rkt
More file actions
239 lines (232 loc) · 13.1 KB
/
tests.rkt
File metadata and controls
239 lines (232 loc) · 13.1 KB
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
#lang racket/base
(require rackunit
rackunit/text-ui
racket/function
racket/file
racket/match
racket/promise
web-server/http
web-server/test
web-server/servlet-dispatch
db
json
uuid
net/url-string
(except-in net/cookies/server
make-cookie)
"src/sql.rkt"
"src/data.rkt"
"src/state.rkt"
"src/server.rkt"
"src/auth.rkt")
(define api (make-parameter #f))
(define (start-admin-session)
(sql:new-session (conn/w) 1)) ;In the prepared testing environment, the prepared admin+manager user has id 1
(define (start-user-session)
(sql:new-session (conn/w) 2)) ;In the prepared testing environment, the prepared normal user has id 2
(define (session-header session-token)
(header #"Cookie" (string->bytes/utf-8 (format "session=~a" (cookie-value (prepare-session-cookie session-token))))))
(define (fetch uri
#:method [method #"post"]
#:bindings [bindings '()]
#:headers [headers '()]
#:body [body #f]
#:impersonate [impersonating 'none])
(let ([extra-headers (if body (list (header #"Content-Type" #"application/json")) '())])
(bytes->jsexpr ((api)
(request method (string->url uri)
(append (match impersonating
['none headers]
['user (cons (session-header (start-user-session)) headers)]
['admin (cons (session-header (start-admin-session)) headers)]
[(? string? token) (cons (session-header token) headers)])
extra-headers)
(delay bindings)
body "localhost" 80 "localhost")
#:raw? #t))))
(define sample-image-contents (file->bytes "sample.jpg"))
(define db-test-suite
(test-suite "Contour Tests"
(test-case "Database/Users"
(check-match
(sql:get-user (conn/r) (sql:new-user (conn/w) "test1@mail.com" "Test1" "Password" "/path" #f #f))
(user _ "test1@mail.com" "Test1" "/path" #f #f #f))
(check-exn
exn:fail?
(thunk
(sql:new-user (conn/w) "test1@mail.com" "Test3" "Password" "/path" #f #f))
"Disallow duplicate email address")
(check-exn
exn:fail?
(thunk
(sql:new-user (conn/w) "test3@mail.com" "Test1" "Password" "/path" #f #f))
"Disallow duplicate username")
(check-equal?
(sql:get-user (conn/r) 3)
(user 3 "manager@mail.com" "Manager" "/avatars/manager.jpg" #t #f #f)
"Get user by id")
(check-equal?
(map user-id (sql:list-users (conn/r) #:like "User"))
'(2 4 5)
"List users with matching usernames")
(check-equal?
(map user-id (sql:list-users (conn/r) #:banned? #t))
'(4)
"List banned users")
(check-equal?
(begin
(sql:toggle-ban-user (conn/w) 5 #t #:issuer-id 1)
(user-banned? (sql:get-user (conn/r) 5)))
#t))
(test-case "Database/Pictures"
(check-match
(begin
(let* ([id (sql:new-picture (conn/w) 3 "/images/placeholder.jpg" "/images/placeholder-thumb.jpg" "Some picture" "A picture created for tests")]
[pic (sql:get-picture (conn/r) id)])
pic))
(picture _ "/images/placeholder.jpg" "/images/placeholder-thumb.jpg" 3 "Some picture" "A picture created for tests" '() #f #f _ #f))
(check-equal?
(begin
(sql:assign-tags (conn/w) 5 '("test" "picture#12" "a-ok"))
(let ([pic (sql:get-picture (conn/r) 5)])
(picture-tags pic)))
'("test" "picture#12" "a-ok")
)
(check-true
(begin
(sql:toggle-picture-state (conn/w) 4 'deleted #t #:issuer-id 1)
(let ([pic (sql:get-picture (conn/r) 4)])
(picture-deleted? pic)))
"Delete picture")
(check-equal?
(map picture-id (sql:list-pictures (conn/r) #:author "User"))
'(3 2 1)
"List pictures created by User")
(check-equal?
(map picture-id (sql:list-pictures (conn/r) #:tags '("fantasy")))
'(2 1)
"List pictures tagged 'fantasy'")
)
(test-case "Api/Users"
(check-match
(bytes->jsexpr ((api)
(request #"post" (string->url "/api/users/")
'()
(delay (list
(binding:form #"email" #"test@test.test")
(binding:form #"username" #"test")
(binding:form #"password" #"testpass")
(binding:form #"repeat-password" #"testpass")
(binding:file #"avatar" #"filename" '() sample-image-contents)))
#f "localhost" 80 "localhost")
#:raw? #t))
(hash 'id (? exact-integer?)
'location (? string?)
'session-token (? uuid-string?)
'status "Registered successfully."))
(check-match
(bytes->jsexpr ((api)
(request #"post" (string->url "/api/users/")
'()
(delay (list
(binding:form #"email" #"test1@test.test")
(binding:form #"username" #"test")
(binding:form #"password" #"testpass")
(binding:form #"repeat-password" #"testpass")
(binding:file #"avatar" #"filename" '() sample-image-contents)))
#f "localhost" 80 "localhost")
#:raw? #t))
(hash 'error #t
'status (? string?)))
(check-match
(bytes->jsexpr ((api)
(request #"post" (string->url "/api/sessions/")
'()
(delay (list
(binding:form #"username" #"test")
(binding:form #"password" #"testpass")))
#f "localhost" 80 "localhost")
#:raw? #t))
(hash 'session-token (? uuid-string?) 'status (? string?)))
(check-match
(bytes->jsexpr ((api)
(request #"post" (string->url "/api/sessions/")
'()
(delay (list
(binding:form #"username" #"test")
(binding:form #"password" #"testpass")))
#f "localhost" 80 "localhost")
#:raw? #t))
(hash 'session-token (? uuid-string?) 'status (? string?)))
(check-match
(let* ([token (hash-ref (fetch "/api/sessions/"
#:bindings (list
(binding:form #"username" #"test")
(binding:form #"password" #"testpass")))
'session-token)])
(fetch "/api/sessions/current"
#:method #"delete"
#:impersonate token))
(hash 'status "Ok"))
(let ([test-user (sql:get-user-id/email (conn/r) "test@test.test")])
(check-match
(fetch (format "/api/users/~a/banned" test-user)
#:bindings (list
(binding:form #"banned" #"true"))
#:impersonate 'user)
(hash 'error #t 'status (? string?)))
(check-match
(fetch (format "/api/users/~a/banned" test-user)
#:body (jsexpr->bytes (hash 'banned #t))
#:impersonate 'admin)
(hash 'banned #t))
)
(check-match
(fetch "/api/authors/?page=1"
#:method #"get")
(hash 'page 1 'page-count (? exact-integer?)
'authors (list (hash 'id (? exact-integer?) 'name (? string?) 'avatar (? string?) 'extra (hash 'thumbnail (? string?))) ...))
))
(test-case "Api/Pictures"
(check-match
(fetch "/api/pictures/"
#:method #"post"
#:impersonate 'admin
#:bindings (list (binding:form #"title" #"TEST-TITLE")
(binding:form #"description" #"TEST-DESCRIPTION")
(binding:form #"tags" #"ala ma kota")
(binding:file #"picture" #"filename" '() sample-image-contents)))
(hash 'id (? exact-integer?) 'location (? string?) 'status (? string?)))
(let
([posted-picture-id (query-value (conn/r) "SELECT max(id) FROM Picture;")])
(check-match
(fetch (format "/api/pictures/~a/deleted" posted-picture-id)
#:impersonate 'user
#:body (jsexpr->bytes (hash 'deleted #t)))
(hash 'error #t 'status (? string?)))
(check-match
(fetch (format "/api/pictures/~a/deleted" posted-picture-id)
#:impersonate 'admin
#:body (jsexpr->bytes (hash 'deleted #t)))
(hash 'deleted #t))
))))
(define (test #:keep [keep-test-db? #f])
(define filename "contour-current-test.db")
(when (file-exists? filename)
(delete-file filename))
(copy-file "contour-test.db" "contour-current-test.db")
(define dbr (sqlite3-connect
#:database "contour-current-test.db"
#:mode 'read-only))
(define dbw (sqlite3-connect
#:database "contour-current-test.db"
#:mode 'read/write))
(parameterize ([conn/r dbr]
[conn/w dbw]
[api (make-dispatcher-tester (make-dispatcher #:conn/r dbr #:conn/w dbw #:log #f))])
(run-tests db-test-suite))
(disconnect dbr)
(disconnect dbw)
(when (not keep-test-db?)
(delete-file filename)))
(define tester (test #:keep #f))