Skip to content

Commit

Permalink
Check for NA and blank tokens
Browse files Browse the repository at this point in the history
Closes #144
  • Loading branch information
wibeasley committed Mar 24, 2017
1 parent e6df5b3 commit 14badb5
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ Description: Encapsulates functions to streamline calls from R to the REDCap
University. The Application Programming Interface (API) offers an avenue
to access and modify data programmatically, improving the capacity for
literate and reproducible programming.
Version: 0.9.7.9000
Date: 2017-02-06
Version: 0.9.7.9001
Date: 2017-03-23
Authors@R: c(person("Will", "Beasley", role = c("aut", "cre"), email =
"[email protected]"), person("David", "Bard", role = "ctb"),
person("Thomas", "Wilson", role = "ctb"), person(given="John J",
Expand Down
8 changes: 6 additions & 2 deletions R/sanitize-token.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,15 @@
#' sanitize_token(secret_token_2)

sanitize_token <- function( token ) {
# Validate only 32-character hexadecimals, with an optional line ending.
pattern <- "^([0-9A-F]{32})(?:\\n)?$"

if( !grepl(pattern, token, perl=TRUE) )
if( is.na(token) ) {
stop("The token is `NA`, not a valid 32-character hexademical value.")
} else if( nchar(token)==0L ) {
stop("The token is an empty string, not a valid 32-character hexademical value.")
} else if( !grepl(pattern, token, perl=TRUE) ) {
stop("The token is not a valid 32-character hexademical value.")
}

sub(pattern, "\\1", token, perl=TRUE)
}
14 changes: 14 additions & 0 deletions tests/testthat/test-sanitize.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,18 @@ test_that("sanitize token w/o line endings", {
)
})

test_that("sanitize token -NA", {
secret_token <- NA_character_
expect_error(
object = sanitize_token(secret_token),
regexp = "The token is `NA`, not a valid 32-character hexademical value\\."
)
})

test_that("sanitize token -empty", {
secret_token <- ""
expect_error(
object = sanitize_token(secret_token),
regexp = "The token is an empty string, not a valid 32-character hexademical value\\."
)
})

0 comments on commit 14badb5

Please sign in to comment.