-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #638 from hngprojects/feat/delete-user-by-email
feat: delete user by email
- Loading branch information
Showing
6 changed files
with
113 additions
and
5 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
14 changes: 14 additions & 0 deletions
14
src/main/java/hng_java_boilerplate/user/dto/request/DeleteUserRequest.java
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,14 @@ | ||
package hng_java_boilerplate.user.dto.request; | ||
|
||
import jakarta.validation.constraints.NotBlank; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
@Getter | ||
@Setter | ||
public class DeleteUserRequest { | ||
|
||
@NotBlank(message = "Email is required and cannot be blank.") | ||
private String email; | ||
|
||
} |
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
63 changes: 63 additions & 0 deletions
63
src/test/java/hng_java_boilerplate/user/crud_operations_test/DeleteUserTest.java
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,63 @@ | ||
package hng_java_boilerplate.user.crud_operations_test; | ||
|
||
import hng_java_boilerplate.exception.BadRequestException; | ||
import hng_java_boilerplate.exception.NotFoundException; | ||
import hng_java_boilerplate.user.dto.request.DeleteUserRequest; | ||
import hng_java_boilerplate.user.dto.response.Response; | ||
import hng_java_boilerplate.user.repository.UserRepository; | ||
import hng_java_boilerplate.user.serviceImpl.UserServiceImpl; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
import org.springframework.security.core.Authentication; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
import static org.mockito.ArgumentMatchers.anyString; | ||
import static org.mockito.Mockito.*; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
public class DeleteUserTest { | ||
|
||
@InjectMocks | ||
private UserServiceImpl userService; | ||
|
||
@Mock | ||
private UserRepository userRepository; | ||
|
||
@Mock | ||
private Authentication authentication; | ||
|
||
@Test | ||
void deleteUserSuccessfullyTest() { | ||
String email = "[email protected]"; | ||
DeleteUserRequest request = new DeleteUserRequest(); | ||
request.setEmail(email); | ||
|
||
when(userRepository.existsByEmail(email)).thenReturn(true); | ||
|
||
Response<?> response = userService.deleteUserByEmail(request, authentication); | ||
|
||
assertEquals("success", response.getStatus_code()); | ||
assertEquals("The account has been successfully deleted.", response.getMessage()); | ||
verify(userRepository, times(1)).deleteByEmail(email); | ||
} | ||
|
||
@Test | ||
void deleteUserNotFoundTest() { | ||
String email = "[email protected]"; | ||
DeleteUserRequest request = new DeleteUserRequest(); | ||
request.setEmail(email); | ||
|
||
when(userRepository.existsByEmail(email)).thenReturn(false); | ||
NotFoundException exception = assertThrows(NotFoundException.class, () -> { | ||
userService.deleteUserByEmail(request, authentication); | ||
}); | ||
assertEquals("User not found with email: [email protected]", exception.getMessage()); | ||
verify(userRepository, never()).deleteByEmail(anyString()); | ||
} | ||
|
||
|
||
} |