Skip to content

Commit

Permalink
add validation to repo URL
Browse files Browse the repository at this point in the history
  • Loading branch information
cjrace committed Feb 15, 2024
1 parent c58e7ba commit b3ae9d9
Show file tree
Hide file tree
Showing 5 changed files with 176 additions and 35 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Imports:
shiny,
styler
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.2.3
RoxygenNote: 7.3.1
Suggests:
testthat (>= 3.0.0)
Config/testthat/edition: 3
105 changes: 82 additions & 23 deletions R/standard_panels.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,34 @@ library(shinyGovstyle)
#' @description
#' Create the standard DfE R-Shiny support and feedback dashboard panel.
#'
#' @param team_email Your team e-mail address as a string, this must be a education.gov.uk email
#' @param team_email Your team e-mail address, must be a education.gov.uk email
#' @param repo_name The repository name as listed on GitHub
#' @param ees_publication Whether the parent publication is hosted on Explore Education Statistics
#' @param publication_name The parent publication name
#' @param publication_stub The parent publication stub on Explore Education Statistics
#' @param alt_href Alternative link to the parent publication (if not hosted on Explore Education Statistics)
#' @param ees_publication Whether the source publication is hosted on
#' Explore Education Statistics
#' @param publication_name The source publication name
#' @param publication_stub The source publication stub on
#' Explore Education Statistics
#' @param alt_href Alternative link to the parent publication
#' (if not hosted on Explore Education Statistics)
#' @param form_url URL to a feedback form for the dashboard
#' @param cookie_status_output name of cookie status output object
#'
#' @return
#' @return a standardised panel for a public R Shiny dashboard in DfE
#' @export
#'
#' @examples
#' # TODO: add basic example of EES publication
#' # TODO: add basic example of non-EES publication
support_panel <- function(
team_email = "",
repo_name = "",
ees_publication = TRUE,
publication_name = NULL,
publication_stub = "",
alt_href = NULL,
form_url = NULL) {

# Check that the team_email is a valid email
form_url = NULL,
cookie_status_output = "") {
# Check that the team_email is a valid dfe email ----------------------------
is_valid_dfe_email <- function(email) {
grepl(
"\\<[A-Z0-9._%+-][email protected]\\>",
Expand All @@ -39,10 +45,26 @@ support_panel <- function(
if (is_valid_dfe_email(team_email) == FALSE) {
stop(
"You have entered an invalid email in the team_email argument.
Please enter a @education.gov.uk email.")
Please enter a @education.gov.uk email."
)
}
# Check that the repo_name is a valid dfe repo ------------------------------
is_valid_repo_name <- function(url) {
grepl(
"\\https://github.com/dfe-analytical-services/+.",
as.character(url),
ignore.case = TRUE
)
}

if (is_valid_repo_name(repo_name) == FALSE) {
stop(
"Please ensure the repo URL points to a repository on the
dfe-analytical-services GitHub area."
)
}

# Build the support page ----------------------------------------------------
shiny::tabPanel(
"Support and feedback",
shinyGovstyle::gov_main_layout(
Expand All @@ -56,10 +78,15 @@ support_panel <- function(
"This dashboard is a new service that we are developing.
If you have any feedback or suggestions for improvements,
please submit them using our ",
shiny::tags$a(href = form_url, "feedback form", .noWS = c("after"))
shiny::tags$a(
href = form_url,
"feedback form",
.noWS = c("after")
)
)
} else {
shiny::tags$p("This dashboard is a new service that we are developing.")
shiny::tags$p("This dashboard is a new service that we are
developing.")
},
shiny::tags$p(
paste0(
Expand All @@ -74,36 +101,57 @@ support_panel <- function(
shiny::tags$a(
href = paste0("mailto:", team_email),
team_email,
.noWS = c("after")),
.noWS = c("after")
),
"."
),
shiny::tags$h2("Find more information on the data"),
if (ees_publication) {
shiny::tags$p(
"The parent statistical release of this dashboard,
along with methodological information, is available at the following link: ",
along with methodological information, is available at the
following link: ",
shiny::tags$a(
href = paste0("https://explore-education-statistics.service.gov.uk/find-statistics/", publication_stub),
ifelse(!is.null(publication_name), publication_name, "Explore Education Statistics"),
href = paste0(
"https://explore-education-statistics.service.gov.uk
/find-statistics/",
publication_stub
),
ifelse(!is.null(publication_name),
publication_name,
"Explore Education Statistics"
),
.noWS = c("after")
),
". The statistical release provides additional ",
shiny::tags$a(
href = paste0("https://explore-education-statistics.service.gov.uk/find-statistics/", publication_stub, "/data guidance"),
href = paste0(
"https://explore-education-statistics
.service.gov.uk/find-statistics/",
publication_stub,
"/data guidance"
),
"data guidance",
.noWS = c("after")
),
" and ",
shiny::tags$a(
href = paste0("https://explore-education-statistics.service.gov.uk/find-statistics/", publication_stub, "#explore-data-and-files"),
href = paste0(
"https://explore-education-statistics
.service.gov.uk/find-statistics/",
publication_stub,
"#explore-data-and-files"
),
"tools to access and interogate the underling data",
.noWS = c("after")
),
" contained in this dashboard."
)
} else {
shiny::tags$p(
"The parent statistical release of this dashboard, along with methodological information, is available at the following link: ",
"The parent statistical release of this dashboard,
along with methodological information,
is available at the following link: ",
shiny::tags$a(
href = alt_href,
publication_name,
Expand All @@ -113,17 +161,28 @@ support_panel <- function(
},
shiny::tags$h2("Contact us"),
shiny::tags$p(
"If you have questions about the dashboard or data within it, please contact us at ",
shiny::tags$a(href = paste0("mailto:", team_email), team_email, .noWS = c("after"))
"If you have questions about the dashboard or data within it,
please contact us at ",
shiny::tags$a(
href = paste0("mailto:", team_email),
team_email,
.noWS = c("after")
)
),
shiny::tags$h2("See the source code"),
shiny::tags$p(
"The source code for this dashboard is available in our ",
shiny::tags$a(href = paste0("https://github.com/dfe-analytical-services/", repo_name), "GitHub repository", .noWS = c("after")),
shiny::tags$a(
href = paste0(
"https://github.com/dfe-analytical-services/", repo_name
),
"GitHub repository",
.noWS = c("after")
),
"."
),
shiny::tags$h2("Use of cookies"),
shiny::textOutput("cookie_status"),
shiny::textOutput(cookie_status_output),
shiny::actionButton("remove", "Reset cookie consent"),
)
)
Expand Down
11 changes: 8 additions & 3 deletions R/testing.R
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#' Tidy code
#'
#' @description Script to apply styler code styling to scripts within the shiny directory structure.
#' @description Script to apply styler code styling to scripts
#' within the shiny directory structure.
#'
#' @param subdirs List of sub-directories to (recursively search for R scripts to be styled)
#' @param subdirs List of sub-directories to
#' recursively search for R scripts to be styled
#'
#' @return
#' @export
Expand All @@ -23,7 +25,10 @@ tidy_code <- function(subdirs = c("R", "tests")) {
}
}
if (any(script_changes)) {
message("Styling changes have been made to your scripts, please review any changes made.")
message(
"Styling changes have been made to your scripts,
please review any changes made."
)
}
return(any(script_changes))
}
12 changes: 11 additions & 1 deletion man/support_panel.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

81 changes: 74 additions & 7 deletions tests/testthat/test-support-panel.R
Original file line number Diff line number Diff line change
@@ -1,18 +1,85 @@
test_that("email needs to follow standard pattern", {
# Test that a @education.gov.uk passes
expect_no_error(support_panel(team_email = "[email protected]"))
expect_no_error(support_panel(team_email = "[email protected]"))
expect_no_error(
support_panel(
team_email = "[email protected]",
repo_name = "https://github.com/dfe-analytical-services/dfeshiny"
)
)
expect_no_error(
support_panel(
team_email = "[email protected]",
repo_name = "https://github.com/dfe-analytical-services/dfeshiny"
)
)

# Testing that it will fail if it follows the [email protected] pattern
expect_error(support_panel(team_email = "[email protected]"))
expect_error(support_panel(team_email = "[email protected]"))
expect_error(
support_panel(
team_email = "[email protected]",
repo_name = "https://github.com/dfe-analytical-services/dfeshiny"
)
)
expect_error(
support_panel(
team_email = "[email protected]",
repo_name = "https://github.com/dfe-analytical-services/dfeshiny"
)
)

# Testing that it will fail if there is no final dot and section
expect_error(support_panel(team_email = "thisshoulfail@something"))
expect_error(
support_panel(
team_email = "thisshoulfail@something",
repo_name = "https://github.com/dfe-analytical-services/dfeshiny"
)
)

# Test Wales will fail
expect_error(support_panel(team_email = "[email protected]"))
expect_error(
support_panel(
team_email = "[email protected]",
repo_name = "https://github.com/dfe-analytical-services/dfeshiny"
)
)

# Test that it fails if there is no @ symbol
expect_error(support_panel(team_email = "team.team"))
expect_error(
support_panel(
team_email = "team.team",
repo_name = "https://github.com/dfe-analytical-services/dfeshiny"
)
)
})

test_that("repo URL needs to follow standard pattern", {
# Test that a github repo in dfe analytical services passes
expect_no_error(
support_panel(
team_email = "[email protected]",
repo_name = "https://github.com/dfe-analytical-services/dfeshiny"
)
)

# Testing that it will fail if it's on GitHub but not in DfE area
expect_error(
support_panel(
team_email = "[email protected]",
repo_name = "https://github.com/cjrace/dfeR"
)
)
expect_error(
support_panel(
team_email = "[email protected]",
repo_name = "https://github.com/DfE-R-Community/ggdfe"
)
)

# Testing that it will fail if there is no final dot and section
expect_error(
support_panel(
team_email = "[email protected]",
repo_name = "https://explore-education-statistics.service.gov.uk/"
)
)
})

0 comments on commit b3ae9d9

Please sign in to comment.