Skip to content

Add tests for connectClient() #1184

@toph-allen

Description

@toph-allen

The Connect client object is not tested.

There was an old test that was labeled "Users API" but tested the entire Connect object workflow. It was deleted in #1183 and is pasted below. Perhaps using this as a starting point makes sense.

test_that("Users API", {
  skip_on_cran()
  skip_on_os("windows")
  skip("connect user test skipped.")

  ## rm db/*.db
  server <- serverInfo(findServer())

  connect <- connectClient(server$url, list())
  id <- paste(as.hexmode(sample(256, bytes) - 1), collapse = "")

  # add a user
  record <- userRecord(
    email = paste0("user", id, "@gmail.com"),
    username = paste0("user", id),
    first_name = "User",
    last_name = "Resu",
    password = paste0("password", id)
  )

  response <- connect$addUser(record)

  # check a couple main fields
  expect_equal(
    response[c("email", "first_name", "last_name", "username")],
    list(
      email = record$email,
      first_name = record$first_name,
      last_name = record$last_name,
      username = record$username
    )
  )

  # make sure we returned an empty password field (or no password field?)
  expect_true(response$password %in% "" || is.null(response$password))

  # generate a token
  accountId <- response$id
  token <- generateToken()

  tokenResponse <- connect$addToken(list(
    token = token$token,
    public_key = token$public_key,
    user_id = accountId
  ))

  Sys.sleep(1)

  # open the URL in the browser
  utils::browseURL(tokenResponse$token_claim_url)

  # Sleep for a second so we can be sure that automatic auth happened
  Sys.sleep(2)

  # finally, create a fully authenticated client using the new token, and
  # keep trying to authenticate until we're successful
  connect <- connectClient(
    service = server$url,
    authInfo = list(token = token$token, private_key = token$private_key)
  )

  repeat {
    tryCatch(
      {
        user <- connect$currentUser()
        break
      },
      error = function(e, ...) {
        # we expect this to return unauthorized until the token becomes active,
        # but bubble other errors
        if (length(grep("401 - Unauthorized", e$message)) == 0) {
          stop(e)
        }
        Sys.sleep(1)
      }
    )
  }

  # Create and remove an example application

  ## Create it
  splineReticulator <- connect$createApplication("SplineReticulator")

  # Update the account id (since we have moved from unauthed user
  # to authed user)
  accountId <- splineReticulator$user_id

  ## Confirm it exists
  apps <- connect$listApplications(accountId)
  app <- apps[[which(sapply(apps, `[[`, "id") == splineReticulator$id)]]
  expect_true(app$name == "SplineReticulator")

  ## Upload a bundle
  pwd_splat <- strsplit(getwd(), "/")[[1]]
  if (pwd_splat[length(pwd_splat)] == "rsconnect") {
    path <- "tests/testthat/test-shinyApp/test-shinyApp.tar.gz"
  } else {
    path <- "test-shinyApp/test-shinyApp.tar.gz"
  }

  response <- connect$uploadApplication(
    splineReticulator$id,
    normalizePath(path = path)
  )

  ## Deploy an application
  appId <- response$app_id
  response <- connect$deployApplication(list(id = appId))
  id <- response$id

  ## Query the app for success / failure
  response <- connect$waitForTask(id)

  ## Terminate the application (does not remove it from store)
  connect$terminateApplication(appId)

  apps <- connect$listApplications(accountId)
  expect_false(response$id %in% sapply(apps, "[[", "id"))
})

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions