Skip to content

Commit

Permalink
Test download_file
Browse files Browse the repository at this point in the history
  • Loading branch information
alice-hannah committed May 9, 2024
1 parent 5a7b7d8 commit 0df570f
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 0 deletions.
13 changes: 13 additions & 0 deletions tests/testthat/api/assets/test_document.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
structure(list(method = "GET", url = "https://api/assets/test_document",
status_code = 200L, headers = structure(list(Date = "Mon, 08 Apr 2024 16:38:16 GMT",
`Content-Type` = "application/json", `Transfer-Encoding` = "chunked",
Connection = "keep-alive", `X-CONNECT-MDC` = "apigyQdrHej",
`X-Frame-Options` = "deny", `X-XSS-Protection` = "1; mode=block",
`Cache-Control` = "no-cache, no-store", Expires = "0",
Pragma = "no-cache", `Strict-Transport-Security` = "max-age=31536000 ; includeSubDomains",
`Content-Security-Policy` = "script-src 'self' 'unsafe-inline'",
`X-Content-Type-Options` = "nosniff", `Referrer-Policy` = "strict-origin-when-cross-origin",
`Feature-Policy` = "vibrate 'none'; geolocation 'none'",
Authorization = "REDACTED", `Set-Cookie` = "REDACTED"), class = "httr2_headers"),
body = charToRaw("{\"model\":[\"Asset\"],\"uuid\":[\"test_document\"],\"name\":[\"test_document_name\"],\"workspaceUuid\":[\"test_workspace_uuid\"],\"type\":[\"DOCUMENT\"],\"annotationCount\":[0],\"commentCount\":[0],\"contentVersion\":[2],\"createdTime\":[1712567186000],\"modifiedTime\":[1712567224000],\"extension\":[\"txt\"],\"fileSize\":[23],\"isLocked\":[false],\"isLockedInOffice\":[false],\"modifiedByUuid\":[\"test_modified_uuid\"],\"previewStatus\":[\"COMPLETED\"],\"status\":[\"COMPLETE\"],\"mandatoryTagsPresent\":[true]}"),
cache = new.env(parent = emptyenv())), class = "httr2_response")
13 changes: 13 additions & 0 deletions tests/testthat/api/documents/test_document/download.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
structure(list(method = "GET", url = "https://api/documents/test_document/download",
status_code = 200L, headers = structure(list(Date = "Wed, 10 Apr 2024 15:19:02 GMT",
`Content-Type` = "application/json", `Content-Length` = "29179",
Connection = "keep-alive", `X-CONNECT-MDC` = "apiOcfLjCpc",
`X-Frame-Options` = "deny", `X-XSS-Protection` = "1; mode=block",
`Cache-Control` = "no-cache, no-store", Expires = "0",
Pragma = "no-cache", `Strict-Transport-Security` = "max-age=31536000 ; includeSubDomains",
`Content-Security-Policy` = "script-src 'self' 'unsafe-inline'",
`X-Content-Type-Options` = "nosniff", `Referrer-Policy` = "strict-origin-when-cross-origin",
`Feature-Policy` = "vibrate 'none'; geolocation 'none'",
Authorization = "REDACTED", `Set-Cookie` = "REDACTED",
`Content-Disposition` = "attachment; filename=\"test_document_name.txt\"; filename*=UTF-8''test_document_name.txt"), class = "httr2_headers"),
body = charToRaw(""), cache = new.env(parent = emptyenv())), class = "httr2_response")
50 changes: 50 additions & 0 deletions tests/testthat/test-download_file.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
without_internet({

test_that("Valid request", {

expect_GET(
download_file(document_uuid = "test_document",
folder = "test"),
"https://secure.objectiveconnect.co.uk/publicapi/1/assets/test_document"
)

})

})

with_mock_api({

with_envvar(

new = c("OBJECTIVER_USR" = "test_usr",
"OBJECTIVER_PWD" = "test_pwd"),

code = {

test_that("Function returns invisible", {

expect_invisible(
suppressMessages(download_file(document_uuid = "test_document",
folder = tempdir()))
)

})

test_that("Function returns success message", {

expect_message(download_file(document_uuid = "test_document",
folder = tempdir()))

})

test_that("Function creates new file", {
suppressMessages(download_file(document_uuid = "test_document",
folder = tempdir()))
expect_true(file.exists(paste0(tempdir(), "/test_document_name.txt")))
})

file.remove(paste0(tempdir(), "/test_document_name.txt"))

})

})

0 comments on commit 0df570f

Please sign in to comment.