diff --git a/R/keyring.R b/R/keyring.R index 5fa1af5..8689f56 100644 --- a/R/keyring.R +++ b/R/keyring.R @@ -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)) @@ -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) diff --git a/tests/test-keyring.R b/tests/test-keyring.R new file mode 100644 index 0000000..bcfe791 --- /dev/null +++ b/tests/test-keyring.R @@ -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') +