From 5512a1ecf152248be67403227d3f2991962e1695 Mon Sep 17 00:00:00 2001 From: Katrin Leinweber Date: Fri, 9 Aug 2019 22:02:08 +0200 Subject: [PATCH] Avoid both .Renviron mocking & signature expansion (fix #110) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Reverts 4ca7f5dc. Insert `…user:pw@…` into the download URL to enable testing via this old-school authentication. --- R/util-download.R | 20 +++++++++++++------- R/util-prepare_Renviron.R | 10 ++++------ tests/testthat/test-download.R | 21 ++------------------- 3 files changed, 19 insertions(+), 32 deletions(-) diff --git a/R/util-download.R b/R/util-download.R index 0c53ad7..64698f3 100644 --- a/R/util-download.R +++ b/R/util-download.R @@ -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) diff --git a/R/util-prepare_Renviron.R b/R/util-prepare_Renviron.R index 077c00e..e2e3bec 100644 --- a/R/util-prepare_Renviron.R +++ b/R/util-prepare_Renviron.R @@ -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") -} diff --git a/tests/testthat/test-download.R b/tests/testthat/test-download.R index 4d199f0..3bd1924 100644 --- a/tests/testthat/test-download.R +++ b/tests/testthat/test-download.R @@ -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", {