diff --git a/pom.xml b/pom.xml index f223fa90c7ea5b31d95b0e3daba6446dc0830e39..5a5a744f094003afa4cf32b130e7d276ade41c58 100644 --- a/pom.xml +++ b/pom.xml @@ -38,13 +38,11 @@ <artifactId>hibernate-jpa-2.1-api</artifactId> <version>1.0.2.Final</version> </dependency> - <dependency> <groupId>org.hibernate.validator</groupId> <artifactId>hibernate-validator</artifactId> <version>6.1.0.Final</version> </dependency> - <dependency> <groupId>javax.xml.bind</groupId> <artifactId>jaxb-api</artifactId> @@ -110,14 +108,12 @@ <artifactId>junit</artifactId> <version>4.13</version> </dependency> - <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <scope>test</scope> <version>1.4.194</version> </dependency> - </dependencies> <build> <plugins> diff --git a/src/main/java/NTNU/IDATT1002/App.java b/src/main/java/NTNU/IDATT1002/App.java index 5d464e8d1291ac44f7ab9b6146a797018eb2c62c..3ea184e7f38b066750bc21a1e5c06a86e00600ac 100644 --- a/src/main/java/NTNU/IDATT1002/App.java +++ b/src/main/java/NTNU/IDATT1002/App.java @@ -1,11 +1,16 @@ package NTNU.IDATT1002; +import NTNU.IDATT1002.repository.LoginRepository; import javafx.application.Application; import javafx.fxml.FXMLLoader; import javafx.scene.Parent; import javafx.scene.Scene; import javafx.stage.Stage; +import org.slf4j.Logger; +import javax.persistence.EntityManager; +import javax.persistence.EntityManagerFactory; +import javax.persistence.Persistence; import java.io.IOException; @@ -13,6 +18,8 @@ public class App extends Application { private static Scene scene; + private static Logger log; + @Override public void start(Stage stage) throws IOException { scene = new Scene(loadFXML("login")); diff --git a/src/main/java/NTNU/IDATT1002/models/Login.java b/src/main/java/NTNU/IDATT1002/models/Login.java new file mode 100644 index 0000000000000000000000000000000000000000..6d196fb58d538e75bea81468fd2e06b07cd6d177 --- /dev/null +++ b/src/main/java/NTNU/IDATT1002/models/Login.java @@ -0,0 +1,57 @@ +package NTNU.IDATT1002.models; + +import org.hibernate.annotations.NaturalId; + +import javax.persistence.*; +import javax.validation.constraints.NotBlank; +import javax.validation.constraints.Size; + +@Entity +@Table(name = "login") +public class Login { + + + @Id + @JoinColumn(name = "username") + private String username; + + + @OneToOne(cascade = {CascadeType.ALL}) + private User user; + + @NotBlank(message = "Hash salt may not be blank") + private String hash; + + @NotBlank(message = "Password salt may not be blank") + private String passwordSalt; + + public Login() { + } + + public Login(User user, String passwordSalt , String hash) { + this.username = user.getUsername(); + this.user = user; + this.hash = hash; + this.passwordSalt = passwordSalt; + } + + public User getUser() { + return user; + } + + public void setHash(String hash) { + this.hash = hash; + } + + public void setPasswordSalt(String passwordSalt) { + this.passwordSalt = passwordSalt; + } + + public String getHash() { + return hash; + } + + public String getPasswordSalt() { + return passwordSalt; + } +} diff --git a/src/main/java/NTNU/IDATT1002/models/User.java b/src/main/java/NTNU/IDATT1002/models/User.java index bcd11e8535e9118139d6571c1f7363da9c92de7e..29320bbe571cad5d25dcc995a9c4ef551645fbbc 100644 --- a/src/main/java/NTNU/IDATT1002/models/User.java +++ b/src/main/java/NTNU/IDATT1002/models/User.java @@ -1,16 +1,43 @@ package NTNU.IDATT1002.models; import javax.persistence.*; +import javax.validation.constraints.Email; +import javax.validation.constraints.NotBlank; +import javax.validation.constraints.Past; import java.util.ArrayList; +import java.util.Date; import java.util.List; @Entity @Table(name = "user") public class User { - @Id + @Id @NotBlank(message = "Username may not be blank") private String username; + @Email + @NotBlank(message = "Email may not be blank") + private String email; + + @NotBlank(message = "Fist name may not be blank") + private String firstName; + + @NotBlank(message = "Last name may not be blank") + private String lastName; + + @NotBlank(message = "Calling code may not be blank") + private String callingCode; + + @NotBlank(message = "Phone number may not be blank") + private String phoneNumber; + + @Past(message = "Birth date must be in the past") + private Date birthDate; + + private boolean isAdmin; + private boolean isActive; + + @OneToMany( mappedBy = "user", cascade = CascadeType.ALL, @@ -18,6 +45,85 @@ public class User { ) private List<ImageAlbum> imageAlbums = new ArrayList<>(); + + public User() { + } + + public User(String email, String username, String firstName, String lastName, String callingCode, String phoneNumber, Date birthDate) { + this.email = email; + this.username = username; + this.firstName = firstName; + this.lastName = lastName; + this.callingCode = callingCode; + this.phoneNumber = phoneNumber; + this.birthDate = birthDate; + this.isAdmin = false; + this.isActive = true; + this.imageAlbums = imageAlbums; + } + + public User(User user) { + this(user.getEmail(), + user.getUsername(), + user.getFirstName(), + user.getLastName(), + user.getCallingCode(), + user.getPhoneNumber(), + user.getBirthDate(), + user.isAdmin(), + user.isActive() + ); + } + + public User(String email, String username, String firstName, String lastName, String callingCode, String phoneNumber, Date birthDate, boolean isAdmin, boolean isActive) { + this.email = email; + this.username = username; + this.firstName = firstName; + this.lastName = lastName; + this.callingCode = callingCode; + this.phoneNumber = phoneNumber; + this.birthDate = birthDate; + this.isAdmin = isAdmin; + this.isActive = isActive; + this.imageAlbums = imageAlbums; + } + + public String getEmail() { + return email; + } + + public String getUsername() { + return username; + } + + public String getFirstName() { + return firstName; + } + + public String getLastName() { + return lastName; + } + + public String getCallingCode() { + return callingCode; + } + + public String getPhoneNumber() { + return phoneNumber; + } + + public Date getBirthDate() { + return birthDate; + } + + public boolean isAdmin() { + return isAdmin; + } + + public boolean isActive() { + return isActive; + } + /** * Add given image album. * @@ -38,4 +144,11 @@ public class User { imageAlbum.setUser(null); } + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + User user = (User) o; + return username.equals(user.username); + } } diff --git a/src/main/java/NTNU/IDATT1002/repository/LoginRepository.java b/src/main/java/NTNU/IDATT1002/repository/LoginRepository.java new file mode 100644 index 0000000000000000000000000000000000000000..f38fc25fcd2b18c065ba93f69f96fc2dba828205 --- /dev/null +++ b/src/main/java/NTNU/IDATT1002/repository/LoginRepository.java @@ -0,0 +1,63 @@ +package NTNU.IDATT1002.repository; + + +import NTNU.IDATT1002.models.Login; +import NTNU.IDATT1002.models.User; +import NTNU.IDATT1002.utils.Authentication; + +import javax.persistence.EntityManager; +import java.lang.reflect.Array; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; +import java.util.Optional; + + +public class LoginRepository extends GenericRepository<Login, String>{ + + private EntityManager entityManager; + + public LoginRepository(EntityManager entityManager) { + super(entityManager); + setClassType(Login.class); + } + + public boolean logIn(String username, String password) { + try { + Optional<Login> login = findById(username); + if(login.isPresent()) { + String salt = login.get().getPasswordSalt(); + String expectedHash = login.get().getHash(); + return Authentication.isCorrectPassword(salt, password, expectedHash); + } + } + catch (IllegalArgumentException e) { + e.printStackTrace(); + } + return false; + } + + public boolean changePassword(String username, String oldPassword, String newPassword) { + ArrayList<String> info = new ArrayList<>(); + try { + Optional<Login> login = findById(username); + if(login.isPresent()) { + String salt = login.get().getPasswordSalt(); + String expectedHash = login.get().getHash(); + if(Authentication.isCorrectPassword(salt, oldPassword, expectedHash)) { + info = Authentication.setPassword(newPassword); + String saltString = info.get(0); + String hashString = info.get(1); + login.get().setPasswordSalt(saltString); + login.get().setHash(hashString); + + save(login.get()); + return true; + } + } + } catch (IllegalArgumentException e) { + e.printStackTrace(); + } + return false; + } +} \ No newline at end of file diff --git a/src/main/java/NTNU/IDATT1002/utils/Authentication.java b/src/main/java/NTNU/IDATT1002/utils/Authentication.java index a0321ac9d2f4cc2ec99ae391811df2d76c9dacea..a43da0ed5974836bde590401e0b3100c4980879c 100644 --- a/src/main/java/NTNU/IDATT1002/utils/Authentication.java +++ b/src/main/java/NTNU/IDATT1002/utils/Authentication.java @@ -1,8 +1,11 @@ package NTNU.IDATT1002.utils; +import javax.xml.bind.DatatypeConverter; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.security.SecureRandom; +import java.util.ArrayList; +import java.util.Arrays; import java.util.Random; public class Authentication { @@ -15,74 +18,54 @@ public class Authentication { * @return hashed password * @throws NoSuchAlgorithmException */ - public static boolean setPassword(String username, String password) throws NoSuchAlgorithmException { + public static ArrayList<String> setPassword(String password) { String hashedPassword = null; - byte[] salt = getSalt(); + ArrayList<String> info = new ArrayList<>(); StringBuilder sb = new StringBuilder(); - if (username == null || password == null) { + if (password == null) { throw new IllegalArgumentException("Input cannot be null"); } if(password.isBlank() || password.isEmpty()) { throw new IllegalArgumentException("Password cannot be blank"); } - if (username.isEmpty() || username.isBlank()) { - throw new IllegalArgumentException("Username cannot be blank"); - } - - try{ - MessageDigest md = MessageDigest.getInstance("SHA-512"); - md.update(salt); - byte[] bytes = md.digest(password.getBytes()); - - //Converts the StringBuilder to hexadecimal - for(int i = 0; i < bytes.length; i++) { - sb.append((Integer.toString((bytes[i] & 0xff) + 0x100, 16).substring(1))); - } - //Gets the whole hash in hexformat into a string - hashedPassword = sb.toString(); - } - catch (NoSuchAlgorithmException e) { - e.printStackTrace(); - } - - //TODO: Need to make database connection here - return false; + return createHash(password); } /** - * Gets hash and salt from database with the username + * Gets hash and salt from database with the salt * Hashes input password with same algorithm and salt as when created * Compares the expected has and the new hash - * @param username to get the stored hash on give user + * @param salt to get the stored hash on give user * @param password that will be hashed and compared to original hash * @return boolean of whether the hashes are similiar or not */ - public static boolean isCorrectPassword(String username, String password) { - //TODO: get password salt and hash with username from db - - byte[] dbSalt = null; - String expectedHash = null; - - if(dbSalt == null) { + public static boolean isCorrectPassword(String salt, String password, String expectedHash) { + if(salt == null) { throw new IllegalArgumentException("Salt cannot be null"); } else if(expectedHash == null) { throw new IllegalArgumentException("Hash cannot be null"); } - else if(username.isEmpty() || password.isEmpty()) { + else if(password == null) { + throw new IllegalArgumentException("Password cannot be null"); + } + if(salt.isEmpty() || password.isEmpty()) { throw new IllegalArgumentException("Input cannot be empty"); } - if(password.isBlank() || username.isBlank()) { + if(password.isBlank() || salt.isBlank()) { throw new IllegalArgumentException("Password cannot be blank"); } + if(expectedHash.isBlank() || expectedHash.isEmpty()) { + throw new IllegalArgumentException("Hash from db is blank"); + } - String hashedInputPassword = createHashWithPredeterminedSalt(dbSalt, password); + byte[] dbSalt = buildBytes(salt); + String hashedInputPassword = createHashWithPredeterminedSalt(dbSalt, password); if(expectedHash.equals(hashedInputPassword)) { return true; } - return false; } @@ -120,10 +103,12 @@ public class Authentication { * @param password that wil be hashed * @return hashed password with salt */ - private static String createHash(String password) { + private static ArrayList<String> createHash(String password) { StringBuilder sb = new StringBuilder(); + ArrayList<String> info = new ArrayList<>(); String hashedPassword = null; byte[] salt = getSalt(); + String saltAsString = buildHexString(salt); try { MessageDigest md = MessageDigest.getInstance("SHA-512"); @@ -139,7 +124,9 @@ public class Authentication { catch (NoSuchAlgorithmException e ) { e.printStackTrace(); } - return hashedPassword; + info.add(saltAsString); + info.add(hashedPassword); + return info; } @@ -152,4 +139,32 @@ public class Authentication { r.nextBytes(salt); return salt; } + + /** + * Byte-to-Hex converter + * @param bytes is an array of byte + * @return String bytes in hex + */ + private static String buildHexString(byte[] bytes) { + StringBuilder sb = new StringBuilder(); + for (byte b : bytes) { + //Convert from byte to hex + sb.append(String.format("%02x", b)); + } + return sb.toString(); + } + + /** + * Hex-to-byte converter + * @param hex input to get an byte array + * @return byte[] converted from hex + */ + private static byte[] buildBytes(String hex) { + byte[] b = new byte[hex.length() / 2]; + for (int i = 0; i < hex.length(); i+=2) { + int v = Integer.parseInt(hex.substring(i, i + 2), 16); + b[i/2] = (byte) v; + } + return b; + } } diff --git a/src/main/resources/META-INF/persistence.xml b/src/main/resources/META-INF/persistence.xml index a63d089884ffaa3145eab2b7829c8b646140954d..9e30e6271f7c851771708ce80703c25d814c7711 100644 --- a/src/main/resources/META-INF/persistence.xml +++ b/src/main/resources/META-INF/persistence.xml @@ -9,6 +9,7 @@ <!-- Entity classes --> <class>NTNU.IDATT1002.models.User</class> + <class>NTNU.IDATT1002.models.Login</class> <class>NTNU.IDATT1002.models.Image</class> <class>NTNU.IDATT1002.models.ImageAlbum</class> <class>NTNU.IDATT1002.models.Tag</class> diff --git a/src/test/java/NTNU/IDATT1002/repository/ImageAlbumRepositoryTest.java b/src/test/java/NTNU/IDATT1002/repository/ImageAlbumRepositoryTest.java index f8ba07fbf0d0fe425270a3008b2a147931a4e57e..4a81096c026c980b3a947a3c75e6f771b2209a78 100644 --- a/src/test/java/NTNU/IDATT1002/repository/ImageAlbumRepositoryTest.java +++ b/src/test/java/NTNU/IDATT1002/repository/ImageAlbumRepositoryTest.java @@ -67,7 +67,6 @@ class ImageAlbumRepositoryTest { imageAlbumRepository.save(new ImageAlbum()); List<?> foundImageAlbums = imageAlbumRepository.findAll(); - assertEquals(2, foundImageAlbums.size()); } diff --git a/src/test/java/NTNU/IDATT1002/repository/LoginRepositoryTest.java b/src/test/java/NTNU/IDATT1002/repository/LoginRepositoryTest.java new file mode 100644 index 0000000000000000000000000000000000000000..6e01e3d5807dc857ea1821f911974d9a315ba9ef --- /dev/null +++ b/src/test/java/NTNU/IDATT1002/repository/LoginRepositoryTest.java @@ -0,0 +1,184 @@ +package NTNU.IDATT1002.repository; + +import NTNU.IDATT1002.models.Login; +import NTNU.IDATT1002.models.User; +import NTNU.IDATT1002.utils.Authentication; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import javax.persistence.EntityManager; +import javax.persistence.EntityManagerFactory; +import javax.persistence.Persistence; + +import java.lang.reflect.Array; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; +import java.util.Optional; + +import static org.junit.jupiter.api.Assertions.*; + +class LoginRepositoryTest { + + + private LoginRepository loginRepository; + + private String id1; + private String id2; + private String password; + private String newPassword; + private Date date; + private User user1; + private User user2; + private Login login1; + private Login login2; + + + + @BeforeEach + public void setUp() { + EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("ImageApplicationTest"); + EntityManager entityManager = entityManagerFactory.createEntityManager(); + + id1 = "test1"; + id2 = "test2"; + password = "Test123"; + newPassword = "Test321"; + date = new Date(System.currentTimeMillis()); + user1 = new User("epost", id1, "fornavn", "etternavn", "test" , "test", date); + user2 = new User("epost2" , id2, "fornavn2", "etternavn2", "test2", "test2", date); + login1 = new Login(user1, "test", "test"); + login2 = new Login(user2, "test2", "test2"); + loginRepository = new LoginRepository(entityManager); + } + + @Test + void testSaveReturnsInstance() { + Optional<Login> optionalLogin = loginRepository.save(login1); + assertTrue(optionalLogin.isPresent()); + } + + @Test + void testSaveReturnsAllSavedEntities() { + + loginRepository.save(login1); + loginRepository.save(login2); + + List<?> foundLogins = loginRepository.findAll(); + assertEquals(2, foundLogins.size()); + } + + @Test + void testSaveInvalidEntityReturnsEmptyOptional() { + Optional<Login> savedLogin = loginRepository.save(null); + + assertTrue(savedLogin.isEmpty()); + } + + @Test + void testFindByIdReturnsOptionalWithEntityWithId() { + + loginRepository.save(login1); + Optional<Login> foundLogins = loginRepository.findById(id1); + + assertTrue(foundLogins.isPresent()); + assertEquals(id1, foundLogins.get().getUser().getUsername()); + } + + @Test + void testDeleteById() { + loginRepository.save(login1); + Optional<Login> foundLogins = loginRepository.findById(id1); + + foundLogins.ifPresent(Login -> loginRepository.deleteById(id1)); + Optional<Login> deletedLogin = loginRepository.findById(id1); + + assertTrue(deletedLogin.isEmpty()); + } + + @Test + void testCountReturnsAmountOfSavedEntities() { + loginRepository.save(login1); + loginRepository.save(login2); + + long loginCount = loginRepository.count(); + + assertEquals(2, loginCount); + } + + @Test + void testLogin() { + ArrayList<String> credentials = Authentication.setPassword(password); + String salt = credentials.get(0); + String hash = credentials.get(1); + Login login3 = new Login(user1, salt, hash); + loginRepository.save(login3); + + assertTrue(loginRepository.logIn(id1, password)); + } + + @Test + void testChangePassword() { + ArrayList<String> credentials = Authentication.setPassword(password); + String salt = credentials.get(0); + String hash = credentials.get(1); + Login login3 = new Login(user1, salt, hash); + loginRepository.save(login3); + + assertTrue(loginRepository.changePassword(id1, password, newPassword)); + } + + @Test + void testLoginWithNewPassword() { + ArrayList<String> credentials = Authentication.setPassword(password); + String salt = credentials.get(0); + String hash = credentials.get(1); + Login login3 = new Login(user1, salt, hash); + loginRepository.save(login3); + + assertTrue(loginRepository.logIn(id1, password)); + assertTrue(loginRepository.changePassword(id1, password, newPassword)); + assertTrue(loginRepository.logIn(id1, newPassword)); + } + + @Test + void testWrongPasswordDoesNotLogIn() { + ArrayList<String> credentials = Authentication.setPassword(password); + String salt = credentials.get(0); + String hash = credentials.get(1); + Login login3 = new Login(user1, salt, hash); + loginRepository.save(login3); + assertFalse(loginRepository.logIn(id1, newPassword)); + } + + @Test + void testWrongPasswordDoesNotChangePassword() { + ArrayList<String> credentials = Authentication.setPassword(password); + String salt = credentials.get(0); + String hash = credentials.get(1); + Login login3 = new Login(user1, salt, hash); + loginRepository.save(login3); + assertFalse(loginRepository.changePassword(id1, newPassword, password)); + assertTrue(loginRepository.logIn(id1, password)); + } + + @Test + void testLoginWithNullReturnsFalse() { + ArrayList<String> credentials = Authentication.setPassword(password); + String salt = credentials.get(0); + String hash = credentials.get(1); + Login login3 = new Login(user1, salt, hash); + loginRepository.save(login3); + assertFalse(loginRepository.logIn(id1, null)); + } + + @Test + void testChangeWithNullReturnsFalse() { + ArrayList<String> credentials = Authentication.setPassword(password); + String salt = credentials.get(0); + String hash = credentials.get(1); + Login login3 = new Login(user1, salt, hash); + loginRepository.save(login3); + assertFalse(loginRepository.changePassword(id1, null, newPassword)); + } +} \ No newline at end of file diff --git a/src/test/java/NTNU/IDATT1002/utils/AuthenticationTest.java b/src/test/java/NTNU/IDATT1002/utils/AuthenticationTest.java index b4ed0d37b4b4ce42ac7598ba9b0af3dd10ca84a5..1be9561702d4664fae533dfd7709d287462068f2 100644 --- a/src/test/java/NTNU/IDATT1002/utils/AuthenticationTest.java +++ b/src/test/java/NTNU/IDATT1002/utils/AuthenticationTest.java @@ -2,75 +2,75 @@ package NTNU.IDATT1002.utils; import org.junit.jupiter.api.Test; +import java.nio.charset.Charset; import java.security.NoSuchAlgorithmException; +import java.util.ArrayList; import static org.junit.jupiter.api.Assertions.*; class AuthenticationTest { + /** + * Testing the method with null as input + */ @Test void testNullAsInputOnsetPassword() throws NoSuchAlgorithmException { - /** - * Testing the method with null as input - */ + try { - Authentication.setPassword(null, null); + Authentication.setPassword(null); fail("Expected action to throw IllegalArgumentException "); } catch (IllegalArgumentException e) { //Test is a Success } } + /** + * Testing the method with an empty password and an credible username + */ @Test void testBlankPasswordOnsetPassword() throws NoSuchAlgorithmException { - /** - * Testing the method with an empty password and an credible username - */ try { - Authentication.setPassword("Username", ""); + Authentication.setPassword(""); fail("Expected action to throw IllegalArgumentException "); } catch (IllegalArgumentException e) { //Test is a success as you are not supposed to get anything checked with empty password } } + /** + * Testing the method with an empty username and an credible username + */ @Test void testBlankUsernameOnsetPassword() throws NoSuchAlgorithmException { - /** - * Testing the method with an empty username and an credible username - */ try { - Authentication.setPassword("", "Password"); - fail("Expected action to throw IllegalArgumentException "); + Authentication.setPassword(""); + fail("Expected action to throw IllegalArgumentException "); } catch (IllegalArgumentException e) { //Test is a success as you are not supposed to get anything checked with empty password } } + /** + * Testing the method with an credible username and password + */ @Test void testCredibleInputOnsetPassword() throws NoSuchAlgorithmException { - /** - * TODO: Maybe add actual user credentials as input. Change the boolean - * Testing the method with an credible username and password - */ - try { - assertEquals(false, Authentication.setPassword("Username", "Password")); - } - catch (Exception e) { - fail("Method should not throw an exception with this input"); - } + String password = "Test123"; + ArrayList<String> credentials = Authentication.setPassword(password); + String salt = credentials.get(0); + String hash = credentials.get(1); + assertTrue(Authentication.isCorrectPassword(salt, password, hash)); } - + /** + * Testing the method with null ass input + */ @Test void testNullAsInputOnisCorrectPassword() { - /** - * Testing the method with null ass input - */ try { - Authentication.isCorrectPassword(null, null); + Authentication.isCorrectPassword(null, null, null); fail("Expected action to throw IllegalArgumentException "); } catch (IllegalArgumentException e) { @@ -78,26 +78,27 @@ class AuthenticationTest { } } + /** + * Testing the method with an empty password and an credible username + */ @Test void testBlankPasswordOnisCorrectPassword() throws NoSuchAlgorithmException { - /** - * Testing the method with an empty password and an credible username - */ try { - Authentication.isCorrectPassword("Username", ""); + Authentication.isCorrectPassword("Username", "", ""); fail("Expected action to throw IllegalArgumentException "); } catch (IllegalArgumentException e) { //Test is a success as you are not supposed to get anything checked with empty password } } + + /** + * Testing the method with an empty username and an credible username + */ @Test void testBlankUsernameOnisCorrectPassword() throws NoSuchAlgorithmException { - /** - * Testing the method with an empty username and an credible username - */ try { - Authentication.isCorrectPassword("", "Password"); + Authentication.isCorrectPassword("", "Password", ""); fail("Expected action to throw IllegalArgumentException "); } catch (IllegalArgumentException e) { @@ -105,19 +106,16 @@ class AuthenticationTest { } } + + /** + * Testing the method with an credible username and password + */ @Test void testCredibleInputOnisCorrectPassword() throws NoSuchAlgorithmException { - /** - * TODO: Maybe add actual user credentials as input. Change the boolean - * Testing the method with an credible username and password - */ - try { - assertEquals(false, Authentication.isCorrectPassword("Username", "Password")); - fail("The salt is set as null automatically. The test should therefore throw IllegalArgumentException"); - } - catch (Exception e) { - //Salt is set to null since it is not connected to database and should therefore throw exception - } + ArrayList<String> credentials = Authentication.setPassword("test"); + String saltAsString = credentials.get(0); + String hash = credentials.get(1); + assertTrue(Authentication.isCorrectPassword(saltAsString, "test", hash)); } } diff --git a/src/test/resources/META-INF/persistence.xml b/src/test/resources/META-INF/persistence.xml index 5af16a1cdbdc93f107d15dd5c00bec66cd883601..301f6533893083696667bc99108580932415da8f 100644 --- a/src/test/resources/META-INF/persistence.xml +++ b/src/test/resources/META-INF/persistence.xml @@ -9,6 +9,7 @@ <!-- Entity classes --> <class>NTNU.IDATT1002.models.User</class> + <class>NTNU.IDATT1002.models.Login</class> <class>NTNU.IDATT1002.models.Image</class> <class>NTNU.IDATT1002.models.ImageAlbum</class>