Make the stop endpoint more robust and test (#6217)

* Make the stop endpoint more robust and test

changelog_begin
changelog_end

* Stopping an unknown trigger gives 404
This commit is contained in:
Shayne Fletcher 2020-06-03 14:56:51 -04:00 committed by GitHub
parent 8390dff7f9
commit c2961b1957
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 61 additions and 9 deletions

View File

@ -103,8 +103,8 @@ class Server(dar: Option[Dar[(PackageId, Package)]], jdbcConfig: Option[JdbcConf
}
}
private def getRunningTrigger(uuid: UUID): RunningTrigger = {
triggers(uuid) // TODO: Improve as might throw NoSuchElementException.
private def getRunningTrigger(uuid: UUID): Option[RunningTrigger] = {
triggers.get(uuid)
}
private def addRunningTrigger(t: RunningTrigger): Either[String, Unit] = {
@ -234,16 +234,21 @@ object Server {
JsObject(("triggerId", triggerInstance.toString.toJson))
}
def stopTrigger(uuid: UUID, token: (Jwt, JwtPayload)): JsValue = {
def stopTrigger(uuid: UUID, token: (Jwt, JwtPayload)): Option[JsValue] = {
//TODO(SF, 2020-05-20): At least check that the provided token
//is the same as the one used to start the trigger and fail with
//'Unauthorized' if not (expect we'll be able to do better than
//this).
val runningTrigger = server.getRunningTrigger(uuid)
runningTrigger.runner ! TriggerRunner.Stop
server.logTriggerStatus(runningTrigger, "stopped: by user request")
server.removeRunningTrigger(runningTrigger)
JsObject(("triggerId", uuid.toString.toJson))
server
.getRunningTrigger(uuid)
.map(
runningTrigger => {
runningTrigger.runner ! TriggerRunner.Stop
server.logTriggerStatus(runningTrigger, "stopped: by user request")
server.removeRunningTrigger(runningTrigger)
JsObject(("triggerId", uuid.toString.toJson))
}
)
}
def listTriggers(jwt: Jwt): (StatusCode, JsObject) = {
@ -373,7 +378,15 @@ object Server {
unauthorized =>
complete(
errorResponse(StatusCodes.UnprocessableEntity, unauthorized.message)),
triggerInstance => complete(successResponse(triggerInstance))
triggerInstance =>
triggerInstance match {
case Some(stoppedTriggerId) => complete(successResponse(stoppedTriggerId))
case None =>
complete(
errorResponse(
StatusCodes.NotFound,
"Unknown trigger: '" + uuid.toString + "'"))
}
)
}
}

View File

@ -500,4 +500,43 @@ class ServiceTest extends AsyncFlatSpec with Eventually with Matchers with Postg
} yield succeed
}
it should "stopping a trigger without providing a token should be unauthorized" in withHttpService(
None) { (uri: Uri, client: LedgerClient, ledgerProxy: Proxy) =>
val uuid: String = "ffffffff-ffff-ffff-ffff-ffffffffffff"
val req = HttpRequest(
method = HttpMethods.DELETE,
uri = uri.withPath(Uri.Path(s"/v1/stop/$uuid")),
)
for {
resp <- Http().singleRequest(req)
body <- responseBodyToString(resp)
JsObject(fields) = body.parseJson
_ <- fields.get("status") should equal(Some(JsNumber(422)))
_ <- fields.get("errors") should equal(
Some(JsArray(JsString("missing Authorization header with OAuth 2.0 Bearer Token"))))
} yield succeed
}
it should "stopping a trigger that can't parse as a UUID gives a 404 response" in withHttpService(
None) { (uri: Uri, client: LedgerClient, ledgerProxy: Proxy) =>
for {
resp <- stopTrigger(uri, "No More Mr Nice Guy", "Alice")
_ <- assert(resp.status.isFailure() && resp.status.intValue() == 404)
} yield succeed
}
it should "stopping an unknown trigger gives an error response" in withHttpService(None) {
(uri: Uri, client: LedgerClient, ledgerProxy: Proxy) =>
val uuid: String = "ffffffff-ffff-ffff-ffff-ffffffffffff"
for {
resp <- stopTrigger(uri, uuid, "Alice")
_ <- assert(resp.status.isFailure() && resp.status.intValue() == 404)
body <- responseBodyToString(resp)
JsObject(fields) = body.parseJson
_ <- fields.get("status") should equal(Some(JsNumber(404)))
_ <- fields.get("errors") should equal(
Some(JsArray(JsString("Unknown trigger: '" + uuid.toString + "'"))))
} yield succeed
}
}