From 764a46e791eb94965d6fb4befacd9a4b67c2ea3d Mon Sep 17 00:00:00 2001 From: Katrin Leinweber <9948149+katrinleinweber@users.noreply.github.com> Date: Wed, 10 Mar 2021 13:36:38 +0100 Subject: [PATCH 1/2] Sanitise species names with partial matches of invalid terms correctly --- R/bd_retrieve_data.R | 2 +- tests/testthat/test-sanitise_input.R | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/R/bd_retrieve_data.R b/R/bd_retrieve_data.R index 7f2b7e4..8743a02 100644 --- a/R/bd_retrieve_data.R +++ b/R/bd_retrieve_data.R @@ -63,7 +63,7 @@ sanitise_term <- function(searchTerm, searchType) { x = searchTerm, ignore.case = TRUE ) | - grepl("(true|false|nil)", searchTerm, ignore.case = TRUE)) { + grepl("\\b(true|false|nil)\\b", searchTerm, ignore.case = TRUE)) { stop( "Illegal character detected! My apologies, but your search can only contain letters, numbers and white-space. Abbreviating genus names (e.g. 'B. subtilis') is not supported. Please spell out your searchTerm ('Bacillus subtilis'), don't use any 'special' characters and try again." ) diff --git a/tests/testthat/test-sanitise_input.R b/tests/testthat/test-sanitise_input.R index 9bb716f..85c2c3e 100644 --- a/tests/testthat/test-sanitise_input.R +++ b/tests/testthat/test-sanitise_input.R @@ -10,4 +10,5 @@ test_that("Taxon species is escaped to taxon/species", { test_that("Normal searchTerm is return without sanitation", { expect_identical(sanitise_term("Bacillus", "taxon"), "Bacillus") + expect_identical(sanitise_term("Cellulomonas xylanilytica", "taxon"), "Cellulomonas/xylanilytica") }) From fcfd2410182eb5823bed260b7e98f44b000ad037 Mon Sep 17 00:00:00 2001 From: Katrin Leinweber <9948149+katrinleinweber@users.noreply.github.com> Date: Wed, 10 Mar 2021 13:36:38 +0100 Subject: [PATCH 2/2] Separate & test invalid test cases --- R/bd_retrieve_data.R | 7 +++++-- tests/testthat/test-sanitise_input.R | 6 ++++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/R/bd_retrieve_data.R b/R/bd_retrieve_data.R index 8743a02..6155c68 100644 --- a/R/bd_retrieve_data.R +++ b/R/bd_retrieve_data.R @@ -62,11 +62,14 @@ sanitise_term <- function(searchTerm, searchType) { pattern = "[^[:alnum:] ]", x = searchTerm, ignore.case = TRUE - ) | - grepl("\\b(true|false|nil)\\b", searchTerm, ignore.case = TRUE)) { + )) { stop( "Illegal character detected! My apologies, but your search can only contain letters, numbers and white-space. Abbreviating genus names (e.g. 'B. subtilis') is not supported. Please spell out your searchTerm ('Bacillus subtilis'), don't use any 'special' characters and try again." ) + } else if (grepl("\\b(true|false|nil)\\b", searchTerm, ignore.case = TRUE)) { + stop( + "Invalid search term! My apologies, but your search can not contain only 'true', 'false' or 'nil." + ) } else if (identical(searchType, "taxon") & grepl("\\s", searchTerm)) { gsub(pattern = "\\s", replacement = "/", searchTerm) # expand "Taxon species" to "taxon/species" diff --git a/tests/testthat/test-sanitise_input.R b/tests/testthat/test-sanitise_input.R index 85c2c3e..50c855f 100644 --- a/tests/testthat/test-sanitise_input.R +++ b/tests/testthat/test-sanitise_input.R @@ -12,3 +12,9 @@ test_that("Normal searchTerm is return without sanitation", { expect_identical(sanitise_term("Bacillus", "taxon"), "Bacillus") expect_identical(sanitise_term("Cellulomonas xylanilytica", "taxon"), "Cellulomonas/xylanilytica") }) + +test_that("Invalid search terms cause errors", { + expect_error(sanitise_term("true", "taxon")) + expect_error(sanitise_term("false", "taxon")) + expect_error(sanitise_term("nil", "taxon")) +})