Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tlangs committed Oct 13, 2023
1 parent 5682628 commit 6cd8837
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ abstract class MockSamRoutes(
val openTelemetry: OpenTelemetryMetrics[IO]
) extends LazyLogging
with ResourceRoutes
with OldUserRoutes
with OldUserRoutes
with UserRoutesV1
with MockStatusRoutes
with TermsOfServiceRoutes
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package org.broadinstitute.dsde.workbench.sam.api

import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport._
import akka.http.scaladsl.model.StatusCodes
import akka.http.scaladsl.testkit.ScalatestRouteTest
import org.broadinstitute.dsde.workbench.model.{ErrorReport, WorkbenchEmail}
import org.broadinstitute.dsde.workbench.model.ErrorReportJsonSupport._
import org.broadinstitute.dsde.workbench.sam.model._
import org.broadinstitute.dsde.workbench.sam.model.api.SamJsonSupport._
import org.broadinstitute.dsde.workbench.sam.model.api.SamUser
import org.broadinstitute.dsde.workbench.sam.service._
import org.broadinstitute.dsde.workbench.sam.{Generator, TestSupport}
import org.mockito.scalatest.MockitoSugar
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers

class UserRoutesV2Spec extends AnyFlatSpec with Matchers with ScalatestRouteTest with MockitoSugar with TestSupport {
val defaultUser: SamUser = Generator.genWorkbenchUserGoogle.sample.get
val otherUser: SamUser = Generator.genWorkbenchUserGoogle.sample.get
val adminGroupEmail: WorkbenchEmail = Generator.genFirecloudEmail.sample.get
val allUsersGroup: BasicWorkbenchGroup = BasicWorkbenchGroup(CloudExtensions.allUsersGroupName, Set(), WorkbenchEmail("all_users@fake.com"))

"GET /api/users/v2/self" should "get the user object of the requesting user" in {
// Arrange
val samRoutes = new MockSamRoutesBuilder(allUsersGroup)
.withEnabledUser(defaultUser) // "persisted/enabled" user we will check the status of
.callAsNonAdminUser()
.build

// Act and Assert
Get(s"/api/users/v2/self") ~> samRoutes.route ~> check {
status shouldEqual StatusCodes.OK
responseAs[SamUser] should be(defaultUser)
}
}

"GET /api/users/v2/{sam_user_id}" should "return the regular user if they're getting themselves" in {
// Arrange
val samRoutes = new MockSamRoutesBuilder(allUsersGroup)
.withEnabledUsers(Seq(defaultUser, otherUser))
.callAsNonAdminUser()
.build

// Act and Assert
Get(s"/api/users/v2/${defaultUser.id}") ~> samRoutes.route ~> check {
status shouldEqual StatusCodes.OK
responseAs[SamUser] should be(defaultUser)
}
}

it should "fail with Not Found if a regular user is getting another user" in {
// Arrange
val otherUser = Generator.genWorkbenchUserGoogle.sample.get
val samRoutes = new MockSamRoutesBuilder(allUsersGroup)
.withEnabledUsers(Seq(defaultUser, otherUser))
.callAsNonAdminUser()
.build

// Act and Assert
Get(s"/api/users/v2/${otherUser.id}") ~> samRoutes.route ~> check {
status shouldEqual StatusCodes.NotFound
val foo = responseAs[ErrorReport]
foo.message contains "You must be an admin"
}
}

it should "succeed if an admin user is getting another user" in {
// Arrange
val samRoutes = new MockSamRoutesBuilder(allUsersGroup)
.withEnabledUsers(Seq(defaultUser, otherUser))
.callAsAdminUser()
.build

// Act and Assert
Get(s"/api/users/v2/${defaultUser.id}") ~> samRoutes.route ~> check {
status shouldEqual StatusCodes.OK
responseAs[SamUser] should be(defaultUser)
}

Get(s"/api/users/v2/${otherUser.id}") ~> samRoutes.route ~> check {
status shouldEqual StatusCodes.OK
responseAs[SamUser] should be(otherUser)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package org.broadinstitute.dsde.workbench.sam.matchers

import org.broadinstitute.dsde.workbench.sam.model.api.SamUser
import org.scalatest.matchers.{MatchResult, Matcher}

import scala.collection.mutable.ListBuffer

/** Asserts that the passed UserStatus.userInfo matches the passed in SamUser Id and Email
*
* @param expectedUser:
* user to expect
*/
class BeForSamUserMatcher(expectedUser: SamUser) extends Matcher[SamUser] {
def apply(samUser: SamUser): MatchResult = {
val doEmailsMatch = samUser.email == expectedUser.email
val doIdsMatch = samUser.id == expectedUser.id

val failureMessageList: ListBuffer[String] = ListBuffer.empty
val failureMessageNegatedList: ListBuffer[String] = ListBuffer.empty

if (!doEmailsMatch) {
failureMessageList += s"""SamUser email ${samUser.email} did not equal ${expectedUser.email}"""
failureMessageNegatedList += s"""SamUser email ${samUser.email} equals ${expectedUser.email}"""
}

if (!doIdsMatch) {
failureMessageList += s"""SamUser id ${samUser.id} did not equal ${expectedUser.id}"""
failureMessageNegatedList += s"""SamUser id ${samUser.id} equals ${expectedUser.id}"""
}

MatchResult(
doEmailsMatch && doIdsMatch,
failureMessageList.mkString(" and "),
failureMessageNegatedList.mkString(" and ")
)
}
}

object BeForSamUserMatcher {
def beForSamUser(expectedUser: SamUser) = new BeForUserMatcher(expectedUser)
}

0 comments on commit 6cd8837

Please sign in to comment.