-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce comprehensive mocking support.
Signed-off-by: Aaron Jacobs <[email protected]>
- Loading branch information
Showing
5 changed files
with
187 additions
and
11 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,9 @@ | ||
# Generated by roxygen2: do not edit by hand | ||
|
||
export(connect_viewer_token) | ||
export(example_connect_session) | ||
export(has_viewer_token) | ||
export(local_mocked_connect_responses) | ||
export(with_mocked_connect_responses) | ||
import(httr2) | ||
import(rlang) |
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,82 @@ | ||
#' Mock responses from the Posit Connect server | ||
#' | ||
#' These functions can be used to temporarily mock responses from the Connect | ||
#' server, which is useful for writing tests that verify the behaviour of | ||
#' viewer-based credentials. | ||
#' | ||
#' @param token When not `NULL`, return this token from the Connect server. | ||
#' @param error When `TRUE`, return an error from the Connect server. | ||
#' @inheritParams httr2::with_mocked_responses | ||
#' @examples | ||
#' with_mocked_connect_responses( | ||
#' connect_viewer_token(example_connect_session()), | ||
#' token = "test" | ||
#' ) | ||
#' @export | ||
with_mocked_connect_responses <- function(code, mock = NULL, token = NULL, error = FALSE, env = caller_env()) { | ||
check_string(token, allow_empty = FALSE, allow_null = TRUE) | ||
check_bool(error) | ||
check_exclusive(mock, token, error) | ||
mock <- mock %||% connect_mock_fn(token, error) | ||
withr::with_envvar( | ||
c( | ||
RSTUDIO_PRODUCT = "CONNECT", | ||
CONNECT_SERVER = "localhost:3030", | ||
CONNECT_API_KEY = "key", | ||
.local_envir = env | ||
), | ||
with_mocked_responses(mock, code) | ||
) | ||
} | ||
|
||
#' @inheritParams httr2::local_mocked_responses | ||
#' @rdname with_mocked_connect_responses | ||
#' @export | ||
local_mocked_connect_responses <- function(mock = NULL, token = NULL, error = FALSE, env = caller_env()) { | ||
check_string(token, allow_empty = FALSE, allow_null = TRUE) | ||
check_bool(error) | ||
check_exclusive(mock, token, error) | ||
mock <- mock %||% connect_mock_fn(token, error) | ||
withr::local_envvar( | ||
RSTUDIO_PRODUCT = "CONNECT", | ||
CONNECT_SERVER = "localhost:3030", | ||
CONNECT_API_KEY = "key", | ||
.local_envir = env | ||
) | ||
local_mocked_responses(mock, env = env) | ||
} | ||
|
||
connect_mock_fn <- function(token = NULL, error = FALSE) { | ||
function(req) { | ||
if (!grepl("localhost:3030", req$url, fixed = TRUE)) { | ||
return(NULL) | ||
} | ||
if (!error) { | ||
body <- list( | ||
access_token = token, | ||
issued_token_type = "urn:ietf:params:oauth:token-type:access_token", | ||
token_type = "Bearer" | ||
) | ||
} else { | ||
body <- list( | ||
error_code = 212, | ||
error_message = "No OAuth integrations have been associated with this content item." | ||
) | ||
} | ||
response_json( | ||
status_code = if (!error) 200L else 400L, | ||
url = req$url, | ||
method = req$method %||% "GET", | ||
body = body | ||
) | ||
} | ||
} | ||
|
||
#' @rdname with_mocked_connect_responses | ||
#' @export | ||
example_connect_session <- function() { | ||
structure( | ||
list(request = list(HTTP_POSIT_CONNECT_USER_SESSION_TOKEN = "user-token")), | ||
class = "ShinySession" | ||
) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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