Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FDS-2213 add to synapse api tests #613

Merged
merged 3 commits into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions R/synapse_rest_api.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)

}

Expand All @@ -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()
Expand All @@ -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) {
Expand Down
143 changes: 141 additions & 2 deletions tests/testthat/test_synapse_rest_api.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,152 @@ test_that("is_certified returns TRUE or FALSE", {

})

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="")
)
})

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"))
)

# bad PAT (string)
expect_error(
synapse_access(id="syn23643255", access = "DOWNLOAD", auth="adfadsf")
)
})

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", {
#
# })
Loading