Skip to content
Snippets Groups Projects
Commit b86549ba authored by Eirik Steira's avatar Eirik Steira
Browse files

Merge branch 'remove_serviceTest' into 'dev'

Remove service test

See merge request !72
parents f6523c6a 23ac8d70
No related branches found
No related tags found
2 merge requests!104Weekly merge to Master,!72Remove service test
......@@ -3,8 +3,13 @@ package NTNU.IDATT1002.controllers;
import java.io.IOException;
import NTNU.IDATT1002.App;
import NTNU.IDATT1002.ApplicationState;
import NTNU.IDATT1002.models.User;
import NTNU.IDATT1002.service.UserService;
import javafx.event.ActionEvent;
import javafx.scene.control.Button;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
/**
* Controls the buttons and changeable elements on login.fxml,
......@@ -13,8 +18,12 @@ import javafx.scene.control.Button;
*/
public class Login {
public TextField Username;
public PasswordField Password;
public Button signup;
public Button login;
private UserService userService;
/**
* Method that changes scene to Sign Up page
......@@ -31,7 +40,12 @@ public class Login {
* @throws IOException
*/
public void login(ActionEvent actionEvent) throws IOException {
//TODO: Verify username and password
App.setRoot("main");
userService = new UserService();
String username = Username.getText();
String password = Password.getText();
if(userService.logIn(username, password)); {
App.setRoot("main");
}
//TODO: Else raise warning maybe?
}
}
package NTNU.IDATT1002.controllers;
import java.io.IOException;
import java.util.Date;
import java.util.Optional;
import NTNU.IDATT1002.App;
import NTNU.IDATT1002.models.User;
import NTNU.IDATT1002.service.UserService;
import javafx.event.ActionEvent;
import javafx.scene.control.Button;
import javafx.scene.control.DatePicker;
......@@ -26,6 +30,7 @@ public class SignUp {
public TextField signup_phoneCode;
public TextField signup_phoneNr;
public DatePicker signup_birthDate;
public UserService userService = new UserService();
public Button signup_btn;
......@@ -35,8 +40,22 @@ public class SignUp {
* @throws IOException
*/
public void signup(ActionEvent actionEvent) throws IOException {
//TODO: Verify that all fields is properly filled
//TODO: Register new user in database
App.setRoot("login");
String username = signup_username.getText();
String firstName = signup_firstName.getText();
String lastName = signup_lastName.getText();
String email = signup_email.getText();
String password = signup_password.getText();
String phoneCode = signup_phoneCode.getText();
String phoneNr = signup_phoneNr.getText();
//TODO: Find out how to take date as a Date object
Date date = new Date(System.currentTimeMillis());
if(userService.createUser(email, username, firstName, lastName, phoneCode, phoneNr, date, password).isPresent()) {
//TODO: Return message to user to confirm that user has been succsessfully registered
App.setRoot("login");
}
//TODO: Verify that all fields is properly filled. I think this will be done by Usermodel.
}
}
\ No newline at end of file
}
......@@ -39,6 +39,10 @@ public class Login {
return user;
}
public void setUser(User user) {
this.user = user;
}
public void setHash(String hash) {
this.hash = hash;
}
......
......@@ -38,87 +38,4 @@ public class LoginRepository extends GenericRepository<Login, String>{
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);
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;
}
/**
* 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 {
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;
}
/**
* Method for setting the password on the user for the first time
*
* @param username of the user that will have the password set
* @param password password that will be set
* @return
*/
public boolean setPassword(String username, String password) {
ArrayList<String> info = new ArrayList<>();
try {
Optional<Login> login = findById(username);
if(login.isPresent()) {
info = Authentication.setPassword(password);
String saltString = info.get(0);
String hastString = info.get(1);
login.get().setPasswordSalt(saltString);
login.get().setHash(hastString);
save(login.get());
return true;
}
}
catch (IllegalArgumentException e) {
e.printStackTrace();
}
return false;
}
}
\ No newline at end of file
package NTNU.IDATT1002.service;
import NTNU.IDATT1002.App;
import NTNU.IDATT1002.ApplicationState;
import NTNU.IDATT1002.models.Login;
import NTNU.IDATT1002.models.User;
import NTNU.IDATT1002.repository.LoginRepository;
import NTNU.IDATT1002.repository.UserRepository;
import NTNU.IDATT1002.utils.Authentication;
import org.apache.log4j.helpers.AbsoluteTimeDateFormat;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import java.util.ArrayList;
import java.util.Date;
import java.util.Optional;
......@@ -48,12 +52,9 @@ public class UserService {
* @return Optional with the user
*/
public Optional<User> createUser(String email, String username, String firstName, String lastName, String callingCode, String phoneNumber, Date birthDate, String password) {
User user = new User(email, username, firstName, lastName, callingCode, phoneNumber, birthDate);
User user = new User(username, email, firstName, lastName, callingCode, phoneNumber, birthDate);
Login login = new Login(user, "", "");
userRepository.save(user);
loginRepository.save(login);
loginRepository.setPassword(username, password);
setPassword(login, password);
return userRepository.save(user);
}
......@@ -65,10 +66,19 @@ public class UserService {
* @return
*/
public boolean logIn(String username, String password) {
if(loginRepository.logIn(username, password)) {
User user = loginRepository.findById(username).get().getUser();
ApplicationState.setCurrentUser(user);
return true;
try {
Optional<Login> login = loginRepository.findById(username);
if (login.isPresent()) {
String salt = login.get().getPasswordSalt();
String hash = login.get().getHash();
if(Authentication.isCorrectPassword(salt, password, hash)) {
ApplicationState.setCurrentUser(login.get().getUser());
return true;
}
}
}
catch (IllegalArgumentException e) {
e.printStackTrace();
}
return false;
}
......@@ -81,7 +91,46 @@ public class UserService {
* @param newPassword that will be set
* @return
*/
public boolean changePassword(String username, String oldPassword, String newPassword) {
return loginRepository.changePassword(username, oldPassword, newPassword);
boolean changePassword(String username, String oldPassword, String newPassword) {
ArrayList<String> info = new ArrayList<>();
try {
Optional<Login> login = loginRepository.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);
loginRepository.save(login.get());
return true;
}
}
}
catch (IllegalArgumentException e) {
e.printStackTrace();
}
return false;
}
private boolean setPassword(Login login, String password) {
ArrayList<String> info = new ArrayList<>();
try {
info = Authentication.setPassword(password);
String saltString = info.get(0);
String hashString = info.get(1);
login.setPasswordSalt(saltString);
login.setHash(hashString);
if(loginRepository.save(login).isPresent()) {
return true;
}
}
catch (IllegalArgumentException e) {
e.printStackTrace();
}
return false;
}
}
src/main/resources/Images/Sequences/Login.png

26.4 KiB

src/main/resources/Images/Sequences/SignUp.png

26.8 KiB

......@@ -27,8 +27,8 @@
<children>
<Text strokeType="OUTSIDE" strokeWidth="0.0" text="Username:" GridPane.halignment="RIGHT" />
<Text strokeType="OUTSIDE" strokeWidth="0.0" text="Password:" GridPane.halignment="RIGHT" GridPane.rowIndex="1" />
<TextField GridPane.columnIndex="1" />
<PasswordField GridPane.columnIndex="1" GridPane.rowIndex="1" />
<TextField fx:id="Username" GridPane.columnIndex="1" />
<PasswordField fx:id="Password" GridPane.columnIndex="1" GridPane.rowIndex="1" />
<Button fx:id="signup" onAction="#switchToSignup" text="Sign Up" GridPane.columnIndex="1" GridPane.halignment="RIGHT" GridPane.rowIndex="2" />
<Button fx:id="login" onAction="#login" layoutX="171.0" layoutY="74.0" text="Log In" GridPane.columnIndex="1" GridPane.halignment="LEFT" GridPane.rowIndex="2" />
</children>
......
package NTNU.IDATT1002.repository;
import NTNU.IDATT1002.models.Login;
......@@ -16,6 +17,7 @@ import java.util.Optional;
import static org.junit.jupiter.api.Assertions.*;
/**
* Tests for {@link LoginRepository}
*
......@@ -23,6 +25,7 @@ import static org.junit.jupiter.api.Assertions.*;
* @version 1.0 17.03.20
*/
class LoginRepositoryTest {
......@@ -39,10 +42,12 @@ 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");
......@@ -60,18 +65,22 @@ class LoginRepositoryTest {
login2 = new Login(user2, "test2", "test2");
}
/**
/**
* 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() {
......@@ -82,9 +91,11 @@ 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);
......@@ -92,9 +103,11 @@ class LoginRepositoryTest {
assertTrue(savedLogin.isEmpty());
}
/**
/**
* Test that finding entity by id returns optional with the correct id
*/
@Test
void testFindByIdReturnsOptionalWithEntityWithId() {
......@@ -105,7 +118,7 @@ class LoginRepositoryTest {
assertEquals(username1, foundLogins.get().getUser().getUsername());
}
/**
/**
* Test that deleting by id removes the given entity and returns empty optional
*/
@Test
......@@ -119,9 +132,10 @@ class LoginRepositoryTest {
assertTrue(deletedLogin.isEmpty());
}
/**
/**
* Test that count returns correct amount of enities
*/
@Test
void testCountReturnsAmountOfSavedEntities() {
loginRepository.save(login1);
......@@ -131,101 +145,4 @@ class LoginRepositoryTest {
assertEquals(2, loginCount);
}
/**
* Test that a created user can log in
*/
@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(username1, password));
}
/**
* Test that a created user can change the 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(username1, password, newPassword));
}
/**
* Test that a user can log in, change password then log in again
*/
@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(username1, password));
assertTrue(loginRepository.changePassword(username1, password, newPassword));
assertTrue(loginRepository.logIn(username1, newPassword));
}
/**
* Test that trying to login with wrong password returns false
*/
@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(username1, newPassword));
}
/**
* Test that trying to change password with wrong password returns false
*/
@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(username1, newPassword, password));
assertTrue(loginRepository.logIn(username1, password));
}
/**
* Test that trying to login with null returns false
*/
@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(username1, null));
}
/**
* Test that trying to login with null returns false
*/
@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(username1, null, newPassword));
}
}
\ No newline at end of file
}
package NTNU.IDATT1002.service;
import NTNU.IDATT1002.models.User;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import javax.validation.constraints.AssertTrue;
import java.lang.management.OperatingSystemMXBean;
import java.util.Date;
import java.util.Optional;
import static org.junit.jupiter.api.Assertions.*;
class UserServiceTest {
private UserService userService;
private String email;
private String username;
private String firstName;
private String lastName;
private String phoneNumber;
private String callingCode;
private String password;
private Date date;
@BeforeEach
void setUp() {
userService = new UserService();
email = "email";
username = "test";
firstName = "test";
lastName = "testesen";
phoneNumber = "12345678";
callingCode = "+47";
password = "Test123";
date = new Date(System.currentTimeMillis());
}
@Test
void testCreateuser() {
Optional<User> user = userService.createUser(email, username, firstName, lastName, callingCode, phoneNumber, date, password);
assertTrue(user.isPresent());
assertEquals(username, user.get().getUsername());
}
@Test
void testChangePassword() {
String newPassword = "Test321";
Optional<User> user = userService.createUser(email, username, firstName, lastName, callingCode, phoneNumber, date, password);
assertTrue(user.isPresent());
assertTrue(userService.changePassword(username, password, newPassword));
}
@Test
void testLogIn() {
Optional<User> user = userService.createUser(email, username, firstName, lastName, callingCode, phoneNumber, date, password);
assertTrue(user.isPresent());
assertTrue(userService.logIn(username, password));
}
}
\ No newline at end of file
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