File: test-secret.R

package info (click to toggle)
r-cran-httr2 1.1.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,604 kB
  • sloc: sh: 21; makefile: 2
file content (71 lines) | stat: -rw-r--r-- 1,886 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
test_that("encryption and decryption of string is symmetric", {
  key <- secret_make_key()

  x <- "Testing 1...2...3..."
  enc <- secret_encrypt(x, key)
  dec <- secret_decrypt(enc, key)
  expect_equal(dec, x)
})

test_that("encryption and decryption of object is symmetric", {
  key <- secret_make_key()
  path <- withr::local_tempfile()

  x1 <- list(1:10, letters)
  secret_write_rds(x1, path, key)
  x2 <- secret_read_rds(path, key)
  expect_equal(x1, x2)
})

test_that("encryption and decryption of file is symmetric", {
  key <- secret_make_key()
  path <- withr::local_tempfile(lines = letters)

  secret_encrypt_file(path, key)

  local({
    path_dec <<- secret_decrypt_file(path, key)
    expect_equal(readLines(path_dec, warn = FALSE), letters)
  })
  expect_false(file.exists(path_dec))

})

test_that("can unobfuscate obfuscated string", {
  x <- obfuscated("qw6Ua_n2LR_xzuk2uqp2dhb5OaE")
  expect_equal(unobfuscate(x), "test")
})

test_that("obfuscated strings are hidden", {
  expect_snapshot({
    x <- obfuscated("abcdef")
    x
    str(x)
  })
})

test_that("unobfuscate operates recursively", {
  expect_equal(unobfuscate(NULL), NULL)
  expect_equal(unobfuscate("x"), "x")
  expect_equal(unobfuscate(list(list(obfuscated("qw6Ua_n2LR_xzuk2uqp2dhb5OaE")))), list(list("test")))
})

test_that("secret_has_key returns FALSE/TRUE", {
  withr::local_envvar(ENVVAR_THAT_DOES_EXIST = "1")
  expect_equal(secret_has_key("ENVVAR_THAT_DOESNT_EXIST"), FALSE)
  expect_equal(secret_has_key("ENVVAR_THAT_DOES_EXIST"), TRUE)
})


test_that("can coerce to a key", {
  expect_equal(as_key(I("YWJj")), charToRaw("abc"))
  expect_equal(as_key(as.raw(c(1, 2, 3))), as.raw(c(1, 2, 3)))

  withr::local_envvar(KEY = "YWJj", TESTTHAT = "false")
  expect_equal(as_key("KEY"), charToRaw("abc"))

  expect_snapshot(error = TRUE, {
    as_key("ENVVAR_THAT_DOESNT_EXIST")
    as_key(1)
  })
})