From 3361993b8d669d0a44d4d75e54b2eb2a81346224 Mon Sep 17 00:00:00 2001 From: lakikowolfe Date: Wed, 21 Aug 2024 13:28:32 -0700 Subject: [PATCH 1/3] minor changes to improve tests --- R/synapse_rest_api.R | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/R/synapse_rest_api.R b/R/synapse_rest_api.R index 94cac87f..191fb8da 100644 --- a/R/synapse_rest_api.R +++ b/R/synapse_rest_api.R @@ -146,7 +146,7 @@ synapse_entity_children <- function(url = "https://repo-prod.prod.sagebase.org/r resp <- httr::content(req) output <- c(output, resp$page) } - bind_rows(output) + dplyr::bind_rows(output) } @@ -157,6 +157,9 @@ synapse_entity_children <- function(url = "https://repo-prod.prod.sagebase.org/r #' @param nextPageToken Synapse next page token synapse_projects_user <- function(url = "https://repo-prod.prod.sagebase.org/repo/v1/projects/user", auth, nextPageToken=NULL) { principalId <- synapse_user_profile(auth = auth)[["ownerId"]] + + if (is.null(principalId)) stop("Synapse token not valid") + hreq <- httr::GET(url = file.path(url, principalId), query = list(nextPageToken=nextPageToken)) output <- list() @@ -174,7 +177,7 @@ synapse_projects_user <- function(url = "https://repo-prod.prod.sagebase.org/rep #' @title Get projects within scope of Synapse project #' #' @param url Synapse api endpoint -#' @param id Synapse ID +#' @param id Synapse project ID #' @param auth Synapse token synapse_get_project_scope <- function(url = "https://repo-prod.prod.sagebase.org/repo/v1/entity/", id, auth) { From 9011fa045857b2d588bc04520746ca171acc1ee4 Mon Sep 17 00:00:00 2001 From: lakikowolfe Date: Wed, 21 Aug 2024 13:40:07 -0700 Subject: [PATCH 2/3] add synapse api unit tests --- tests/testthat/test_synapse_rest_api.R | 145 ++++++++++++++++++++++++- 1 file changed, 142 insertions(+), 3 deletions(-) diff --git a/tests/testthat/test_synapse_rest_api.R b/tests/testthat/test_synapse_rest_api.R index 7165fd20..08e46c30 100644 --- a/tests/testthat/test_synapse_rest_api.R +++ b/tests/testthat/test_synapse_rest_api.R @@ -18,19 +18,158 @@ test_that("synapse_user_profile returns list with NULL auth", { test_that("is_certified returns TRUE or FALSE", { - expect_true(synapse_is_certified(auth=Sys.getenv("SYNAPSE_PAT"))) + expect_false(synapse_is_certified(auth=Sys.getenv("SYNAPSE_PAT"))) expect_false(synapse_is_certified(auth=NULL)) expect_false(synapse_is_certified(auth="bad auth")) }) -test_that("get returns a tibble or error", { +test_that("synapse_get returns a tibble", { - good_req <- synapse_get(id="syn23643255", auth=Sys.getenv("SYNAPSE_PAT")) + good_req <- synapse_get(id="syn61941085", auth=Sys.getenv("SYNAPSE_PAT")) expect_true(length(good_req) > 1) +}) + +test_that("synapse_get errors as expected", { + # nonexistant id expect_error(synapse_get(id="bad", auth=Sys.getenv("SYNAPSE_PAT"))) + + # NULL id expect_error(synapse_get(id=NULL, auth=Sys.getenv("SYNAPSE_PAT"))) + + # nonexistant id and auth expect_error(synapse_get(id="bad", auth="bad")) }) + +test_that("synapse_access returns TRUE/FALSE", { + + # has DOWNLOAD access + expect_true( + synapse_access(id="syn62147982", access = "DOWNLOAD", auth=Sys.getenv("SYNAPSE_PAT")) + ) + + # doesn not have DOWNLOAD access + expect_false( + synapse_access(id="syn23643255", access = "DOWNLOAD", auth=Sys.getenv("SYNAPSE_PAT")) + ) + + # Bad PAT + expect_false( + synapse_access(id="syn23643255", access = "DOWNLOAD", auth=Sys.getenv("ABCD")) + ) +}) + +test_that("synapse_access returns errors as expected", { + + # non existent access argument + expect_error( + synapse_access(id="syn23643255", access = "TYPO", auth=Sys.getenv("SYNAPSE_PAT")) + ) + + # non existent id argument + expect_error( + synapse_access(id="not an id", access = "DOWNLOAD", auth=Sys.getenv("SYNAPSE_PAT")) + ) + + # non existant auth orgument + expect_error( + synapse_access(id="syn23643255", access = "DOWNLOAD", auth="Not an auth") + ) +}) + +test_that("synapse_entity_children returns a tibble", { + + # all arguments valid + req <- synapse_entity_children( + parentId="syn35187716", includeTypes = list("file", "folder"), auth=Sys.getenv("SYNAPSE_PAT") + ) + + expect_true( + all(c("tbl_df", "data.frame") %in% class(req)) + ) + + # no children to return + req_no_children <- synapse_entity_children( + parentId="syn35187716", includeTypes = "projects", auth=Sys.getenv("SYNAPSE_PAT") + ) + + expect_true( + all(c("tbl_df", "data.frame") %in% class(req_no_children)) + ) + + # typo in parentId + req_parentId_typo <- synapse_entity_children( + parentId="not an id", includeTypes = "projects", auth=Sys.getenv("SYNAPSE_PAT") + ) + + expect_true( + all(c("tbl_df", "data.frame") %in% class(req_parentId_typo)) + ) + + # typo in auth + req_auth_typo <- synapse_entity_children( + parentId="syn35187716", includeTypes = "projects", auth="not an auth" + ) + + expect_true( + all(c("tbl_df", "data.frame") %in% class(req_auth_typo)) + ) +}) + +test_that("synapse_projects_user returns expected tibble", { + req <- synapse_projects_user(auth=Sys.getenv("SYNAPSE_PAT")) + expect_identical(names(req), c("name", "id", "lastActivity", "modifiedOn", "modifiedBy")) +}) + +test_that("synapse_projects_user errors when auth token is not valid", { + expect_error( + req <- synapse_projects_user(auth="ABC") + ) +}) + +test_that("synapse_table_query", { + + # Return token + id <- "syn61941085" + query <- sprintf("select id from %s limit 1", id) + token <- synapse_table_query(id = id, auth = Sys.getenv("SYNAPSE_PAT"), query = query, partMask = 0x10) + + expect_identical(names(token), "token") + + # not a table (returns error message but does not error) + id <- "syn52578158" + query <- sprintf("select id from %s limit 1", id) + project_resp <- synapse_table_query(id = id, query = query, partMask = 0x10, auth = Sys.getenv("SYNAPSE_PAT")) + + expect_identical(project_resp$reason, "syn52578158 is not a table or view") + + # invalid access token (returns error message but does not error) + id <- "syn52578158" + query <- sprintf("select id from %s limit 1", id) + invalid_auth_resp <- synapse_table_query(id = id, query = query, partMask = 0x10, auth = "auth") + + expect_identical(invalid_auth_resp$reason, "Invalid access token") +}) + +# TODO: Add tests for functions used in DCA dashboard modules +# test_that("synapse_table_get success", { +# +# }) +# +# test_that("get_synapse_table_names success", { +# +# }) + +# test_that("synapse_storage_projects success", { +# +# }) +# +# test_that("synapse_download_file_handle success", { +# +# }) +# +# test_that("synapse_get_manifests_in_asset_view success", { +# +# }) From 74d0d1fbc84785baa67e55a93bf93650a8699370 Mon Sep 17 00:00:00 2001 From: lakikowolfe Date: Wed, 21 Aug 2024 16:15:26 -0700 Subject: [PATCH 3/3] make changes suggested in review --- tests/testthat/test_synapse_rest_api.R | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/testthat/test_synapse_rest_api.R b/tests/testthat/test_synapse_rest_api.R index 08e46c30..050b78f2 100644 --- a/tests/testthat/test_synapse_rest_api.R +++ b/tests/testthat/test_synapse_rest_api.R @@ -18,7 +18,7 @@ test_that("synapse_user_profile returns list with NULL auth", { test_that("is_certified returns TRUE or FALSE", { - expect_false(synapse_is_certified(auth=Sys.getenv("SYNAPSE_PAT"))) + expect_true(synapse_is_certified(auth=Sys.getenv("SYNAPSE_PAT"))) expect_false(synapse_is_certified(auth=NULL)) expect_false(synapse_is_certified(auth="bad auth")) @@ -55,9 +55,9 @@ test_that("synapse_access returns TRUE/FALSE", { synapse_access(id="syn23643255", access = "DOWNLOAD", auth=Sys.getenv("SYNAPSE_PAT")) ) - # Bad PAT + # Bad PAT ("") expect_false( - synapse_access(id="syn23643255", access = "DOWNLOAD", auth=Sys.getenv("ABCD")) + synapse_access(id="syn23643255", access = "DOWNLOAD", auth="") ) }) @@ -73,9 +73,9 @@ test_that("synapse_access returns errors as expected", { synapse_access(id="not an id", access = "DOWNLOAD", auth=Sys.getenv("SYNAPSE_PAT")) ) - # non existant auth orgument + # bad PAT (string) expect_error( - synapse_access(id="syn23643255", access = "DOWNLOAD", auth="Not an auth") + synapse_access(id="syn23643255", access = "DOWNLOAD", auth="adfadsf") ) })