Skip to content

Commit

Permalink
Merge pull request #15658 from CDCgov/platform/david-navapbc/13925/si…
Browse files Browse the repository at this point in the history
…ngleton_http_client/redux

Platform/david navapbc/13925/singleton http client/redux
  • Loading branch information
david-navapbc authored Aug 27, 2024
2 parents 69486b4 + 184db65 commit 8bb0755
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 117 deletions.
95 changes: 35 additions & 60 deletions prime-router/src/main/kotlin/cli/tests/AuthTests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,9 @@ import gov.cdc.prime.router.tokens.AuthUtils
import gov.cdc.prime.router.tokens.DatabaseJtiCache
import gov.cdc.prime.router.tokens.Scope
import io.ktor.client.plugins.timeout
import io.ktor.client.request.accept
import io.ktor.client.request.get
import io.ktor.http.ContentType
import io.ktor.http.HttpStatusCode
import kotlinx.coroutines.runBlocking
import java.io.File
import java.io.IOException
import java.net.URLEncoder
Expand Down Expand Up @@ -1172,107 +1170,84 @@ class Server2ServerAuthTests : CoolTest() {
)
val orgEndpoint = "${environment.url}/api/settings/organizations"

val client = HttpClientUtils.createDefaultHttpClient(
userToken
)

val clientAdmin = HttpClientUtils.createDefaultHttpClient(
adminToken
)

// Case: GET All Org Settings (Admin-only endpoint)
// Unhappy Path: user on admin-only endpoint
val response = runBlocking {
client.get(orgEndpoint) {
timeout {
requestTimeoutMillis = 45000
// default timeout is 15s; raising higher due to slow Function startup issues
}
accept(ContentType.Application.Json)
}
}
val response = HttpClientUtils.get(
url = orgEndpoint,
accessToken = userToken,
timeout = 45000, // default timeout is 15s; raising higher due to slow Function startup issues
acceptedContent = ContentType.Application.Json
)

if (response.status != HttpStatusCode.Unauthorized) {
bad(
"***$name Test settings/organizations Unhappy Path (user-GET All Orgs) FAILED:" +
" Expected HttpStatus ${HttpStatusCode.Unauthorized}. Got ${response.status.value}"
" Expected HttpStatus ${HttpStatusCode.Unauthorized}. Got ${response.status.value}"
)
return false
}

// Happy Path: admin on admin-only endpoint
val response2 = runBlocking {
clientAdmin.get(orgEndpoint) {
timeout {
requestTimeoutMillis = 45000
// default timeout is 15s; raising higher due to slow Function startup issues
}
accept(ContentType.Application.Json)
}
}
val response2 = HttpClientUtils.get(
url = orgEndpoint,
accessToken = adminToken,
timeout = 45000, // default timeout is 15s; raising higher due to slow Function startup issues
acceptedContent = ContentType.Application.Json
)

if (response2.status != HttpStatusCode.OK) {
bad(
"***$name Test settings/organizations Happy Path (admin-GET All Orgs) FAILED:" +
" Expected HttpStatus ${HttpStatusCode.OK}. Got ${response2.status.value}"
" Expected HttpStatus ${HttpStatusCode.OK}. Got ${response2.status.value}"
)
return false
}

// Case: GET Receivers for an Org (Endpoint allowed for admins and members of the org)
// Happy Path: user on user-allowed endpoint
val response3 = runBlocking {
client.get("$orgEndpoint/${authorizedOrg.name}/receivers") {
timeout {
requestTimeoutMillis = 45000
// default timeout is 15s; raising higher due to slow Function startup issues
}
accept(ContentType.Application.Json)
}
}
val response3 = HttpClientUtils.get(
url = "$orgEndpoint/${authorizedOrg.name}/receivers",
accessToken = userToken,
timeout = 45000, // default timeout is 15s; raising higher due to slow Function startup issues
acceptedContent = ContentType.Application.Json
)

if (response3.status != HttpStatusCode.OK) {
bad(
"***$name Test settings/organizations Happy Path (user-GET Org Receivers) FAILED:" +
" Expected HttpStatus ${HttpStatusCode.OK}. Got ${response3.status.value}"
" Expected HttpStatus ${HttpStatusCode.OK}. Got ${response3.status.value}"
)
return false
}

// Happy Path: admin on user-allowed endpoint
val response4 = runBlocking {
clientAdmin.get("$orgEndpoint/${authorizedOrg.name}/receivers") {
timeout {
requestTimeoutMillis = 45000
// default timeout is 15s; raising higher due to slow Function startup issues
}
accept(ContentType.Application.Json)
}
}
val response4 = HttpClientUtils.get(
url = "$orgEndpoint/${authorizedOrg.name}/receivers",
accessToken = adminToken,
timeout = 45000, // default timeout is 15s; raising higher due to slow Function startup issues
acceptedContent = ContentType.Application.Json
)

if (response4.status != HttpStatusCode.OK) {
bad(
"***$name Test settings/organizations Happy Path (admin-GET Org Receivers) FAILED:" +
" Expected HttpStatus ${HttpStatusCode.OK}. Got ${response4.status.value}"
" Expected HttpStatus ${HttpStatusCode.OK}. Got ${response4.status.value}"
)
return false
}

// UnhappyPath: user on an unauthorized org name
val response5 = runBlocking {
client.get("$orgEndpoint/${unauthorizedOrg.name}/receivers") {
timeout {
requestTimeoutMillis = 45000
// default timeout is 15s; raising higher due to slow Function startup issues
}
accept(ContentType.Application.Json)
}
}
val response5 = HttpClientUtils.get(
url = "$orgEndpoint/${unauthorizedOrg.name}/receivers",
accessToken = userToken,
timeout = 45000, // default timeout is 15s; raising higher due to slow Function startup issues
acceptedContent = ContentType.Application.Json
)

if (response5.status != HttpStatusCode.Unauthorized) {
bad(
"***$name Test settings/organizations Unhappy Path (user-GET Unauthorized Org Receivers) FAILED:" +
" Expected HttpStatus ${HttpStatusCode.Unauthorized}. Got ${response5.status.value}"
" Expected HttpStatus ${HttpStatusCode.Unauthorized}. Got ${response5.status.value}"
)
return false
}
Expand Down
Loading

0 comments on commit 8bb0755

Please sign in to comment.