From 3d08537bec7a8a705412fc7ddf1aa9498fcb831e Mon Sep 17 00:00:00 2001 From: Ethan Weaver Date: Mon, 27 Jun 2022 15:09:12 -0400 Subject: [PATCH] Issue #624: GET /orgs/{orgid}/status now returns 404 when the specified orgId does not exist Signed-off-by: Ethan Weaver --- .../scala/com/horizon/exchangeapi/OrgsRoutes.scala | 11 +++++++++-- .../route/organization/TestGetOrgStatusRoute.scala | 13 ++----------- 2 files changed, 11 insertions(+), 13 deletions(-) diff --git a/src/main/scala/com/horizon/exchangeapi/OrgsRoutes.scala b/src/main/scala/com/horizon/exchangeapi/OrgsRoutes.scala index 16874260..6d2fbeb5 100644 --- a/src/main/scala/com/horizon/exchangeapi/OrgsRoutes.scala +++ b/src/main/scala/com/horizon/exchangeapi/OrgsRoutes.scala @@ -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._ @@ -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 @@ -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) diff --git a/src/test/scala/com/horizon/exchangeapi/route/organization/TestGetOrgStatusRoute.scala b/src/test/scala/com/horizon/exchangeapi/route/organization/TestGetOrgStatusRoute.scala index afa1520e..6cb5b881 100644 --- a/src/test/scala/com/horizon/exchangeapi/route/organization/TestGetOrgStatusRoute.scala +++ b/src/test/scala/com/horizon/exchangeapi/route/organization/TestGetOrgStatusRoute.scala @@ -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") {