Skip to content

Commit

Permalink
add testing for keyring; improve messaging to user for setting api token
Browse files Browse the repository at this point in the history
  • Loading branch information
dewittpe committed Sep 19, 2024
1 parent ff831f9 commit afb183c
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 2 deletions.
8 changes: 6 additions & 2 deletions R/keyring.R
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ REDCapExporter_add_api_token <- function(project, keyring = "REDCapExporter", us
cannot_get_token <- inherits(try(kr$get(service = project, username = user, keyring = keyring), silent = TRUE), "try-error")

if (overwrite || cannot_get_token) {
message(sprintf("Please enter your API token (at the Password: prompt)\n\nProject: %s\nUser: %s\nKeyring: %s\n",
message(sprintf("Please enter your API token\n\nProject: %s\nUser: %s\nKeyring: %s\n",
project, user, keyring))
kr$set(service = project, username = user, keyring = keyring)
kr$set(service = project, username = user, keyring = keyring, prompt: "API Token:")
} else {
message(sprintf("API token exisits for\n\nProject: %s\nUser: %s\nKeyring: %s\n",
project, user, keyring))
Expand All @@ -92,6 +92,10 @@ REDCapExporter_get_api_token <- function(project, keyring = "REDCapExporter", us
password <- ""
}

if (is.null(user)) {
user <- Sys.info()[["user"]]
}

kr <- keyring::backend_file$new()
kr$keyring_unlock(keyring = keyring, password = password)
token <- kr$get(service = project, username = user, keyring = keyring)
Expand Down
50 changes: 50 additions & 0 deletions tests/test-keyring.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
library(REDCapExporter)

# Test if a new keyring can be built
kr <- keyring::backend_file$new()
try(kr$keyring_delete("testingring"), silent = TRUE)

x <- tryCatch(REDCapExporter_keyring_check("testingring"), message = function(m) {m})
stopifnot(identical(x$message, "File based keyring testingring has been created\n"))
x <- tryCatch(REDCapExporter_keyring_check("testingring"), message = function(m) {m})
stopifnot(identical(x$message, "File based keyring testingring exists\n"))

# Expect that this will error because we are not interactive and a password
# prompt cannot be filled in
x <-
tryCatch(
REDCapExporter_add_api_token(project = 'testingproject', keyring = 'testingring'),
error = function(e) e
)

stopifnot(inherits(x, "error"))
stopifnot(isTRUE(grepl("Aborted setting keyring key", x$message)))

# expect the get api token to fail as the token for the testingproject has not
# been set
x <-
tryCatch(
REDCapExporter_get_api_token(project = 'testingproject', keyring = 'testingring'),
error = function(e) e
)

stopifnot(inherits(x, "error"))
stopifnot(isTRUE(grepl("specified item could not be found in the keychain", x$message)))

# create token
kr$set_with_value(service = "testingproject", password = "testingTOKEN", keyring = "testingring")


# verify you can get the token
stopifnot(
identical(
REDCapExporter_get_api_token(project = 'testingproject', keyring = 'testingring')
,
"testingTOKEN"
))


# the REDCapExporter_add_api_token should return a message that the token
# already exists
#REDCapExporter_add_api_token(project = 'testingproject', keyring = 'testingring')

0 comments on commit afb183c

Please sign in to comment.