-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
254 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,4 +43,3 @@ run_unbiased <- function() { | |
plumber::pr_run(host = host, port = port) # nocov end | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
#' Assert Events Logged in Audit Trail | ||
#' | ||
#' This function checks if the expected events have been logged in the 'audit_log' table in the database. | ||
#' This function should be used at the beginning of a test to ensure that the expected events are logged. | ||
#' @param events A vector of expected event types that should be logged, in order | ||
#' | ||
#' @return This function does not return a value. It throws an error if the assertions fail. | ||
#' | ||
#' @examples | ||
#' \dontrun{ | ||
#' assert_events_logged(c("event1", "event2")) | ||
#' } | ||
assert_audit_trail_for_test <- function(events = list(), env = parent.frame()) { | ||
# Get count of events logged from audit_log table in database | ||
pool <- get("db_connection_pool", envir = .GlobalEnv) | ||
conn <- pool::localCheckout(pool) | ||
|
||
event_count <- DBI::dbGetQuery( | ||
conn, | ||
"SELECT COUNT(*) FROM audit_log" | ||
)$count | ||
|
||
withr::defer( | ||
{ | ||
# gen new count | ||
new_event_count <- DBI::dbGetQuery( | ||
conn, | ||
"SELECT COUNT(*) FROM audit_log" | ||
)$count | ||
|
||
n <- length(events) | ||
|
||
# assert that the count has increased by number of events | ||
testthat::expect_equal( | ||
new_event_count, | ||
event_count + n, | ||
info = "Expected events to be logged" | ||
) | ||
|
||
if (n > 0) { | ||
# get the last n events | ||
last_n_events <- DBI::dbGetQuery( | ||
conn, | ||
glue::glue_sql( | ||
"SELECT * FROM audit_log ORDER BY created_at DESC LIMIT {n};", | ||
.con = conn | ||
) | ||
) | ||
|
||
event_types <- last_n_events |> | ||
dplyr::pull("event_type") |> | ||
rev() | ||
|
||
# assert that the last n events are the expected events | ||
testthat::expect_equal( | ||
event_types, | ||
events, | ||
info = "Expected events to be logged" | ||
) | ||
} | ||
}, | ||
env | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
study: | ||
- identifier: 'TEST' | ||
name: 'Test Study' | ||
method: 'minimisation_pocock' | ||
parameters: '{}' | ||
- identifier: 'TEST2' | ||
name: 'Test Study 2' | ||
method: 'minimisation_pocock' | ||
parameters: '{}' | ||
- identifier: 'TEST3' | ||
name: 'Test Study 3' | ||
method: 'minimisation_pocock' | ||
parameters: '{}' | ||
|
||
audit_log: | ||
- id: "c12d29e7-1b44-4cb6-a9c1-1f427fe70001" | ||
created_at: "2022-02-16T10:27:53Z" | ||
event_type: "example_event" | ||
request_id: "427ac2db-166d-4236-b040-94213f1b0001" | ||
study_id: 1 | ||
endpoint_url: "/api/example" | ||
request_method: "GET" | ||
request_body: '{"key1": "value1", "key2": "value2"}' | ||
response_code: 200 | ||
response_body: '{"key1": "value1", "key2": "value2"}' | ||
- id: "c12d29e7-1b44-4cb6-a9c1-1f427fe70002" | ||
created_at: "2022-02-16T10:27:53Z" | ||
event_type: "example_event" | ||
request_id: "427ac2db-166d-4236-b040-94213f1b0002" | ||
study_id: 2 | ||
endpoint_url: "/api/example" | ||
request_method: "GET" | ||
request_body: '{"key1": "value1", "key2": "value2"}' | ||
response_code: 200 | ||
response_body: '{"key1": "value1", "key2": "value2"}' | ||
- id: "c12d29e7-1b44-4cb6-a9c1-1f427fe70003" | ||
created_at: "2022-02-16T10:27:53Z" | ||
event_type: "example_event" | ||
request_id: "427ac2db-166d-4236-b040-94213f1b0003" | ||
study_id: 2 | ||
endpoint_url: "/api/example" | ||
request_method: "GET" | ||
request_body: '{"key1": "value1", "key2": "value2"}' | ||
response_code: 200 | ||
response_body: '{"key1": "value1", "key2": "value2"}' | ||
- id: "c12d29e7-1b44-4cb6-a9c1-1f427fe70004" | ||
created_at: "2022-02-16T10:27:53Z" | ||
event_type: "example_event" | ||
request_id: "427ac2db-166d-4236-b040-94213f1b0004" | ||
study_id: 2 | ||
endpoint_url: "/api/example" | ||
request_method: "GET" | ||
request_body: '{"key1": "value1", "key2": "value2"}' | ||
response_code: 200 | ||
response_body: '{"key1": "value1", "key2": "value2"}' | ||
- id: "c12d29e7-1b44-4cb6-a9c1-1f427fe70005" | ||
created_at: "2022-02-16T10:27:53Z" | ||
event_type: "example_event" | ||
request_id: "427ac2db-166d-4236-b040-94213f1b0004" | ||
study_id: 2 | ||
endpoint_url: "/api/example" | ||
request_method: "GET" | ||
request_body: '{"key1": "value1", "key2": "value2"}' | ||
response_code: 200 | ||
response_body: '{"key1": "value1", "key2": "value2"}' | ||
- id: "c12d29e7-1b44-4cb6-a9c1-1f427fe70006" | ||
created_at: "2022-02-16T10:27:53Z" | ||
event_type: "example_event" | ||
request_id: "427ac2db-166d-4236-b040-94213f1b0006" | ||
study_id: 3 | ||
endpoint_url: "/api/example" | ||
request_method: "GET" | ||
request_body: '{"key1": "value1", "key2": "value2"}' | ||
response_code: 200 | ||
response_body: '{"key1": "value1", "key2": "value2"}' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
source("./test-helpers.R") | ||
source("./audit-log-test-helpers.R") | ||
|
||
testthat::test_that("audit logs for study are returned correctly from the database", { | ||
with_db_fixtures("fixtures/example_audit_logs.yml") | ||
studies <- c(1, 2, 3) | ||
counts <- c(1, 4, 1) | ||
for (i in 1:3) { | ||
study_id <- studies[i] | ||
count <- counts[i] | ||
response <- request(api_url) |> | ||
req_url_path("study", study_id, "audit") |> | ||
req_method("GET") |> | ||
req_perform() | ||
|
||
response_body <- | ||
response |> | ||
resp_body_json() | ||
|
||
testthat::expect_equal(response$status_code, 200) | ||
testthat::expect_equal(length(response_body), count) | ||
|
||
if (count > 0) { | ||
body <- response_body[[1]] | ||
testthat::expect_equal(names(body), c( | ||
"id", | ||
"created_at", | ||
"event_type", | ||
"request_id", | ||
"study_id", | ||
"endpoint_url", | ||
"request_method", | ||
"request_body", | ||
"response_code", | ||
"response_body" | ||
)) | ||
testthat::expect_equal(body$study_id, study_id) | ||
testthat::expect_equal(body$event_type, "example_event") | ||
testthat::expect_equal(body$request_method, "GET") | ||
testthat::expect_equal(body$endpoint_url, "/api/example") | ||
testthat::expect_equal(body$response_code, 200) | ||
testthat::expect_equal(body$request_body, list(key1 = "value1", key2 = "value2")) | ||
testthat::expect_equal(body$response_body, list(key1 = "value1", key2 = "value2")) | ||
} | ||
} | ||
}) | ||
|
||
testthat::test_that("should return 404 when study does not exist", { | ||
response <- request(api_url) |> | ||
req_url_path("study", 1111, "audit") |> | ||
req_method("GET") |> | ||
req_error(is_error = \(x) FALSE) |> | ||
req_perform() | ||
|
||
response_body <- | ||
response |> | ||
resp_body_json() | ||
|
||
testthat::expect_equal(response$status_code, 404) | ||
testthat::expect_equal(response_body$error, "Study not found") | ||
}) | ||
|
||
testthat::test_that("should not log audit trail for non-existent endpoint", { | ||
assert_audit_trail_for_test(events = c()) | ||
response <- request(api_url) |> | ||
req_url_path("study", 1, "non-existent-endpoint") |> | ||
req_method("GET") |> | ||
req_error(is_error = \(x) FALSE) |> | ||
req_perform() | ||
|
||
response_body <- | ||
response |> | ||
resp_body_json() | ||
|
||
testthat::expect_equal(response$status_code, 404) | ||
}) |