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

test session expiry behaviour, use session cookies #4

Merged
merged 2 commits into from
Sep 6, 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
5 changes: 2 additions & 3 deletions R/router.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
build_routes <- function(cookie_key = plumber::random_cookie_key(),
cache = cachem::cache_mem(max_age = 60)) {
cache = cachem::cache_mem(max_age = 3600 * 60)) {
if (!dir.exists("uploads")) {
dir.create("uploads")
}
Expand Down Expand Up @@ -28,8 +28,7 @@ build_routes <- function(cookie_key = plumber::random_cookie_key(),

pr$registerHooks(plumber::session_cookie(cookie_key,
name = "serovizr",
path = "/",
expiration = 60))
path = "/"))

pr$handle(get_root())
pr$handle(get_version())
Expand Down
2 changes: 1 addition & 1 deletion docker/smoke-test
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ wait_for()
if [[ $result -eq "200" ]]; then
end_ts=$(date +%s)
echo "App available after $((end_ts - start_ts)) seconds"
break
exit 0
fi
sleep 1
echo "...still waiting"
Expand Down
95 changes: 92 additions & 3 deletions tests/testthat/test-session.R
Original file line number Diff line number Diff line change
Expand Up @@ -110,15 +110,104 @@ test_that("existing session id is used if present on GET /dataset/trace/", {
expect_equal(res$status, 200)

get_request_without_cookie <- make_req("GET",
"/dataset/testdataset/trace/ab/")
"/dataset/testdataset/trace/ab/")

res <- router$call(get_request_without_cookie)
expect_equal(res$status, 404)

get_request_with_cookie <- make_req("GET",
"/dataset/testdataset/trace/ab/",
HTTP_COOKIE = cookie)
"/dataset/testdataset/trace/ab/",
HTTP_COOKIE = cookie)

res <- router$call(get_request_with_cookie)
expect_equal(res$status, 200)
})

test_that("cache is updated on each request", {
key <- plumber::random_cookie_key()
cache <- cachem::cache_mem(max_age = 1)
router <- build_routes(key, cache)
session <- list(id = "1234")
cookie <- plumber:::cookieToStr("serovizr",
plumber:::encodeCookie(session,
plumber:::asCookieKey(key)))
request <- local_POST_dataset_request(data.frame(biomarker = "ab",
time = 1:10,
value = 1),
"testdataset",
xcol = "time",
session = "1234",
cookie = cookie)
res <- router$call(request)
expect_equal(res$status, 200)
expect_equal(cache$get("1234"), TRUE)

Sys.sleep(1)

# expect session to have expired from cache
expect_equal(length(cache$keys()), 0)

get_request_with_cookie <- make_req("GET",
"/dataset/testdataset/trace/ab/",
HTTP_COOKIE = cookie)

res <- router$call(get_request_with_cookie)
expect_equal(res$status, 200)

# expect session to have been re-added to cache
expect_equal(cache$get("1234"), TRUE)
})


test_that("inactive uploads are purged", {
key <- plumber::random_cookie_key()
cache <- cachem::cache_mem(max_age = 1)
router <- build_routes(key, cache)
old_session <- list(id = "1234")
old_cookie <- plumber:::cookieToStr("serovizr",
plumber:::encodeCookie(old_session,
plumber:::asCookieKey(key)))
request <- local_POST_dataset_request(data.frame(biomarker = "ab",
time = 1:10,
value = 1),
"testdataset",
xcol = "time",
session = "1234",
cookie = old_cookie)
expect_true(dir.exists("uploads/1234"))

Sys.sleep(1)

new_session <- list(id = "5678")
new_cookie <- plumber:::cookieToStr("serovizr",
plumber:::encodeCookie(new_session,
plumber:::asCookieKey(key)))
request <- local_POST_dataset_request(data.frame(biomarker = "ab",
time = 1:10,
value = 1),
"testdataset",
xcol = "time",
session = "1234",
cookie = new_cookie)
res <- router$call(request)
expect_equal(res$status, 200)

Sys.sleep(1)

# expect both sessions to have expired from cache
expect_equal(length(cache$keys()), 0)

get_request_with_new_cookie <- make_req("GET",
"/dataset/testdataset/trace/ab/",
HTTP_COOKIE = new_cookie)

res <- router$call(get_request_with_new_cookie)
expect_equal(res$status, 200)

get_request_with_old_cookie <- make_req("GET",
"/dataset/testdataset/trace/ab/",
HTTP_COOKIE = old_cookie)

res <- router$call(get_request_with_old_cookie)
expect_equal(res$status, 404)
})