Skip to content

Commit

Permalink
Make create_token and auth_setup testable
Browse files Browse the repository at this point in the history
add testable `rtoot_ask` to replace `readline`
  • Loading branch information
chainsawriot committed Nov 28, 2022
1 parent 5821642 commit c316899
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 9 deletions.
12 changes: 3 additions & 9 deletions R/auth.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@
#' @export
auth_setup <- function(instance = NULL, type = NULL, name = NULL, path = NULL, clipboard = FALSE, verbose = TRUE) {
while (is.null(instance) || instance == "") {
instance <- readline(prompt = "On which instance do you want to authenticate (e.g., \"mastodon.social\")? ")
instance <- rtoot_ask(prompt = "On which instance do you want to authenticate (e.g., \"mastodon.social\")? ", pass = FALSE)
}
client <- get_client(instance = instance)
if (!isTRUE(type %in% c("public", "user"))) {
type <- c("public", "user")[utils::menu(c("public", "user"), title = "What type of token do you want?")]
type <- c("public", "user")[rtoot_menu(choices = c("public", "user"), title = "What type of token do you want?", verbose = TRUE)]
}
token <- process_created_token(create_token(client, type = type), name = name, path = path, clipboard = clipboard, verify = TRUE)
return(token) ## explicit
Expand Down Expand Up @@ -94,13 +94,7 @@ create_token <- function(client, type = "public"){
scope='read write follow',
response_type="code"
))
passFun <- readline
if (requireNamespace("rstudioapi", quietly = TRUE)) {
if (rstudioapi::isAvailable()) {
passFun <- rstudioapi::askForPassword
}
}
auth_code <- passFun(prompt = "enter authorization code: ")
auth_code <- rtoot_ask(prompt = "enter authorization code: ", pass = TRUE, check_rstudio = TRUE, default = "")
auth2 <- httr::POST(httr::modify_url(url = url, path = "oauth/token"),body=list(
client_id=client$client_id ,
client_secret=client$client_secret,
Expand Down
21 changes: 21 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -174,3 +174,24 @@ rtoot_menu <- function(choices = c("yes", "no"), title, default = 2L, verbose =
}
return(utils::menu(choices = choices, title = title))
}

## A kind of drop-in replacement of base:readline, with a plus
rtoot_ask <- function(prompt = "enter authorization code: ", pass = TRUE, check_rstudio = TRUE, default = "pass", verbose = TRUE) {
if (!is.null(options("rtoot_cheatcode")$rtoot_cheatcode)) {
if (options("rtoot_cheatcode")$rtoot_cheatcode == "uuddlrlrba") {
sayif(verbose, prompt)
return(options("rtoot_cheat_ask_answer")$rtoot_cheat_ask_answer)
}
}
if (isFALSE(interactive())) {
sayif(verbose, prompt)
return(default)
}
passFun <- readline
if (isTRUE(pass) && isTRUE(check_rstudio) && (requireNamespace("rstudioapi", quietly = TRUE))) {
if (rstudioapi::isAvailable()) {
passFun <- rstudioapi::askForPassword
}
}
return(passFun(prompt = prompt))
}
6 changes: 6 additions & 0 deletions tests/testthat/test-auth_create_token.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
test_that("defensive", {
fake_client <- list()
class(fake_client) <- "rtoot_cilent"
expect_error(create_token(iris), "client is not an object of type rtoot_client")
expect_error(create_token(fake_client, type = "elon"), "should be one of")
})

0 comments on commit c316899

Please sign in to comment.