Skip to content

Commit

Permalink
Merge pull request #46 from mrc-ide/mrc-4853
Browse files Browse the repository at this point in the history
Add windows username to api and configuration
  • Loading branch information
weshinsley authored Jan 3, 2024
2 parents 1c6318b + ce6b6e5 commit 962c89e
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 1 deletion.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ export(task_submit)
export(task_wait)
export(windows_authenticate)
export(windows_path)
export(windows_username)
11 changes: 10 additions & 1 deletion R/configuration.R
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,16 @@ configuration_paths <- function(root) {


configuration_drivers <- function(root) {
root$config
ret <- root$config
if (!is.null(ret$windows)) {
## This is not really part of the configuration (because windows
## username/password are saved globally), but we will add it here
## because it's useful to report, and this is where we'd want it
## reported. We could add this into the configuration itself, but
## that causes some pain for the testing there.
ret$windows$username <- windows_username()
}
ret
}


Expand Down
16 changes: 16 additions & 0 deletions R/windows.R
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,19 @@ windows_path <- function(path_local, path_remote, drive_remote) {
ns <- ensure_package("hipercow.windows", rlang::current_env())
ns$windows_path(path_local, path_remote, drive_remote)
}


##' Report the username used to log into the web portal for use with
##' the windows cluster. This may or may not be the same as your
##' local username. We may ask you to run this when helping debug
##' cluster failures.
##'
##' @title Report windows username
##'
##' @return Your username, as a string
##'
##' @export
windows_username <- function() {
ns <- ensure_package("hipercow.windows", rlang::current_env())
ns$windows_username()
}
5 changes: 5 additions & 0 deletions drivers/windows/R/dide_auth.R
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ windows_authenticate <- function() {
}


windows_username <- function() {
windows_credentials()$username
}


windows_credentials <- function() {
tryCatch({
username <- keyring::key_get("hipercow/dide/username")
Expand Down
9 changes: 9 additions & 0 deletions drivers/windows/tests/testthat/test-dide-auth.R
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,15 @@ test_that("can fetch dide credentials", {
})


test_that("can fetch dide username", {
mock_credentials <- mockery::mock(credentials("alice", "pw"))
mockery::stub(windows_username, "windows_credentials", mock_credentials)
expect_equal(windows_username(), "alice")
mockery::expect_called(mock_credentials, 1)
expect_equal(mockery::mock_args(mock_credentials)[[1]], list())
})


test_that("can store credentials in keychain", {
## This is pretty grim, but I've not seen another approach to this.
mock_keyring_is_locked <- mockery::mock(TRUE)
Expand Down
17 changes: 17 additions & 0 deletions man/windows_username.Rd

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

25 changes: 25 additions & 0 deletions tests/testthat/test-configuration.R
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,28 @@ test_that("can report about everything being missing", {
c("x" = "hipercow.windows is not installed",
"x" = "conan2 is not installed"))
})


test_that("can add windows username to configuration", {
elsewhere_register()
path_here <- withr::local_tempdir()
path_there <- withr::local_tempdir()
init_quietly(path_here)
init_quietly(path_there)
root <- hipercow_root(path_here)
suppressMessages(
hipercow_configure("elsewhere", path = path_there, root = path_here))

mock_username <- mockery::mock("alice")
mockery::stub(configuration_drivers, "windows_username", mock_username)
res <- configuration_drivers(root)
mockery::expect_called(mock_username, 0)
expect_equal(res, root$config)

root$config <- c(root$config, list(windows = list(a = 1, b = 2)))
res <- configuration_drivers(root)
mockery::expect_called(mock_username, 1)
cmp <- root$config
cmp$windows$username <- "alice"
expect_equal(res, cmp)
})
17 changes: 17 additions & 0 deletions tests/testthat/test-windows.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,20 @@ test_that("windows authenticate passes through to hipercow.windows", {
expect_equal(mockery::mock_args(mock_pkg$windows_authenticate)[[1]],
list())
})


test_that("windows username passes through to hipercow.windows", {
mock_pkg <- list(windows_username = mockery::mock())
mock_ensure_package <- mockery::mock(mock_pkg)
mockery::stub(windows_username, "ensure_package", mock_ensure_package)
windows_username()

mockery::expect_called(mock_ensure_package, 1)
args <- mockery::mock_args(mock_ensure_package)[[1]]
expect_equal(args[[1]], "hipercow.windows")
expect_type(args[[2]], "environment")

mockery::expect_called(mock_pkg$windows_username, 1)
expect_equal(mockery::mock_args(mock_pkg$windows_username)[[1]],
list())
})

0 comments on commit 962c89e

Please sign in to comment.