Skip to content

Commit

Permalink
Add users tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nealrichardson committed Apr 5, 2024
1 parent 8331c8b commit 6a7005b
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 9 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,4 @@ Encoding: UTF-8
Language: en-US
RoxygenNote: 7.3.1
Roxygen: list(markdown = TRUE)
Config/testthat/edition: 3
Config/testthat/edition: 3
11 changes: 4 additions & 7 deletions R/get.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@


#' Get user information from the Posit Connect server
#'
#' @param src The source object
Expand Down Expand Up @@ -29,7 +27,7 @@
#' * `guid`: The user's GUID, or unique identifier, in UUID RFC4122 format
#'
#' @details
#' Please see https://docs.posit.co/connect/api/#getUsers for more information.
#' Please see https://docs.posit.co/connect/api/#get-/v1/users for more information.
#'
#' @examples
#' \dontrun{
Expand Down Expand Up @@ -290,7 +288,6 @@ get_content <- function(src, guid = NULL, owner_guid = NULL, name = NULL, ..., .
.make_predicate <- function(.expr) {
function(.x) {
masked_expr <- rlang::enexpr(.expr)

}
}

Expand Down Expand Up @@ -337,7 +334,7 @@ content_list_with_permissions <- function(src, ..., .p = NULL) {
content_list <- get_content(src, .p = .p)

message("Getting permission list")
pb <- progress::progress_bar$new(total = nrow(content_list), format="[:bar] :percent :eta")
pb <- progress::progress_bar$new(total = nrow(content_list), format = "[:bar] :percent :eta")
updated_list <- content_list %>% dplyr::mutate(
permission = purrr::map(guid, function(.x) .get_content_permission_with_progress(src, .x, pb))
)
Expand Down Expand Up @@ -373,8 +370,8 @@ content_list_guid_has_access <- function(content_list, guid) {
filtered <- content_list %>% dplyr::filter(
access_type == "all" |
access_type == "logged_in" |
owner_guid == {{guid}} |
purrr::map_lgl(permission, ~ {{guid}} %in% .x$principal_guid)
owner_guid == {{ guid }} |
purrr::map_lgl(permission, ~ {{ guid }} %in% .x$principal_guid)
)
return(filtered)
}
Expand Down
2 changes: 1 addition & 1 deletion man/get_users.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions tests/testthat/connect.example/__api__/v1/users-990342.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// v1/users?page_number=3&page_size=500
{
"results": [],
"current_page": 3,
"total": 3
}
20 changes: 20 additions & 0 deletions tests/testthat/connect.example/__api__/v1/users-a48327.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// v1/users?page_number=1&page_size=500
{
"results": [
{
"email": "carlos@connect.example",
"username": "carlos12",
"first_name": "Carlos",
"last_name": "User",
"user_role": "publisher",
"created_time": "2019-09-09T15:24:32Z",
"updated_time": "2022-03-02T20:25:06Z",
"active_time": "2020-05-11T16:58:45Z",
"confirmed": true,
"locked": false,
"guid": "20a79ce3-6e87-4522-9faf-be24228800a4"
}
],
"current_page": 2,
"total": 3
}
33 changes: 33 additions & 0 deletions tests/testthat/connect.example/__api__/v1/users-b1ca4b.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// v1/users?page_number=2&page_size=500.json
{
"results": [
{
"email": "alice@connect.example",
"username": "al",
"first_name": "Alice",
"last_name": "User",
"user_role": "publisher",
"created_time": "2017-08-08T15:24:32Z",
"updated_time": "2023-03-02T20:25:06Z",
"active_time": "2018-05-09T16:58:45Z",
"confirmed": true,
"locked": false,
"guid": "a01792e3-2e67-402e-99af-be04a48da074"
},
{
"email": "bob@connect.example",
"username": "robert",
"first_name": "Bob",
"last_name": "Loblaw",
"user_role": "publisher",
"created_time": "2023-01-06T19:47:29Z",
"updated_time": "2023-05-05T19:08:45Z",
"active_time": "2023-05-05T20:29:11Z",
"confirmed": true,
"locked": false,
"guid": "87c12c08-11cd-4de1-8da3-12a7579c4998"
}
],
"current_page": 1,
"total": 3
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"email": "carlos@connect.example",
"username": "carlos12",
"first_name": "Carlos",
"last_name": "User",
"user_role": "publisher",
"created_time": "2019-09-09T15:24:32Z",
"updated_time": "2022-03-02T20:25:06Z",
"active_time": "2020-05-11T16:58:45Z",
"confirmed": true,
"locked": false,
"guid": "20a79ce3-6e87-4522-9faf-be24228800a4"
}
16 changes: 16 additions & 0 deletions tests/testthat/test-users.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
with_mock_api({
test_that("we can retrieve the users list", {
con <- Connect$new(server = "https://connect.example", api_key = "fake")
users <- get_users(con)
expect_is(users, "data.frame")
expect_equal(nrow(users), 3)
})

test_that("we can retrieve a user by id", {
con <- Connect$new(server = "https://connect.example", api_key = "fake")
a_user <- con$user("20a79ce3-6e87-4522-9faf-be24228800a4")
# There is no User class?
expect_is(a_user, "list")
expect_equal(a_user$first_name, "Carlos")
})
})

0 comments on commit 6a7005b

Please sign in to comment.