mirror of
https://github.com/VinylDNS/vinyldns
synced 2025-08-31 06:15:49 +00:00
Merge branch 'master' into update_get_user_docs
This commit is contained in:
@@ -104,14 +104,16 @@ object UserInfo {
|
||||
|
||||
case class UserResponseInfo(
|
||||
id: String,
|
||||
userName: Option[String] = None
|
||||
userName: Option[String] = None,
|
||||
groupId: Set[String] = Set.empty
|
||||
)
|
||||
|
||||
object UserResponseInfo {
|
||||
def apply(user: User): UserResponseInfo =
|
||||
def apply(user: User , group: Group): UserResponseInfo =
|
||||
UserResponseInfo(
|
||||
id = user.id,
|
||||
userName = Some(user.userName)
|
||||
userName = Some(user.userName),
|
||||
groupId = Set(group.id)
|
||||
)
|
||||
}
|
||||
|
||||
|
@@ -362,6 +362,14 @@ class MembershipService(
|
||||
.orFail(UserNotFoundError(s"User $userIdentifier was not found"))
|
||||
.toResult[User]
|
||||
|
||||
|
||||
def getUserDetails(userIdentifier: String, authPrincipal: AuthPrincipal): Result[UserResponseInfo] =
|
||||
for{
|
||||
user <- getUser(userIdentifier,authPrincipal)
|
||||
group <- membershipRepo.getGroupsForUser(user.id).toResult[Set[String]]
|
||||
} yield UserResponseInfo(user.id, Some(user.userName), group)
|
||||
|
||||
|
||||
def getUsers(
|
||||
userIds: Set[String],
|
||||
startFrom: Option[String] = None,
|
||||
|
@@ -78,4 +78,9 @@ trait MembershipServiceAlgebra {
|
||||
userIdentifier: String,
|
||||
authPrincipal: AuthPrincipal
|
||||
): Result[User]
|
||||
|
||||
def getUserDetails(
|
||||
userIdentifier: String,
|
||||
authPrincipal: AuthPrincipal
|
||||
): Result[UserResponseInfo]
|
||||
}
|
||||
|
@@ -212,9 +212,9 @@ class MembershipRoute(
|
||||
} ~
|
||||
path("users" / Segment) { id =>
|
||||
(get & monitor("Endpoint.getUser")) {
|
||||
authenticateAndExecute(membershipService.getUser(id, _)) {
|
||||
authenticateAndExecute(membershipService.getUserDetails(id, _)) {
|
||||
user =>
|
||||
complete(StatusCodes.OK, UserResponseInfo(user))
|
||||
complete(StatusCodes.OK, user)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -1402,5 +1402,21 @@ class MembershipServiceSpec
|
||||
error shouldBe a[UserNotFoundError]
|
||||
}
|
||||
}
|
||||
"get user info" should {
|
||||
"return the user info" in {
|
||||
doReturn(IO.pure(Some(okUser))).when(mockUserRepo).getUserByIdOrName(anyString)
|
||||
doReturn(IO.pure(Set(okGroup.id))).when(mockMembershipRepo).getGroupsForUser(anyString)
|
||||
val result: UserResponseInfo = underTest.getUserDetails(okUser.id, okAuth).value.unsafeRunSync().toOption.get
|
||||
result.id shouldBe okUser.id
|
||||
result.userName.get shouldBe okUser.userName
|
||||
result.groupId shouldBe Set(okGroup.id)
|
||||
}
|
||||
|
||||
"return an error if the user is not found" in {
|
||||
doReturn(IO.pure(None)).when(mockUserRepo).getUserByIdOrName(anyString)
|
||||
val error = underTest.getUserDetails("notfound", okAuth).value.unsafeRunSync().swap.toOption.get
|
||||
error shouldBe a[UserNotFoundError]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -19,6 +19,7 @@ package vinyldns.api.route
|
||||
import akka.http.scaladsl.model.{ContentTypes, HttpEntity, StatusCodes}
|
||||
import akka.http.scaladsl.server.Route
|
||||
import akka.http.scaladsl.testkit.ScalatestRouteTest
|
||||
|
||||
import java.time.Instant
|
||||
import org.json4s.JsonDSL._
|
||||
import org.json4s._
|
||||
@@ -32,7 +33,7 @@ import org.scalatest.wordspec.AnyWordSpec
|
||||
import org.scalatest.BeforeAndAfterEach
|
||||
import vinyldns.api.Interfaces._
|
||||
import vinyldns.api.config.LimitsConfig
|
||||
import vinyldns.api.domain.membership._
|
||||
import vinyldns.api.domain.membership.{UserResponseInfo, _}
|
||||
import vinyldns.api.domain.zone.NotAuthorizedError
|
||||
import vinyldns.api.route.MembershipJsonProtocol.{CreateGroupInput, UpdateGroupInput}
|
||||
import vinyldns.core.TestMembershipData._
|
||||
@@ -78,6 +79,8 @@ class MembershipRoutingSpec
|
||||
val okGroupChangeInfo: GroupChangeInfo = GroupChangeInfo(okGroupChange)
|
||||
val okGroupChangeUpdateInfo: GroupChangeInfo = GroupChangeInfo(okGroupChangeUpdate)
|
||||
val okGroupChangeDeleteInfo: GroupChangeInfo = GroupChangeInfo(okGroupChangeDelete)
|
||||
val okUserResponseInfo: UserResponseInfo = UserResponseInfo(okUser, okGroup)
|
||||
val dummyUserResponseInfo: UserResponseInfo = UserResponseInfo(listOfDummyUsers.head,dummyGroup)
|
||||
|
||||
"POST groups" should {
|
||||
"return a 200 response when successful" in {
|
||||
@@ -817,9 +820,9 @@ class MembershipRoutingSpec
|
||||
|
||||
"GET user" should {
|
||||
"return a 200 response with the user info" in {
|
||||
doReturn(result(okUser))
|
||||
doReturn(result(okUserResponseInfo))
|
||||
.when(membershipService)
|
||||
.getUser("ok", okAuth)
|
||||
.getUserDetails("ok", okAuth)
|
||||
Get("/users/ok") ~> membershipRoute ~> check {
|
||||
status shouldBe StatusCodes.OK
|
||||
val result = responseAs[UserResponseInfo]
|
||||
@@ -829,9 +832,9 @@ class MembershipRoutingSpec
|
||||
|
||||
"return a 200 response with the user info when the user ID is valid" in {
|
||||
val testUser = listOfDummyUsers.head
|
||||
doReturn(result(testUser))
|
||||
doReturn(result(dummyUserResponseInfo))
|
||||
.when(membershipService)
|
||||
.getUser("dummy000", okAuth)
|
||||
.getUserDetails("dummy000", okAuth)
|
||||
Get("/users/dummy000") ~> membershipRoute ~> check {
|
||||
status shouldBe StatusCodes.OK
|
||||
val result = responseAs[UserResponseInfo]
|
||||
@@ -841,9 +844,9 @@ class MembershipRoutingSpec
|
||||
|
||||
"return a 200 response with the user info when the username is valid" in {
|
||||
val testUser = listOfDummyUsers.head
|
||||
doReturn(result(testUser))
|
||||
doReturn(result(dummyUserResponseInfo))
|
||||
.when(membershipService)
|
||||
.getUser("name-dummy000", okAuth)
|
||||
.getUserDetails("name-dummy000", okAuth)
|
||||
Get("/users/name-dummy000") ~> membershipRoute ~> check {
|
||||
status shouldBe StatusCodes.OK
|
||||
val result = responseAs[UserResponseInfo]
|
||||
@@ -851,10 +854,21 @@ class MembershipRoutingSpec
|
||||
}
|
||||
}
|
||||
|
||||
"return a 200 response with the user info when the group Id is valid" in {
|
||||
doReturn(result(dummyUserResponseInfo))
|
||||
.when(membershipService)
|
||||
.getUserDetails("name-dummy000", okAuth)
|
||||
Get("/users/name-dummy000") ~> membershipRoute ~> check {
|
||||
status shouldBe StatusCodes.OK
|
||||
val result = responseAs[UserResponseInfo]
|
||||
result.groupId shouldBe dummyUserResponseInfo.groupId
|
||||
}
|
||||
}
|
||||
|
||||
"return a 404 Not Found response when the userIdentifier is not a valid user ID or username" in {
|
||||
doReturn(result(UserNotFoundError("fail")))
|
||||
.when(membershipService)
|
||||
.getUser("fail", okAuth)
|
||||
.getUserDetails("fail", okAuth)
|
||||
Get("/users/fail") ~> membershipRoute ~> check {
|
||||
status shouldBe StatusCodes.NotFound
|
||||
}
|
||||
|
Reference in New Issue
Block a user