Skip to content
Snippets Groups Projects
Commit c9432c92 authored by Mads Lundegaard's avatar Mads Lundegaard
Browse files

Merge branch 'fix/login' into 'dev'

Fix/login

See merge request !50
parents b9ce0af8 9e5f09a4
No related branches found
No related tags found
2 merge requests!104Weekly merge to Master,!50Fix/login
Pipeline #76464 passed
......@@ -6,7 +6,7 @@ import NTNU.IDATT1002.models.User;
/**
* Class Application State. Keeps a record of the global application state, such as the current logged in user.
*/
final class ApplicationState {
public final class ApplicationState {
/**
* The current logged in user.
......
package NTNU.IDATT1002.repository;
import NTNU.IDATT1002.ApplicationState;
import NTNU.IDATT1002.models.ImageAlbum;
import NTNU.IDATT1002.models.Login;
import NTNU.IDATT1002.models.User;
import NTNU.IDATT1002.utils.Authentication;
......@@ -12,16 +14,38 @@ import java.util.Date;
import java.util.List;
import java.util.Optional;
/**
* Login Repository
*
* Implements {@link Repository} whick supports CRUD operations.
*
* @author madslun
* @version 1.0 22.03.20
* @see NTNU.IDATT1002.repository.Repository
*/
public class LoginRepository extends GenericRepository<Login, String>{
private EntityManager entityManager;
/**
* Constructor to inject {@link EntityManager} dependency and sets the class type to {@link Login}
*
* @param entityManager the entity manager to utilize
*/
public LoginRepository(EntityManager entityManager) {
super(entityManager);
setClassType(Login.class);
}
/**
* Logs a user into the sytem
*
* @param username of user that should login
* @param password of the user that should login
* @return boolean of the operation
*/
public boolean logIn(String username, String password) {
try {
Optional<Login> login = findById(username);
......@@ -37,6 +61,15 @@ public class LoginRepository extends GenericRepository<Login, String>{
return false;
}
/**
* Checks the old password on the user and changes to the new password if the old is correct
*
* @param username of the user that want to change password
* @param oldPassword that will be checked to the database
* @param newPassword that will be changed to
* @return boolean of the operation
*/
public boolean changePassword(String username, String oldPassword, String newPassword) {
ArrayList<String> info = new ArrayList<>();
try {
......
......@@ -18,6 +18,13 @@ import java.util.Optional;
import static org.junit.jupiter.api.Assertions.*;
/**
* Tests for {@link LoginRepository}
*
* @author madslun
* @version 1.0 17.03.20
*/
class LoginRepositoryTest {
......@@ -34,7 +41,10 @@ class LoginRepositoryTest {
private Login login2;
/**
* Sets up some testdata for thorough testing
* So much information has been added for making sure every part works as intended
*/
@BeforeEach
public void setUp() {
EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("ImageApplicationTest");
......@@ -52,12 +62,18 @@ class LoginRepositoryTest {
loginRepository = new LoginRepository(entityManager);
}
/**
* Test that saving an entity returns the saved instance
*/
@Test
void testSaveReturnsInstance() {
Optional<Login> optionalLogin = loginRepository.save(login1);
assertTrue(optionalLogin.isPresent());
}
/**
* Test that finding all entities returns all entities
*/
@Test
void testSaveReturnsAllSavedEntities() {
......@@ -68,6 +84,9 @@ class LoginRepositoryTest {
assertEquals(2, foundLogins.size());
}
/**
* Test that saving invalid entity will fail and return empty optional
*/
@Test
void testSaveInvalidEntityReturnsEmptyOptional() {
Optional<Login> savedLogin = loginRepository.save(null);
......@@ -75,6 +94,9 @@ class LoginRepositoryTest {
assertTrue(savedLogin.isEmpty());
}
/**
* Test that finding entity by id returns optional with the correct id
*/
@Test
void testFindByIdReturnsOptionalWithEntityWithId() {
......@@ -85,6 +107,9 @@ class LoginRepositoryTest {
assertEquals(id1, foundLogins.get().getUser().getUsername());
}
/**
* Test that deleting by id removes the given entity and returns empty optional
*/
@Test
void testDeleteById() {
loginRepository.save(login1);
......@@ -96,6 +121,9 @@ class LoginRepositoryTest {
assertTrue(deletedLogin.isEmpty());
}
/**
* Test that count returns correct amount of enities
*/
@Test
void testCountReturnsAmountOfSavedEntities() {
loginRepository.save(login1);
......@@ -106,6 +134,9 @@ class LoginRepositoryTest {
assertEquals(2, loginCount);
}
/**
* Test that a created user can log in
*/
@Test
void testLogin() {
ArrayList<String> credentials = Authentication.setPassword(password);
......@@ -117,6 +148,9 @@ class LoginRepositoryTest {
assertTrue(loginRepository.logIn(id1, password));
}
/**
* Test that a created user can change the password
*/
@Test
void testChangePassword() {
ArrayList<String> credentials = Authentication.setPassword(password);
......@@ -128,6 +162,9 @@ class LoginRepositoryTest {
assertTrue(loginRepository.changePassword(id1, password, newPassword));
}
/**
* Test that a user can log in, change password then log in again
*/
@Test
void testLoginWithNewPassword() {
ArrayList<String> credentials = Authentication.setPassword(password);
......@@ -141,6 +178,9 @@ class LoginRepositoryTest {
assertTrue(loginRepository.logIn(id1, newPassword));
}
/**
* Test that trying to login with wrong password returns false
*/
@Test
void testWrongPasswordDoesNotLogIn() {
ArrayList<String> credentials = Authentication.setPassword(password);
......@@ -151,6 +191,9 @@ class LoginRepositoryTest {
assertFalse(loginRepository.logIn(id1, newPassword));
}
/**
* Test that trying to change password with wrong password returns false
*/
@Test
void testWrongPasswordDoesNotChangePassword() {
ArrayList<String> credentials = Authentication.setPassword(password);
......@@ -162,6 +205,9 @@ class LoginRepositoryTest {
assertTrue(loginRepository.logIn(id1, password));
}
/**
* Test that trying to login with null returns false
*/
@Test
void testLoginWithNullReturnsFalse() {
ArrayList<String> credentials = Authentication.setPassword(password);
......@@ -172,6 +218,9 @@ class LoginRepositoryTest {
assertFalse(loginRepository.logIn(id1, null));
}
/**
* Test that trying to login with null returns false
*/
@Test
void testChangeWithNullReturnsFalse() {
ArrayList<String> credentials = Authentication.setPassword(password);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment