Skip to content

Commit

Permalink
Merge pull request #49 from Group-5/feat/User
Browse files Browse the repository at this point in the history
Merge updated changes for backend classes from feat/User to release/v1.0.0
  • Loading branch information
fredrjm authored Mar 10, 2026
2 parents 54cd266 + 1e60e44 commit 075f0e5
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 17 deletions.
20 changes: 19 additions & 1 deletion src/main/java/edu/group5/app/model/donation/DonationService.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,24 @@ public DonationService(DonationRepository donationRepository,
this.organizationRepository = organizationRepository;
}

/**
* Getter for the DonationRepository used by this service.
* This method allows access to the donation repository for managing donation records and retrieving donation information.
* @return the DonationRepository instance used by this service
*/
public DonationRepository getDonationRepository() {
return this.donationRepository;
}

/**
* Getter for the OrganizationRepository used by this service.
* This method allows access to the organization repository for validating organization information when processing donations.
* @return the OrganizationRepository instance used by this service
*/
public OrganizationRepository getOrganizationRepository() {
return this.organizationRepository;
}

/**
* Processes a donation from a customer to a specified organization with a given amount.
* Validates the customer, organization number, and donation amount before creating a donation record.
Expand All @@ -57,7 +75,7 @@ public boolean donate(Customer customer, int orgNumber, BigDecimal amount, Strin
Donation donation =
new Donation(donationRepository.getNextDonationId(),
customer.getUserId(), org.orgNumber(), amount, Timestamp.from(Instant.now()), paymentMethod);
donationRepository.addContent(donation);
this.donationRepository.addContent(donation);
return true;
}
}
10 changes: 7 additions & 3 deletions src/main/java/edu/group5/app/model/user/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,16 @@ public String getPasswordHash() {
* This method uses BCrypt to compare the plaintext password with the hashed password.
* @param password the plaintext password to verify
* @return true if the password is correct, false otherwise
* @throws IllegalArgumentException if the password is null, empty, or longer than 72 characters (BCrypt limit)
*/
public boolean verifyPassword(String password) {
if (password == null || password.isEmpty()) {
public boolean verifyPassword(char[] password) {
if (password == null || password.length == 0) {
return false;
}
if (password.length > 72) { // BCrypt has a maximum password length of 72 bytes
throw new IllegalArgumentException("Password cannot be longer than 72 characters");
}
BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
return encoder.matches(password, this.passwordHash);
return encoder.matches(new String(password), this.passwordHash);
}
}
17 changes: 13 additions & 4 deletions src/main/java/edu/group5/app/model/user/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@ public UserService(UserRepository userRepository) {
this.userRepository = userRepository;
}

/**
* Getter for the UserRepository used by this service.
* This method allows access to the user repository for managing user data and performing operations such as registration and login.
* @return the UserRepository instance used by this service
*/
public UserRepository getUserRepository() {
return this.userRepository;
}

/**
* Registers a new user with the given information. Validates the input data and creates a new User object
* based on the specified role. Currently supports registration for customers only.
Expand Down Expand Up @@ -48,7 +57,7 @@ public boolean registerUser(String role, String firstName, String lastName,
} else { /* TODO when you switch to a real DB, replace getNextUserId with DB auto-increment/identity and ignore manual ID generation in service*/
return false;
}
userRepository.addContent(user);
this.userRepository.addContent(user);
return true;
}

Expand All @@ -60,11 +69,11 @@ public boolean registerUser(String role, String firstName, String lastName,
* (i.e., the user exists and the password is correct), false otherwise
* @throws IllegalArgumentException if email is null or empty, or if password is null or empty
*/
public boolean login(String email, String password) {
if (email == null || email.trim().isEmpty() || password == null || password.trim().isEmpty()) {
public boolean login(String email, char[] password) {
if (email == null || email.trim().isEmpty() || password == null || password.length == 0) {
return false;
}
User user = userRepository.findUserByEmail(email);
User user = this.userRepository.findUserByEmail(email);
return user != null && user.verifyPassword(password);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,16 @@ void testConstructorThrowsIfOrganizationRepositoryIsNull() {
assertEquals("OrganizationRepository cannot be null", exception.getMessage());
}

@Test
void testGetDonationRepository() {
assertEquals(donationRepository, donationService.getDonationRepository());
}

@Test
void testGetOrganizationRepository() {
assertEquals(organizationRepository, donationService.getOrganizationRepository());
}

@Test
void donateReturnsFalseIfCustomerNull() {
boolean result = donationService.donate(null,
Expand Down
18 changes: 15 additions & 3 deletions src/test/java/edu/group5/app/model/user/CustomerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -116,15 +116,15 @@ void verifyPasswordReturnsTrueForCorrectPassword() {
User user = new Customer(testUserId, testFirstName,
testLastName, testEmail, testPasswordHash);

assertTrue(user.verifyPassword(testPassword));
assertTrue(user.verifyPassword(testPassword.toCharArray()));
}

@Test
void verifyPasswordReturnsFalseForIncorrectPassword() {
User user = new Customer(testUserId, testFirstName,
testLastName, testEmail, testPasswordHash);

assertFalse(user.verifyPassword("wrongPassword"));
assertFalse(user.verifyPassword("wrongPassword".toCharArray()));
}

@Test
Expand All @@ -140,7 +140,19 @@ void verifyPasswordReturnsFalseForEmptyPassword() {
User user = new Customer(testUserId, testFirstName,
testLastName, testEmail, testPasswordHash);

assertFalse(user.verifyPassword(""));
assertFalse(user.verifyPassword("".toCharArray()));
}

@Test
void verifyPasswordThrowsExceptionForTooLongPassword() {
User user = new Customer(testUserId, testFirstName,
testLastName, testEmail, testPasswordHash);
char[] longPassword = new char[73]; // 73 characters, exceeding BCrypt limit
IllegalArgumentException exception = assertThrows(
IllegalArgumentException.class,
() -> user.verifyPassword(longPassword)
);
assertEquals("Password cannot be longer than 72 characters", exception.getMessage());
}

@Test
Expand Down
19 changes: 13 additions & 6 deletions src/test/java/edu/group5/app/model/user/UserServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ void constructorthrowsIfNull() {
assertEquals("UserRepository cannot be null", ex.getMessage());
}

@Test
void testGetUserRepository() {
assertEquals(repo, service.getUserRepository());
}

@Test
void registerUserValid() {
boolean result = service.registerUser("Customer", "Alice", "Smith",
Expand Down Expand Up @@ -90,7 +95,7 @@ void loginValidPassword() {
User testUser = new Customer(10, "Test", "User", "test@example.com", hashedPassword);
repo.addContent(testUser);

boolean result = service.login("test@example.com", plainPassword);
boolean result = service.login("test@example.com", plainPassword.toCharArray());
assertTrue(result);
}

Expand All @@ -102,19 +107,21 @@ void loginInvalidPassword() {
User testUser = new Customer(10, "Test", "User", "test@example.com", hashedPassword);
repo.addContent(testUser);

boolean result = service.login("test@example.com", "wrongpass");
boolean result = service.login("test@example.com", "wrongpass".toCharArray());
boolean result2 = service.login("test@example.com", null);
boolean result3 = service.login("test@example.com", " ");
boolean result3 = service.login("test@example.com", " ".toCharArray());
boolean result4 = service.login("test@example.com", new char[0]);
assertFalse(result);
assertFalse(result2);
assertFalse(result3);
assertFalse(result4);
}

@Test
void loginInvalidEmail() {
boolean result = service.login("nonexist@example.com", "password");
boolean result2 = service.login(null, "password");
boolean result3 = service.login(" ", "password");
boolean result = service.login("nonexist@example.com", "password".toCharArray());
boolean result2 = service.login(null, "password".toCharArray());
boolean result3 = service.login(" ", "password".toCharArray());
assertFalse(result);
assertFalse(result2);
assertFalse(result3);
Expand Down

0 comments on commit 075f0e5

Please sign in to comment.