-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCustomUserDetailsServiceTest.java
More file actions
151 lines (126 loc) · 5.47 KB
/
CustomUserDetailsServiceTest.java
File metadata and controls
151 lines (126 loc) · 5.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package com.podzilla.auth.service;
import com.podzilla.auth.dto.CustomUserDetails;
import com.podzilla.auth.exception.NotFoundException;
import com.podzilla.auth.exception.ValidationException; // Added import
import com.podzilla.auth.model.ERole;
import com.podzilla.auth.model.Role;
import com.podzilla.auth.model.User;
import com.podzilla.auth.repository.UserRepository;
import org.junit.jupiter.api.BeforeEach;
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.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import java.util.Collections;
import java.util.HashSet;
import java.util.Optional;
import java.util.Set;
import java.util.UUID;
import java.util.stream.Collectors;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;
@ExtendWith(MockitoExtension.class)
class CustomUserDetailsServiceTest {
@Mock
private UserRepository userRepository;
@InjectMocks
private CustomUserDetailsService customUserDetailsService;
private User user;
private String userEmail;
private String userPassword;
@BeforeEach
void setUp() {
userEmail = "test@example.com";
userPassword = "encodedPassword";
Role userRole = new Role(ERole.ROLE_USER);
Role adminRole = new Role(ERole.ROLE_ADMIN);
Set<Role> roles = new HashSet<>();
roles.add(userRole);
roles.add(adminRole);
user = new User.Builder()
.id(UUID.randomUUID())
.name("Test User")
.email(userEmail)
.password(userPassword)
.roles(roles)
.enabled(true)
.build();
}
@Test
void loadUserByUsername_shouldReturnUserDetails_whenUserExistsAndHasRoles() {
// Arrange
when(userRepository.findByEmail(userEmail)).thenReturn(Optional.of(user));
// Act
UserDetails userDetails = customUserDetailsService.loadUserByUsername(userEmail);
// Assert
assertNotNull(userDetails);
assertEquals(userEmail, userDetails.getUsername());
assertEquals(userPassword, userDetails.getPassword());
assertNotNull(userDetails.getAuthorities());
assertEquals(2, userDetails.getAuthorities().size()); // ROLE_USER and ROLE_ADMIN
// Check specific authorities
Set<String> expectedAuthorities = Set.of(ERole.ROLE_USER.name(), ERole.ROLE_ADMIN.name());
Set<String> actualAuthorities = userDetails.getAuthorities().stream()
.map(GrantedAuthority::getAuthority)
.collect(Collectors.toSet());
assertEquals(expectedAuthorities, actualAuthorities);
assertInstanceOf(CustomUserDetails.class, userDetails, "Should return an instance of CustomUserDetails");
verify(userRepository).findByEmail(userEmail);
}
@Test
void loadUserByUsername_shouldThrowNotFoundException_whenUserDoesNotExist() {
// Arrange
String nonExistentEmail = "notfound@example.com";
when(userRepository.findByEmail(nonExistentEmail)).thenReturn(Optional.empty());
// Act & Assert
NotFoundException exception = assertThrows(NotFoundException.class, () -> {
customUserDetailsService.loadUserByUsername(nonExistentEmail);
});
assertEquals("Not Found: " + nonExistentEmail + " not found.",
exception.getMessage());
verify(userRepository).findByEmail(nonExistentEmail);
}
@Test
void loadUserByUsername_shouldThrowValidationException_whenUserHasEmptyRoles() {
// Arrange
String emailWithNoRoles = "norole@example.com";
User userWithNoRoles = new User.Builder()
.id(UUID.randomUUID())
.name("No Role User")
.email(emailWithNoRoles)
.password("password123")
.roles(Collections.emptySet()) // Empty roles set
.build();
when(userRepository.findByEmail(emailWithNoRoles)).thenReturn(Optional.of(userWithNoRoles));
// Act & Assert
ValidationException exception = assertThrows(ValidationException.class, () -> {
customUserDetailsService.loadUserByUsername(emailWithNoRoles);
});
assertEquals("Validation error: User has no roles assigned.",
exception.getMessage());
verify(userRepository).findByEmail(emailWithNoRoles);
}
@Test
void loadUserByUsername_shouldThrowValidationException_whenUserHasNullRoles() {
// Arrange
String emailWithNullRoles = "nullrole@example.com";
User userWithNullRoles = new User.Builder()
.id(UUID.randomUUID())
.name("Null Role User")
.email(emailWithNullRoles)
.password("password456")
.roles(null) // Null roles set
.build();
when(userRepository.findByEmail(emailWithNullRoles)).thenReturn(Optional.of(userWithNullRoles));
// Act & Assert
ValidationException exception = assertThrows(ValidationException.class, () -> {
customUserDetailsService.loadUserByUsername(emailWithNullRoles);
});
assertEquals("Validation error: User has no roles assigned.",
exception.getMessage());
verify(userRepository).findByEmail(emailWithNullRoles);
}
}