Skip to content

Commit

Permalink
clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
blakery committed Sep 16, 2024
1 parent 1798b23 commit 4a46df5
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package org.broadinstitute.dsde.rawls.webservice

import akka.actor.{ActorRef, PoisonPill}
import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport
import akka.http.scaladsl.model._
import akka.http.scaladsl.model.headers.OAuth2BearerToken
import akka.http.scaladsl.server.Route
import akka.http.scaladsl.server.Route.{seal => sealRoute}
import akka.http.scaladsl.testkit.RouteTestTimeout
import akka.http.scaladsl.testkit.{RouteTestTimeout, ScalatestRouteTest}
import akka.testkit.TestProbe
import io.opentelemetry.context.Context
import org.apache.commons.lang3.RandomStringUtils
import org.broadinstitute.dsde.rawls.WorkspaceAccessDeniedException
import org.broadinstitute.dsde.rawls.{TestExecutionContext, WorkspaceAccessDeniedException}
import org.broadinstitute.dsde.rawls.dataaccess._
import org.broadinstitute.dsde.rawls.dataaccess.slick.{ReadWriteAction, TestData, WorkflowAuditStatusRecord}
import org.broadinstitute.dsde.rawls.google.MockGooglePubSubDAO
Expand All @@ -20,9 +21,12 @@ import org.broadinstitute.dsde.rawls.model.WorkspaceJsonSupport._
import org.broadinstitute.dsde.rawls.model._
import org.broadinstitute.dsde.rawls.openam.MockUserInfoDirectives
import org.broadinstitute.dsde.rawls.submissions.SubmissionsService
import org.broadinstitute.dsde.rawls.util.MockitoTestUtils
import org.broadinstitute.dsde.workbench.model.WorkbenchEmail
import org.joda.time.DateTime
import org.mockito.Mockito.when
import org.mockito.Mockito.{verify, when}
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers
import org.scalatest.prop.TableDrivenPropertyChecks
import org.scalatest.time.{Seconds, Span}
import spray.json.DefaultJsonProtocol._
Expand Down Expand Up @@ -1458,7 +1462,7 @@ class SubmissionApiServiceSpec extends ApiServiceSpec with TableDrivenPropertyCh
List("userComment" -> "user comment updated".toJson): _*
)
) ~>
service.baseApiRoutes(Context.root()) ~>
service.testRoutes ~>
check {
status should be(StatusCodes.NoContent)
}
Expand All @@ -1478,14 +1482,14 @@ class SubmissionApiServiceSpec extends ApiServiceSpec with TableDrivenPropertyCh
List("userComment" -> "user comment updated".toJson): _*
)
) ~>
service.baseApiRoutes(Context.root()) ~>
service.testRoutes ~>
check {
status should be(StatusCodes.NotFound)
}
}

it should "return 403 when access is denied" in {
val wsName = testData.wsName
val wsName = WorkspaceName("ns", "n")
val submissionId = UUID.randomUUID().toString
val submissionsService = mock[SubmissionsService]
val update = UserCommentUpdateOperation("user comment updated")
Expand All @@ -1498,12 +1502,13 @@ class SubmissionApiServiceSpec extends ApiServiceSpec with TableDrivenPropertyCh
List("userComment" -> "user comment updated".toJson): _*
)
) ~>
service.baseApiRoutes(Context.root()) ~>
check { testResult: RouteTestResult =>
service.testRoutes ~>
check {
val response = responseAs[String]
status should be(StatusCodes.Forbidden)
response should include("insufficient permissions to perform operation on myNamespace/myWorkspace")
response should include("insufficient permissions to perform operation on")
}
verify(submissionsService).updateSubmissionUserComment(wsName, submissionId, update)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import akka.http.scaladsl.model.{StatusCodes, _}
import akka.http.scaladsl.model.headers._
import akka.http.scaladsl.testkit.ScalatestRouteTest
import bio.terra.workspace.model.{ErrorReport => _}
import io.opentelemetry.context.Context
import org.broadinstitute.dsde.rawls.{RawlsException, RawlsExceptionWithErrorReport, TestExecutionContext}
import org.broadinstitute.dsde.rawls.model.Attributable.AttributeMap
import org.broadinstitute.dsde.rawls.model.AttributeUpdateOperations._
Expand Down Expand Up @@ -109,14 +110,13 @@ class WorkspaceApiServiceSpec
val workspaceService = mock[WorkspaceService]
val mcWorkspaceService = mock[MultiCloudWorkspaceService]
val workspace = testData.workspace

val newWorkspace = WorkspaceRequest(
namespace = workspace.namespace,
name = workspace.name,
Map.empty
)
val details = WorkspaceDetails(workspace, Set())
when(mcWorkspaceService.createMultiCloudOrRawlsWorkspace(any, any, any))
when(mcWorkspaceService.createMultiCloudOrRawlsWorkspace(newWorkspace, workspaceService))
.thenReturn(Future.successful(details))
val service = new MockApiService(
workspaceServiceConstructor = _ => workspaceService,
Expand All @@ -129,12 +129,7 @@ class WorkspaceApiServiceSpec
val response = responseAs[WorkspaceDetails]
response.name shouldBe workspace.name
}
verify(mcWorkspaceService).createMultiCloudOrRawlsWorkspace(
newWorkspace,
workspaceService,
null
// FIXME: should be: RawlsRequestContext(service.userInfo, Some(Context.root()))
)
verify(mcWorkspaceService).createMultiCloudOrRawlsWorkspace(newWorkspace, workspaceService, null)
}

private val tagsTestParameters = Table[String, Option[String], Option[Int]](
Expand Down Expand Up @@ -173,20 +168,20 @@ class WorkspaceApiServiceSpec
val responseWorkspace = WorkspaceResponse(None, None, None, None, details, None, None, None, None, None)
val response: JsObject = responseWorkspace.toJson.asJsObject
val workspaceService = mock[WorkspaceService]
when(workspaceService.getWorkspaceById(any, any, any)).thenReturn(Future.successful(response))
val params = WorkspaceFieldSpecs(Some(Set("a", "b", "c")))
when(workspaceService.getWorkspaceById(workspace.workspaceId, params)).thenReturn(Future.successful(response))
val service = new MockApiService(
workspaceServiceConstructor = _ => workspaceService,
multiCloudWorkspaceServiceConstructor = _ => mcWorkspaceService
)
Get(s"/workspaces/id/${workspace.workspaceId}") ~>
Get(s"/workspaces/id/${workspace.workspaceId}?fields=a,b,c") ~>
service.testRoutes ~>
check {
status shouldBe StatusCodes.OK
val resp = responseAs[WorkspaceResponse]
resp shouldBe responseWorkspace
}
// TODO: be more specific
verify(workspaceService).getWorkspaceById(any, any, any)
verify(workspaceService).getWorkspaceById(workspace.workspaceId, params, null)
}

it should "get a workspace by name and namespace from the workspace service" in {
Expand All @@ -196,7 +191,8 @@ class WorkspaceApiServiceSpec
val responseWorkspace = WorkspaceResponse(None, None, None, None, details, None, None, None, None, None)
val response: JsObject = responseWorkspace.toJson.asJsObject
val workspaceService = mock[WorkspaceService]
when(workspaceService.getWorkspace(any, any, any)).thenReturn(Future.successful(response))
when(workspaceService.getWorkspace(workspace.toWorkspaceName, WorkspaceFieldSpecs(None)))
.thenReturn(Future.successful(response))
val service = new MockApiService(
workspaceServiceConstructor = _ => workspaceService,
multiCloudWorkspaceServiceConstructor = _ => mcWorkspaceService
Expand All @@ -208,9 +204,30 @@ class WorkspaceApiServiceSpec
val resp = responseAs[WorkspaceResponse]
resp shouldBe responseWorkspace
}
// TODO: be more specific
verify(workspaceService).getWorkspace(ArgumentMatchers.eq(workspace.toWorkspaceName), any, any)
// todo: also test with params (fields) - WorkspaceFieldSpecs.fromQueryParams(allParams, "fields")
verify(workspaceService).getWorkspace(workspace.toWorkspaceName, WorkspaceFieldSpecs(None), null)
}

it should "pass the fields parameter when getting a workspace by name and namespace" in {
val mcWorkspaceService = mock[MultiCloudWorkspaceService]
val workspace = testData.workspace
val details = WorkspaceDetails(workspace, Set())
val responseWorkspace = WorkspaceResponse(None, None, None, None, details, None, None, None, None, None)
val response: JsObject = responseWorkspace.toJson.asJsObject
val workspaceService = mock[WorkspaceService]
val params = WorkspaceFieldSpecs(Some(Set("a", "b", "c")))
when(workspaceService.getWorkspace(workspace.toWorkspaceName, params)).thenReturn(Future.successful(response))
val service = new MockApiService(
workspaceServiceConstructor = _ => workspaceService,
multiCloudWorkspaceServiceConstructor = _ => mcWorkspaceService
)
Get(s"/workspaces/${workspace.namespace}/${workspace.name}?fields=a,b,c") ~>
service.testRoutes ~>
check {
status shouldBe StatusCodes.OK
val resp = responseAs[WorkspaceResponse]
resp shouldBe responseWorkspace
}
verify(workspaceService).getWorkspace(workspace.toWorkspaceName, params, null)
}

it should "update the workspace by name and namespace" in {
Expand Down

0 comments on commit 4a46df5

Please sign in to comment.