tests/testthat/test-api-credentials.R

test_that("absence of API key or base URL raises an error", {
  withr::with_envvar(
    new = c("QUALTRICS_API_KEY" = "", "QUALTRICS_BASE_URL" = ""), 
    expect_snapshot_error(check_credentials())
  )  
})

test_that("can store and access credentials", {
  # Store dummy credentials in environment

  expect_snapshot(
    qualtrics_api_credentials(api_key = "1234", base_url = "abcd"),
    error = TRUE
  )

  # Removes protocol with message
  expect_snapshot(
    qualtrics_api_credentials(
      api_key = "1234", base_url = "https://abcd.qualtrics.com"
    )
  )
  expect_false(
    startsWith(Sys.getenv("QUALTRICS_BASE_URL" ), "https://")
  )

  # Removes the trailing slash:
  qualtrics_api_credentials(api_key = "1234", base_url = "abcd.qualtrics.com/")
  # Now expect this to no longer have slash
  expect_false(
    endsWith(Sys.getenv("QUALTRICS_BASE_URL" ), "/")
  )

  # Checks pass with correct credentials:
  qualtrics_api_credentials(api_key = "1234", base_url = "abcd.qualtrics.com")
  # Now expect this to be NULL
  expect_null(
    check_credentials()
  )
})

# Restore the credentials for other tests:
qualtrics_api_credentials(api_key = holder_API, base_url = holder_URL)

Try the qualtRics package in your browser

Any scripts or data that you put into this service are public.

qualtRics documentation built on Sept. 11, 2024, 5:24 p.m.