Skip to content

Commit e628e7c

Browse files
Move test-connect tests to non-integration
1 parent 690bb8c commit e628e7c

File tree

3 files changed

+73
-72
lines changed

3 files changed

+73
-72
lines changed

R/connect.R

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1005,7 +1005,9 @@ connect <- function(
10051005
...,
10061006
.check_is_fatal = TRUE
10071007
) {
1008-
if (is.null(api_key) || is.na(api_key) || nchar(api_key) == 0) {
1008+
if (
1009+
missing(token) && is.null(api_key) || is.na(api_key) || nchar(api_key) == 0
1010+
) {
10091011
msg <- "Invalid (empty) API key. Please provide a valid API key"
10101012
if (.check_is_fatal) {
10111013
stop(glue::glue("ERROR: {msg}"))

tests/integrated/test-connect.R

Lines changed: 3 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,78 +1,12 @@
1-
test_that("connect works", {
2-
conn <- connect(
3-
server = Sys.getenv("TEST_1_SERVER"),
4-
api_key = Sys.getenv("TEST_1_API_KEY")
5-
)
6-
expect_true(validate_R6_class(conn, "Connect"))
7-
})
8-
9-
test_that("connect works with prefix only", {
10-
conn <- connect(prefix = "TEST_1")
11-
expect_true(validate_R6_class(conn, "Connect"))
12-
})
13-
14-
test_that("connect fails for nonexistent server", {
15-
expect_error({
16-
connect(server = "does-not-exist.rstudio.com", api_key = "bogus")
17-
})
1+
# This whole suite assumes CONNECT_SERVER and CONNECT_API_KEY env vars are set
2+
test_that("connect() works", {
3+
expect_true(validate_R6_class(connect(), "Connect"))
184
})
195

206
test_that("connect fails for good server, bad api key", {
217
expect_error({
228
connect(
23-
server = Sys.getenv("TEST_1_SERVER"),
249
api_key = "bogus"
2510
)
2611
})
2712
})
28-
29-
test_that("error if API key is empty", {
30-
expect_error(
31-
connect(server = Sys.getenv("TEST_1_SERVER"), api_key = ""),
32-
"provide a valid API key"
33-
)
34-
35-
expect_error(
36-
connect(server = Sys.getenv("TEST_1_SERVER"), api_key = NA_character_),
37-
"provide a valid API key"
38-
)
39-
40-
expect_error(
41-
connect(server = Sys.getenv("TEST_1_SERVER"), api_key = NULL),
42-
"provide a valid API key"
43-
)
44-
})
45-
46-
test_that(".check_is_fatal toggle works", {
47-
expect_error(
48-
connect(server = Sys.getenv("TEST_1_SERVER"), api_key = ""),
49-
"provide a valid API key"
50-
)
51-
52-
rsc <- connect(
53-
server = Sys.getenv("TEST_1_SERVER"),
54-
api_key = "",
55-
.check_is_fatal = FALSE
56-
)
57-
expect_true(
58-
validate_R6_class(rsc, "Connect")
59-
)
60-
61-
expect_error(
62-
suppressMessages(connect(
63-
server = "http://fake-value.example.com",
64-
api_key = "fake-value"
65-
)),
66-
"Could not resolve host"
67-
)
68-
69-
# TODO: suppressing the message in the tryCatch handler does not work...?
70-
rsc1 <- suppressMessages(connect(
71-
server = "http://fake-value.example.com",
72-
api_key = "fake-value",
73-
.check_is_fatal = FALSE
74-
))
75-
expect_true(
76-
validate_R6_class(rsc1, "Connect")
77-
)
78-
})

tests/testthat/test-connect.R

Lines changed: 67 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,71 @@ test_that("error if protocol not defined", {
2929
)
3030
})
3131

32+
test_that("connect() errors if API key is empty", {
33+
expect_error(
34+
connect(api_key = ""),
35+
"provide a valid API key"
36+
)
37+
38+
expect_error(
39+
connect(api_key = NA_character_),
40+
"provide a valid API key"
41+
)
42+
43+
expect_error(
44+
connect(api_key = NULL),
45+
"provide a valid API key"
46+
)
47+
})
48+
49+
test_that("connect() just warns on empty API key if .check_is_fatal=FALSE", {
50+
# Use without_internet() because for some reason it doesn't return early
51+
# if there is no API key.
52+
without_internet({
53+
expect_message(
54+
client <- connect(
55+
api_key = "",
56+
.check_is_fatal = FALSE
57+
),
58+
"provide a valid API key"
59+
)
60+
expect_true(
61+
validate_R6_class(client, "Connect")
62+
)
63+
})
64+
})
65+
66+
test_that("connect() fails for bad server", {
67+
without_internet({
68+
expect_error(
69+
connect(server = "does-not-exist.rstudio.com", api_key = "bogus"),
70+
"Please provide a protocol"
71+
)
72+
# This is how without_internet() errors making the ping request
73+
expect_error(
74+
connect(server = "http://does-not-exist.rstudio.com", api_key = "bogus"),
75+
"GET http://does-not-exist.rstudio.com/__ping__"
76+
)
77+
})
78+
})
79+
80+
test_that(".check_is_fatal toggle handles server validation", {
81+
without_internet({
82+
# See above. But the error is caught and returned as a message in this case.
83+
expect_message(
84+
client <- connect(
85+
server = "http://fake-value.example.com",
86+
api_key = "fake-value",
87+
.check_is_fatal = FALSE
88+
),
89+
"GET http://fake-value.example.com/__ping__"
90+
)
91+
expect_true(
92+
validate_R6_class(client, "Connect")
93+
)
94+
})
95+
})
96+
3297
test_that("version is validated", {
3398
skip("not implemented yet")
3499
})
@@ -95,8 +160,8 @@ test_that("Handling deprecation warnings", {
95160
expect_warning(check_debug(resp), NA)
96161

97162
withr::with_options(
98-
list(rlib_warning_verbosity = "default"), {
99-
163+
list(rlib_warning_verbosity = "default"),
164+
{
100165
# Yes warning here
101166
resp <- fake_response(
102167
"https://connect.example/__api__/",

0 commit comments

Comments
 (0)