This repository has been archived by the owner on Apr 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added DeleteUserUseCaseSpec and test cases. Enhanced error message on ui
- Loading branch information
1 parent
272d73c
commit e69df4c
Showing
12 changed files
with
237 additions
and
70 deletions.
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
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
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
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
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
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
37 changes: 37 additions & 0 deletions
37
...ag-service/src/main/scala/com/softwaremill/codebrag/usecases/user/DeleteUserUseCase.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,37 @@ | ||
package com.softwaremill.codebrag.usecases.user | ||
|
||
import com.softwaremill.codebrag.dao.user.UserDAO | ||
import com.softwaremill.codebrag.usecases.assertions.UserAssertions | ||
import org.bson.types.ObjectId | ||
import com.softwaremill.codebrag.domain.{Authentication, User} | ||
import com.typesafe.scalalogging.slf4j.Logging | ||
import com.softwaremill.scalaval.Validation._ | ||
|
||
case class DeleteUserForm(userId: ObjectId) { | ||
|
||
} | ||
|
||
class DeleteUserUseCase(protected val userDao: UserDAO) extends Logging { | ||
|
||
import UserAssertions._ | ||
|
||
def execute(executorId: ObjectId, form: DeleteUserForm): Either[Errors, Unit] = { | ||
assertUserWithId(executorId, mustBeActive, mustBeAdmin)(userDao) | ||
val targetUser = loadUser(form.userId) | ||
validateUserDetails(executorId, targetUser, form).whenOk[Unit] { | ||
logger.debug(s"Validation passed, attempting to delete user $targetUser") | ||
userDao.delete(form.userId) | ||
} | ||
} | ||
|
||
private def loadUser(userId: ObjectId) = userDao.findById(userId).getOrElse(throw new IllegalStateException(s"User $userId not found")) | ||
|
||
private def validateUserDetails(executorId: ObjectId, user: User, form: DeleteUserForm) = { | ||
val changeOwnFlagsCheck = rule("userId") { | ||
val isDeleteFlags = user.admin || user.active | ||
(!isDeleteFlags || (isDeleteFlags && executorId != user.id), "Cannot delete own user") | ||
} | ||
validate(changeOwnFlagsCheck) | ||
} | ||
|
||
} |
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
88 changes: 88 additions & 0 deletions
88
...ervice/src/test/scala/com/softwaremill/codebrag/usecases/user/DeleteUserUseCaseSpec.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,88 @@ | ||
package com.softwaremill.codebrag.usecases.user | ||
|
||
import org.scalatest.{BeforeAndAfter, FlatSpec} | ||
import org.scalatest.matchers.ShouldMatchers | ||
import org.scalatest.mock.MockitoSugar | ||
import com.softwaremill.codebrag.dao.user.UserDAO | ||
import org.mockito.Matchers._ | ||
import org.mockito.Mockito._ | ||
import com.softwaremill.codebrag.domain.builder.UserAssembler | ||
import com.softwaremill.codebrag.domain.{Authentication, User} | ||
import com.softwaremill.codebrag.usecases.assertions.{ActiveUserStatusRequiredException, AdminRoleRequiredException} | ||
import org.bson.types.ObjectId | ||
|
||
class DeleteUserUseCaseSpec extends FlatSpec with ShouldMatchers with BeforeAndAfter with MockitoSugar { | ||
|
||
val userDao = mock[UserDAO] | ||
val useCase = new DeleteUserUseCase(userDao) | ||
|
||
val InactiveUser = UserAssembler.randomUser.withActive(set = false).get | ||
val ActiveUser = UserAssembler.randomUser.withActive().get | ||
|
||
val ValidExecutor = UserAssembler.randomUser.withAdmin().withActive().get | ||
val NonAdminExecutor = UserAssembler.randomUser.withActive().withAdmin(set = false).get | ||
val InactiveExecutor = UserAssembler.randomUser.withActive(set = false).withAdmin().get | ||
|
||
after { | ||
reset(userDao) | ||
} | ||
|
||
it should "not delete user when executing user is neither admin nor active" in { | ||
// given | ||
setupReturningUserFromDB(NonAdminExecutor, InactiveExecutor) | ||
val form = DeleteUserForm(ActiveUser.id) | ||
|
||
// when | ||
intercept[AdminRoleRequiredException] { | ||
useCase.execute(NonAdminExecutor.id, form) | ||
} | ||
intercept[ActiveUserStatusRequiredException] { | ||
useCase.execute(InactiveExecutor.id, form) | ||
} | ||
|
||
// then | ||
verify(userDao, never()).delete(any[ObjectId]) | ||
} | ||
|
||
it should "not allow to delete yourself" in { | ||
// given | ||
setupReturningUserFromDB(ValidExecutor) | ||
|
||
// when | ||
val ownChangeForm = DeleteUserForm(ValidExecutor.id) | ||
val Left(result) = useCase.execute(ValidExecutor.id, ownChangeForm) | ||
|
||
// then | ||
result should be(Map("userId" -> List("Cannot delete own user"))) | ||
verify(userDao, never()).delete(any[ObjectId]) | ||
} | ||
|
||
|
||
it should "delete user when validation passes" in { | ||
// given | ||
stubCurrentlyActiveUsersCountTo(0) | ||
setupReturningUserFromDB(ValidExecutor, ActiveUser) | ||
|
||
// when | ||
val newAuth = Authentication.basic(ActiveUser.authentication.username, "secret") | ||
val form = new DeleteUserForm(ActiveUser.id) | ||
val result = useCase.execute(ValidExecutor.id, form) | ||
|
||
// then | ||
result should be('right) | ||
val expectedUser = form | ||
verify(userDao).delete(expectedUser.userId) | ||
} | ||
|
||
private def stubCurrentlyActiveUsersCountTo(activeUsersCount: Int) { | ||
when(userDao.countAllActive()).thenReturn(activeUsersCount) | ||
} | ||
|
||
private def setupReturningUserFromDB(users: User*) { | ||
users.foreach { user => | ||
when(userDao.findById(user.id)).thenReturn(Some(user)) | ||
} | ||
} | ||
|
||
} | ||
|
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
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
Oops, something went wrong.