Skip to content

Commit

Permalink
Add IP address and user agent to audit logs api
Browse files Browse the repository at this point in the history
  • Loading branch information
lwalejko committed Mar 4, 2024
1 parent bb61fa3 commit 72c0a3a
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 7 deletions.
1 change: 1 addition & 0 deletions R/api-audit-log.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ api_get_audit_log <- function(study_id, req, res) {
# Get audit trial
audit_trail <- dplyr::tbl(db_connection_pool, "audit_log") |>
dplyr::filter(study_id == !!study_id) |>
dplyr::arrange(created_at) |>
dplyr::collect()

audit_trail$request_body <- purrr::map(
Expand Down
16 changes: 14 additions & 2 deletions tests/testthat/fixtures/example_audit_logs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ audit_log:
request_body: '{"key1": "value1", "key2": "value2"}'
response_code: 200
response_body: '{"key1": "value1", "key2": "value2"}'
ip_address: "8.8.8.8"
user_agent: "Mozilla"
- id: "c12d29e7-1b44-4cb6-a9c1-1f427fe70002"
created_at: "2022-02-16T10:27:53Z"
event_type: "example_event"
Expand All @@ -33,6 +35,8 @@ audit_log:
request_body: '{"key1": "value1", "key2": "value2"}'
response_code: 200
response_body: '{"key1": "value1", "key2": "value2"}'
ip_address: "8.8.8.8"
user_agent: "Mozilla"
- id: "c12d29e7-1b44-4cb6-a9c1-1f427fe70003"
created_at: "2022-02-16T10:27:53Z"
event_type: "example_event"
Expand All @@ -43,8 +47,10 @@ audit_log:
request_body: '{"key1": "value1", "key2": "value2"}'
response_code: 200
response_body: '{"key1": "value1", "key2": "value2"}'
ip_address: "8.8.8.8"
user_agent: "Mozilla"
- id: "c12d29e7-1b44-4cb6-a9c1-1f427fe70004"
created_at: "2022-02-16T10:27:53Z"
created_at: "2023-02-16T10:27:53Z"
event_type: "example_event"
request_id: "427ac2db-166d-4236-b040-94213f1b0004"
study_id: 2
Expand All @@ -53,8 +59,10 @@ audit_log:
request_body: '{"key1": "value1", "key2": "value2"}'
response_code: 200
response_body: '{"key1": "value1", "key2": "value2"}'
ip_address: "8.8.8.8"
user_agent: "Mozilla"
- id: "c12d29e7-1b44-4cb6-a9c1-1f427fe70005"
created_at: "2022-02-16T10:27:53Z"
created_at: "2022-02-16T10:27:54Z"
event_type: "example_event"
request_id: "427ac2db-166d-4236-b040-94213f1b0004"
study_id: 2
Expand All @@ -63,6 +71,8 @@ audit_log:
request_body: '{"key1": "value1", "key2": "value2"}'
response_code: 200
response_body: '{"key1": "value1", "key2": "value2"}'
ip_address: "8.8.8.8"
user_agent: "Mozilla"
- id: "c12d29e7-1b44-4cb6-a9c1-1f427fe70006"
created_at: "2022-02-16T10:27:53Z"
event_type: "example_event"
Expand All @@ -73,3 +83,5 @@ audit_log:
request_body: '{"key1": "value1", "key2": "value2"}'
response_code: 200
response_body: '{"key1": "value1", "key2": "value2"}'
ip_address: "8.8.8.8"
user_agent: "Mozilla"
20 changes: 15 additions & 5 deletions tests/testthat/test-api-audit-log.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ testthat::test_that("audit logs for study are returned correctly from the databa
counts <- c(1, 4, 1)
for (i in 1:3) {
study_id <- studies[i]
count <- counts[i]
count <- counts[i] |>
as.integer()
response <- request(api_url) |>
req_url_path("study", study_id, "audit") |>
req_method("GET") |>
Expand All @@ -17,12 +18,18 @@ testthat::test_that("audit logs for study are returned correctly from the databa
response |>
resp_body_json()

testthat::expect_equal(response$status_code, 200)
testthat::expect_equal(length(response_body), count)
testthat::expect_identical(response$status_code, 200L)
testthat::expect_identical(length(response_body), count)

created_at <- response_body |> dplyr::bind_rows() |> dplyr::pull("created_at")
testthat::expect_equal(
created_at,
created_at |> sort()
)

if (count > 0) {
body <- response_body[[1]]
testthat::expect_equal(names(body), c(
testthat::expect_setequal(names(body), c(
"id",
"created_at",
"event_type",
Expand All @@ -32,8 +39,11 @@ testthat::test_that("audit logs for study are returned correctly from the databa
"request_method",
"request_body",
"response_code",
"response_body"
"response_body",
"user_agent",
"ip_address"
))

testthat::expect_equal(body$study_id, study_id)
testthat::expect_equal(body$event_type, "example_event")
testthat::expect_equal(body$request_method, "GET")
Expand Down

0 comments on commit 72c0a3a

Please sign in to comment.