Skip to content

Commit

Permalink
Avoid both .Renviron mocking & signature expansion (fix #110)
Browse files Browse the repository at this point in the history
Reverts 4ca7f5d. Insert `…user:pw@…` into the download URL
to enable testing via this old-school authentication.
katrinleinweber committed Aug 11, 2019

Verified

This commit was signed with the committer’s verified signature.
Okabe-Junya Junya Okabe
1 parent c83f089 commit 5512a1e
Showing 3 changed files with 19 additions and 32 deletions.
20 changes: 13 additions & 7 deletions R/util-download.R
Original file line number Diff line number Diff line change
@@ -10,17 +10,23 @@
#' @import httr
#' @importFrom jsonlite fromJSON
download <- function(URL) {
message(URLs_to_IDs(URL), " ", appendLF = FALSE)

URL_p1 <- "^https:\\/\\/"
URL_p2 <- "bacdive\\.dsmz\\.de\\/api\\/bacdive"

# Check for valid BacDive URL
if (!grepl("^https:\\/\\/bacdive\\.dsmz\\.de\\/api\\/bacdive", URL) |
!grepl("?format=json$", URL)) {
# Check for "proper credentials" test in URL (see test-download.R),
# or check for valid, but non-test BacDive URL
if (grepl(paste0(URL_p1, "\\w+", ":", "\\w+", URL_p2), URL)) {
response <- GET(URL)
} else if (
!grepl(paste0(URL_p1, URL_p2), URL) | !grepl("?format=json$", URL)) {
stop("I refuse to connect to", URL, "because it's not a BacDive URL!")
} else {
cred <- get_credentials()
response <- GET(URL, authenticate(cred[1], cred[2]))
}

message(URLs_to_IDs(URL), " ", appendLF = FALSE)
cred <- get_credentials()

response <- GET(URL, authenticate(cred[1], cred[2]))
payload <- content(response, as = "text", encoding = "UTF-8")
data <- fromJSON(payload)

10 changes: 4 additions & 6 deletions R/util-prepare_Renviron.R
Original file line number Diff line number Diff line change
@@ -13,7 +13,10 @@
prepare_Renviron <- function() {
r_env_file <- construct_Renviron_path()

if (!file.exists(r_env_file)) create_new(r_env_file)
if (!file.exists(r_env_file)) {
file.create(r_env_file)
Sys.chmod(r_env_file, mode = "0600")
}

message <-
"add your BacDive login credentials.\n# See https://github.com/tibhannover/BacDiveR/\n# for more installation instructions."
@@ -45,8 +48,3 @@ prepare_Renviron <- function() {
utils::file.edit(r_env_file)
}
}

create_new <- function(r_env_file) {
file.create(r_env_file)
Sys.chmod(r_env_file, mode = "0600")
}
21 changes: 2 additions & 19 deletions tests/testthat/test-download.R
Original file line number Diff line number Diff line change
@@ -1,24 +1,7 @@
random <- paste(sample(letters, 16), collapse = "")

test_that("Downloading without preper credentials raises an error", {

# arrange
r_env_file <- construct_Renviron_path()
r_env_backup <- paste(r_env_file, "backup", sep = ".")
file.rename(r_env_file, r_env_backup)

## mock .Renviron file
create_new(r_env_file)
for (x in c("email", "password")) {
line <- paste0("BacDive_", x, "=", random, collapse = "")
write(line, r_env_file, append = TRUE)
}

# act & assert
expect_error(download(construct_url(717)), regexp = "Check your credentials")

# clean up
file.copy(r_env_backup, r_env_file, overwrite = TRUE)
test_that("Downloading without proper credentials raises an error", {
expect_error(download(paste0("https://", random, ":", random, "@", "bacdive.dsmz.de/api/bacdive/bacdive_id/717/?format=json", collapse = TRUE)))
})

test_that("Downloader refuses unexpected URLs", {

0 comments on commit 5512a1e

Please sign in to comment.