-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAuthenticationServiceTest.java
More file actions
243 lines (203 loc) · 9.69 KB
/
Copy pathAuthenticationServiceTest.java
File metadata and controls
243 lines (203 loc) · 9.69 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
package com.devoops.user.service;
import com.devoops.user.dto.request.LoginRequest;
import com.devoops.user.dto.request.RegisterRequest;
import com.devoops.user.dto.response.AuthenticationResponse;
import com.devoops.user.dto.response.UserResponse;
import com.devoops.user.entity.Role;
import com.devoops.user.entity.User;
import com.devoops.user.exception.InvalidCredentialsException;
import com.devoops.user.exception.UserAlreadyExistsException;
import com.devoops.user.mapper.UserMapper;
import com.devoops.user.repository.UserRepository;
import com.devoops.user.security.JwtService;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
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.authentication.AuthenticationManager;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.crypto.password.PasswordEncoder;
import java.util.Optional;
import java.util.UUID;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.*;
@ExtendWith(MockitoExtension.class)
class AuthenticationServiceTest {
@Mock
private UserRepository userRepository;
@Mock
private PasswordEncoder passwordEncoder;
@Mock
private JwtService jwtService;
@Mock
private AuthenticationManager authenticationManager;
@Mock
private UserMapper userMapper;
@Mock
private UserEventPublisherService userEventPublisherService;
@InjectMocks
private AuthenticationService authenticationService;
private RegisterRequest registerRequest;
private LoginRequest loginRequest;
private User user;
private UserResponse userResponse;
@BeforeEach
void setUp() {
registerRequest = new RegisterRequest(
"testuser",
"password123",
"test@example.com",
"Test",
"User",
"Belgrade",
Role.GUEST
);
loginRequest = new LoginRequest("testuser", "password123");
user = User.builder()
.id(UUID.randomUUID())
.username("testuser")
.password("encodedPassword")
.email("test@example.com")
.firstName("Test")
.lastName("User")
.residence("Belgrade")
.role(Role.GUEST)
.build();
userResponse = new UserResponse(
user.getId(),
user.getUsername(),
user.getEmail(),
user.getFirstName(),
user.getLastName(),
user.getResidence(),
user.getRole()
);
}
@Nested
@DisplayName("Register Tests")
class RegisterTests {
@Test
@DisplayName("Should register user successfully")
void register_WithValidData_ReturnsAuthenticationResponse() {
// Given
when(userRepository.existsByUsername(anyString())).thenReturn(false);
when(userRepository.existsByEmail(anyString())).thenReturn(false);
when(userMapper.toEntity(any(RegisterRequest.class))).thenReturn(user);
when(passwordEncoder.encode(anyString())).thenReturn("encodedPassword");
when(userRepository.save(any(User.class))).thenReturn(user);
when(jwtService.generateToken(any(User.class))).thenReturn("jwt-token");
when(jwtService.getExpirationTime()).thenReturn(86400000L);
when(userMapper.toUserResponse(any(User.class))).thenReturn(userResponse);
// When
AuthenticationResponse response = authenticationService.register(registerRequest);
// Then
assertThat(response).isNotNull();
assertThat(response.accessToken()).isEqualTo("jwt-token");
assertThat(response.tokenType()).isEqualTo("Bearer");
assertThat(response.user().username()).isEqualTo("testuser");
assertThat(response.user().role()).isEqualTo(Role.GUEST);
verify(userRepository).existsByUsername("testuser");
verify(userRepository).existsByEmail("test@example.com");
verify(userRepository).save(any(User.class));
verify(passwordEncoder).encode("password123");
}
@Test
@DisplayName("Should throw exception when username already exists")
void register_WithExistingUsername_ThrowsUserAlreadyExistsException() {
// Given
when(userRepository.existsByUsername("testuser")).thenReturn(true);
// When/Then
assertThatThrownBy(() -> authenticationService.register(registerRequest))
.isInstanceOf(UserAlreadyExistsException.class)
.hasMessageContaining("Username already taken");
verify(userRepository).existsByUsername("testuser");
verify(userRepository, never()).save(any(User.class));
}
@Test
@DisplayName("Should throw exception when email already exists")
void register_WithExistingEmail_ThrowsUserAlreadyExistsException() {
// Given
when(userRepository.existsByUsername(anyString())).thenReturn(false);
when(userRepository.existsByEmail("test@example.com")).thenReturn(true);
// When/Then
assertThatThrownBy(() -> authenticationService.register(registerRequest))
.isInstanceOf(UserAlreadyExistsException.class)
.hasMessageContaining("Email already registered");
verify(userRepository, never()).save(any(User.class));
}
}
@Nested
@DisplayName("Login Tests")
class LoginTests {
@Test
@DisplayName("Should login user successfully with username")
void login_WithValidCredentials_ReturnsAuthenticationResponse() {
// Given
when(userRepository.findByUsernameOrEmail("testuser", "testuser"))
.thenReturn(Optional.of(user));
when(authenticationManager.authenticate(any(UsernamePasswordAuthenticationToken.class)))
.thenReturn(new UsernamePasswordAuthenticationToken(user, null));
when(jwtService.generateToken(any(User.class))).thenReturn("jwt-token");
when(jwtService.getExpirationTime()).thenReturn(86400000L);
when(userMapper.toUserResponse(any(User.class))).thenReturn(userResponse);
// When
AuthenticationResponse response = authenticationService.login(loginRequest);
// Then
assertThat(response).isNotNull();
assertThat(response.accessToken()).isEqualTo("jwt-token");
assertThat(response.user().username()).isEqualTo("testuser");
verify(authenticationManager).authenticate(any(UsernamePasswordAuthenticationToken.class));
}
@Test
@DisplayName("Should login user successfully with email")
void login_WithEmail_ReturnsAuthenticationResponse() {
// Given
LoginRequest emailLoginRequest = new LoginRequest("test@example.com", "password123");
when(userRepository.findByUsernameOrEmail("test@example.com", "test@example.com"))
.thenReturn(Optional.of(user));
when(authenticationManager.authenticate(any(UsernamePasswordAuthenticationToken.class)))
.thenReturn(new UsernamePasswordAuthenticationToken(user, null));
when(jwtService.generateToken(any(User.class))).thenReturn("jwt-token");
when(jwtService.getExpirationTime()).thenReturn(86400000L);
when(userMapper.toUserResponse(any(User.class))).thenReturn(userResponse);
// When
AuthenticationResponse response = authenticationService.login(emailLoginRequest);
// Then
assertThat(response).isNotNull();
assertThat(response.accessToken()).isEqualTo("jwt-token");
}
@Test
@DisplayName("Should throw exception when user not found")
void login_WithNonExistentUser_ThrowsInvalidCredentialsException() {
// Given
when(userRepository.findByUsernameOrEmail(anyString(), anyString()))
.thenReturn(Optional.empty());
// When/Then
assertThatThrownBy(() -> authenticationService.login(loginRequest))
.isInstanceOf(InvalidCredentialsException.class)
.hasMessageContaining("Invalid username/email or password");
verify(authenticationManager, never()).authenticate(any());
}
@Test
@DisplayName("Should throw exception when password is incorrect")
void login_WithWrongPassword_ThrowsInvalidCredentialsException() {
// Given
when(userRepository.findByUsernameOrEmail("testuser", "testuser"))
.thenReturn(Optional.of(user));
when(authenticationManager.authenticate(any(UsernamePasswordAuthenticationToken.class)))
.thenThrow(new BadCredentialsException("Bad credentials"));
// When/Then
assertThatThrownBy(() -> authenticationService.login(loginRequest))
.isInstanceOf(InvalidCredentialsException.class)
.hasMessageContaining("Invalid username/email or password");
}
}
}