mirror of
https://github.com/iflytek/skillhub.git
synced 2026-10-02 02:13:52 +00:00
feat : 调整测试代码
This commit is contained in:
parent
aaa664ce17
commit
ad3ad75eec
2 changed files with 3 additions and 23 deletions
|
|
@ -132,7 +132,7 @@ public class LocalAuthService {
|
|||
ensureNotLocked(credential);
|
||||
|
||||
if (!passwordEncoder.matches(password, credential.getPasswordHash())) {
|
||||
localAuthFailedService.handleFailedLogin(credential);
|
||||
localAuthFailedService.handleFailedLogin(credential.getId());
|
||||
throw invalidCredentials();
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -122,21 +122,12 @@ class LocalAuthServiceTest {
|
|||
given(userAccountRepository.findById("usr_1")).willReturn(Optional.of(user));
|
||||
given(passwordEncoder.matches("bad", "encoded")).willReturn(false);
|
||||
|
||||
// Mock handleFailedLogin to increment failedAttempts
|
||||
doAnswer(invocation -> {
|
||||
LocalCredential cred = invocation.getArgument(0);
|
||||
cred.setFailedAttempts(cred.getFailedAttempts() + 1);
|
||||
credentialRepository.save(cred);
|
||||
return null;
|
||||
}).when(localAuthFailedService).handleFailedLogin(any(LocalCredential.class));
|
||||
|
||||
assertThatThrownBy(() -> service.login("alice", "bad"))
|
||||
.isInstanceOf(AuthFlowException.class)
|
||||
.extracting("status")
|
||||
.isEqualTo(HttpStatus.UNAUTHORIZED);
|
||||
|
||||
assertThat(credential.getFailedAttempts()).isEqualTo(1);
|
||||
verify(credentialRepository).save(credential);
|
||||
verify(localAuthFailedService).handleFailedLogin(credential.getId());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
@ -149,23 +140,12 @@ class LocalAuthServiceTest {
|
|||
given(userAccountRepository.findById("usr_1")).willReturn(Optional.of(user));
|
||||
given(passwordEncoder.matches("bad", "encoded")).willReturn(false);
|
||||
|
||||
// Mock handleFailedLogin to set lockedUntil using CLOCK
|
||||
doAnswer(invocation -> {
|
||||
LocalCredential cred = invocation.getArgument(0);
|
||||
cred.setFailedAttempts(cred.getFailedAttempts() + 1);
|
||||
cred.setLockedUntil(Instant.now(CLOCK).plus(java.time.Duration.ofMinutes(15)));
|
||||
credentialRepository.save(cred);
|
||||
return null;
|
||||
}).when(localAuthFailedService).handleFailedLogin(any(LocalCredential.class));
|
||||
|
||||
assertThatThrownBy(() -> service.login("alice", "bad"))
|
||||
.isInstanceOf(AuthFlowException.class)
|
||||
.extracting("status")
|
||||
.isEqualTo(HttpStatus.UNAUTHORIZED);
|
||||
|
||||
assertThat(credential.getFailedAttempts()).isEqualTo(5);
|
||||
assertThat(credential.getLockedUntil()).isEqualTo(Instant.now(CLOCK).plusSeconds(15 * 60));
|
||||
verify(credentialRepository).save(credential);
|
||||
verify(localAuthFailedService).handleFailedLogin(credential.getId());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue