Skip to content

Commit

Permalink
Issue open-horizon#624: GET /orgs/{orgid}/status now returns 404 when…
Browse files Browse the repository at this point in the history
… the specified orgId does not exist

Signed-off-by: Ethan Weaver <emw0022@mix.wvu.edu>
  • Loading branch information
ewee33 committed Jun 27, 2022
1 parent f646035 commit 3d08537
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 13 deletions.
11 changes: 9 additions & 2 deletions src/main/scala/com/horizon/exchangeapi/OrgsRoutes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import akka.http.scaladsl.server.Directives._
import akka.http.scaladsl.server.Route
import com.horizon
import com.horizon.exchangeapi
import com.horizon.exchangeapi.auth.{DBProcessingError, IamAccountInfo, IbmCloudAuth}
import com.horizon.exchangeapi.auth.{DBProcessingError, IamAccountInfo, IbmCloudAuth, ResourceNotFoundException}

import scala.concurrent.ExecutionContext
import de.heikoseeberger.akkahttpjackson._
Expand Down Expand Up @@ -361,7 +361,12 @@ trait OrgsRoutes extends JacksonSupport with AuthenticationSupport {
complete({
val statusResp = new OrgStatus()
//perf: use a DBIO.sequence instead. It does essentially the same thing, but more efficiently
db.run(UsersTQ.getAllUsers(orgId).length.result.asTry.flatMap({
db.run(OrgsTQ.getOrgid(orgId).exists.result.asTry.flatMap({
case Success(orgExists) =>
if (orgExists) UsersTQ.getAllUsers(orgId).length.result.asTry
else DBIO.failed(new ResourceNotFoundException()).asTry
case Failure(t) => DBIO.failed(t).asTry
}).flatMap({
case Success(v) => statusResp.numberOfUsers = v
NodesTQ.getAllNodes(orgId).length.result.asTry
case Failure(t) => DBIO.failed(t).asTry
Expand All @@ -385,6 +390,8 @@ trait OrgsRoutes extends JacksonSupport with AuthenticationSupport {
case Success(v) => statusResp.dbSchemaVersion = v.head
statusResp.msg = ExchMsg.translate("exchange.server.operating.normally")
(HttpCode.OK, statusResp.toGetOrgStatusResponse)
case Failure(t: com.horizon.exchangeapi.auth.ResourceNotFoundException) =>
(HttpCode.NOT_FOUND, ApiResponse(ApiRespType.NOT_FOUND, ExchMsg.translate("org.not.found", orgId)))
case Failure(t: org.postgresql.util.PSQLException) =>
if (t.getMessage.contains("An I/O error occurred while sending to the backend")) (HttpCode.BAD_GW, statusResp.toGetOrgStatusResponse)
else (HttpCode.INTERNAL_ERROR, statusResp.toGetOrgStatusResponse)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,20 +205,11 @@ class TestGetOrgStatusRoute extends AnyFunSuite with BeforeAndAfterAll {
DBCONNECTION.getDb.close()
}

//is this intended? I would think this should fail with 404 not found
test("GET /orgs/doesNotExist" + ROUTE + " -- success, but returns 0 for everything") {
test("GET /orgs/doesNotExist" + ROUTE + " -- 404 NOT FOUND") {
val response: HttpResponse[String] = Http(URL + "doesNotExist" + ROUTE).headers(ACCEPT).headers(ROOTAUTH).asString
info("Code: " + response.code)
info("Body: " + response.body)
assert(response.code === HttpCode.OK.intValue)
val status: GetOrgStatusResponse = JsonMethods.parse(response.body).extract[GetOrgStatusResponse]
assert(status.msg === ExchMsg.translate("exchange.server.operating.normally"))
assert(status.numberOfNodes === 0)
assert(status.numberOfUsers === 0)
assert(status.numberOfNodeMsgs === 0)
assert(status.numberOfNodeAgreements === 0)
assert(status.numberOfRegisteredNodes === 0)
assert(status.SchemaVersion === SCHEMAVERSION)
assert(response.code === HttpCode.NOT_FOUND.intValue)
}

test("GET /orgs/" + TESTORGS(0).orgId + ROUTE + " as root -- normal success") {
Expand Down

0 comments on commit 3d08537

Please sign in to comment.