Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better username msgs #161

Merged
merged 4 commits into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: hipercow
Title: High Performance Computing
Version: 1.0.50
Version: 1.0.51
Authors@R: c(person("Rich", "FitzJohn", role = c("aut", "cre"),
email = "[email protected]"),
person("Wes", "Hinsley", role = "aut"),
Expand Down
2 changes: 1 addition & 1 deletion drivers/windows/DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: hipercow.windows
Title: DIDE HPC Support for Windows
Version: 1.0.50
Version: 1.0.51
Authors@R: c(person("Rich", "FitzJohn", role = c("aut", "cre"),
email = "[email protected]"),
person("Wes", "Hinsley", role = "aut"),
Expand Down
17 changes: 17 additions & 0 deletions drivers/windows/R/dide_auth.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,22 @@ windows_authenticate <- function(call = NULL) {

username <- check_username(
readline_with_default("DIDE username", windows_guess_username()))
if (grepl("[# ]", username)) {
spaces <- sum(gregexpr(" ", username)[[1]] > 0)
hashes <- sum(gregexpr("#", username)[[1]] > 0)
spaces <- if (spaces > 0) cli::pluralize("{spaces} space{?s}") else ""
hashes <- if (hashes > 0) cli::pluralize("{hashes} hash{?es}") else ""
if ((nchar(spaces) > 0) && (nchar(hashes) > 0)) {
spaces <- paste(spaces, "and ")
}

cli::cli_abort(
c("The username you provided does not look valid.",
x = "It contains {spaces}{hashes}",
i = "I tried to login as user {.strong {username}}",
i = "Please try again with 'windows_authenticate()'"),
call = call)
}
keyring::key_set("hipercow/dide/password", username = username)
password <- keyring::key_get("hipercow/dide/password", username = username)

Expand All @@ -39,6 +55,7 @@ windows_authenticate <- function(call = NULL) {
cli::cli_abort(
c("That username/password combination did not work, I'm afraid",
x = result$message,
i = "The username provided was {.strong {username}}",
i = "Please try again with 'windows_authenticate()'"),
call = call)
}
Expand Down
23 changes: 23 additions & 0 deletions drivers/windows/tests/testthat/test-dide-auth.R
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,28 @@ test_that("can store credentials in keychain", {
list("hipercow/dide/username", password = "alice"))
})

test_that("invalid username rejected", {
mock_keyring_is_locked <- mockery::mock(FALSE)
mock_guess <- mockery::mock("bob")
mock_readline <- mockery::mock("# I pasted this")
mock_login <- mockery::mock(stop("invalid credentials"))

mockery::stub(windows_authenticate, "keyring::keyring_is_locked",
mock_keyring_is_locked)
mockery::stub(windows_authenticate, "windows_guess_username", mock_guess)
mockery::stub(windows_authenticate, "readline_with_default", mock_readline)
mockery::stub(windows_authenticate, "api_client_login", mock_login)

err <- expect_error(
suppressMessages(windows_authenticate()),
"The username you provided does not look valid")
expect_equal(
err$body,
c(x = "It contains 3 spaces and 1 hash",
i = "I tried to login as user # I pasted this",
i = "Please try again with 'windows_authenticate()'"))

})

test_that("delete username on error", {
mock_keyring_is_locked <- mockery::mock(FALSE)
Expand Down Expand Up @@ -128,6 +150,7 @@ test_that("delete username on error", {
expect_equal(
err$body,
c(x = "invalid credentials",
i = "The username provided was alice",
i = "Please try again with 'windows_authenticate()'"))

mockery::expect_called(mock_keyring_is_locked, 1)
Expand Down
Loading