-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
127 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
src/test/scala/org/broadinstitute/dsde/workbench/sam/api/UserRoutesV2Spec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
src/test/scala/org/broadinstitute/dsde/workbench/sam/matchers/BeForSamUserMatcher.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |