Skip to content

Commit 17e1b0d

Browse files
committed
add tests
1 parent 4a6f548 commit 17e1b0d

File tree

3 files changed

+199
-0
lines changed

3 files changed

+199
-0
lines changed

tests/testthat/test-content.R

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,32 @@ with_mock_api({
144144
"https://connect.example/__api__/v1/content/f2f37341-e21d-3d80-c698-a935ad614066/permissions"
145145
)
146146
})
147+
148+
test_that("Content$permissions_add() accepts connect_user object for users", {
149+
con <- Connect$new(server = "https://connect.example", api_key = "fake")
150+
item <- content_item(con, "f2f37341-e21d-3d80-c698-a935ad614066")
151+
user_guid <- "a01792e3-2e67-402e-99af-be04a48da074"
152+
user <- structure(list(guid = user_guid), class = "connect_user")
153+
154+
expect_POST(
155+
item$permissions_add(user, "user", "viewer"),
156+
"https://connect.example/__api__/v1/content/f2f37341-e21d-3d80-c698-a935ad614066/permissions",
157+
'{"principal_guid":"a01792e3-2e67-402e-99af-be04a48da074","principal_type":"user","role":"viewer"}'
158+
)
159+
})
160+
161+
test_that("Content$permissions_update() accepts connect_user object for users", {
162+
con <- Connect$new(server = "https://connect.example", api_key = "fake")
163+
item <- content_item(con, "f2f37341-e21d-3d80-c698-a935ad614066")
164+
user_guid <- "a01792e3-2e67-402e-99af-be04a48da074"
165+
user <- structure(list(guid = user_guid), class = "connect_user")
166+
167+
expect_PUT(
168+
item$permissions_update(94, user, "user", "editor"),
169+
"https://connect.example/__api__/v1/content/f2f37341-e21d-3d80-c698-a935ad614066/permissions/94",
170+
'{"principal_guid":"a01792e3-2e67-402e-99af-be04a48da074","principal_type":"user","role":"editor"}'
171+
)
172+
})
147173
})
148174

149175
without_internet({
@@ -467,6 +493,55 @@ test_that("get_content_packages() gets packages", {
467493
})
468494
})
469495

496+
with_mock_api({
497+
test_that("content_add_user() accepts connect_user object", {
498+
con <- Connect$new(server = "https://connect.example", api_key = "fake")
499+
item <- content_item(con, "f2f37341-e21d-3d80-c698-a935ad614066")
500+
user_guid <- "a01792e3-2e67-402e-99af-be04a48da074"
501+
user <- structure(list(guid = user_guid), class = "connect_user")
502+
503+
expect_POST(
504+
suppressMessages(content_add_user(item, user)),
505+
"https://connect.example/__api__/v1/content/f2f37341-e21d-3d80-c698-a935ad614066/permissions"
506+
)
507+
508+
# Test with list of connect_user objects
509+
user2 <- structure(list(guid = "b02892e3-2e67-402e-99af-be04a48da075"), class = "connect_user")
510+
expect_POST(
511+
suppressMessages(content_add_user(item, list(user, user2))),
512+
"https://connect.example/__api__/v1/content/f2f37341-e21d-3d80-c698-a935ad614066/permissions"
513+
)
514+
})
515+
516+
test_that("get_user_permission() accepts connect_user object", {
517+
con <- Connect$new(server = "https://connect.example", api_key = "fake")
518+
item <- content_item(con, "f2f37341-e21d-3d80-c698-a935ad614066")
519+
user_guid <- "a01792e3-2e67-402e-99af-be04a48da074"
520+
user <- structure(list(guid = user_guid), class = "connect_user")
521+
522+
# Test with GUID string - should work
523+
perm1 <- suppressMessages(get_user_permission(item, user_guid))
524+
525+
# Test with connect_user object - should also work and return same result
526+
perm2 <- suppressMessages(get_user_permission(item, user))
527+
528+
# Both should return the same permission (or NULL if not found)
529+
expect_equal(perm1, perm2)
530+
})
531+
532+
test_that("content_update_owner() accepts connect_user object", {
533+
con <- Connect$new(server = "https://connect.example", api_key = "fake")
534+
item <- content_item(con, "f2f37341-e21d-3d80-c698-a935ad614066")
535+
user_guid <- "a01792e3-2e67-402e-99af-be04a48da074"
536+
user <- structure(list(guid = user_guid), class = "connect_user")
537+
538+
expect_PATCH(
539+
suppressMessages(content_update_owner(item, user)),
540+
"https://connect.example/__api__/v1/content/f2f37341-e21d-3d80-c698-a935ad614066"
541+
)
542+
})
543+
})
544+
470545
# content search ----
471546

472547
with_mock_dir("2025.09.0", {

tests/testthat/test-groups.R

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,30 @@ without_internet({
3434
"https://connect.example/__api__/v1/groups/remote?limit=500"
3535
)
3636
})
37+
38+
test_that("group_member_add() accepts connect_user object", {
39+
client <- Connect$new(server = "https://connect.example", api_key = "fake")
40+
group_guid <- "a6fb5cea-0123-4567-89ab-cdef01234567"
41+
user_guid <- "20a79ce3-6e87-4522-9faf-be24228800a4"
42+
user <- structure(list(guid = user_guid), class = "connect_user")
43+
44+
expect_POST(
45+
client$group_member_add(group_guid, user),
46+
"https://connect.example/__api__/v1/groups/a6fb5cea-0123-4567-89ab-cdef01234567/members"
47+
)
48+
})
49+
50+
test_that("group_member_remove() accepts connect_user object", {
51+
client <- Connect$new(server = "https://connect.example", api_key = "fake")
52+
group_guid <- "a6fb5cea-0123-4567-89ab-cdef01234567"
53+
user_guid <- "20a79ce3-6e87-4522-9faf-be24228800a4"
54+
user <- structure(list(guid = user_guid), class = "connect_user")
55+
56+
expect_DELETE(
57+
client$group_member_remove(group_guid, user),
58+
"https://connect.example/__api__/v1/groups/a6fb5cea-0123-4567-89ab-cdef01234567/members/20a79ce3-6e87-4522-9faf-be24228800a4"
59+
)
60+
})
3761
})
3862

3963
with_mock_dir("2024.08.0", {

tests/testthat/test-users.R

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,47 @@
1+
# Tests for get_user_guid() helper function ----
2+
3+
test_that("get_user_guid() works with character GUID", {
4+
guid <- "20a79ce3-6e87-4522-9faf-be24228800a4"
5+
expect_equal(get_user_guid(guid), guid)
6+
})
7+
8+
test_that("get_user_guid() works with connect_user object", {
9+
user <- structure(
10+
list(
11+
guid = "20a79ce3-6e87-4522-9faf-be24228800a4",
12+
username = "carlos12",
13+
first_name = "Carlos",
14+
last_name = "User"
15+
),
16+
class = "connect_user"
17+
)
18+
expect_equal(get_user_guid(user), "20a79ce3-6e87-4522-9faf-be24228800a4")
19+
})
20+
21+
test_that("get_user_guid() errors with invalid input", {
22+
expect_error(
23+
get_user_guid(123),
24+
"user must be either a character string \\(GUID\\) or a connect_user object"
25+
)
26+
expect_error(
27+
get_user_guid(list(name = "test")),
28+
"user must be either a character string \\(GUID\\) or a connect_user object"
29+
)
30+
})
31+
32+
test_that("get_user_guid() errors when connect_user has no guid field", {
33+
user <- structure(
34+
list(username = "carlos12"),
35+
class = "connect_user"
36+
)
37+
expect_error(
38+
get_user_guid(user),
39+
"connect_user object does not contain a guid field"
40+
)
41+
})
42+
43+
# Tests for Connect client methods accepting connect_user objects ----
44+
145
with_mock_api({
246
test_that("we can retrieve the users list", {
347
con <- Connect$new(server = "https://connect.example", api_key = "fake")
@@ -13,6 +57,14 @@ with_mock_api({
1357
expect_type(a_user, "list")
1458
expect_equal(a_user$first_name, "Carlos")
1559
})
60+
61+
test_that("Connect$user() accepts connect_user object", {
62+
con <- Connect$new(server = "https://connect.example", api_key = "fake")
63+
user <- con$user("20a79ce3-6e87-4522-9faf-be24228800a4")
64+
same_user <- con$user(user)
65+
expect_equal(same_user$guid, "20a79ce3-6e87-4522-9faf-be24228800a4")
66+
expect_equal(same_user$first_name, "Carlos")
67+
})
1668
})
1769

1870
without_internet({
@@ -43,4 +95,52 @@ without_internet({
4395
"https://connect.example/__api__/v1/users/remote?prefix=A%20user%20name"
4496
)
4597
})
98+
99+
test_that("users_lock() accepts connect_user object or GUID string", {
100+
client <- Connect$new(server = "https://connect.example", api_key = "fake")
101+
user_guid <- "20a79ce3-6e87-4522-9faf-be24228800a4"
102+
user <- structure(list(guid = user_guid), class = "connect_user")
103+
104+
expect_POST(
105+
client$users_lock(user_guid),
106+
"https://connect.example/__api__/v1/users/20a79ce3-6e87-4522-9faf-be24228800a4/lock"
107+
)
108+
109+
expect_POST(
110+
client$users_lock(user),
111+
"https://connect.example/__api__/v1/users/20a79ce3-6e87-4522-9faf-be24228800a4/lock"
112+
)
113+
})
114+
115+
test_that("users_unlock() accepts connect_user object or GUID string", {
116+
client <- Connect$new(server = "https://connect.example", api_key = "fake")
117+
user_guid <- "20a79ce3-6e87-4522-9faf-be24228800a4"
118+
user <- structure(list(guid = user_guid), class = "connect_user")
119+
120+
expect_POST(
121+
client$users_unlock(user_guid),
122+
"https://connect.example/__api__/v1/users/20a79ce3-6e87-4522-9faf-be24228800a4/lock"
123+
)
124+
125+
expect_POST(
126+
client$users_unlock(user),
127+
"https://connect.example/__api__/v1/users/20a79ce3-6e87-4522-9faf-be24228800a4/lock"
128+
)
129+
})
130+
131+
test_that("users_update() accepts connect_user object or GUID string", {
132+
client <- Connect$new(server = "https://connect.example", api_key = "fake")
133+
user_guid <- "20a79ce3-6e87-4522-9faf-be24228800a4"
134+
user <- structure(list(guid = user_guid), class = "connect_user")
135+
136+
expect_PUT(
137+
client$users_update(user_guid, first_name = "New Name"),
138+
"https://connect.example/__api__/v1/users/20a79ce3-6e87-4522-9faf-be24228800a4"
139+
)
140+
141+
expect_PUT(
142+
client$users_update(user, first_name = "New Name"),
143+
"https://connect.example/__api__/v1/users/20a79ce3-6e87-4522-9faf-be24228800a4"
144+
)
145+
})
46146
})

0 commit comments

Comments
 (0)