Skip to content
This repository has been archived by the owner on Jul 12, 2022. It is now read-only.

Commit

Permalink
fix deletion
Browse files Browse the repository at this point in the history
Signed-off-by: thalles <thalles.freitas@zup.com.br>
  • Loading branch information
thallesfreitaszup committed May 17, 2021
1 parent 938ae6f commit 2d7f461
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,11 @@ class UserServiceLegacy(
}

private fun deleteUser(user: User): User =
user.also { userRepository.delete(it) }
user.also {
userRepository.deleteFromUserGroupById(user.id)
}.also {
userRepository.delete(it)
}

private fun toRepresentation(user: User): UserRepresentation = user.toRepresentation()
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,23 @@ class UserServiceLegacyUnitTest extends Specification {
thrown(BusinessExceptionLegacy)
}

void "should delete a user successfully"() {
given:
def userId = "1"
def user = new User(userId, "User", "user@zup.com.br", "http://teste.com", true, null, LocalDateTime.now())
when:
this.userServiceLegacy.delete(userId, authorization)

then:
1 * this.userRepository.findById(userId) >> Optional.of(user)
1 * this.keycloakServiceLegacy.getEmailByAuthorizationToken(authorization) >> user.email
1 * this.userRepository.findByEmail(user.email) >> Optional.of(user)
1 * this.userRepository.delete(user)
1 * this.userRepository.deleteFromUserGroupById(user.id)
1 * this.keycloakServiceLegacy.deleteUserByEmail(user.email)
notThrown()
}

static String getAuthorization() {
return "Bearer eydGF0ZSI6ImE4OTZmOGFhLTIwZDUtNDI5Ny04YzM2LTdhZWJmZ_qq3"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,17 @@ package io.charlescd.moove.legacy.repository
import io.charlescd.moove.legacy.repository.entity.User
import java.util.*
import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.data.jpa.repository.Modifying
import org.springframework.data.jpa.repository.Query
import org.springframework.data.repository.query.Param

interface UserRepository : JpaRepository<User, String> {

fun findByEmail(email: String): Optional<User>

fun findBySystemTokenId(systemTokenId: String): Optional<User>

@Modifying
@Query("delete from user_groups_users ug where ug.user_id=:id", nativeQuery = true)
fun deleteFromUserGroupById(@Param("id") id: String): Unit
}

0 comments on commit 2d7f461

Please sign in to comment.