Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

Commit

Permalink
Merge pull request #10 from BudgetBuddyDE/add-tests
Browse files Browse the repository at this point in the history
test: Add some tests
  • Loading branch information
tklein1801 authored Jan 2, 2024
2 parents f03161a + 1a739a8 commit 28c2a89
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,8 @@ public ResponseEntity<ApiResponse<UserPasswordReset>> requestPasswordReset(
.body(new ApiResponse<>(HttpStatus.NOT_FOUND.value(), "No user found for the provided email"));
}

UserPasswordReset passwordReset = userPasswordResetRepository.save(new UserPasswordReset(optUser.get()));
UserPasswordReset passwordReset = userPasswordResetRepository
.save(new UserPasswordReset(optUser.get()));

try {
if (!mailService.trigger(MailService.getRequestPasswordMailPayload(
Expand Down
130 changes: 130 additions & 0 deletions src/test/java/de/budgetbuddy/backend/auth/PasswordResetTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
package de.budgetbuddy.backend.auth;

import de.budgetbuddy.backend.ApiResponse;
import de.budgetbuddy.backend.MailService;
import de.budgetbuddy.backend.user.User;
import de.budgetbuddy.backend.user.UserRepository;
import org.junit.jupiter.api.Test;
import org.springframework.core.env.Environment;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

import java.util.Date;
import java.util.Objects;
import java.util.Optional;
import java.util.UUID;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class PasswordResetTest {
private final UserRepository userRepository;
private final UserPasswordResetRepository userPasswordResetRepository;
private final AuthController authController;

PasswordResetTest() {
this.userRepository = mock(UserRepository.class);
this.userPasswordResetRepository = mock(UserPasswordResetRepository.class);
this.authController = new AuthController(userRepository,
userPasswordResetRepository,
new MailService(mock(Environment.class)));
}

@Test
void requestPasswordReset_UserByEmailNotFound() {
User user = new User(UUID.randomUUID());
user.setEmail("[email protected]");

when(userRepository.findByEmail(user.getEmail()))
.thenReturn(Optional.empty());

ResponseEntity<ApiResponse<UserPasswordReset>> response =
authController.requestPasswordReset(user.getEmail());

assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode());
assertEquals("No user found for the provided email",
Objects.requireNonNull(response.getBody()).getMessage());
}

@Test
void requestPasswordReset_Success() {
User user = new User(UUID.randomUUID());
user.setEmail("[email protected]");

UserPasswordReset userPasswordReset = new UserPasswordReset(user);

when(userRepository.findByEmail(user.getEmail()))
.thenReturn(Optional.of(user));

when(userPasswordResetRepository.save(userPasswordReset))
.thenReturn(userPasswordReset);

ResponseEntity<ApiResponse<UserPasswordReset>> response =
authController.requestPasswordReset(user.getEmail());

assertEquals(HttpStatus.OK, response.getStatusCode());
assertNull(Objects.requireNonNull(response.getBody()).getMessage());
}

@Test
void validateOtp_OtpNotFound() {
UUID otp = UUID.randomUUID();

when(userPasswordResetRepository.findByOtp(otp))
.thenReturn(Optional.empty());

ApiResponse<Boolean> result = authController.isOtpValid(otp);

assertEquals(HttpStatus.NOT_FOUND.value(), result.getStatus());
assertEquals("No session found for the provided session", result.getMessage());
assertFalse(result.getData());
}

@Test
void validateOtp_OtpHasExpired() {
User user = new User(UUID.randomUUID());
UserPasswordReset passwordReset = new UserPasswordReset(user);
passwordReset.setCreatedAt(new Date(23, 11, 30));

when(userPasswordResetRepository.findByOtp(passwordReset.getOtp()))
.thenReturn(Optional.of(passwordReset));

ApiResponse<Boolean> result = authController.isOtpValid(passwordReset.getOtp());

assertEquals(HttpStatus.UNAUTHORIZED.value(), result.getStatus());
assertEquals("This OTP has expired", result.getMessage());
assertFalse(result.getData());
}

@Test
void validateOtp_WasUsed() {
User user = new User(UUID.randomUUID());
UserPasswordReset passwordReset = new UserPasswordReset(user);
passwordReset.setUsed(true);

when(userPasswordResetRepository.findByOtp(passwordReset.getOtp()))
.thenReturn(Optional.of(passwordReset));

ApiResponse<Boolean> result = authController.isOtpValid(passwordReset.getOtp());

assertEquals(HttpStatus.UNAUTHORIZED.value(), result.getStatus());
assertEquals("This OTP has already been used", result.getMessage());
assertFalse(result.getData());
}

@Test
void validateOtp_Success() {
User user = new User(UUID.randomUUID());
UserPasswordReset passwordReset = new UserPasswordReset(user);

when(userPasswordResetRepository.findByOtp(passwordReset.getOtp()))
.thenReturn(Optional.of(passwordReset));

ApiResponse<Boolean> result = authController.isOtpValid(passwordReset.getOtp());

assertEquals(HttpStatus.OK.value(), result.getStatus());
assertNull(result.getMessage());
assertTrue(result.getData());
}
}

0 comments on commit 28c2a89

Please sign in to comment.