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 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
|
gc_test_that("gitcreds_set_new", os = c("windows", "macos"), {
mockery::stub(gitcreds$gitcreds_set_new, "readline", "new-secret")
mockery::stub(gitcreds$gitcreds_set_new, "cat", NULL)
gitcreds$gitcreds_set_new("https://github.com")
cred <- gitcreds_get(set_cache = FALSE)
expect_equal(cred$host, "github.com")
expect_equal(cred$password, "new-secret")
})
gc_test_that("gitcreds_set_replace", os = c("windows", "macos"), {
cred <- list(
url = "https://github.com",
username = "PersonalAccessToken",
password = "secret"
)
gitcreds_approve(cred)
mockery::stub(gitcreds$gitcreds_set_replace, "ack", FALSE)
mockery::stub(gitcreds$gitcreds_set_replace, "readline", "new-secret-2")
mockery::stub(gitcreds$gitcreds_set_replace, "cat", NULL)
expect_error(
gitcreds$gitcreds_set_replace("https://github.com", gitcreds_get()),
class = "gitcreds_abort_replace_error"
)
mockery::stub(gitcreds$gitcreds_set_replace, "ack", TRUE)
gitcreds$gitcreds_set_replace("https://github.com", gitcreds_get())
cred <- gitcreds_get(use_cache = FALSE)
expect_equal(cred$host, "github.com")
expect_equal(cred$password, "new-secret-2")
})
gc_test_that("gitcreds_set", {
# fails if not interactive
mockery::stub(gitcreds_set, "is_interactive", FALSE)
expect_error(
gitcreds_set(),
class = "gitcreds_not_interactive_error"
)
called <- NULL
mockery::stub(gitcreds_set, "is_interactive", TRUE)
mockery::stub(gitcreds_set, "gitcreds_set_replace", function(...) {
called <<- "gitcreds_set_replace"
})
mockery::stub(gitcreds_set, "gitcreds_set_new", function(...) {
called <<- "gitcreds_set_new"
})
# calls set_new if none
called <- NULL
gitcreds_set()
expect_equal(called, "gitcreds_set_new")
# calls set_replace if there is one
cred <- list(
url = "https://github.com",
username = "PersonalAccessToken",
password = "secret"
)
gitcreds_approve(cred)
called <- NULL
gitcreds_set()
expect_equal(called, "gitcreds_set_replace")
# does not use the cache
gitcreds_get()
gitcreds_reject(cred)
called <- NULL
gitcreds_set()
expect_equal(called, "gitcreds_set_new")
# deletes the cache
expect_null(gitcreds$gitcreds_get_cache(
gitcreds_cache_envvar("https://github.com")
))
})
gc_test_that("multiple matching credentials", {
cred <- list(
url = "https://github.com",
username = "PersonalAccessToken",
password = "secret"
)
gitcreds_approve(cred)
cred2 <- list(
url = "https://github.com",
username = "PersonalAccessToken2",
password = "secret2"
)
gitcreds_approve(cred2)
mockery::stub(gitcreds$gitcreds_set_replace, "ack", FALSE)
mockery::stub(gitcreds$gitcreds_set_replace, "readline", "new-secret-2")
mockery::stub(gitcreds$gitcreds_set_replace, "cat", NULL)
expect_error(
gitcreds$gitcreds_set_replace("https://github.com", gitcreds_get()),
class = "gitcreds_abort_replace_error"
)
mockery::stub(gitcreds$gitcreds_set_replace, "ack", TRUE)
gitcreds$gitcreds_set_replace("https://github.com", gitcreds_get())
cred <- gitcreds_get(use_cache = FALSE)
expect_equal(cred$host, "github.com")
expect_equal(cred$password, "new-secret-2")
})
|