Skip to content

Commit

Permalink
a wee bit of a refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
tlangs committed Oct 13, 2023
1 parent 8da07d8 commit 5682628
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import akka.http.scaladsl.server.Directives._
import akka.http.scaladsl.server.{Directive0, ExceptionHandler}
import org.broadinstitute.dsde.workbench.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.UserService
import org.broadinstitute.dsde.workbench.sam.util.SamRequestContext
import spray.json.JsBoolean
Expand All @@ -17,7 +16,7 @@ import scala.concurrent.ExecutionContext

/** Created by mbemis on 5/22/17.
*/
trait UserRoutes extends SamUserDirectives with SamRequestContextDirectives {
trait OldUserRoutes extends SamUserDirectives with SamRequestContextDirectives {
implicit val executionContext: ExecutionContext
val userService: UserService

Expand Down Expand Up @@ -151,33 +150,4 @@ trait UserRoutes extends SamUserDirectives with SamRequestContextDirectives {
}
}
}

def apiUserRoutes(samUser: SamUser, samRequestContext: SamRequestContext): server.Route = pathPrefix("users") {
pathPrefix("v1") {
get {
path(Segment) { email =>
pathEnd {
complete {
userService.getUserIdInfoFromEmail(WorkbenchEmail(email), samRequestContext).map {
case Left(_) => StatusCodes.NotFound -> None
case Right(None) => StatusCodes.NoContent -> None
case Right(Some(userIdInfo)) => StatusCodes.OK -> Some(userIdInfo)
}
}
}
}
} ~
pathPrefix("invite") {
post {
path(Segment) { inviteeEmail =>
complete {
userService
.inviteUser(WorkbenchEmail(inviteeEmail.trim), samRequestContext)
.map(userStatus => StatusCodes.Created -> userStatus)
}
}
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,16 @@ abstract class SamRoutes(
val openTelemetry: OpenTelemetryMetrics[IO]
) extends LazyLogging
with ResourceRoutes
with UserRoutes
with OldUserRoutes
with StatusRoutes
with TermsOfServiceRoutes
with ExtensionRoutes
with ManagedGroupRoutes
with AdminRoutes
with AzureRoutes
with ServiceAdminRoutes
with UserRoutesV3 {
with UserRoutesV1
with UserRoutesV2 {

def route: server.Route = (logRequestResult & handleExceptions(myExceptionHandler)) {
oidcConfig.swaggerRoutes("swagger/api-docs.yaml") ~
Expand All @@ -76,9 +77,9 @@ abstract class SamRoutes(
adminRoutes(samUser, samRequestContextWithUser) ~
extensionRoutes(samUser, samRequestContextWithUser) ~
groupRoutes(samUser, samRequestContextWithUser) ~
apiUserRoutes(samUser, samRequestContextWithUser) ~
azureRoutes(samUser, samRequestContextWithUser) ~
userRoutesV3(samUser, samRequestContextWithUser)
userRoutesV1(samUser, samRequestContextWithUser) ~
userRoutesV2(samUser, samRequestContextWithUser)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package org.broadinstitute.dsde.workbench.sam.api

import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport._
import akka.http.scaladsl.model.StatusCodes
import akka.http.scaladsl.server
import akka.http.scaladsl.server.Directives._
import org.broadinstitute.dsde.workbench.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.UserService
import org.broadinstitute.dsde.workbench.sam.util.SamRequestContext

trait UserRoutesV1 extends SamUserDirectives with SamRequestContextDirectives {
val userService: UserService

def userRoutesV1(samUser: SamUser, samRequestContext: SamRequestContext): server.Route = pathPrefix("users") {
pathPrefix("v1") {
get {
path(Segment) { email =>
pathEnd {
complete {
userService.getUserIdInfoFromEmail(WorkbenchEmail(email), samRequestContext).map {
case Left(_) => StatusCodes.NotFound -> None
case Right(None) => StatusCodes.NoContent -> None
case Right(Some(userIdInfo)) => StatusCodes.OK -> Some(userIdInfo)
}
}
}
}
} ~
pathPrefix("invite") {
post {
path(Segment) { inviteeEmail =>
complete {
userService
.inviteUser(WorkbenchEmail(inviteeEmail.trim), samRequestContext)
.map(userStatus => StatusCodes.Created -> userStatus)
}
}
}
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import org.broadinstitute.dsde.workbench.sam.util.SamRequestContext

/** Created by tlangs on 10/12/2023.
*/
trait UserRoutesV3 extends SamUserDirectives with SamRequestContextDirectives {
trait UserRoutesV2 extends SamUserDirectives with SamRequestContextDirectives {
val userService: UserService

/** Changes a 403 error to a 404 error. Used when `UserInfoDirectives` throws a 403 in the case where a user is not found. In most routes that is appropriate
Expand All @@ -29,7 +29,7 @@ trait UserRoutesV3 extends SamUserDirectives with SamRequestContextDirectives {
})
}

def userRoutesV3(samUser: SamUser, samRequestContext: SamRequestContext): server.Route =
def userRoutesV2(samUser: SamUser, samRequestContext: SamRequestContext): server.Route =
pathPrefix("users") {
pathPrefix("v2") {
pathPrefix("self") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ abstract class MockSamRoutes(
val openTelemetry: OpenTelemetryMetrics[IO]
) extends LazyLogging
with ResourceRoutes
with UserRoutes
with OldUserRoutes
with UserRoutesV1
with MockStatusRoutes
with TermsOfServiceRoutes
with ExtensionRoutes
Expand All @@ -71,7 +72,7 @@ abstract class MockSamRoutes(
adminRoutes(samUser, samRequestContextWithUser) ~
extensionRoutes(samUser, samRequestContextWithUser) ~
groupRoutes(samUser, samRequestContextWithUser) ~
apiUserRoutes(samUser, samRequestContextWithUser) ~
userRoutesV1(samUser, samRequestContextWithUser) ~
azureRoutes(samUser, samRequestContextWithUser)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import org.mockito.scalatest.MockitoSugar

/** Created by dvoet on 6/7/17.
*/
class UserRoutesSpec extends UserRoutesSpecHelper {
class OldUserRoutesSpec extends OldUserRoutesSpecHelper {
"POST /register/user" should "create user" in withDefaultRoutes { samRoutes =>
Post("/register/user") ~> samRoutes.route ~> check {
status shouldEqual StatusCodes.Created
Expand All @@ -42,7 +42,7 @@ class UserRoutesSpec extends UserRoutesSpecHelper {
}
}

trait UserRoutesSpecHelper extends AnyFlatSpec with Matchers with ScalatestRouteTest with MockitoSugar with TestSupport {
trait OldUserRoutesSpecHelper extends AnyFlatSpec with Matchers with ScalatestRouteTest with MockitoSugar with TestSupport {
val defaultUser = Generator.genWorkbenchUserGoogle.sample.get
val defaultUserId = defaultUser.id
val defaultUserEmail = defaultUser.email
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import org.broadinstitute.dsde.workbench.sam.service.{NoExtensions, StatusServic

/** Created by dvoet on 6/7/17.
*/
class UserRoutesV1Spec extends UserRoutesSpecHelper {
class OldUserRoutesV1Spec extends OldUserRoutesSpecHelper {

def withSARoutes[T](testCode: (TestSamRoutes, TestSamRoutes) => T): T = {
val directoryDAO = new MockDirectoryDAO()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import org.broadinstitute.dsde.workbench.sam.service.{NoExtensions, StatusServic

/** Created by mtalbott on 8/8/18.
*/
class UserRoutesV2Spec extends UserRoutesSpecHelper {
class OldUserRoutesV2Spec extends OldUserRoutesSpecHelper {
def withSARoutes[T](testCode: (TestSamRoutes, TestSamRoutes) => T): T = {
val directoryDAO = new MockDirectoryDAO()

Expand Down

0 comments on commit 5682628

Please sign in to comment.