aboutsummaryrefslogtreecommitdiff
path: root/tests/test/webdav.scm
blob: 10dcf95b940c092f61394368443a63e642973297 (plain)
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
(define-module (test webdav)
  :use-module (srfi srfi-64)
  :use-module (srfi srfi-71)
  :use-module (srfi srfi-88)
  :use-module (srfi srfi-1)
  :use-module (sxml namespaced)
  :use-module (oop goops)
  :use-module (calp namespaces)
  :use-module ((hnh util) :select (sort*))
  :use-module (datetime)

  :use-module (calp webdav property)
  :use-module (calp webdav propfind)
  :use-module (calp webdav resource)
  :use-module (calp webdav resource virtual)
  )

;;; NOTE these tests don't check that XML namespaces work correctly, but only as
;;; far as not checking that the correct namespace is choosen. They should fail if
;;; namespacing gets completely broken.

;;; TODO tests for a missing resource?

(define (swap p) (xcons (car p) (cdr p)))

(define dt #2010-11-12T13:14:15)

(define resource (make <virtual-resource>
                   ;; local-path: '("")
                   name: "*root"
                   content: #vu8(1 2 3 4)
                   creation-time: dt))

(define (sort-propstats propstats)
  (map
   (lambda (propstat)
     (make-propstat (propstat-status-code propstat)
                    (sort* (propstat-property propstat)
                           string< (compose symbol->string xml-element-tagname car))
                    (propstat-error propstat)
                    (propstat-response-description propstat)))
   (sort* propstats < propstat-status-code))
  )

;; (test-equal "/" (href->string (href resource)))
(test-equal "Basic propstat"
    (propstat 200 (list (list (xml webdav 'getcontentlength) 4)))
    (getcontentlength resource))


(define (sort-symbols symbs)
  (sort* symbs string<=? symbol->string))



;;; NOTE propstat's return order isn't stable, making this test possibly fail
(let ((ps (list (propstat 200 (list `(,(xml webdav 'displayname) "Displayname")))
                (propstat 200 (list `(,(xml webdav 'getcontenttype) "text/plain"))))))
  (test-equal "Propstat merger"
    (list (propstat 200
                    (list (list (xml webdav 'getcontenttype) "text/plain")
                          (list (xml webdav 'displayname) "Displayname"))))
    (merge-propstats ps)))



(test-group "All live properties"
 (let ((props (live-properties resource)))
   (test-assert (list? props))
   (for-each (lambda (pair)
               ;; (test-assert (xml-element? (car pair)))
               (test-assert (live-property? (cdr pair)))
               (test-assert (procedure? (property-getter (cdr pair))))
               (test-assert (procedure? (property-setter-generator (cdr pair)))))
             props)))

(test-group "\"All\" live properties"
  (let ((most (propfind-most-live-properties resource)))
    (test-equal "Correct amount of keys" 10 (length most))
    (for-each (lambda (propstat)
                (test-assert "Propstat is propstat" (propstat? propstat))
                (test-equal (format #f "Propstat well formed: ~a" (propstat-property propstat))
                  1 (length (propstat-property propstat)))
                (test-assert "Propstat child is xml"
                  (xml-element? (caar (propstat-property propstat)))))
              most)

    (test-equal "Correct keys"
      '(creationdate displayname getcontentlanguage getcontentlength
                     getcontenttype getetag getlastmodified
                     lockdiscovery resourcetype supportedlock)
      (sort-symbols (map (compose xml-element-tagname caar propstat-property) most)))))



(define ns1 (string->symbol "http://example.com/namespace"))

(set-dead-property! resource `(,(xml ns1 'test) "Content"))

(test-equal "Get dead property"
  (propstat 200 (list (list (xml ns1 'test) "Content")))
  (get-dead-property resource (xml ns1 'test)))

(test-equal "Get live property"
  (propstat 404 (list (list (xml ns1 'test))))
  (get-live-property resource (xml ns1 'test)))

(test-group "Dead properties"
  (test-equal "Existing property"
    (propstat 200 (list (list (xml ns1 'test) "Content")))
    (get-property resource (xml ns1 'test)))

  (test-equal "Missing property"
    (propstat 404 (list (list (xml ns1 'test2))))
    (get-property resource (xml ns1 'test2)))

  (test-equal "All dead properties"
    (list (propstat 200 (list (list (xml ns1 'test) "Content"))))
    (propfind-all-dead-properties resource)))

(test-group "Live Properties"

  ;; TODO these tests were written when displayname always returned 200, but have since changed to test for 404.
  ;; Change to another property which return 200
  (test-equal "Existing live property (through get-live-property)"
    (propstat 404 `((,(xml webdav 'displayname))))
    (get-live-property resource (xml webdav 'displayname)))

  (test-equal "Existing live property (thrtough get-property)"
    (propstat 404 `((,(xml webdav 'displayname))))
    (get-property resource (xml webdav 'displayname)))
  )

(test-equal "propfind-selected-properties"
  (list (propstat 404 `((,(xml webdav 'displayname)))))
  (propfind-selected-properties resource (list (xml webdav 'displayname))))

(test-group "parse-propfind"
  (test-group "propname"
   (let ((props (parse-propfind `(d:propfind (d:propname))
                                `((d . ,webdav))
                                resource)))


     (test-group "Propfind should NEVER fail for an existing resource"
       (test-equal 1 (length props))
       (test-equal 200 (propstat-status-code (car props))))

     (test-assert "Propstat objects are returned" (propstat? (car props)))
     (for-each (lambda (el)
                 (test-assert "Base is list" (list? el))
                 (test-eqv "List only contains head el" 1 (length el))
                 #;
                 (test-assert (format #f "Head is an xml tag: ~a" el)
                   (xml-element? (car el))))
               (propstat-property (car props)))

     #;
     (test-equal "Correct property keys"
       (sort-symbols (cons* 'test 'is-virtual webdav-keys))
       (sort-symbols (map (compose xml-element-tagname car)
                          (propstat-property (car props)))))

     (test-group "No property should contain any data"
       (for-each (lambda (el)
                   (test-eqv (format #f "Propname property: ~s" el)
                     1 (length el)))
                 (propstat-property (car props))))))


  (test-group "direct property list"
    (let ((props (parse-propfind `(d:propfind (d:prop (d:displayname)))
                                 `((d . ,webdav))
                                 resource)))
      (test-equal "Simple lookup"
        (list (propstat 404 (list (list (xml webdav 'displayname)
                                        ))))
        props)))

  ;; TODO test that calendar properties are reported by propname
  ;; TODO test that non-native caldav propreties aren't reported by allprop

  (test-group "allprop"
    (let ((props (parse-propfind '(d:propfind (d:allprop))
                                 `((d . ,webdav))
                                 resource)))


      (test-equal "Propfind result"
        (list
         (propstat 200
                   (list (list (xml webdav 'creationdate)
                               (datetime->string dt "~Y-~m-~dT~H:~M:~SZ"))
                         (list (xml webdav 'getcontentlength)
                               4)
                         (list (xml webdav 'getcontenttype)
                               "application/binary")
                         (list (xml webdav 'getlastmodified)
                               "Thu, 01 Jan 1970 00:00:00 GMT")
                         (list (xml webdav 'lockdiscovery) '())
                         (list (xml webdav 'resourcetype)
                                        ; (list (xml webdav 'collection))
                               )
                         (list (xml webdav 'supportedlock) '())
                         (list (xml ns1 'test) "Content")
                         ))
         (propstat 404 (list (list (xml webdav 'displayname))
                             (list (xml webdav 'getcontentlanguage))))
         (propstat 501
                   (list (list (xml webdav 'getetag))
                        )))
        (sort-propstats props))))


  (test-group "allprop with include"
    (let ((props (parse-propfind '(d:propfind (d:allprop) (d:include))
                                 `((d . ,webdav))
                                 resource)))


      (test-equal "Include NOTHING"
        (list
         (propstat 200
                   (list (list (xml webdav 'creationdate)
                               (datetime->string dt "~Y-~m-~dT~H:~M:~SZ"))
                         (list (xml webdav 'getcontentlength)
                               4)
                         (list (xml webdav 'getcontenttype)
                               "application/binary")
                         (list (xml webdav 'getlastmodified)
                               "Thu, 01 Jan 1970 00:00:00 GMT")
                         (list (xml webdav 'lockdiscovery) '())
                         (list (xml webdav 'resourcetype)
                                        ; (list (xml webdav 'collection))
                               )
                         (list (xml webdav 'supportedlock) '())
                         (list (xml ns1 'test) "Content")
                         ))
         (propstat 404 (list (list (xml webdav 'displayname))
                             (list (xml webdav 'getcontentlanguage))))
         (propstat 501
                   (list (list (xml webdav 'getetag))
                         )))
        (sort-propstats props)))


    (let ((props (parse-propfind `(d:propfind (d:allprop)
                                              (d:include (x:isvirtual)))
                                 `((d . ,webdav)
                                   (x . ,virtual-ns))
                                 resource)))

      (test-equal "Include isvirtual"
        (list
         (propstat 200
                   (list (list (xml webdav 'creationdate) (datetime->string dt "~Y-~m-~dT~H:~M:~SZ"))
                         (list (xml webdav 'getcontentlength) 4)
                         (list (xml webdav 'getcontenttype) "application/binary")
                         (list (xml webdav 'getlastmodified) "Thu, 01 Jan 1970 00:00:00 GMT")
                         (list (xml virtual-ns 'isvirtual) "true")
                         (list (xml webdav 'lockdiscovery) '())
                         (list (xml webdav 'resourcetype))
                         (list (xml webdav 'supportedlock) '())
                         (list (xml ns1 'test) "Content")
                         ))
         (propstat 404 (list (list (xml webdav 'displayname))
                             (list (xml webdav 'getcontentlanguage))))
         (propstat 501
                   (list (list (xml webdav 'getetag))
                     )))
        (sort-propstats props)))))




;;; Setting properties

;;; We already use set-dead-property! above, but for testing get we need set,
;;; and for testing set we need get, and get is more independent, so we start there.



(test-group "Propstat -> namespaced sxml"
  (test-equal "Simple"
    `(,(xml webdav 'propstat)
      (,(xml webdav 'prop) (,(xml webdav 'displayname) "test"))
      (,(xml webdav 'status) "HTTP/1.1 200 OK"))
    (propstat->namespaced-sxml (propstat 200 `((,(xml webdav 'displayname) "test")) )))

  ;; TODO populated error field

  (test-equal "With response description"
    `(,(xml webdav 'propstat)
      (,(xml webdav 'prop) (,(xml webdav 'displayname) "test"))
      (,(xml webdav 'status) "HTTP/1.1 403 Forbidden")
      (,(xml webdav 'responsedescription) "Try logging in"))
    (propstat->namespaced-sxml (propstat 403 `((,(xml webdav 'displayname) "test"))
                                         responsedescription: "Try logging in"))))




;;; TODO what am I doing here?

(test-equal
    (list (propstat 200
                    `((,(xml webdav 'getcontentlength) 4)
                      (,(xml webdav 'getlastmodified) "Thu, 01 Jan 1970 00:00:00 GMT")
                      (,(xml webdav 'resourcetype))))
          (propstat 404
                    `((,(xml webdav 'checked-in))
                      (,(xml webdav 'checked-out))
                      (,(xml (string->symbol "http://apache.org/dav/props/") 'executable)))))
  (let ((request namespaces
                 (namespaced-sxml->sxml/namespaces
                  (xml->namespaced-sxml
                   "<?xml version=\"1.0\" encoding=\"utf-8\"?>
<propfind xmlns=\"DAV:\">
  <prop>
    <getcontentlength/>
    <getlastmodified/>
    <executable xmlns=\"http://apache.org/dav/props/\"/>
    <resourcetype/>
    <checked-in/>
    <checked-out/>
  </prop>
</propfind>")
                  `((,(string->symbol "DAV:") . d)))))

    (sort-propstats (parse-propfind (caddr request) (map swap namespaces) resource))))



(test-group "lookup-resource"
  (let* ((root (make <virtual-resource> name: "*root*"))
         (a (add-collection! root "a"))
         (b (add-collection! a "b"))
         (c (add-resource! b "c" "~~Nothing~~")))
    (test-eq "Lookup root"
      root (lookup-resource root '()))
    (test-eq "Lookup direct child"
      a (lookup-resource root '("a")))
    (test-eq "Lookup deep child"
      c (lookup-resource root '("a" "b" "c")))
    (test-assert "Lookup missing"
      (not (lookup-resource root '("a" "d" "c"))))))




(test-group "mkcol"
  (let ((root (make <virtual-resource> name: "*root*")))
    (add-collection! root "child")
    (test-eqv "Child got added" 1 (length (children root)))))