Browse Source

Merge branch 'newDevBranch'

remotes/origin/hotfix/client/improve-chat-window-appearence
Richard Schmidt 11 months ago
parent
commit
5d57a887f6
  1. 70
      src/main/java/CreateUser.java
  2. 57
      src/main/java/LoginGUI.java
  3. 18
      src/main/java/SignUpGUI.java
  4. 58
      src/test/java/CreateUserTest.java
  5. 19
      src/test/java/LoginGUITest.java
  6. 31
      src/test/java/SignUpGUITest.java

70
src/main/java/CreateUser.java

@ -27,70 +27,70 @@ class CreateUser {
// Constructor // Constructor
public CreateUser(String id, String name, String password, String birthday, String firstName, String surname) { public CreateUser(String id, String name, String password, String birthday, String firstName, String surname) {
this.id = id;
this.id = id;
this.userName = name; this.userName = name;
this.password = hashPassword(password);
this.birthday = birthday;
this.firstName = firstName;
this.surname = surname;
this.password = hashPassword(password);
this.birthday = birthday;
this.firstName = firstName;
this.surname = surname;
} }
// Getters and Setters // Getters and Setters
public String getId() { public String getId() {
return id; return id;
}
}
public void setId(String id) {
public void setId(String id) {
this.id = id; this.id = id;
}
}
public String getUserName() { public String getUserName() {
return userName;
return userName;
} }
public void setUserName(String userName) { public void setUserName(String userName) {
this.userName = userName;
this.userName = userName;
} }
public String getPassword() { public String getPassword() {
return password;
return password;
} }
public void setPassword(String password) { public void setPassword(String password) {
this.password = password;
this.password = password;
} }
public String getBirthday() { public String getBirthday() {
return birthday; return birthday;
} }
public void setBirthday(String birthday) {
public void setBirthday(String birthday) {
this.birthday = birthday; this.birthday = birthday;
}
}
public String getFirstName() {
return firstName;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String surname() {
return surname;
}
public String surname() {
return surname;
}
public void setSurname(String surname) {
this.surname = surname;
}
public void setSurname(String surname) {
this.surname = surname;
}
public boolean isStayLoggedIn() {
public boolean isStayLoggedIn() {
return stayLoggedIn; return stayLoggedIn;
}
}
public void setStayLoggedIn(boolean stayLoggedIn) {
public void setStayLoggedIn(boolean stayLoggedIn) {
this.stayLoggedIn = stayLoggedIn; this.stayLoggedIn = stayLoggedIn;
}
}
// Function to create user with validation // Function to create user with validation
public static CreateUser createUser(String id, String userName, String password, String birthday, String firstName, String surname) { public static CreateUser createUser(String id, String userName, String password, String birthday, String firstName, String surname) {
@ -105,7 +105,7 @@ class CreateUser {
} return new CreateUser(id, userName, password, birthday, firstName, surname); } return new CreateUser(id, userName, password, birthday, firstName, surname);
} }
// Function to hash the password using SHA-256 algorithm
// Function to hash the password using SHA-256 algorithm
private String hashPassword(String password) { private String hashPassword(String password) {
try { try {
MessageDigest digest = MessageDigest.getInstance("SHA-256"); MessageDigest digest = MessageDigest.getInstance("SHA-256");
@ -123,7 +123,7 @@ class CreateUser {
} }
} }
// Function to save to JSON file, replace with database call later
// Function to save to JSON file, replace with database call later
public void saveToJsonFile(String filename) { public void saveToJsonFile(String filename) {
List<CreateUser> userList = readUserListFromJsonFile(filename); List<CreateUser> userList = readUserListFromJsonFile(filename);
userList.add(this); userList.add(this);
@ -133,11 +133,11 @@ class CreateUser {
gson.toJson(userList, fileWriter); gson.toJson(userList, fileWriter);
System.out.println("User information appended to " + filename); System.out.println("User information appended to " + filename);
} catch (IOException e) { } catch (IOException e) {
System.out.println("Error occurred while saving user information to JSON file: " + e.getMessage());
System.out.println("Error occurred while saving user information to JSON file: " + e.getMessage());
} }
} }
// Function to read user information from a JSON file
// Function to read user information from a JSON file
public static List<CreateUser> readUserListFromJsonFile(String filename) { public static List<CreateUser> readUserListFromJsonFile(String filename) {
List<CreateUser> userList = new ArrayList<>(); List<CreateUser> userList = new ArrayList<>();
try (Reader reader = new FileReader(filename)) { try (Reader reader = new FileReader(filename)) {
@ -152,7 +152,7 @@ class CreateUser {
return userList; return userList;
} }
// Function to update stayLoggedIn variable in the JSON file
// Function to update stayLoggedIn variable in the JSON file
public static void updateStayLoggedIn(String filename, String username, boolean stayLoggedIn) { public static void updateStayLoggedIn(String filename, String username, boolean stayLoggedIn) {
List<CreateUser> userList = readUserListFromJsonFile(filename); List<CreateUser> userList = readUserListFromJsonFile(filename);

57
src/main/java/LoginGUI.java

@ -35,7 +35,6 @@ public class LoginGUI extends JFrame implements ActionListener {
return signUpButton; return signUpButton;
} }
public LoginGUI() { public LoginGUI() {
setTitle("Login"); setTitle("Login");
setSize(300, 220); setSize(300, 220);
@ -81,9 +80,9 @@ public class LoginGUI extends JFrame implements ActionListener {
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
boolean stayLoggedIn = stayLoggedInCheckbox.isSelected(); boolean stayLoggedIn = stayLoggedInCheckbox.isSelected();
String username = usernameField.getText(); String username = usernameField.getText();
// Set stayLoggedIn to false if the checkbox is unchecked
// Set stayLoggedIn to false if the checkbox is unchecked
if (!stayLoggedInCheckbox.isSelected()) { if (!stayLoggedInCheckbox.isSelected()) {
stayLoggedIn = false;
stayLoggedIn = false;
} }
updateStayLoggedIn(username, stayLoggedIn); updateStayLoggedIn(username, stayLoggedIn);
} }
@ -94,63 +93,57 @@ public class LoginGUI extends JFrame implements ActionListener {
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
if (e.getSource() == loginButton) { if (e.getSource() == loginButton) {
login();
login();
}else if (e.getSource() == signUpButton) { }else if (e.getSource() == signUpButton) {
SignUpGUI signUpGUI = new SignUpGUI(); SignUpGUI signUpGUI = new SignUpGUI();
signUpGUI.setVisible(true); signUpGUI.setVisible(true);
} }
} }
private void login() { private void login() {
String username = usernameField.getText(); String username = usernameField.getText();
String password = new String(passwordField.getPassword()); String password = new String(passwordField.getPassword());
boolean stayLoggedIn = stayLoggedInCheckbox.isSelected(); // Get checkbox state
if (authenticateUser(username, password)) { if (authenticateUser(username, password)) {
JOptionPane.showMessageDialog(this, "Login successful!");
// Perform actions after successful login
JOptionPane.showMessageDialog(this, "Login successful!");
dispose(); dispose();
} else { } else {
JOptionPane.showMessageDialog(this, "Invalid username or password", "Login Error", JOptionPane.ERROR_MESSAGE); JOptionPane.showMessageDialog(this, "Invalid username or password", "Login Error", JOptionPane.ERROR_MESSAGE);
} }
} }
private void updateStayLoggedIn(String username, boolean stayLoggedIn) {
// Update stayLoggedIn in the JSON file for the user // Update stayLoggedIn in the JSON file for the user
CreateUser.updateStayLoggedIn("user.json", username, stayLoggedIn);
private void updateStayLoggedIn(String username, boolean stayLoggedIn) {
CreateUser.updateStayLoggedIn("user.json", username, stayLoggedIn);
} }
// Function to authenticate the user by comparing the entered username and password with the saved user data
// Function to authenticate the user by comparing the entered username and password with the saved user data
private boolean authenticateUser(String username, String password) { private boolean authenticateUser(String username, String password) {
List<CreateUser> userList = CreateUser.readUserListFromJsonFile("user.json"); List<CreateUser> userList = CreateUser.readUserListFromJsonFile("user.json");
if (userList != null) { if (userList != null) {
for (CreateUser user : userList) { for (CreateUser user : userList) {
if (user.getUserName().equals(username)) { if (user.getUserName().equals(username)) {
// Hash the user input password
String hashedPassword = hashPassword(password);
// Compare the hashed passwords
if (user.getPassword().equals(hashedPassword)) {
return true; // Success
}
}
}
String hashedPassword = hashPassword(password);
if (user.getPassword().equals(hashedPassword)) {
return true;
}
}
}
} }
return false; // Fail
return false;
} }
private String hashPassword(String password) {
try {
MessageDigest digest = MessageDigest.getInstance("SHA-256");
byte[] hash = digest.digest(password.getBytes());
StringBuilder hexString = new StringBuilder();
for (byte b : hash) {
String hex = Integer.toHexString(0xff & b);
if (hex.length() == 1) {
hexString.append('0');
}
hexString.append(hex);
private String hashPassword(String password) {
try {
MessageDigest digest = MessageDigest.getInstance("SHA-256");
byte[] hash = digest.digest(password.getBytes());
StringBuilder hexString = new StringBuilder();
for (byte b : hash) {
String hex = Integer.toHexString(0xff & b);
if (hex.length() == 1) {
hexString.append('0');
}
hexString.append(hex);
} }
return hexString.toString(); return hexString.toString();
} catch (NoSuchAlgorithmException e) { } catch (NoSuchAlgorithmException e) {

18
src/main/java/SignUpGUI.java

@ -36,7 +36,7 @@ public class SignUpGUI extends JFrame implements ActionListener {
public SignUpGUI() { public SignUpGUI() {
setTitle("Sign Up"); setTitle("Sign Up");
setSize(400, 300); // Adjusted size for accommodating more fields
setSize(400, 300);
setDefaultCloseOperation(EXIT_ON_CLOSE); setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(null); setLayout(null);
@ -64,7 +64,7 @@ public class SignUpGUI extends JFrame implements ActionListener {
confirmPasswordField.setBounds(140, 80, 160, 25); confirmPasswordField.setBounds(140, 80, 160, 25);
add(confirmPasswordField); add(confirmPasswordField);
JLabel firstNameLabel = new JLabel("First Name:"); // New field for first name
JLabel firstNameLabel = new JLabel("First Name:");
firstNameLabel.setBounds(20, 110, 80, 25); firstNameLabel.setBounds(20, 110, 80, 25);
add(firstNameLabel); add(firstNameLabel);
@ -72,7 +72,7 @@ public class SignUpGUI extends JFrame implements ActionListener {
firstNameField.setBounds(140, 110, 160, 25); firstNameField.setBounds(140, 110, 160, 25);
add(firstNameField); add(firstNameField);
JLabel surnameLabel = new JLabel("Surname:"); // New field for surname
JLabel surnameLabel = new JLabel("Surname:");
surnameLabel.setBounds(20, 140, 80, 25); surnameLabel.setBounds(20, 140, 80, 25);
add(surnameLabel); add(surnameLabel);
@ -89,7 +89,7 @@ public class SignUpGUI extends JFrame implements ActionListener {
add(birthdayField); add(birthdayField);
signUpButton = new JButton("Sign Up"); signUpButton = new JButton("Sign Up");
signUpButton.setBounds(140, 210, 100, 25); // Adjusted position
signUpButton.setBounds(140, 210, 100, 25);
signUpButton.addActionListener(this); signUpButton.addActionListener(this);
add(signUpButton); add(signUpButton);
} }
@ -97,7 +97,7 @@ public class SignUpGUI extends JFrame implements ActionListener {
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
if (e.getSource() == signUpButton) { if (e.getSource() == signUpButton) {
String username = usernameField.getText();
String username = usernameField.getText();
String password = passwordField.getText(); String password = passwordField.getText();
String confirmPassword = confirmPasswordField.getText(); String confirmPassword = confirmPasswordField.getText();
String birthday = birthdayField.getText(); String birthday = birthdayField.getText();
@ -113,6 +113,7 @@ public class SignUpGUI extends JFrame implements ActionListener {
JOptionPane.showMessageDialog(this, "Username already exists!", "Sign Up Error", JOptionPane.ERROR_MESSAGE); JOptionPane.showMessageDialog(this, "Username already exists!", "Sign Up Error", JOptionPane.ERROR_MESSAGE);
return; return;
} }
try { try {
UUID randomUUID = UUID.randomUUID(); UUID randomUUID = UUID.randomUUID();
CreateUser user = CreateUser.createUser(randomUUID.toString(), username, password, birthday, firstName, surname); CreateUser user = CreateUser.createUser(randomUUID.toString(), username, password, birthday, firstName, surname);
@ -124,17 +125,18 @@ public class SignUpGUI extends JFrame implements ActionListener {
} }
} }
} }
// Function to check if the input username doesn't already exist in the JSON file
// Function to check if the input username doesn't already exist in the JSON file
private boolean isUsernameAvailable(String filename, String username) { private boolean isUsernameAvailable(String filename, String username) {
List<CreateUser> userList = CreateUser.readUserListFromJsonFile(filename); List<CreateUser> userList = CreateUser.readUserListFromJsonFile(filename);
if (userList != null) { if (userList != null) {
for (CreateUser user : userList) { for (CreateUser user : userList) {
if (user.getUserName().equals(username)) { if (user.getUserName().equals(username)) {
return false; // Username already exists
return false;
} }
} }
} }
return true; // Username is available
return true;
} }
public static void main(String[] args) { public static void main(String[] args) {

58
src/test/java/CreateUserTest.java

@ -8,23 +8,22 @@ class CreateUserTest {
@Test @Test
public void testCreateUser() { public void testCreateUser() {
String id = "1";
String userName = "testUser";
String password = "password123";
String birthday = "2000-01-01";
String firstName = "John";
String surname = "Doe";
CreateUser user = new CreateUser(id, userName, password, birthday, firstName, surname);
assertEquals(id, user.getId());
assertEquals(userName, user.getUserName());
assertEquals(birthday, user.getBirthday());
assertEquals(firstName, user.getFirstName());
assertEquals(surname, user.surname());
// Password should be hashed
assertNotEquals(password, user.getPassword());
String id = "1";
String userName = "testUser";
String password = "password123";
String birthday = "2000-01-01";
String firstName = "John";
String surname = "Doe";
CreateUser user = new CreateUser(id, userName, password, birthday, firstName, surname);
assertEquals(id, user.getId());
assertEquals(userName, user.getUserName());
assertEquals(birthday, user.getBirthday());
assertEquals(firstName, user.getFirstName());
assertEquals(surname, user.surname());
assertNotEquals(password, user.getPassword());
} }
@ -38,16 +37,12 @@ class CreateUserTest {
String firstName = "Jane"; String firstName = "Jane";
String surname = "Doe"; String surname = "Doe";
// Create a user
CreateUser user = new CreateUser(id, userName, password, birthday, firstName, surname); CreateUser user = new CreateUser(id, userName, password, birthday, firstName, surname);
// Save user to JSON file
user.saveToJsonFile(filename); user.saveToJsonFile(filename);
// Read users from JSON file
List<CreateUser> userList = CreateUser.readUserListFromJsonFile(filename); List<CreateUser> userList = CreateUser.readUserListFromJsonFile(filename);
// Ensure that the user has been saved and can be read correctly
assertEquals(1, userList.size()); assertEquals(1, userList.size());
CreateUser savedUser = userList.get(0); CreateUser savedUser = userList.get(0);
assertEquals(id, savedUser.getId()); assertEquals(id, savedUser.getId());
@ -56,37 +51,30 @@ class CreateUserTest {
assertEquals(firstName, savedUser.getFirstName()); assertEquals(firstName, savedUser.getFirstName());
assertEquals(surname, savedUser.surname()); assertEquals(surname, savedUser.surname());
// Clean up - delete the test file after the test
File file = new File(filename); File file = new File(filename);
file.delete(); file.delete();
} }
@Test @Test
public void testUpdateStayLoggedIn() { public void testUpdateStayLoggedIn() {
String filename = "test_users.json";
String id = "3";
String userName = "testUser3";
String password = "password789";
String birthday = "2002-01-01";
String firstName = "Jack";
String surname = "Doe";
String filename = "test_users.json";
String id = "3";
String userName = "testUser3";
String password = "password789";
String birthday = "2002-01-01";
String firstName = "Jack";
String surname = "Doe";
// Create a user
CreateUser user = new CreateUser(id, userName, password, birthday, firstName, surname); CreateUser user = new CreateUser(id, userName, password, birthday, firstName, surname);
// Save user to JSON file
user.saveToJsonFile(filename); user.saveToJsonFile(filename);
// Update stayLoggedIn flag
CreateUser.updateStayLoggedIn(filename, userName, true); CreateUser.updateStayLoggedIn(filename, userName, true);
// Read updated user from JSON file
List<CreateUser> userList = CreateUser.readUserListFromJsonFile(filename); List<CreateUser> userList = CreateUser.readUserListFromJsonFile(filename);
// Ensure that the stayLoggedIn flag is updated
assertTrue(userList.get(0).isStayLoggedIn()); assertTrue(userList.get(0).isStayLoggedIn());
// Clean up - delete the test file after the test
File file = new File(filename); File file = new File(filename);
file.delete(); file.delete();
} }

19
src/test/java/LoginGUITest.java

@ -12,6 +12,7 @@ class LoginGUITest {
loginGUI = new LoginGUI(); loginGUI = new LoginGUI();
} }
//Tests initial state of the GUI
@Test @Test
public void testInitialState() { public void testInitialState() {
assertNotNull(loginGUI.getUsernameField()); assertNotNull(loginGUI.getUsernameField());
@ -21,6 +22,7 @@ class LoginGUITest {
assertNotNull(loginGUI.getSignUpButton()); assertNotNull(loginGUI.getSignUpButton());
} }
//Tests the login button
@Test @Test
public void testActionPerformed_LoginButton() { public void testActionPerformed_LoginButton() {
LoginGUI loginGUI = new LoginGUI(); LoginGUI loginGUI = new LoginGUI();
@ -28,25 +30,18 @@ class LoginGUITest {
loginGUI.getPasswordField().setText("testPassword"); loginGUI.getPasswordField().setText("testPassword");
loginGUI.getStayLoggedInCheckbox().setSelected(true); loginGUI.getStayLoggedInCheckbox().setSelected(true);
// Simulate action performed event on login button
loginGUI.actionPerformed(new ActionEvent(loginGUI.getLoginButton(), ActionEvent.ACTION_PERFORMED, null)); loginGUI.actionPerformed(new ActionEvent(loginGUI.getLoginButton(), ActionEvent.ACTION_PERFORMED, null));
// Verify if login was successful
assertFalse(loginGUI.isVisible()); assertFalse(loginGUI.isVisible());
} }
//Tests the check-box for staying logged in
@Test @Test
public void testStayLoggedInCheckbox() { public void testStayLoggedInCheckbox() {
// Initially, checkbox should be unchecked
assertFalse(loginGUI.getStayLoggedInCheckbox().isSelected());
// Simulate checking the checkbox
loginGUI.getStayLoggedInCheckbox().doClick();
assertTrue(loginGUI.getStayLoggedInCheckbox().isSelected());
}
assertFalse(loginGUI.getStayLoggedInCheckbox().isSelected());
loginGUI.getStayLoggedInCheckbox().doClick();
assertTrue(loginGUI.getStayLoggedInCheckbox().isSelected());
}
} }

31
src/test/java/SignUpGUITest.java

@ -1,4 +1,3 @@
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
@ -6,31 +5,29 @@ import org.junit.jupiter.api.BeforeEach;
import java.awt.Component; import java.awt.Component;
import java.awt.event.ActionEvent; import java.awt.event.ActionEvent;
import javax.swing.*; import javax.swing.*;
import java.util.ArrayList;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
class SignUpGUITest { class SignUpGUITest {
private SignUpGUI signUpGUI; private SignUpGUI signUpGUI;
//Instantiates a new instance of SignUpGUI
@BeforeEach @BeforeEach
void setUp() { void setUp() {
signUpGUI = new SignUpGUI(); signUpGUI = new SignUpGUI();
signUpGUI.setVisible(true); signUpGUI.setVisible(true);
} }
//Disposes of the instance
@AfterEach @AfterEach
void tearDown() { void tearDown() {
signUpGUI.dispose(); signUpGUI.dispose();
} }
//Tests text field and button inputs
@Test @Test
public void testSignUpButtonActionPerformed() { public void testSignUpButtonActionPerformed() {
// Set up text fields
signUpGUI.getUsernameField().setText("testUser"); signUpGUI.getUsernameField().setText("testUser");
signUpGUI.getPasswordField().setText("password"); signUpGUI.getPasswordField().setText("password");
signUpGUI.getConfirmPasswordField().setText("password"); signUpGUI.getConfirmPasswordField().setText("password");
@ -38,14 +35,11 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
signUpGUI.getFirstNameField().setText("John"); signUpGUI.getFirstNameField().setText("John");
signUpGUI.getSurnameField().setText("Doe"); signUpGUI.getSurnameField().setText("Doe");
// Perform action
ActionEvent actionEvent = new ActionEvent(signUpGUI.getSignUpButton(), ActionEvent.ACTION_PERFORMED, ""); ActionEvent actionEvent = new ActionEvent(signUpGUI.getSignUpButton(), ActionEvent.ACTION_PERFORMED, "");
signUpGUI.actionPerformed(actionEvent); signUpGUI.actionPerformed(actionEvent);
} }
//Tests if passwords are mismatched
@Test @Test
void testPasswordMismatch() { void testPasswordMismatch() {
SignUpGUI signUpGUI = new SignUpGUI(); SignUpGUI signUpGUI = new SignUpGUI();
@ -56,12 +50,12 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
signUpGUI.getFirstNameField().setText("John"); signUpGUI.getFirstNameField().setText("John");
signUpGUI.getSurnameField().setText("Doe"); signUpGUI.getSurnameField().setText("Doe");
signUpGUI.getSignUpButton().doClick(); // Simulate button click
signUpGUI.getSignUpButton().doClick();
// Make sure an error message dialog is shown
assertTrue(isErrorMessageShown(signUpGUI)); assertTrue(isErrorMessageShown(signUpGUI));
} }
//Tests if user-name already exists
@Test @Test
void testExistingUsername() { void testExistingUsername() {
SignUpGUI signUpGUI = new SignUpGUI(); SignUpGUI signUpGUI = new SignUpGUI();
@ -72,22 +66,19 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
signUpGUI.getFirstNameField().setText("John"); signUpGUI.getFirstNameField().setText("John");
signUpGUI.getSurnameField().setText("Doe"); signUpGUI.getSurnameField().setText("Doe");
signUpGUI.getSignUpButton().doClick(); // Simulate button click
signUpGUI.getSignUpButton().doClick();
// Make sure an error message dialog is shown
assertTrue(isErrorMessageShown(signUpGUI)); assertTrue(isErrorMessageShown(signUpGUI));
} }
// Helper method to check if an error message dialog is shown
// Helper method to check if an error message dialog is shown
private boolean isErrorMessageShown(JFrame frame) { private boolean isErrorMessageShown(JFrame frame) {
Component[] components = frame.getComponents(); Component[] components = frame.getComponents();
for (Component component : components) { for (Component component : components) {
if (component instanceof JOptionPane) { if (component instanceof JOptionPane) {
return false;
}
return false;
}
} }
return true; return true;
} }
} }
Loading…
Cancel
Save