Skip to content

Commit

Permalink
test[User]: Add dependency for the Bcrypt, and add positive and Negat…
Browse files Browse the repository at this point in the history
…ive JUnit tests
  • Loading branch information
Fredrik Marjoni committed Feb 26, 2026
1 parent 00cb18b commit 8bd6986
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 2 deletions.
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@
<artifactId>spring-security-crypto</artifactId>
<version>7.0.2</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.3.5</version>
</dependency>
</dependencies>

<build>
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/edu/group5/app/model/user/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* by comparing the provided plaintext password with the stored hashed password using BCrypt.
*
*/
public abstract class User {
public class User {
private int userId;
private String role;
private String firstName;
Expand Down
108 changes: 107 additions & 1 deletion src/test/java/edu/group5/app/model/user/UserTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,111 @@
package edu.group5.app.model.user;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

import org.junit.jupiter.api.Test;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

public class UserTest {

private static final int TEST_USER_ID = 1;
private static final String TEST_ROLE = "Donor";
private static final String TEST_FIRST_NAME = "John";
private static final String TEST_LAST_NAME = "Doe";
private static final String TEST_EMAIL = "john.doe@example.com";
private static final String TEST_PASSWORD = "password123";
private static final String TEST_PASSWORD_HASH = new BCryptPasswordEncoder().encode(TEST_PASSWORD);

private static final int WRONG_USER_ID = -5;
private static final String WRONG_ROLE = "";
private static final String WRONG_FIRST_NAME = "";
private static final String WRONG_LAST_NAME = "";
private static final String WRONG_EMAIL = "";
private static final String WRONG_PASSWORD_HASH = "";

private void constructorTest(int userId, String role, String firstName, String lastName, String email, String passwordHash, boolean negativeTest) {
boolean exceptionThrown = negativeTest;
try {
new User(userId, role, firstName, lastName, email, passwordHash);
} catch (Exception e) {
exceptionThrown = !negativeTest;
} finally {
assertFalse(exceptionThrown);
}
}

@Test
public void constructorThrowsNoException() {
constructorTest(TEST_USER_ID, TEST_ROLE, TEST_FIRST_NAME, TEST_LAST_NAME, TEST_EMAIL, TEST_PASSWORD_HASH, false);
}

@Test
public void constructorWithNegativeUserIdThrowsException() {
constructorTest(WRONG_USER_ID, TEST_ROLE, TEST_FIRST_NAME, TEST_LAST_NAME, TEST_EMAIL, TEST_PASSWORD_HASH, true);
}

@Test
public void constructorWithEmptyRoleThrowsException() {
constructorTest(TEST_USER_ID, WRONG_ROLE, TEST_FIRST_NAME, TEST_LAST_NAME, TEST_EMAIL, TEST_PASSWORD_HASH, true);
}

@Test
public void constructorWithEmptyFirstNameThrowsException() {
constructorTest(TEST_USER_ID, TEST_ROLE, WRONG_FIRST_NAME, TEST_LAST_NAME, TEST_EMAIL, TEST_PASSWORD_HASH, true);
}

@Test
public void constructorWithEmptyLastNameThrowsException() {
constructorTest(TEST_USER_ID, TEST_ROLE, TEST_FIRST_NAME, WRONG_LAST_NAME, TEST_EMAIL, TEST_PASSWORD_HASH, true);
}

@Test
public void constructorWithEmptyEmailThrowsException() {
constructorTest(TEST_USER_ID, TEST_ROLE, TEST_FIRST_NAME, TEST_LAST_NAME, WRONG_EMAIL, TEST_PASSWORD_HASH, true);
}

@Test
public void constructorWithEmptyPasswordHashThrowsException() {
constructorTest(TEST_USER_ID, TEST_ROLE, TEST_FIRST_NAME, TEST_LAST_NAME, TEST_EMAIL, WRONG_PASSWORD_HASH, true);
}

private void getMethodComparer(User user, boolean negativeTest) {
if (user.getUserId() == TEST_USER_ID &&
user.getRole().equals(TEST_ROLE) &&
user.getFirstName().equals(TEST_FIRST_NAME) &&
user.getLastName().equals(TEST_LAST_NAME) &&
user.getEmail().equals(TEST_EMAIL) &&
user.getPasswordHash() != null) {
assertFalse(negativeTest);
}
}

@Test
public void getMethodsReturnCorrectInformation() {
User testUser = new User(TEST_USER_ID, TEST_ROLE, TEST_FIRST_NAME, TEST_LAST_NAME, TEST_EMAIL, TEST_PASSWORD_HASH);
getMethodComparer(testUser, false);
}

@Test
public void verifyPasswordReturnsTrueForCorrectPassword() {
User testUser = new User(TEST_USER_ID, TEST_ROLE, TEST_FIRST_NAME, TEST_LAST_NAME, TEST_EMAIL, TEST_PASSWORD_HASH);
assertTrue(testUser.verifyPassword(TEST_PASSWORD));
}

@Test
public void verifyPasswordReturnsFalseForIncorrectPassword() {
User testUser = new User(TEST_USER_ID, TEST_ROLE, TEST_FIRST_NAME, TEST_LAST_NAME, TEST_EMAIL, TEST_PASSWORD_HASH);
assertFalse(testUser.verifyPassword("wrongPassword"));
}

@Test
public void verifyPasswordReturnsFalseForNullPassword() {
User testUser = new User(TEST_USER_ID, TEST_ROLE, TEST_FIRST_NAME, TEST_LAST_NAME, TEST_EMAIL, TEST_PASSWORD_HASH);
assertFalse(testUser.verifyPassword(null));
}

@Test
public void verifyPasswordReturnsFalseForEmptyPassword() {
User testUser = new User(TEST_USER_ID, TEST_ROLE, TEST_FIRST_NAME, TEST_LAST_NAME, TEST_EMAIL, TEST_PASSWORD_HASH);
assertFalse(testUser.verifyPassword(""));
}
}

0 comments on commit 8bd6986

Please sign in to comment.