library(mockery)
library(patrick)
library(withr)
test_that("new_key() returns a vault_key", {
x <- new_key(strrep("a", 64))
expect_s3_class(x, "vault_key")
expect_s3_class(x, "vault_secret")
})
with_parameters_test_that(
"key() fails on bad input",
{
expect_error(key(val), class = "vault_invalid_key")
},
cases(
integer = list(val = 1L),
double = list(val = 1),
short = list(val = strrep("a", 32L)),
long = list(val = strrep("a", 66L)),
bad_char = list(val = strrep("z", 64)),
extra = list(val = paste0(strrep("a", 64), "zz"))
)
)
with_parameters_test_that(
"key() works on good input",
{
expect_s3_class(key(val), "vault_key")
},
cases(
random = list(
val = "024e92803eceb33681319cab7683e5423a45702dcbf356eddebba23fc6b2a1ab"
),
valid = list(val = strrep("a", 64)),
upper = list(val = strrep("A", 64))
)
)
test_that("is_key() works", {
expect_true(is_key(new_key()))
expect_false(is_key(new_secret()))
})
test_that("as_key() works", {
expect_s3_class(as_key(strrep("a", 64)), "vault_key")
})
with_parameters_test_that(
"as_key() fails for non-characters",
{
expect_error(as_key(val))
},
cases(
integer = list(val = 1L),
double = list(val = 1),
logical = list(val = TRUE)
)
)
test_that("a key can be cast to a secret", {
x <- vec_cast(generate_key(), new_secret())
expect_s3_class(x, c("vault_secret", "vctrs_vctr"), exact = TRUE)
})
test_that("a secret can be cast to a key if it has the correct format", {
x <- as_key(secret(strrep("a", 64)))
expect_s3_class(x, c("vault_key", "vault_secret", "vctrs_vctr"), exact = TRUE)
expect_error(as_key(secret("asdf")))
expect_error(as_key(secret(strrep("z", 64))))
})
test_that("vec_ptype_{abbr,full}.vault_key() is correct", {
key <- new_key("asdf")
expect_equal(vec_ptype_abbr(key), "key")
expect_equal(vec_ptype_full(key), "key")
})
test_that("is_32_byte_hexstring() uses grepl is stringr is not available", {
mock <- mock(FALSE)
orig <- base::requireNamespace
local_mock(
"requireNamespace" = function(package, ...) {
if (package == "stringr") {
return(FALSE)
}
orig(package, ...)
},
"grepl" = mock
)
is_32_byte_hexstring("testme")
expect_called(mock, n = 1L)
})
test_that("vec_ptype2.vault_key works for two keys", {
expect_equal(vec_ptype2(new_key(), new_key()), new_key())
})
with_parameters_test_that(
paste0(
"vec_ptype2.vault_key.XXX and vec_ptype2.XXX.vault_key ",
"fails where XXX != vault_key"
),
{
expect_error(vec_ptype2(x, y))
expect_error(vec_ptype2(y, x))
},
cases(
list(x = new_key(), y = character()),
list(x = new_key(), y = logical()),
list(x = new_key(), y = integer()),
list(x = new_key(), y = double())
)
)
test_that("generate_key() returns a vault_key", {
local_options(list(usethis.quiet = TRUE))
x <- generate_key()
expect_s3_class(x, "vault_key")
})
test_that("read_vault_key() reads from envvar", {
key <- strrep("b", 64)
local_envvar(list(VAULT_KEY = key))
x <- read_vault_key()
expect_s3_class(x, "vault_key")
expect_equal(unclass(x), key)
})
test_that("read_vault_key() errors if value not set", {
local_envvar(list(VAULT_KEY = NA))
expect_error(read_vault_key())
})
verify_output("test-key-use-vault-key.txt", {
skip_on_os("windows")
use_vault_key()
})
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.