Skip to content

Commit

Permalink
Fix #116: Add support for passphrase to settings
Browse files Browse the repository at this point in the history
  • Loading branch information
Florian Mayer authored and Florian Mayer committed Mar 15, 2021
1 parent c9eebe7 commit bd6a80c
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tic.yml
Expand Up @@ -34,7 +34,7 @@ jobs:
- { os: ubuntu-20.04, r: "release"}
- { os: ubuntu-20.04, r: "oldrel" }
- { os: ubuntu-18.04, r: "devel" }
- { os: ubuntu-18.04, r: "release"}
- { os: ubuntu-18.04, r: "release" }
- { os: ubuntu-18.04, r: "oldrel" }

env:
Expand Down
2 changes: 1 addition & 1 deletion DESCRIPTION
@@ -1,7 +1,7 @@
Type: Package
Package: ruODK
Title: An R Client for the ODK Central API
Version: 0.9.7
Version: 0.9.8
Authors@R:
c(person(given = c("Florian", "W."),
family = "Mayer",
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Expand Up @@ -21,6 +21,7 @@ export(form_xml)
export(get_default_fid)
export(get_default_odkc_version)
export(get_default_pid)
export(get_default_pp)
export(get_default_pw)
export(get_default_tz)
export(get_default_un)
Expand Down
8 changes: 8 additions & 0 deletions NEWS.md
Expand Up @@ -5,6 +5,14 @@
## Data
## Maintenance

# `ruODK` 0.9.8
## Major fixes
## Minor fixes
* Add support for passphrase to the `ru_setup` family (#116)
## Documentation
## Data
## Maintenance

# `ruODK` 0.9.7
## Major fixes
* `odata_submission_get()` bugfix: `handle_ru_attachments()`
Expand Down
18 changes: 18 additions & 0 deletions R/ru_setup.R
Expand Up @@ -14,6 +14,7 @@
#' \code{\link{get_default_url}},
#' \code{\link{get_default_un}},
#' \code{\link{get_default_pw}},
#' \code{\link{get_default_pp}},
#' \code{\link{get_default_tz}},
#' \code{\link{get_default_odkc_version}},
#' \code{\link{get_retries}},
Expand All @@ -39,6 +40,7 @@ ru_settings <- function() {
url = Sys.getenv("ODKC_URL", ""),
un = Sys.getenv("ODKC_UN", ""),
pw = Sys.getenv("ODKC_PW", ""),
pp = Sys.getenv("ODKC_PP", ""),
tz = Sys.getenv("RU_TIMEZONE", "UTC"),
odkc_version = Sys.getenv("ODKC_VERSION", 0.8),
retries = get_retries(),
Expand Down Expand Up @@ -66,6 +68,7 @@ print.ru_settings <- function(x, ...) {
cat(" Default ODK Central URL:", x$url, "\n")
cat(" Default ODK Central Username:", x$un, "\n")
cat(" Default ODK Central Password: run ruODK::get_default_pw() to show \n")
cat(" Default ODK Central Passphrase: run ruODK::get_default_pp() to show \n")
cat(" Default Time Zone:", x$tz, "\n")
cat(" Default ODK Central Version:", x$odkc_version, "\n")
cat(" Default HTTP GET retries:", x$retries, "\n")
Expand Down Expand Up @@ -138,6 +141,7 @@ odata_svc_parse <- function(svc) {
#' @param un An ODK Central username which is the email of a "web user" in the
#' specified ODK Central instance \code{url} (optional, character).
#' @param pw The password for user \code{un} (optional, character).
#' @param pp The passphrase (optional, character) for an encrypted form.
#' @param odkc_version The ODK Central version as major/minor version, e.g. 0.8.
#' @param tz Global default time zone.
#' `ruODK`'s time zone is determined in order of precedence:
Expand Down Expand Up @@ -225,6 +229,7 @@ odata_svc_parse <- function(svc) {
#' url = "https://odkcentral.dbca.wa.gov.au",
#' un = "me@email.com",
#' pw = "...",
#' pp = "...",
#' test_url = "https://sandbox.central.getodk.org",
#' test_un = "me@email.com",
#' test_pw = "...",
Expand All @@ -244,6 +249,7 @@ ru_setup <- function(svc = NULL,
url = NULL,
un = NULL,
pw = NULL,
pp = NULL,
tz = NULL,
odkc_version = NULL,
retries = NULL,
Expand Down Expand Up @@ -275,6 +281,7 @@ ru_setup <- function(svc = NULL,
if (!is.null(url)) Sys.setenv("ODKC_URL" = url)
if (!is.null(un)) Sys.setenv("ODKC_UN" = un)
if (!is.null(pw)) Sys.setenv("ODKC_PW" = pw)
if (!is.null(pp)) Sys.setenv("ODKC_PP" = pp)
if (!is.null(tz)) Sys.setenv("RU_TIMEZONE" = tz)
if (!is.null(odkc_version)) Sys.setenv("ODKC_VERSION" = odkc_version)
if (!is.null(retries)) Sys.setenv("RU_RETRIES" = retries)
Expand Down Expand Up @@ -365,6 +372,17 @@ get_default_pw <- function() {
x
}

#' `r lifecycle::badge("stable")`
#' @export
#' @rdname ru_settings
get_default_pp <- function() {
x <- Sys.getenv("ODKC_PP")
if (identical(x, "")) {
ru_msg_warn("No default ODK Central passphrase set. ru_setup()?")
}
x
}

#' `r lifecycle::badge("stable")`
#' @export
#' @rdname ru_settings
Expand Down
4 changes: 4 additions & 0 deletions man/ru_settings.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions man/ru_setup.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions tests/testthat/test-ru_setup.R
Expand Up @@ -9,6 +9,7 @@ test_that("ru_setup does not update settings if given NULL", {
url = NULL,
un = NULL,
pw = NULL,
pp = NULL,
tz = NULL,
odkc_version = NULL,
verbose = NULL,
Expand All @@ -35,6 +36,7 @@ test_that("ru_setup does not update settings if given NULL", {
testthat::expect_equal(x$url, xx$url)
testthat::expect_equal(x$un, xx$un)
testthat::expect_equal(x$pw, xx$pw)
testthat::expect_equal(x$pp, xx$pp)
testthat::expect_equal(x$odkc_version, xx$odkc_version)
testthat::expect_equal(x$tz, xx$tz)
testthat::expect_equal(x$test_url, xx$test_url)
Expand All @@ -58,6 +60,7 @@ test_that("ru_setup resets settings if given empty string", {
url <- get_default_url()
un <- get_default_un()
pw <- get_default_pw()
pp <- get_default_pp()
tz <- get_default_tz()
odkcv <- get_default_odkc_version()
retries <- get_retries()
Expand All @@ -82,6 +85,7 @@ test_that("ru_setup resets settings if given empty string", {
url = "",
un = "",
pw = "",
pp = "",
tz = "",
odkc_version = "",
retries = "",
Expand All @@ -106,6 +110,7 @@ test_that("ru_setup resets settings if given empty string", {
testthat::expect_warning(get_default_url())
testthat::expect_warning(get_default_un())
testthat::expect_warning(get_default_pw())
testthat::expect_warning(get_default_pp())
# testthat::expect_warning(get_default_odkc_version()) # nolint
testthat::expect_warning(get_default_tz())
testthat::expect_equal(get_retries(), 3L) # fallback for empty RU_RETRIES
Expand All @@ -124,6 +129,7 @@ test_that("ru_setup resets settings if given empty string", {
testthat::expect_equal(x$url, "")
testthat::expect_equal(x$un, "")
testthat::expect_equal(x$pw, "")
testthat::expect_equal(x$pp, "")
# testthat::expect_equal(x$tz, "") # nolint
testthat::expect_equal(x$test_url, "")
testthat::expect_equal(x$test_un, "")
Expand All @@ -143,6 +149,7 @@ test_that("ru_setup resets settings if given empty string", {
url = url,
un = un,
pw = pw,
pp = pp,
tz = tz,
odkc_version = odkcv,
retries = retries,
Expand Down
9 changes: 7 additions & 2 deletions vignettes/setup.Rmd
Expand Up @@ -90,6 +90,9 @@ sourced at the beginning of a new session at
`ruODK::get_default_{pid,fid,url,un.pw}()`. These getters in turn look up their
values from environment variables.

The getters and setters are documented in the "Settings" family of the
[ruODK function reference](https://docs.ropensci.org/ruODK/reference/index.html).

A convenient way to have often used environment variables available is to add
them to `~/.Renviron` using `usethis::edit_r_environ(scope = "user")`. This
loads them freshly into a new session, eliminating the need to run `ru_setup()`.
Expand All @@ -108,6 +111,7 @@ ODKC_FID="build_Flora-Quadrat-0-2_1558575936"
ODKC_URL="https://odkcentral.dbca.wa.gov.au"
ODKC_UN="me@email.com"
ODKC_PW="..."
ODKC_PP="..."
ODKC_VERSION=0.8
# Test settings
Expand Down Expand Up @@ -142,6 +146,7 @@ can set them through `Sys.setenv()`:
Sys.setenv(ODKC_URL="https://odkc.dbca.wa.gov.au")
Sys.setenv(ODKC_UN="me@mail.com")
Sys.setenv(ODKC_PW="...")
Sys.setenv(ODKC_PP="...")
Sys.setenv(ODKC_TEST_URL="...")
Sys.setenv(ODKC_TEST_UN="...")
Sys.setenv(ODKC_TEST_PW="...")
Expand Down Expand Up @@ -173,8 +178,8 @@ ruODK::project_list(
```

An example use case are the `ruODK` tests, which explicitly set `url`, `un`,
`pw`, `pid` and `fid` from the test variables
`ruODK::get_test_{url, un, pw, pid, fid}()`. Note that this uses functions
`pw`, `pp`, `pid` and `fid` from the test variables
`ruODK::get_test_{url, un, pw, pp, pid, fid}()`. Note that this uses functions
instead of plain text versions of sensitive credentials. Alternatively,
variables could also be used to set credentials per function call.

Expand Down

0 comments on commit bd6a80c

Please sign in to comment.