Skip to content

Commit 0d6d7f0

Browse files
ci: rewrite Connect integration tests using with-connect Action (#884)
* Simplify connect test helpers to only use a single test user * Move connect test helpers out of the package * Update GHA workflow to use with-connect * Add matrix, switch license secrets * fix matrix yaml * Move test helpers back into package * gitignore .orig files * Action is now in posit-dev * Update R/board_connect.R * Prefer PTD for testing
1 parent 7ab9286 commit 0d6d7f0

File tree

9 files changed

+42
-176
lines changed

9 files changed

+42
-176
lines changed

.github/setup-connect/add-users.sh

Lines changed: 0 additions & 1 deletion
This file was deleted.

.github/setup-connect/rstudio-connect.gcfg

Lines changed: 0 additions & 20 deletions
This file was deleted.

.github/setup-connect/users.txt

Lines changed: 0 additions & 4 deletions
This file was deleted.

.github/workflows/posit-connect.yaml

Lines changed: 19 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,21 @@ name: posit-connect
99
jobs:
1010
posit-connect:
1111
runs-on: ubuntu-latest
12+
strategy:
13+
fail-fast: false
14+
matrix:
15+
version:
16+
- "jammy" # As long as we're building on ubuntu 22.04, this will track the latest release
17+
- "2024.09.0" # jammy
18+
- "2023.09.0" # jammy
19+
- "2022.10.0" # bionic
20+
name: Posit Connect ${{ matrix.version }}
21+
1222
if: github.event_name == 'push' || github.event.pull_request.head.repo.fork == false
1323
env:
14-
RSC_LICENSE: ${{ secrets.RSC_LICENSE }}
1524
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
1625
steps:
17-
- uses: actions/checkout@v4
18-
19-
- name: run Connect
20-
run: docker compose up -d --build
26+
- uses: actions/checkout@v5
2127

2228
- uses: r-lib/actions/setup-r@v2
2329
with:
@@ -26,38 +32,14 @@ jobs:
2632
- uses: r-lib/actions/setup-r-dependencies@v2
2733
with:
2834
extra-packages: |
29-
any::rcmdcheck
30-
rstudio/connectapi
31-
needs: check
32-
33-
- name: create Connect users
34-
run: |
35-
curl -s --retry 10 --retry-connrefused http://localhost:3939
36-
docker compose exec -T rsconnect bash < .github/setup-connect/add-users.sh
37-
38-
- name: initialize Connect users
39-
shell: Rscript {0}
40-
run: |
41-
admin_key <- connectapi:::create_first_admin(url = "http://localhost:3939", user = "admin", password = "admin0", email = "admin@example.com")
42-
43-
susan_key <- connectapi:::create_first_admin(url = "http://localhost:3939", user = "susan", password = "susan", email = "susan@example.com")
44-
derek_key <- connectapi:::create_first_admin(url = "http://localhost:3939", user = "derek", password = "derek", email = "derek@example.com")
45-
46-
saveRDS(
47-
list(
48-
admin_key = admin_key$api_key,
49-
susan_key = susan_key$api_key,
50-
derek_key = derek_key$api_key
51-
),
52-
"tests/testthat/creds.rds"
53-
)
54-
55-
- uses: r-lib/actions/check-r-package@v2
35+
local::.
36+
37+
- name: Run Connect integration tests
38+
uses: posit-dev/with-connect@main
5639
with:
57-
upload-snapshots: true
40+
# This license is valid until 2026-10-28
41+
license: ${{ secrets.CONNECT_LICENSE }}
42+
version: ${{ matrix.version }}
43+
command: Rscript -e 'testthat::test_local(".", filter="board_connect")'
5844

59-
- name: shutdown Posit Connect
60-
if: always()
61-
run: |
62-
docker compose down
6345

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ docs
99
inst/doc
1010
.Renviron
1111
google-pins.json
12+
*.orig

R/board_connect.R

Lines changed: 14 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -733,15 +733,26 @@ rsc_v1 <- function(...) {
733733

734734
# Testing setup -----------------------------------------------------------
735735

736+
# Connect testing: prefers using Posit's demo PTD server (if you've logged in
737+
# with rsconnect), else checks CONNECT_SERVER and CONNECT_API_KEY env vars,
738+
# and if neither are available, skips the tests.
736739
board_connect_test <- function(...) {
737740
if (connect_has_ptd()) {
738-
board_connect_ptd(...)
741+
board_connect(
742+
...,
743+
server = "pub.demo.posit.team",
744+
auth = "rsconnect",
745+
cache = fs::file_temp()
746+
)
747+
} else if (nzchar(Sys.getenv("CONNECT_API_KEY"))) {
748+
board_connect(auth = "envvar", ...)
739749
} else {
740-
board_connect_susan(...)
750+
testthat::skip(
751+
"board_connect_test() requires CONNECT_API_KEY or Posit's demo PTD server"
752+
)
741753
}
742754
}
743755

744-
# Use demo.posit.team PTD for local testing
745756
connect_has_ptd <- function() {
746757
accounts <- rsconnect::accounts()
747758
if (is.null(accounts) || nrow(accounts) == 0) {
@@ -750,68 +761,3 @@ connect_has_ptd <- function() {
750761
"pub.demo.posit.team" %in% accounts$server
751762
}
752763
}
753-
754-
board_connect_ptd <- function(...) {
755-
if (!connect_has_ptd()) {
756-
testthat::skip(
757-
"board_connect_ptd() only works with Posit's demo PTD server"
758-
)
759-
}
760-
board_connect(
761-
...,
762-
server = "pub.demo.posit.team",
763-
auth = "rsconnect",
764-
cache = fs::file_temp()
765-
)
766-
}
767-
768-
board_connect_susan <- function(...) {
769-
creds <- read_creds()
770-
board_connect(
771-
server = "http://localhost:3939",
772-
account = "susan",
773-
key = creds$susan_key
774-
)
775-
}
776-
board_connect_derek <- function(...) {
777-
creds <- read_creds()
778-
board_connect(
779-
server = "http://localhost:3939",
780-
account = "derek",
781-
key = creds$derek_key
782-
)
783-
}
784-
read_creds <- function() {
785-
path <- testthat::test_path("creds.rds")
786-
if (!file.exists(path)) {
787-
testthat::skip(glue("board_connect() tests requires `{path}`"))
788-
}
789-
readRDS(path)
790-
}
791-
add_another_user <- function(board, user_name, content_id) {
792-
## get user GUID for new owner from user_name
793-
path <- glue("v1/users/")
794-
path <- rsc_path(board, path)
795-
auth <- rsc_auth(board, path, "GET")
796-
query <- glue("prefix={user_name}")
797-
resp <- httr::GET(board$url, path = path, query = query, auth)
798-
httr::stop_for_status(resp)
799-
res <- httr::content(resp)
800-
principal_guid <- res$results[[1]]$guid
801-
802-
## add user_name as owner for content at GUID
803-
body <- glue(
804-
'{{
805-
"principal_guid": "{principal_guid}",
806-
"principal_type": "user",
807-
"role": "owner"
808-
}}'
809-
)
810-
811-
path <- glue("v1/content/{content_id}/permissions")
812-
path <- rsc_path(board, path)
813-
auth <- rsc_auth(board, path, "POST")
814-
resp <- httr::POST(board$url, path = path, body = body, auth)
815-
httr::stop_for_status(resp)
816-
invisible(resp)
817-
}

R/board_connect_url.R

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ connect_auth_headers <- function(key = Sys.getenv("CONNECT_API_KEY")) {
4747
c(Authorization = paste("Key", key))
4848
}
4949

50-
50+
# Test helpers ------------------------------------------------------------
5151
vanity_url_test <- function(env = parent.frame()) {
5252
board <- board_connect_test()
5353
name <- pin_write(board, 1:10, random_pin_name())
@@ -71,25 +71,15 @@ vanity_url_test <- function(env = parent.frame()) {
7171

7272
board_connect_url_test <- function(...) {
7373
if (connect_has_ptd()) {
74-
board_connect_url_ptd(...)
74+
board_connect_url(..., cache = fs::file_temp())
75+
} else if (nzchar(Sys.getenv("CONNECT_API_KEY"))) {
76+
board_connect_url(
77+
...,
78+
headers = connect_auth_headers(Sys.getenv("CONNECT_API_KEY"))
79+
)
7580
} else {
76-
board_connect_url_susan(...)
77-
}
78-
}
79-
80-
board_connect_url_ptd <- function(...) {
81-
if (!connect_has_ptd()) {
8281
testthat::skip(
83-
"board_connect_url_ptd() only works with Posit's demo server"
82+
"board_connect_url_test() requires CONNECT_API_KEY or Posit's demo PTD server"
8483
)
8584
}
86-
board_connect_url(..., cache = fs::file_temp())
87-
}
88-
89-
board_connect_url_susan <- function(...) {
90-
creds <- read_creds()
91-
board_connect_url(
92-
...,
93-
headers = connect_auth_headers(creds$susan_key)
94-
)
9585
}

docker-compose.yml

Lines changed: 0 additions & 16 deletions
This file was deleted.

tests/testthat/test-board_connect.R

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,18 +42,6 @@ test_that("can update access_type", {
4242
expect_equal(rsc_content_info(board, guid)$access_type, "logged_in")
4343
})
4444

45-
test_that("can write pin created another user", {
46-
board1 <- board_connect_susan()
47-
name <- local_pin(board1, 1:5)
48-
guid <- pin_meta(board1, name)$local$content_id
49-
add_another_user(board1, "derek", guid)
50-
51-
board2 <- board_connect_derek()
52-
pin_write(board2, 10:15, name)
53-
54-
expect_equal(pin_read(board1, name), 10:15)
55-
})
56-
5745
test_that("can deparse", {
5846
board <- new_board_v1(
5947
"pins_board_connect",

0 commit comments

Comments
 (0)