Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ID-1300 Admin Get Users by ID #1498

Merged
merged 4 commits into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import org.broadinstitute.dsde.workbench.model._
import org.broadinstitute.dsde.workbench.sam.model.api.SamJsonSupport._
import org.broadinstitute.dsde.workbench.sam.service.ResourceService
import org.broadinstitute.dsde.workbench.sam.util.SamRequestContext
import org.broadinstitute.dsde.workbench.model.WorkbenchIdentityJsonSupport._
import spray.json.DefaultJsonProtocol._

trait ServiceAdminRoutes extends SecurityDirectives with SamRequestContextDirectives with SamUserDirectives with SamModelDirectives {
Expand Down Expand Up @@ -53,6 +54,13 @@ trait ServiceAdminRoutes extends SecurityDirectives with SamRequestContextDirect
.map(users => (if (users.nonEmpty) OK else NotFound) -> users)
}
}
} ~
postWithTelemetry(samRequestContext) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldnt this be get?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm thinking POST, since the number of IDs could get pretty unwieldy in a url.

entity(as[Seq[WorkbenchUserId]]) { samUserIds =>
complete {
userService.getUsersByIds(samUserIds, samRequestContext)
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we have a limit on how many you can get at one time?

}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ trait DirectoryDAO {

def loadUser(userId: WorkbenchUserId, samRequestContext: SamRequestContext): IO[Option[SamUser]]

def batchLoadUsers(
samUserIds: Set[WorkbenchUserId],
samRequestContext: SamRequestContext
): IO[Seq[SamUser]]

def loadUsersByQuery(
userId: Option[WorkbenchUserId],
googleSubjectId: Option[GoogleSubjectId],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,25 @@ class PostgresDirectoryDAO(protected val writeDbRef: DbReference, protected val
.map(UserTable.unmarshalUserRecord)
}

override def batchLoadUsers(
samUserIds: Set[WorkbenchUserId],
samRequestContext: SamRequestContext
): IO[Seq[SamUser]] =
if (samUserIds.isEmpty) {
IO.pure(Seq.empty)
} else {
readOnlyTransaction("batchLoadUsers", samRequestContext) { implicit session =>
val userTable = UserTable.syntax
val loadUserQuery = samsql"select ${userTable.resultAll} from ${UserTable as userTable} where ${userTable.id} in (${samUserIds})"

loadUserQuery
.map(UserTable(userTable))
.list()
.apply()
.map(UserTable.unmarshalUserRecord)
}
}

override def loadUsersByQuery(
userId: Option[WorkbenchUserId],
googleSubjectId: Option[GoogleSubjectId],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,9 @@ class UserService(
def getUser(userId: WorkbenchUserId, samRequestContext: SamRequestContext): IO[Option[SamUser]] =
directoryDAO.loadUser(userId, samRequestContext)

def getUsersByIds(samUserIds: Seq[WorkbenchUserId], samRequestContext: SamRequestContext): IO[Seq[SamUser]] =
directoryDAO.batchLoadUsers(samUserIds.toSet, samRequestContext)

def getUsersByQuery(
userId: Option[WorkbenchUserId],
googleSubjectId: Option[GoogleSubjectId],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,11 @@ class MockDirectoryDAO(val groups: mutable.Map[WorkbenchGroupIdentity, Workbench
users.get(userId)
}

override def batchLoadUsers(
samUserIds: Set[WorkbenchUserId],
samRequestContext: SamRequestContext
): IO[Seq[SamUser]] = IO(samUserIds.flatMap(users.get).toSeq)

override def loadUsersByQuery(
userId: Option[WorkbenchUserId],
googleSubjectId: Option[GoogleSubjectId],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -625,6 +625,16 @@ class PostgresDirectoryDAOSpec extends RetryableAnyFreeSpec with Matchers with B
}
}

"batchLoadUsers" - {
"loads a list of users" in {
assume(databaseEnabled, databaseEnabledClue)
val users = Seq.range(0, 10).map(_ => Generator.genWorkbenchUserBoth.sample.get)
users.foreach(user => dao.createUser(user, samRequestContext).unsafeRunSync())
val loadedUsers = dao.batchLoadUsers(users.map(_.id).toSet, samRequestContext).unsafeRunSync()
loadedUsers should contain theSameElementsAs users
}
}

"deleteUser" - {
"delete users" in {
assume(databaseEnabled, databaseEnabledClue)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,9 @@ case class StatefulMockDirectoryDaoBuilder() extends MockitoSugar {
.toSet
)
)
mockedDirectoryDAO.batchLoadUsers(any[Set[WorkbenchUserId]], any[SamRequestContext]) answers ((samUserIds: Set[WorkbenchUserId], _: SamRequestContext) =>
IO(samUsers.filter(user => samUserIds.contains(user.id)).toSeq)
)
this
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package org.broadinstitute.dsde.workbench.sam.service.UserServiceSpecs

import org.broadinstitute.dsde.workbench.model.WorkbenchEmail
import org.broadinstitute.dsde.workbench.sam.Generator.genWorkbenchUserBoth
import org.broadinstitute.dsde.workbench.sam.model.BasicWorkbenchGroup
import org.broadinstitute.dsde.workbench.sam.service.{CloudExtensions, TestUserServiceBuilder}

import scala.concurrent.ExecutionContextExecutor

class GetUsersByIdsSpec extends UserServiceTestTraits {
implicit val ec: ExecutionContextExecutor = scala.concurrent.ExecutionContext.global

val allUsersGroup: BasicWorkbenchGroup = BasicWorkbenchGroup(CloudExtensions.allUsersGroupName, Set(), WorkbenchEmail("[email protected]"))

describe("When getting") {
describe("users by IDs") {
it("should be successful") {
// Arrange
val users = Seq.range(0, 10).map(_ => genWorkbenchUserBoth.sample.get)
val userService = TestUserServiceBuilder()
.withAllUsersGroup(allUsersGroup)
.withExistingUsers(users)
.withEnabledUsers(users)
.build
// Act
val response = runAndWait(userService.getUsersByIds(users.map(_.id), samRequestContext))

// Assert
assert(response.nonEmpty, "Getting users by ID should return a list of users")
response should contain theSameElementsAs users
}
}

}
describe("a user that does not exist") {
it("should be unsuccessful") {
// Arrange
val userWithBothIds = genWorkbenchUserBoth.sample.get
val userService = TestUserServiceBuilder()
.withAllUsersGroup(allUsersGroup)
.build
// Act
val response = runAndWait(userService.getUser(userWithBothIds.id, samRequestContext))

// Assert
assert(response.isEmpty, "Getting a nonexistent user should not find a user")
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we have any test cases that assert the admin only functionality or is that properly covered by other tests?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this is covered by other tests, but I'll check.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Found the test, but it was just named slightly wrong. It tests that only authorized service accounts can call the API


}
Loading