Skip to content

Commit

Permalink
fix: unit test of userService
Browse files Browse the repository at this point in the history
  • Loading branch information
ShiinaKin committed Sep 27, 2024
1 parent 06ceffa commit 593ad8b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,13 @@ class UserServiceImpl(
)

val isModifyGroup = patchRequest.groupId != null
val isModifyDefaultAlbum = patchRequest.defaultAlbumId != null

runCatching {
if (isModifyDefaultAlbum) {
val album = albumDao.findAlbumById(userUpdateDTO.defaultAlbumId!!) ?: throw AlbumNotFoundException()
if (album.userId != id) throw WrongParameterException("Album not belong to user")
}
val influenceRowCnt = userDao.updateUserById(userUpdateDTO)
if (influenceRowCnt < 1) throw UserNotFoundException()
if (isModifyGroup) imageDao.updateImageGroupIdByUserId(id, userUpdateDTO.groupId)
Expand Down
21 changes: 21 additions & 0 deletions app/src/test/kotlin/io/sakurasou/service/user/UserServiceTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import io.sakurasou.model.dto.ImageCountAndTotalSizeDTO
import io.sakurasou.model.dto.UserInsertDTO
import io.sakurasou.model.dto.UserManageUpdateDTO
import io.sakurasou.model.dto.UserSelfUpdateDTO
import io.sakurasou.model.entity.Album
import io.sakurasou.model.entity.Group
import io.sakurasou.model.entity.User
import io.sakurasou.model.setting.SystemSetting
Expand Down Expand Up @@ -200,6 +201,7 @@ class UserServiceTest {
@Test
fun `deleteUser should delete user successfully when user exists`() = runBlocking {
val userId = 1L
every { imageDao.deleteImageByUserId(userId) } returns 1
every { userDao.deleteUserById(userId) } returns 1

userService.deleteUser(userId)
Expand All @@ -210,6 +212,7 @@ class UserServiceTest {
@Test
fun `deleteUser should throw UserDeleteFailedException when user not found`() = runBlocking {
val userId = 1L
every { imageDao.deleteImageByUserId(userId) } returns 1
every { userDao.deleteUserById(userId) } returns 0

assertFailsWith<UserDeleteFailedException> {
Expand All @@ -234,6 +237,14 @@ class UserServiceTest {
updateTime = now,
createTime = now
)
val newAlbum = Album(
id = 2,
name = "newAlbum",
userId = userId,
createTime = now,
imageCount = 1,
isUncategorized = false,
)
val patchRequest = UserSelfPatchRequest(
password = "newPassword",
email = "[email protected]",
Expand All @@ -251,6 +262,7 @@ class UserServiceTest {
updateTime = now
)

every { albumDao.findAlbumById(exceptedUpdateDTO.defaultAlbumId!!) } returns newAlbum
every { userDao.findUserById(userId) } returns oldUserInfo
every { BCrypt.withDefaults().hashToString(12, patchRequest.password!!.toCharArray()) } returns encodedPassword
every { userDao.updateSelfById(exceptedUpdateDTO) } returns 1
Expand Down Expand Up @@ -342,6 +354,14 @@ class UserServiceTest {
updateTime = now,
createTime = now
)
val newAlbum = Album(
id = 2,
name = "newAlbum",
userId = userId,
createTime = now,
imageCount = 1,
isUncategorized = false,
)
val patchRequest = UserManagePatchRequest(
groupId = 2,
password = "newPassword",
Expand All @@ -361,6 +381,7 @@ class UserServiceTest {
updateTime = now
)

every { albumDao.findAlbumById(exceptedUpdateDTO.defaultAlbumId!!) } returns newAlbum
every { userDao.findUserById(userId) } returns oldUserInfo
every { BCrypt.withDefaults().hashToString(12, patchRequest.password!!.toCharArray()) } returns encodedPassword
every { userDao.updateUserById(exceptedUpdateDTO) } returns 1
Expand Down

0 comments on commit 593ad8b

Please sign in to comment.