tests/testthat/test-secret-ops.R

library(mockery)
library(withr)

test_that("writing and reading returns the secret", {
  local_tempfile("tmp")
  local_mock("vault:::vault_path" = mock(tmp, cycle = TRUE))

  key <- generate_key()
  write_secret("Test", "username", "bob_loblaw", key = key)

  x <- read_secret("Test", "username", key = key)
  expect_s3_class(x, "vault_secret")
  expect_equal(vec_data(x), "bob_loblaw")
})

test_that("read_secret and write_secret fail with and ... args", {
  local_tempfile("tmp")
  local_vault(tmp)
  key <- generate_key()
  expect_error(read_secret("Test", "username", a = 1, key = key))
  expect_error(write_secret("Test", "username", "bob_loblaw", a = 1, key = key))
})

test_that("read_secret returns NULL if secret does not exist", {
  local_tempfile("tmp")
  local_vault(tmp)
  key <- generate_key()
  expect_null(read_secret("Test", "username", key = key))
})
shunsambongi/vault documentation built on March 19, 2020, 4:58 p.m.