From c0edeb4db560de4c10b67c322be3fb2d2d4af953 Mon Sep 17 00:00:00 2001 From: Romain Lesur Date: Thu, 31 Oct 2019 03:25:14 +0100 Subject: [PATCH] use last cached token as fallback value --- DESCRIPTION | 2 +- R/utils.R | 8 ++++++++ tests/testthat/test-auth.R | 25 +++++++++++++++---------- 3 files changed, 24 insertions(+), 11 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index d81c4ec..4d6ca3f 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: apinsee Title: Manage Access to Insee APIs -Version: 0.0.0.9040 +Version: 0.0.0.9050 Authors@R: c( person("Romain", "Lesur", role = c("aut", "cre"), email = "romain.lesur@gmail.com", comment = c(ORCID = "0000-0002-0721-5595")), person() diff --git a/R/utils.R b/R/utils.R index 6b84d94..752a7d1 100644 --- a/R/utils.R +++ b/R/utils.R @@ -6,8 +6,16 @@ NULL cache_in_memory <- function(token) { rlang::env_bind(.memory_cache, !!token$app$key := token) + rlang::env_bind(.memory_cache, last_token = token) } load_from_memory_cache <- function(key) { + if (!nzchar(key)) { + return(rlang::env_get(.memory_cache, "last_token", NULL)) + } rlang::env_get(.memory_cache, key, NULL) } + +clear_memory_cache <- function() { + rlang::env_unbind(.memory_cache, rlang::env_names(.memory_cache)) +} diff --git a/tests/testthat/test-auth.R b/tests/testthat/test-auth.R index 3636ec5..4d27734 100644 --- a/tests/testthat/test-auth.R +++ b/tests/testthat/test-auth.R @@ -10,35 +10,40 @@ test_that("insee_auth() and insee_deauth() works", { test_that("insee_deauth() works", { check_configuration() skip_if_no_app() - expect_silent(insee_deauth(FALSE)) - expect_message(insee_deauth(TRUE)) + expect_silent(insee_deauth(verbose = FALSE)) + expect_message(insee_deauth(verbose = TRUE)) }) test_that("memory cache works", { check_configuration() skip_if_no_app() - insee_deauth(FALSE) + clear_memory_cache() debug_conf <- httr::config( verbose = TRUE, debugfunction = function(type, msg) cat(readBin(msg, character())) ) with_debug_conf <- function(expr) httr::with_config(config = debug_conf, expr) - expect_output(with_debug_conf(insee_auth())) + expect_output(token <- with_debug_conf(insee_auth())) expect_silent(with_debug_conf(insee_auth())) - insee_deauth(FALSE) + expect_reference(insee_auth(), token) + expect_silent(with_debug_conf(insee_auth(key = "", secret = ""))) + expect_reference(insee_auth(key = "", secret = ""), token) + expect_length(as.list(apinsee:::.memory_cache), 2L) + clear_memory_cache() + expect_length(as.list(apinsee:::.memory_cache), 0L) }) test_that("insee_auth() new_auth parameter works", { check_configuration() skip_if_no_app() - insee_deauth(FALSE) + clear_memory_cache() expect_message(insee_auth(new_auth = TRUE)) }) test_that("insee_auth() fetches seamlessly a new fresh token", { check_configuration() skip_if_no_app() - insee_deauth(FALSE) + clear_memory_cache() token <- insee_auth() token$revoke() expect_true(token$has_expired()) @@ -49,11 +54,11 @@ test_that("insee_auth() fetches seamlessly a new fresh token", { test_that("insee_auth() can be used with several applications", { check_configuration() skip_if_no_app() - insee_deauth(FALSE) + clear_memory_cache() token <- insee_auth() token2 <- insee_auth(key = Sys.getenv("INSEE_APP2_KEY"), secret = Sys.getenv("INSEE_APP2_SECRET")) expect_false(token$has_expired()) expect_false(token2$has_expired()) - expect_length(as.list(apinsee:::.memory_cache), 2L) - insee_deauth(FALSE) + expect_length(as.list(apinsee:::.memory_cache), 3L) + clear_memory_cache() })