From 5515bf3a0606d80f1d5383b0ac34051c07353c06 Mon Sep 17 00:00:00 2001 From: Rich FitzJohn Date: Mon, 8 Jan 2024 17:37:25 +0000 Subject: [PATCH] Log in again after expiry --- DESCRIPTION | 2 +- drivers/windows/DESCRIPTION | 2 +- drivers/windows/R/web.R | 5 +++- drivers/windows/tests/testthat/test-web.R | 30 ++++++++++++++++++----- 4 files changed, 30 insertions(+), 9 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 01df5c48..49a198a5 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: hipercow Title: High Performance Computing -Version: 0.2.7 +Version: 0.2.8 Authors@R: c(person("Rich", "FitzJohn", role = c("aut", "cre"), email = "rich.fitzjohn@gmail.com"), person("Wes", "Hinsley", role = "aut"), diff --git a/drivers/windows/DESCRIPTION b/drivers/windows/DESCRIPTION index 93cd7763..5d2fc1d0 100644 --- a/drivers/windows/DESCRIPTION +++ b/drivers/windows/DESCRIPTION @@ -1,6 +1,6 @@ Package: hipercow.windows Title: DIDE HPC Support for Windows -Version: 0.2.7 +Version: 0.2.8 Authors@R: c(person("Rich", "FitzJohn", role = c("aut", "cre"), email = "rich.fitzjohn@gmail.com"), person("Wes", "Hinsley", role = "aut"), diff --git a/drivers/windows/R/web.R b/drivers/windows/R/web.R index 6ab41123..736a86d4 100644 --- a/drivers/windows/R/web.R +++ b/drivers/windows/R/web.R @@ -152,7 +152,10 @@ api_client <- R6::R6Class( r <- verb(url, ...) status <- httr::status_code(r) if (status %in% c(401, 403)) { - stop("Please login first") + cli::cli_alert_warning( + "Trying to login again, previous session likely expired") + self$login(public, refresh = TRUE) + r <- verb(url, ...) } httr::stop_for_status(r) r diff --git a/drivers/windows/tests/testthat/test-web.R b/drivers/windows/tests/testthat/test-web.R index 7880b1a9..e24b54c3 100644 --- a/drivers/windows/tests/testthat/test-web.R +++ b/drivers/windows/tests/testthat/test-web.R @@ -78,24 +78,42 @@ test_that("logout uses correct endpoint", { test_that("request handles http requests", { verb <- mockery::mock(mock_response(200), - mock_response(403), mock_response(400)) credentials <- example_credentials() cl <- api_client$new(credentials) data <- list(a = 1, b = 2) cl$request(verb, "/path/to", data = data, public = TRUE) - expect_error( - cl$request(verb, "/path/to", data = data, public = TRUE), - "Please login first") expect_error( cl$request(verb, "/path/to", data = data, public = TRUE), "400") - mockery::expect_called(verb, 3) + mockery::expect_called(verb, 2) + expect_equal( + mockery::mock_args(verb), + rep(list(list("https://mrcdata.dide.ic.ac.uk/hpc/path/to", + data = data)), 2)) +}) + + +test_that("request logs back in after expiry", { + verb <- mockery::mock(mock_response(403), + mock_response(200)) + credentials <- example_credentials() + cl <- api_client$new(credentials) + + mock_login <- mockery::mock() + mockery::stub(cl$request, "self$login", mock_login) + + expect_message( + r <- cl$request(verb, "/path/to", data = data, public = FALSE), + "Trying to login again, previous session likely expired") + expect_equal(r, mock_response(200)) + + mockery::expect_called(verb, 2) expect_equal( mockery::mock_args(verb), rep(list(list("https://mrcdata.dide.ic.ac.uk/hpc/path/to", - data = data)), 3)) + data = data)), 2)) })