-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathtest-connect.R
More file actions
231 lines (197 loc) · 6.49 KB
/
test-connect.R
File metadata and controls
231 lines (197 loc) · 6.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
test_that("Connect R6 class preserves provided values", {
server <- "http://myhost.example.com"
api_key <- "fake"
con <- Connect$new(server = server, api_key = api_key)
expect_identical(con$server, server)
expect_identical(con$api_key, api_key)
})
test_that("trailing slash removed from server", {
con <- Connect$new(server = "http://myhost.example.com/", api_key = "fake")
con2 <- Connect$new(server = "http://myhost.example.com", api_key = "fake")
expect_identical(con$server, con2$server)
})
test_that("error if protocol not defined", {
con <- Connect$new(server = "https://myhost.example.com", api_key = "fake")
expect_error(
Connect$new("test.example.com", "fake"),
"protocol"
)
expect_error(
Connect$new("://test.example.com", "fake"),
"protocol"
)
})
test_that("version is validated", {
skip("not implemented yet")
})
test_that("Handling error responses", {
con <- Connect$new(server = "https://connect.example", api_key = "fake")
resp <- fake_response("https://connect.example/__api__/", status_code = 400L)
expect_error(con$raise_error(resp), "Bad Request")
})
test_that("Handling deprecation warnings", {
rlang::reset_warning_verbosity("X-Deprecated-Endpoint")
on.exit(rlang::reset_warning_verbosity("X-Deprecated-Endpoint"))
# No warning here
resp <- fake_response("https://connect.example/__api__/", headers = list(
`Content-Type` = "application/json"
))
expect_warning(check_debug(resp), NA)
# Yes warning here
resp <- fake_response("https://connect.example/__api__/", headers = list(
`X-Deprecated-Endpoint` = "/v1"
))
expect_warning(
check_debug(resp),
paste(
"https://connect.example/__api__/ is deprecated and will be removed in a",
"future version of Connect. Please upgrade `connectapi` in order to use",
"the new APIs."
),
class = "deprecatedWarning"
)
# No warning if you do it again because we only warn the first time
expect_warning(check_debug(resp), NA)
})
with_mock_api({
test_that("browse URLs", {
con <- Connect$new(server = "https://connect.example", api_key = "fake")
# Inject into this function something other than utils::browseURL
# so we can assert that it is being called without actually trying to open a browser
suppressMessages(trace("browse_url", where = connectapi::browse_solo, tracer = quote({
browseURL <- # nolint: object_name_linter
function(x) {
warning(paste("Opening", x))
}
}), at = 1, print = FALSE))
expect_warning(
browse_connect(con),
"Opening https://connect.example"
)
expect_warning(
browse_api_docs(con),
"Opening https://connect.example/__docs__/api"
)
suppressMessages(untrace("browse_url", where = connectapi::browse_solo))
})
test_that("httr_config", {
con <- Connect$new(server = "https://connect.example", api_key = "fake")
con$httr_config(httr::add_headers(MY_MAGIC_HEADER = "value"))
expect_header(
expect_GET(
con$GET("v1/content"),
"https://connect.example/__api__/v1/content"
),
"MY_MAGIC_HEADER: value"
)
})
test_that("client$version is NA when server settings lacks version info", {
con <- Connect$new(server = "https://connect.example", api_key = "fake")
expect_message(v <- con$version, "Version information is not exposed by this Posit Connect instance")
expect_true(is.na(v))
})
})
test_that("client$version is returns version when server settings exposes it", {
with_mock_dir("2024.09.0", {
con <- Connect$new(server = "https://connect.example", api_key = "fake")
expect_equal(con$version, "2024.09.0")
})
})
test_that("Visitor client can successfully be created running on Connect", {
with_mock_api({
withr::local_envvar(
CONNECT_SERVER = "https://connect.example",
CONNECT_API_KEY = "fake",
POSIT_PRODUCT = "CONNECT"
)
client <- connect(token = "my-token")
expect_equal(
client$server,
"https://connect.example"
)
expect_equal(
client$api_key,
"visitor-api-key"
)
})
})
test_that("Visitor client uses fallback api key when running locally", {
with_mock_api({
withr::local_envvar(
CONNECT_SERVER = "https://connect.example",
CONNECT_API_KEY = "fake"
)
# With default fallback
expect_message(
client <- connect(token = NULL),
"Called with `token` but not running on Connect. Continuing with fallback API key."
)
expect_equal(
client$server,
"https://connect.example"
)
expect_equal(
client$api_key,
"fake"
)
# With explicitly-defined fallback
expect_message(
client <- connect(token = NULL, token_local_testing_key = "fallback_fake"),
"Called with `token` but not running on Connect. Continuing with fallback API key."
)
expect_equal(
client$server,
"https://connect.example"
)
expect_equal(
client$api_key,
"fallback_fake"
)
})
})
test_that("Visitor client code path errs with older Connect version", {
with_mock_dir("2024.09.0", {
withr::local_envvar(
CONNECT_SERVER = "https://connect.example",
CONNECT_API_KEY = "fake",
POSIT_PRODUCT = "CONNECT"
)
expect_error(
client <- connect(token = "my-token"),
"This feature requires Posit Connect version 2025.01.0 but you are using 2024.09.0"
)
})
})
test_that("environment detection functions work", {
withr::with_envvar(c(POSIT_PRODUCT = "", RSTUDIO_PRODUCT = ""), {
expect_true(is_local())
expect_false(on_connect())
expect_false(on_workbench())
})
withr::with_envvar(c(POSIT_PRODUCT = "CONNECT", RSTUDIO_PRODUCT = ""), {
expect_false(is_local())
expect_true(on_connect())
expect_false(on_workbench())
})
withr::with_envvar(c(POSIT_PRODUCT = "WORKBENCH", RSTUDIO_PRODUCT = ""), {
expect_false(is_local())
expect_false(on_connect())
expect_true(on_workbench())
})
withr::with_envvar(c(POSIT_PRODUCT = "", RSTUDIO_PRODUCT = "CONNECT"), {
expect_false(is_local())
expect_true(on_connect())
expect_false(on_workbench())
})
withr::with_envvar(c(POSIT_PRODUCT = "", RSTUDIO_PRODUCT = "WORKBENCH"), {
expect_false(is_local())
expect_false(on_connect())
expect_true(on_workbench())
})
# POSIT_PRODUCT takes precedence over RSTUDIO_PRODUCT
withr::with_envvar(c(POSIT_PRODUCT = "CONNECT", RSTUDIO_PRODUCT = "WORKBENCH"), {
expect_false(is_local())
expect_true(on_connect())
expect_false(on_workbench())
})
})