Skip to content

Commit

Permalink
Added Junit tests to PasswordHasher
Browse files Browse the repository at this point in the history
  • Loading branch information
robinsp committed Mar 3, 2026
1 parent 9b9d8ea commit ab2de3d
Show file tree
Hide file tree
Showing 33 changed files with 83 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package ntnu.sytemutvikling.team6.models;

import ntnu.sytemutvikling.team6.models.user.User;

import java.time.LocalDateTime;
import java.util.UUID;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package ntnu.sytemutvikling.team6.models;

import ntnu.sytemutvikling.team6.models.user.User;

import java.time.LocalDateTime;
import java.util.UUID;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ntnu.sytemutvikling.team6.models;
package ntnu.sytemutvikling.team6.models.user;

import java.util.*;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ntnu.sytemutvikling.team6.models;
package ntnu.sytemutvikling.team6.models.user;

/**
* Supported application languages.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ntnu.sytemutvikling.team6.models;
package ntnu.sytemutvikling.team6.models.user;

import java.time.LocalDateTime;
import java.util.UUID;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ntnu.sytemutvikling.team6.models;
package ntnu.sytemutvikling.team6.models.user;

/**
* Available users
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ntnu.sytemutvikling.team6.models;
package ntnu.sytemutvikling.team6.models.user;

// Mangler Enhetstesting

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
package ntnu.sytemutvikling.team6.models;
package ntnu.sytemutvikling.team6.models.user;

import ntnu.sytemutvikling.team6.security.PasswordHasher;

import java.util.UUID;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ntnu.sytemutvikling.team6.models;
package ntnu.sytemutvikling.team6.security;

import java.security.MessageDigest;
import java.security.SecureRandom;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package ntnu.sytemutvikling.team6.security;

import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

class PasswordHasherTest {
private final PasswordHasher hasher = new PasswordHasher();

@Nested
class getHashPasswordTest {

@Test
void shouldThrowIfPasswordIsNull() {
assertThrows(IllegalArgumentException.class, () -> hasher.getHashPassword(null));
}

@Test
void shouldThrowIfPasswordIsBlank() {
assertThrows(IllegalArgumentException.class, () -> hasher.getHashPassword(" "));
}

@Test
void shouldReturnDifferentHashesForSamePasswordBecauseSaltIsRandom() {
String test1 = hasher.getHashPassword("Password");
String test2 = hasher.getHashPassword("Password");

assertNotEquals(test1, test2);
}
}

@Nested
class isValidPasswordTest {

@Test
void shouldReturnTrueForCorrectPassword() {
String test = hasher.getHashPassword("Password");
assertTrue(hasher.isValidPassword("Password", test));
}

@Test
void shouldReturnFalseForWrongPassword() {
String test = hasher.getHashPassword("Password");
assertFalse(hasher.isValidPassword("password", test));
}

@Test
void shouldReturnFalseIfPasswordIsBlank() {
String test = hasher.getHashPassword("Test");
assertFalse(hasher.isValidPassword(" ", test));
}

@Test
void shouldReturnFalseIfPasswordIsNull() {
String test = hasher.getHashPassword("Test");
assertFalse(hasher.isValidPassword(null, test));
}

@Test
void shouldThrowIfStoredHashIsNull() {
assertThrows(RuntimeException.class, () -> hasher.isValidPassword("Password", null));
}

@Test
void shouldThrowIfStoredHashIsBlank() {
assertThrows(RuntimeException.class, () -> hasher.isValidPassword("Password", " "));
}
}
}
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 comments on commit ab2de3d

Please sign in to comment.