Skip to content

Commit

Permalink
Merge 4823253 into 4da0f19
Browse files Browse the repository at this point in the history
  • Loading branch information
breilly2 committed Aug 26, 2019
2 parents 4da0f19 + 4823253 commit 4f5509a
Show file tree
Hide file tree
Showing 3 changed files with 160 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/test/resources/reference.conf
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,6 @@ notification {
fullyQualifiedNotificationTopic = "dummy"
}

arrow {
baseUrl = "http://localhost:9394"
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ object MockUtils {
val cromiamServerPort = 8995
val searchServerPort = 9292
val bagitServerPort = 9393
val arrowServerPort = 9394

def randomPositiveInt(): Int = {
scala.util.Random.nextInt(9) + 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ class WorkspaceApiServiceSpec extends BaseServiceSpec with WorkspaceApiService w
private final val tsvImportPath = workspacesRoot + "/%s/%s/importEntities".format(workspace.namespace, workspace.name)
private final val tsvImportFlexiblePath = workspacesRoot + "/%s/%s/flexibleImportEntities".format(workspace.namespace, workspace.name)
private final val bagitImportPath = workspacesRoot + "/%s/%s/importBagit".format(workspace.namespace, workspace.name)
private final val pfbImportPath = workspacesRoot + "/%s/%s/importPFB".format(workspace.namespace, workspace.name)
private final val bucketUsagePath = s"$workspacesPath/bucketUsage"
private final val storageCostEstimatePath = s"$workspacesPath/storageCostEstimate"
private final val tagAutocompletePath = s"$workspacesRoot/tags"
Expand Down Expand Up @@ -148,19 +149,21 @@ class WorkspaceApiServiceSpec extends BaseServiceSpec with WorkspaceApiService w

var rawlsServer: ClientAndServer = _
var bagitServer: ClientAndServer = _
var arrowServer: ClientAndServer = _

/** Stubs the mock Rawls service to respond to a request. Used for testing passthroughs.
*
* @param method HTTP method to respond to
* @param path request path
* @param status status for the response
*/
def stubRawlsService(method: HttpMethod, path: String, status: StatusCode, body: Option[String] = None, query: Option[(String, String)] = None): Unit = {
def stubRawlsService(method: HttpMethod, path: String, status: StatusCode, body: Option[String] = None, query: Option[(String, String)] = None, requestBody: Option[String] = None): Unit = {
rawlsServer.reset()
val request = org.mockserver.model.HttpRequest.request()
.withMethod(method.name)
.withPath(path)
if (query.isDefined) request.withQueryStringParameter(query.get._1, query.get._2)
requestBody.foreach(request.withBody)
val response = org.mockserver.model.HttpResponse.response()
.withHeaders(MockUtils.header).withStatusCode(status.intValue)
if (body.isDefined) response.withBody(body.get)
Expand Down Expand Up @@ -246,18 +249,21 @@ class WorkspaceApiServiceSpec extends BaseServiceSpec with WorkspaceApiService w
override def beforeAll(): Unit = {
rawlsServer = startClientAndServer(MockUtils.workspaceServerPort)
bagitServer = startClientAndServer(MockUtils.bagitServerPort)
arrowServer = startClientAndServer(MockUtils.arrowServerPort)
}

override def afterAll(): Unit = {
rawlsServer.stop
bagitServer.stop
arrowServer.stop
}

override def beforeEach(): Unit = {
this.searchDao.reset
}

override def afterEach(): Unit = {
arrowServer.reset
this.searchDao.reset
}

Expand Down Expand Up @@ -986,6 +992,155 @@ class WorkspaceApiServiceSpec extends BaseServiceSpec with WorkspaceApiService w
}
}

"WorkspaceService PFB Tests" - {
"should 400 if PFB URL is not https" in {
(Post(pfbImportPath, HttpEntity(MediaTypes.`application/json`, s"""{"url":"http://missing.avro"}"""))
~> dummyUserIdHeaders(dummyUserId)
~> sealRoute(workspaceRoutes)) ~> check {
status should equal(BadRequest)
}
}

"should 401 if avro file access is unauthorized" in {
arrowServer
.when(request().withMethod("POST").withPath("/avroToRawls"))
.respond(org.mockserver.model.HttpResponse.response()
.withStatusCode(401)
.withBody("unauthorized.avro not found"))

(Post(pfbImportPath, HttpEntity(MediaTypes.`application/json`, s"""{"url":"https://unauthorized.avro"}"""))
~> dummyUserIdHeaders(dummyUserId)
~> sealRoute(workspaceRoutes)) ~> check {
status should equal(Unauthorized)
body.asString should include ("unauthorized.avro not found")
}
}

"should 403 if avro file access is forbidden" in {
arrowServer
.when(request().withMethod("POST").withPath("/avroToRawls"))
.respond(org.mockserver.model.HttpResponse.response()
.withStatusCode(403)
.withBody("forbidden.avro not found"))

(Post(pfbImportPath, HttpEntity(MediaTypes.`application/json`, s"""{"url":"https://forbidden.avro"}"""))
~> dummyUserIdHeaders(dummyUserId)
~> sealRoute(workspaceRoutes)) ~> check {
status should equal(Forbidden)
body.asString should include ("forbidden.avro not found")
}
}

"should 404 if avro file is not found" in {
arrowServer
.when(request().withMethod("POST").withPath("/avroToRawls"))
.respond(org.mockserver.model.HttpResponse.response()
.withStatusCode(404)
.withBody("missing.avro not found"))

(Post(pfbImportPath, HttpEntity(MediaTypes.`application/json`, s"""{"url":"https://missing.avro"}"""))
~> dummyUserIdHeaders(dummyUserId)
~> sealRoute(workspaceRoutes)) ~> check {
status should equal(NotFound)
body.asString should include ("missing.avro not found")
}
}

"should 500 if arrow fails" in {
arrowServer
.when(request().withMethod("POST").withPath("/avroToRawls"))
.respond(org.mockserver.model.HttpResponse.response()
.withStatusCode(400)
.withBody("invalid request"))

(Post(pfbImportPath, HttpEntity(MediaTypes.`application/json`, s"""{"url":"https://missing.avro"}"""))
~> dummyUserIdHeaders(dummyUserId)
~> sealRoute(workspaceRoutes)) ~> check {
status should equal(InternalServerError)
body.asString should include ("invalid request")
}
}

"should 401 if workspace access unauthorized" in {
arrowServer
.when(request().withMethod("POST").withPath("/avroToRawls"))
.respond(org.mockserver.model.HttpResponse.response()
.withStatusCode(200)
.withBody("Pretend this is Rawls upsert JSON"))
stubRawlsService(HttpMethods.POST, s"$workspacesPath/entities/batchUpsert", Unauthorized, requestBody = Some("Pretend this is Rawls upsert JSON"), body = Some("workspace access unauthorized"))

(Post(pfbImportPath, HttpEntity(MediaTypes.`application/json`, s"""{"url":"https://good.avro"}"""))
~> dummyUserIdHeaders(dummyUserId)
~> sealRoute(workspaceRoutes)) ~> check {
status should equal(Unauthorized)
body.asString should include ("workspace access unauthorized")
}
}

"should 403 if workspace access forbidden" in {
arrowServer
.when(request().withMethod("POST").withPath("/avroToRawls"))
.respond(org.mockserver.model.HttpResponse.response()
.withStatusCode(200)
.withBody("Pretend this is Rawls upsert JSON"))
stubRawlsService(HttpMethods.POST, s"$workspacesPath/entities/batchUpsert", Forbidden, requestBody = Some("Pretend this is Rawls upsert JSON"), body = Some("workspace access forbidden"))

(Post(pfbImportPath, HttpEntity(MediaTypes.`application/json`, s"""{"url":"https://good.avro"}"""))
~> dummyUserIdHeaders(dummyUserId)
~> sealRoute(workspaceRoutes)) ~> check {
status should equal(Forbidden)
body.asString should include ("workspace access forbidden")
}
}

"should 404 if workspace not found" in {
arrowServer
.when(request().withMethod("POST").withPath("/avroToRawls"))
.respond(org.mockserver.model.HttpResponse.response()
.withStatusCode(200)
.withBody("Pretend this is Rawls upsert JSON"))
stubRawlsService(HttpMethods.POST, s"$workspacesPath/entities/batchUpsert", NotFound, requestBody = Some("Pretend this is Rawls upsert JSON"), body = Some("workspace not found"))

(Post(pfbImportPath, HttpEntity(MediaTypes.`application/json`, s"""{"url":"https://good.avro"}"""))
~> dummyUserIdHeaders(dummyUserId)
~> sealRoute(workspaceRoutes)) ~> check {
status should equal(NotFound)
body.asString should include ("workspace not found")
}
}

"should 500 if rawls fails" in {
arrowServer
.when(request().withMethod("POST").withPath("/avroToRawls"))
.respond(org.mockserver.model.HttpResponse.response()
.withStatusCode(200)
.withBody("Pretend this is Rawls upsert JSON"))
stubRawlsService(HttpMethods.POST, s"$workspacesPath/entities/batchUpsert", BadRequest, requestBody = Some("Pretend this is Rawls upsert JSON"), body = Some("Rawls is unhappy"))

(Post(pfbImportPath, HttpEntity(MediaTypes.`application/json`, s"""{"url":"https://good.avro"}"""))
~> dummyUserIdHeaders(dummyUserId)
~> sealRoute(workspaceRoutes)) ~> check {
status should equal(InternalServerError)
body.asString should include ("Rawls is unhappy")
}
}

"should 204 if everything works" in {
arrowServer
.when(request().withMethod("POST").withPath("/avroToRawls"))
.respond(org.mockserver.model.HttpResponse.response()
.withStatusCode(200)
.withBody("Pretend this is Rawls upsert JSON"))
stubRawlsService(HttpMethods.POST, s"$workspacesPath/entities/batchUpsert", NoContent, requestBody = Some("Pretend this is Rawls upsert JSON"))

(Post(pfbImportPath, HttpEntity(MediaTypes.`application/json`, s"""{"url":"https://good.avro"}"""))
~> dummyUserIdHeaders(dummyUserId)
~> sealRoute(workspaceRoutes)) ~> check {
status should equal(NoContent)
}
}
}

"Workspace updateAttributes tests" - {
"when calling any method other than PATCH on workspaces/*/*/updateAttributes path" - {
"should receive a MethodNotAllowed error" in {
Expand Down

0 comments on commit 4f5509a

Please sign in to comment.