Skip to content

Commit

Permalink
update tests for CRAN release
Browse files Browse the repository at this point in the history
  • Loading branch information
leeper committed Apr 3, 2018
1 parent 704cb4e commit 63d81b3
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 6 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Package: aws.signature
Type: Package
Title: Amazon Web Services Request Signatures
Version: 0.4.0
Date: 2018-04-02
Date: 2018-04-03
Authors@R: c(person("Thomas J.", "Leeper",
role = c("aut", "cre"),
email = "[email protected]",
Expand Down
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# aws.signature 0.4.0

* `read_credentials()` now trims excess whitespace from profile names. (#22, h/t Paul Ingles)
* `locate_credentials()` returns `region = default_region` even when no other credentials are found.

# aws.signature 0.3.7

Expand Down
23 changes: 21 additions & 2 deletions R/locate_credentials.R
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ function(key = NULL,
}
}


# check for user-supplied values
if (isTRUE(verbose)) {
message("Checking for credentials in user-supplied values")
Expand All @@ -81,6 +80,7 @@ function(key = NULL,
}
# now find region, with fail safes
if (!is.null(region) && region != "") {
region <- region
if (isTRUE(verbose)) {
message(sprintf("Using user-supplied value for AWS Region ('%s')", region))
}
Expand Down Expand Up @@ -184,6 +184,7 @@ function(key = NULL,
}
# now find region, with fail safes
if (!is.null(region) && region != "") {
region <- region
if (isTRUE(verbose)) {
message(sprintf("Using user-supplied value for AWS Region ('%s')", region))
}
Expand Down Expand Up @@ -229,8 +230,25 @@ function(key = NULL,
if (isTRUE(verbose)) {
message("No instance metadata, environment variables, or credentials file found!")
}
# now find region, with fail safes
if (!is.null(region) && region != "") {
region <- region
if (isTRUE(verbose)) {
message(sprintf("Using user-supplied value for AWS Region ('%s')", region))
}
} else if (!is.null(env$region) && env$region != "") {
region <- env$region
if (isTRUE(verbose)) {
message(sprintf("Using Environment Variable 'AWS_DEFAULT_REGION' for AWS Region ('%s')", region))
}
} else {
region <- default_region
if (isTRUE(verbose)) {
message(sprintf("Using default value for AWS Region ('%s')", region))
}
}
# return early with list of empty values!
return(list(key = NULL, secret = NULL, session_token = NULL, region = NULL))
return(list(key = NULL, secret = NULL, session_token = NULL, region = region))
}
if (!is.null(cred[["AWS_ACCESS_KEY_ID"]])) {
key <- cred[["AWS_ACCESS_KEY_ID"]]
Expand All @@ -252,6 +270,7 @@ function(key = NULL,
}
# now find region, with fail safes
if (!is.null(region) && region != "") {
region <- region
if (isTRUE(verbose)) {
message(sprintf("Using user-supplied value for AWS Region ('%s')", region))
}
Expand Down
30 changes: 27 additions & 3 deletions tests/testthat/tests-credentials_file.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,18 @@ test_that("default_credentials_file() works", {

context("Tests locate_credentials()")

test_that("locate_credentials() returns non-default region if requested", {
expect_true(locate_credentials(region = "foo")[["region"]] == "foo")
test_that("locate_credentials() returns non-default values if requested", {
e <- Sys.getenv(c("AWS_ACCESS_KEY_ID", "AWS_SECRET_ACCESS_KEY", "AWS_SESSION_TOKEN", "AWS_DEFAULT_REGION"))
Sys.unsetenv("AWS_ACCESS_KEY_ID")
Sys.unsetenv("AWS_SECRET_ACCESS_KEY")
Sys.unsetenv("AWS_SESSION_TOKEN")
Sys.unsetenv("AWS_DEFAULT_REGION")
expect_true(locate_credentials(key = "foo")[["key"]] == "foo", label = "locate_credentials(key = 'foo')")
expect_true(locate_credentials(secret = "foo")[["secret"]] == "foo", label = "locate_credentials(secret = 'foo')")
# session token cannot be set if key and secret are missing
expect_true(is.null(locate_credentials(session_token = "foo")[["session_token"]]), label = "locate_credentials(session_token = 'foo') returns NULL")
expect_true(locate_credentials(region = "foo")[["region"]] == "foo", label = "locate_credentials(region = 'foo')")
do.call("Sys.setenv", as.list(e))
})

test_that("locate_credentials() returns NULLs when environment variables missing", {
Expand All @@ -28,7 +38,21 @@ test_that("locate_credentials() returns NULLs when environment variables missing
expect_true(is.null(cred[["key"]]))
expect_true(is.null(cred[["secret"]]))
expect_true(is.null(cred[["session_token"]]))
expect_true(is.null(cred[["region"]]))
expect_true(cred[["region"]] == "us-east-1")
do.call("Sys.setenv", as.list(e))
})

test_that("locate_credentials() returns NULLs when environment variables present", {
e <- Sys.getenv(c("AWS_ACCESS_KEY_ID", "AWS_SECRET_ACCESS_KEY", "AWS_SESSION_TOKEN", "AWS_DEFAULT_REGION"))
Sys.setenv("AWS_ACCESS_KEY_ID" = "foo-key")
Sys.setenv("AWS_SECRET_ACCESS_KEY" = "foo-secret")
Sys.setenv("AWS_SESSION_TOKEN" = "foo-token")
Sys.setenv("AWS_DEFAULT_REGION" = "foo-region")
cred <- locate_credentials()
expect_true(cred[["key"]] == "foo-key")
expect_true(cred[["secret"]] == "foo-secret")
expect_true(cred[["session_token"]] == "foo-token")
expect_true(cred[["region"]] == "foo-region")
do.call("Sys.setenv", as.list(e))
})

Expand Down

0 comments on commit 63d81b3

Please sign in to comment.