From d7a92bf2d10154a77f2e4c3fc75f65f7ddffe798 Mon Sep 17 00:00:00 2001 From: Aron Atkins Date: Wed, 8 Nov 2023 08:07:27 -0500 Subject: [PATCH] accountInfo() produces name and username fields (#1025) --- NEWS.md | 5 ++++ R/accounts.R | 6 +++++ tests/testthat/test-accounts.R | 46 ++++++++++++++++++++++++++++++++++ 3 files changed, 57 insertions(+) diff --git a/NEWS.md b/NEWS.md index f2f2ea7f..68fb6a65 100644 --- a/NEWS.md +++ b/NEWS.md @@ -14,6 +14,11 @@ * `deployApp()` and friends record multi-value `metadata` entries as comma-separated values. (#1017) +* `accountInfo()` includes `name` and `username` fields. Older versions of + rsconnect store account records with a `username` field. Recent rsconnect + versions record `name`. Both `name` and `username` should contain the same + value. (#1024) + # rsconnect 1.1.1 * Added `space` parameter to deploy directly to a space in Posit Cloud. diff --git a/R/accounts.R b/R/accounts.R index e0c0d84a..6cc07115 100644 --- a/R/accounts.R +++ b/R/accounts.R @@ -294,6 +294,12 @@ findAccountInfo <- function(name = NULL, server = NULL, error_call = caller_env( accountDcf <- read.dcf(configFile, all = TRUE) info <- as.list(accountDcf) + + # Account records previously had username, now have name. Internal callers expect "name", but + # external callers may expect "username". (#1024) + info$name <- info$name %||% info$username + info$username <- info$name + # remove all whitespace from private key if (!is.null(info$private_key)) { info$private_key <- gsub("[[:space:]]", "", info$private_key) diff --git a/tests/testthat/test-accounts.R b/tests/testthat/test-accounts.R index 18382cf3..f6cfdcb7 100644 --- a/tests/testthat/test-accounts.R +++ b/tests/testthat/test-accounts.R @@ -33,3 +33,49 @@ test_that("secrets are hidden from casual inspection", { test_that("setAccountInfo() gives nice error on bad copy and paste", { expect_snapshot(setAccountInfo("name", "token", ""), error = TRUE) }) + +test_that("accountInfo() returns account information", { + local_temp_config() + addTestServer() + addTestAccount("john") + + accountDetails <- accountInfo("john", "example.com") + expect_equal(accountDetails$name, "john") + expect_equal(accountDetails$username, "john") + expect_equal(accountDetails$server, "example.com") +}) + +test_that("accountInfo() returns account information", { + local_temp_config() + addTestServer() + addTestAccount("john") + + accountDetails <- accountInfo("john", "example.com") + expect_equal(accountDetails$name, "john") + # username is included for backwards compatibility. (#1024) + expect_equal(accountDetails$username, "john") + expect_equal(accountDetails$server, "example.com") +}) + +test_that("accountInfo() returns pre-rsconnect-1.0.0 account information", { + local_temp_config() + addTestServer() + + # Subset of rsconnect-0.8.29 account fields. + fields <- list( + username = "john", + server = "example.com", + accountId = "john" + ) + + path <- accountConfigFile("john", "example.com") + dir.create(dirname(path), recursive = TRUE, showWarnings = FALSE) + write.dcf(compact(fields), path, width = 100) + + accountDetails <- accountInfo("john", "example.com") + # name copied from username, as "name" is the current field name. + expect_equal(accountDetails$name, "john") + # username retained for backwards compatibility. + expect_equal(accountDetails$username, "john") + expect_equal(accountDetails$server, "example.com") +})