Skip to content

Commit

Permalink
use last cached token as fallback value
Browse files Browse the repository at this point in the history
  • Loading branch information
RLesur committed Oct 31, 2019
1 parent bf35085 commit c0edeb4
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 11 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -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 = "[email protected]", comment = c(ORCID = "0000-0002-0721-5595")),
person()
Expand Down
8 changes: 8 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
25 changes: 15 additions & 10 deletions tests/testthat/test-auth.R
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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()
})

0 comments on commit c0edeb4

Please sign in to comment.