From 0178fc61d0e99dd9f0cbfe78f9ee219e7e26d838 Mon Sep 17 00:00:00 2001 From: Richard Schmidt Date: Fri, 9 Feb 2024 22:26:23 +0100 Subject: [PATCH] test_implemented PasswordHasherTest and fixed null password test failure for PasswordHasher --- src/main/java/PasswordHasher.java | 4 ++++ src/test/java/PasswordHasherTest.java | 27 ++++++++++++++++++++++++++ user.json | 28 +-------------------------- 3 files changed, 32 insertions(+), 27 deletions(-) create mode 100644 src/test/java/PasswordHasherTest.java diff --git a/src/main/java/PasswordHasher.java b/src/main/java/PasswordHasher.java index 14a1137..2ae5d8d 100644 --- a/src/main/java/PasswordHasher.java +++ b/src/main/java/PasswordHasher.java @@ -3,6 +3,10 @@ import java.security.NoSuchAlgorithmException; public class PasswordHasher { public static String hashPassword(String password) { + if (password == null) { + throw new IllegalArgumentException("Password cannot be null"); + } + try { MessageDigest digest = MessageDigest.getInstance("SHA-256"); byte[] hash = digest.digest(password.getBytes()); diff --git a/src/test/java/PasswordHasherTest.java b/src/test/java/PasswordHasherTest.java new file mode 100644 index 0000000..6177642 --- /dev/null +++ b/src/test/java/PasswordHasherTest.java @@ -0,0 +1,27 @@ +import org.junit.jupiter.api.Test; + +import static org.junit.Assert.assertThrows; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +public class PasswordHasherTest { + + @Test + void testHashPassword() { + String password = "password123"; + String hashedPassword = PasswordHasher.hashPassword(password); + + // Check that the hashed password is not null + assertEquals(64, hashedPassword.length()); + + // Test with an empty password + String emptyPassword = ""; + String hashedEmptyPassword = PasswordHasher.hashPassword(emptyPassword); + assertEquals(64, hashedEmptyPassword.length()); + + // Test with a null password + assertThrows(IllegalArgumentException.class, () -> { + PasswordHasher.hashPassword(null); + }); + } +} \ No newline at end of file diff --git a/user.json b/user.json index 8242745..c44dc44 100644 --- a/user.json +++ b/user.json @@ -1,29 +1,3 @@ [ - { - "id": "c99b1061-13b7-4fa8-b1ac-814e07db3ef4", - "userName": "Test", - "password": "8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92", - "birthday": "", - "firstName": "", - "surname": "", - "stayLoggedIn": false - }, - { - "id": "89de267d-8456-4393-9fab-a5ddb36c7686", - "userName": "existinguser", - "password": "5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8", - "birthday": "1990-01-01", - "firstName": "John", - "surname": "Doe", - "stayLoggedIn": false - }, - { - "id": "43d45e81-3540-427f-8cb9-8f279d7a18d3", - "userName": "testUser", - "password": "5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8", - "birthday": "1990-01-01", - "firstName": "John", - "surname": "Doe", - "stayLoggedIn": false - } + ] \ No newline at end of file