Tweak arvbox build order, API server depends on arvados-server
[arvados.git] / sdk / R / tests / testthat / test-RESTService.R
1 # Copyright (C) The Arvados Authors. All rights reserved.
2 #
3 # SPDX-License-Identifier: Apache-2.0
4
5 source("fakes/FakeArvados.R")
6 source("fakes/FakeHttpRequest.R")
7 source("fakes/FakeHttpParser.R")
8
9 context("REST service")
10
11 test_that("getWebDavHostName calls REST service properly", {
12
13     expectedURL <- "https://host/discovery/v1/apis/arvados/v1/rest"
14     serverResponse <- list(keepWebServiceUrl = "https://myWebDavServer.com")
15     httpRequest <- FakeHttpRequest$new(expectedURL, serverResponse)
16
17     REST <- RESTService$new("token", "host",
18                             httpRequest, FakeHttpParser$new())
19
20     REST$getWebDavHostName()
21
22     expect_that(httpRequest$URLIsProperlyConfigured, is_true())
23     expect_that(httpRequest$requestHeaderContainsAuthorizationField, is_true())
24     expect_that(httpRequest$numberOfGETRequests, equals(1))
25 })
26
27 test_that("getWebDavHostName returns webDAV host name properly", {
28
29     serverResponse <- list(keepWebServiceUrl = "https://myWebDavServer.com")
30     httpRequest <- FakeHttpRequest$new(expectedURL = NULL, serverResponse)
31
32     REST <- RESTService$new("token", "host",
33                             httpRequest, FakeHttpParser$new())
34
35     expect_that("https://myWebDavServer.com", equals(REST$getWebDavHostName()))
36 })
37
38 test_that("create calls REST service properly", {
39
40     uuid <- "aaaaa-j7d0g-ccccccccccccccc"
41     expectedURL <- "https://webDavHost/c=aaaaa-j7d0g-ccccccccccccccc/file"
42     fakeHttp <- FakeHttpRequest$new(expectedURL)
43     fakeHttpParser <- FakeHttpParser$new()
44
45     REST <- RESTService$new("token", "https://host/",
46                             fakeHttp, fakeHttpParser,
47                             0, "https://webDavHost/")
48
49     REST$create("file", uuid)
50
51     expect_that(fakeHttp$URLIsProperlyConfigured, is_true())
52     expect_that(fakeHttp$requestHeaderContainsAuthorizationField, is_true())
53     expect_that(fakeHttp$numberOfPUTRequests, equals(1))
54 })
55
56 test_that("create raises exception if server response code is not between 200 and 300", {
57
58     uuid <- "aaaaa-j7d0g-ccccccccccccccc"
59     response <- list()
60     response$status_code <- 404
61     fakeHttp <- FakeHttpRequest$new(serverResponse = response)
62
63     REST <- RESTService$new("token", "https://host/",
64                             fakeHttp, HttpParser$new(),
65                             0, "https://webDavHost/")
66
67     expect_that(REST$create("file", uuid),
68                 throws_error("Server code: 404"))
69 })
70
71 test_that("delete calls REST service properly", {
72
73     uuid <- "aaaaa-j7d0g-ccccccccccccccc"
74     expectedURL <- "https://webDavHost/c=aaaaa-j7d0g-ccccccccccccccc/file"
75     fakeHttp <- FakeHttpRequest$new(expectedURL)
76     fakeHttpParser <- FakeHttpParser$new()
77
78     REST <- RESTService$new("token", "https://host/",
79                             fakeHttp, fakeHttpParser,
80                             0, "https://webDavHost/")
81
82     REST$delete("file", uuid)
83
84     expect_that(fakeHttp$URLIsProperlyConfigured, is_true())
85     expect_that(fakeHttp$requestHeaderContainsAuthorizationField, is_true())
86     expect_that(fakeHttp$numberOfDELETERequests, equals(1))
87 })
88
89 test_that("delete raises exception if server response code is not between 200 and 300", {
90
91     uuid <- "aaaaa-j7d0g-ccccccccccccccc"
92     response <- list()
93     response$status_code <- 404
94     fakeHttp <- FakeHttpRequest$new(serverResponse = response)
95
96     REST <- RESTService$new("token", "https://host/",
97                             fakeHttp, HttpParser$new(),
98                             0, "https://webDavHost/")
99
100     expect_that(REST$delete("file", uuid),
101                 throws_error("Server code: 404"))
102 })
103
104 test_that("move calls REST service properly", {
105
106     uuid <- "aaaaa-j7d0g-ccccccccccccccc"
107     expectedURL <- "https://webDavHost/c=aaaaa-j7d0g-ccccccccccccccc/file"
108     fakeHttp <- FakeHttpRequest$new(expectedURL)
109     fakeHttpParser <- FakeHttpParser$new()
110
111     REST <- RESTService$new("token", "https://host/",
112                             fakeHttp, fakeHttpParser,
113                             0, "https://webDavHost/")
114
115     REST$move("file", "newDestination/file", uuid)
116
117     expect_that(fakeHttp$URLIsProperlyConfigured, is_true())
118     expect_that(fakeHttp$requestHeaderContainsAuthorizationField, is_true())
119     expect_that(fakeHttp$requestHeaderContainsDestinationField, is_true())
120     expect_that(fakeHttp$numberOfMOVERequests, equals(1))
121 })
122
123 test_that("move raises exception if server response code is not between 200 and 300", {
124
125     uuid <- "aaaaa-j7d0g-ccccccccccccccc"
126     response <- list()
127     response$status_code <- 404
128     fakeHttp <- FakeHttpRequest$new(serverResponse = response)
129
130     REST <- RESTService$new("token", "https://host/",
131                             fakeHttp, HttpParser$new(),
132                             0, "https://webDavHost/")
133
134     expect_that(REST$move("file", "newDestination/file", uuid),
135                 throws_error("Server code: 404"))
136 })
137
138 test_that("copy calls REST service properly", {
139
140     uuid <- "aaaaa-j7d0g-ccccccccccccccc"
141     expectedURL <- "https://webDavHost/c=aaaaa-j7d0g-ccccccccccccccc/file"
142     fakeHttp <- FakeHttpRequest$new(expectedURL)
143     fakeHttpParser <- FakeHttpParser$new()
144
145     REST <- RESTService$new("token", "https://host/",
146                             fakeHttp, fakeHttpParser,
147                             0, "https://webDavHost/")
148
149     REST$copy("file", "newDestination/file", uuid)
150
151     expect_that(fakeHttp$URLIsProperlyConfigured, is_true())
152     expect_that(fakeHttp$requestHeaderContainsAuthorizationField, is_true())
153     expect_that(fakeHttp$requestHeaderContainsDestinationField, is_true())
154     expect_that(fakeHttp$numberOfCOPYRequests, equals(1))
155 })
156
157 test_that("copy raises exception if server response code is not between 200 and 300", {
158
159     uuid <- "aaaaa-j7d0g-ccccccccccccccc"
160     response <- list()
161     response$status_code <- 404
162     fakeHttp <- FakeHttpRequest$new(serverResponse = response)
163
164     REST <- RESTService$new("token", "https://host/",
165                             fakeHttp, HttpParser$new(),
166                             0, "https://webDavHost/")
167
168     expect_that(REST$copy("file", "newDestination/file", uuid),
169                 throws_error("Server code: 404"))
170 })
171
172 test_that("getCollectionContent retreives correct content from WebDAV server", {
173
174     uuid <- "aaaaa-j7d0g-ccccccccccccccc"
175     expectedURL <- "https://webDavHost/c=aaaaa-j7d0g-ccccccccccccccc"
176     returnContent <- list()
177     returnContent$status_code <- 200
178     returnContent$content <- c("animal", "animal/dog", "ball")
179
180     fakeHttp <- FakeHttpRequest$new(expectedURL, returnContent)
181
182     REST <- RESTService$new("token", "https://host/",
183                             fakeHttp, FakeHttpParser$new(),
184                             0, "https://webDavHost/")
185
186     returnResult <- REST$getCollectionContent(uuid)
187     returnedContentMatchExpected <- all.equal(returnResult,
188                                               c("animal", "animal/dog", "ball"))
189
190     expect_that(returnedContentMatchExpected, is_true())
191     expect_that(fakeHttp$requestHeaderContainsAuthorizationField, is_true())
192 })
193
194 test_that("getCollectionContent raises exception if server returns empty response", {
195
196     uuid <- "aaaaa-j7d0g-ccccccccccccccc"
197     response <- ""
198     fakeHttp <- FakeHttpRequest$new(serverResponse = response)
199
200     REST <- RESTService$new("token", "https://host/",
201                             fakeHttp, FakeHttpParser$new(),
202                             0, "https://webDavHost/")
203
204     expect_that(REST$getCollectionContent(uuid),
205                 throws_error("Response is empty, request may be misconfigured"))
206 })
207
208 test_that("getCollectionContent parses server response", {
209
210     uuid <- "aaaaa-j7d0g-ccccccccccccccc"
211     fakeHttpParser <- FakeHttpParser$new()
212     REST <- RESTService$new("token", "https://host/",
213                             FakeHttpRequest$new(), fakeHttpParser,
214                             0, "https://webDavHost/")
215
216     REST$getCollectionContent(uuid)
217
218     expect_that(fakeHttpParser$parserCallCount, equals(1))
219 })
220
221 test_that("getCollectionContent raises exception if server returns empty response", {
222
223     uuid <- "aaaaa-j7d0g-ccccccccccccccc"
224     response <- ""
225     fakeHttp <- FakeHttpRequest$new(serverResponse = response)
226
227     REST <- RESTService$new("token", "https://host/",
228                             fakeHttp, FakeHttpParser$new(),
229                             0, "https://webDavHost/")
230
231     expect_that(REST$getCollectionContent(uuid),
232                 throws_error("Response is empty, request may be misconfigured"))
233 })
234
235 test_that(paste("getCollectionContent raises exception if server",
236                 "response code is not between 200 and 300"), {
237
238     uuid <- "aaaaa-j7d0g-ccccccccccccccc"
239     response <- list()
240     response$status_code <- 404
241     fakeHttp <- FakeHttpRequest$new(serverResponse = response)
242
243     REST <- RESTService$new("token", "https://host/",
244                             fakeHttp, HttpParser$new(),
245                             0, "https://webDavHost/")
246
247     expect_that(REST$getCollectionContent(uuid),
248                 throws_error("Server code: 404"))
249 })
250
251
252 test_that("getResourceSize calls REST service properly", {
253
254     uuid <- "aaaaa-j7d0g-ccccccccccccccc"
255     expectedURL <- "https://webDavHost/c=aaaaa-j7d0g-ccccccccccccccc/file"
256     response <- list()
257     response$status_code <- 200
258     response$content <- c(6, 2, 931, 12003)
259     fakeHttp <- FakeHttpRequest$new(expectedURL, response)
260
261     REST <- RESTService$new("token", "https://host/",
262                             fakeHttp, FakeHttpParser$new(),
263                             0, "https://webDavHost/")
264
265     returnResult <- REST$getResourceSize("file", uuid)
266     returnedContentMatchExpected <- all.equal(returnResult,
267                                               c(6, 2, 931, 12003))
268
269     expect_that(fakeHttp$URLIsProperlyConfigured, is_true())
270     expect_that(fakeHttp$requestHeaderContainsAuthorizationField, is_true())
271     expect_that(returnedContentMatchExpected, is_true())
272 })
273
274 test_that("getResourceSize raises exception if server returns empty response", {
275
276     uuid <- "aaaaa-j7d0g-ccccccccccccccc"
277     response <- ""
278     fakeHttp <- FakeHttpRequest$new(serverResponse = response)
279
280     REST <- RESTService$new("token", "https://host/",
281                             fakeHttp, FakeHttpParser$new(),
282                             0, "https://webDavHost/")
283
284     expect_that(REST$getResourceSize("file", uuid),
285                 throws_error("Response is empty, request may be misconfigured"))
286 })
287
288 test_that(paste("getResourceSize raises exception if server",
289                 "response code is not between 200 and 300"), {
290
291     uuid <- "aaaaa-j7d0g-ccccccccccccccc"
292     response <- list()
293     response$status_code <- 404
294     fakeHttp <- FakeHttpRequest$new(serverResponse = response)
295
296     REST <- RESTService$new("token", "https://host/",
297                             fakeHttp, HttpParser$new(),
298                             0, "https://webDavHost/")
299
300     expect_that(REST$getResourceSize("file", uuid),
301                 throws_error("Server code: 404"))
302 })
303
304 test_that("getResourceSize parses server response", {
305
306     uuid <- "aaaaa-j7d0g-ccccccccccccccc"
307     fakeHttpParser <- FakeHttpParser$new()
308     REST <- RESTService$new("token", "https://host/",
309                             FakeHttpRequest$new(), fakeHttpParser,
310                             0, "https://webDavHost/")
311
312     REST$getResourceSize("file", uuid)
313
314     expect_that(fakeHttpParser$parserCallCount, equals(1))
315 })
316
317 test_that("read calls REST service properly", {
318
319     uuid <- "aaaaa-j7d0g-ccccccccccccccc"
320     expectedURL <- "https://webDavHost/c=aaaaa-j7d0g-ccccccccccccccc/file"
321     serverResponse <- list()
322     serverResponse$status_code <- 200
323     serverResponse$content <- "file content"
324
325     fakeHttp <- FakeHttpRequest$new(expectedURL, serverResponse)
326
327     REST <- RESTService$new("token", "https://host/",
328                             fakeHttp, FakeHttpParser$new(),
329                             0, "https://webDavHost/")
330
331     returnResult <- REST$read("file", uuid, "text", 1024, 512)
332
333     expect_that(fakeHttp$URLIsProperlyConfigured, is_true())
334     expect_that(fakeHttp$requestHeaderContainsAuthorizationField, is_true())
335     expect_that(fakeHttp$requestHeaderContainsRangeField, is_true())
336     expect_that(returnResult, equals("file content"))
337 })
338
339 test_that("read raises exception if server response code is not between 200 and 300", {
340
341     uuid <- "aaaaa-j7d0g-ccccccccccccccc"
342     response <- list()
343     response$status_code <- 404
344     fakeHttp <- FakeHttpRequest$new(serverResponse = response)
345
346     REST <- RESTService$new("token", "https://host/",
347                             fakeHttp, HttpParser$new(),
348                             0, "https://webDavHost/")
349
350     expect_that(REST$read("file", uuid),
351                 throws_error("Server code: 404"))
352 })
353
354 test_that("read raises exception if contentType is not valid", {
355
356     uuid <- "aaaaa-j7d0g-ccccccccccccccc"
357     fakeHttp <- FakeHttpRequest$new()
358
359     REST <- RESTService$new("token", "https://host/",
360                             fakeHttp, HttpParser$new(),
361                             0, "https://webDavHost/")
362
363     expect_that(REST$read("file", uuid, "some invalid content type"),
364                 throws_error("Invalid contentType. Please use text or raw."))
365 })
366
367 test_that("read parses server response", {
368
369     uuid <- "aaaaa-j7d0g-ccccccccccccccc"
370     fakeHttpParser <- FakeHttpParser$new()
371     REST <- RESTService$new("token", "https://host/",
372                             FakeHttpRequest$new(), fakeHttpParser,
373                             0, "https://webDavHost/")
374
375     REST$read("file", uuid, "text", 1024, 512)
376
377     expect_that(fakeHttpParser$parserCallCount, equals(1))
378 })
379
380 test_that("write calls REST service properly", {
381
382     fileContent <- "new file content"
383     uuid <- "aaaaa-j7d0g-ccccccccccccccc"
384     expectedURL <- "https://webDavHost/c=aaaaa-j7d0g-ccccccccccccccc/file"
385     fakeHttp <- FakeHttpRequest$new(expectedURL)
386
387     REST <- RESTService$new("token", "https://host/",
388                             fakeHttp, FakeHttpParser$new(),
389                             0, "https://webDavHost/")
390
391     REST$write("file", uuid, fileContent, "text/html")
392
393     expect_that(fakeHttp$URLIsProperlyConfigured, is_true())
394     expect_that(fakeHttp$requestBodyIsProvided, is_true())
395     expect_that(fakeHttp$requestHeaderContainsAuthorizationField, is_true())
396     expect_that(fakeHttp$requestHeaderContainsContentTypeField, is_true())
397 })
398
399 test_that("write raises exception if server response code is not between 200 and 300", {
400
401     uuid <- "aaaaa-j7d0g-ccccccccccccccc"
402     fileContent <- "new file content"
403     response <- list()
404     response$status_code <- 404
405     fakeHttp <- FakeHttpRequest$new(serverResponse = response)
406
407     REST <- RESTService$new("token", "https://host/",
408                             fakeHttp, HttpParser$new(),
409                             0, "https://webDavHost/")
410
411     expect_that(REST$write("file", uuid, fileContent, "text/html"),
412                 throws_error("Server code: 404"))
413 })
414
415 test_that("getConnection calls REST service properly", {
416     uuid <- "aaaaa-j7d0g-ccccccccccccccc"
417     fakeHttp <- FakeHttpRequest$new()
418
419     REST <- RESTService$new("token", "https://host/",
420                             fakeHttp, FakeHttpParser$new(),
421                             0, "https://webDavHost/")
422
423     REST$getConnection("file", uuid, "r")
424
425     expect_that(fakeHttp$numberOfgetConnectionCalls, equals(1))
426 })