Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions run_preferences_test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/bash
mkdir -p build/test/stubs
mkdir -p build/test/classes

# Compile AlertMaker stub
javac -d build/test/stubs test/stubs/library/assistant/alert/AlertMaker.java

# Compile Preferences and Test
javac -sourcepath "src" -cp "libs/*:libs/test/*:build/test/stubs" -d build/test/classes \
src/library/assistant/ui/settings/Preferences.java \
test/library/assistant/ui/settings/PreferencesTest.java

# Run Test
java -cp "build/test/classes:build/test/stubs:libs/*:libs/test/*" org.junit.runner.JUnitCore library.assistant.ui.settings.PreferencesTest
34 changes: 28 additions & 6 deletions src/library/assistant/ui/login/LoginController.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package library.assistant.ui.login;

import com.jfoenix.controls.JFXButton;
import com.jfoenix.controls.JFXPasswordField;
import com.jfoenix.controls.JFXTextField;
import java.io.IOException;
Expand All @@ -11,6 +12,7 @@
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import library.assistant.ui.settings.Preferences;
Expand All @@ -29,27 +31,47 @@ public class LoginController implements Initializable {
private JFXTextField username;
@FXML
private JFXPasswordField password;
@FXML
private JFXButton loginButton;
@FXML
private Label titleLabel;

Preferences preference;

@Override
public void initialize(URL url, ResourceBundle rb) {
preference = Preferences.getPreferences();
if (preference.getUsername() == null || preference.getUsername().isEmpty()) {
titleLabel.setText("First Run: Create Admin Account");
loginButton.setText("Save");
}
}

@FXML
private void handleLoginButtonAction(ActionEvent event) {
String uname = StringUtils.trimToEmpty(username.getText());
String pword = DigestUtils.shaHex(password.getText());

if (uname.equals(preference.getUsername()) && pword.equals(preference.getPassword())) {
if (preference.getUsername() == null || preference.getUsername().isEmpty()) {
if (uname.isEmpty() || password.getText().isEmpty()) {
titleLabel.setText("Please enter username and password");
return;
}
preference.setUsername(uname);
preference.setPassword(pword);
Preferences.writePreferenceToFile(preference);
closeStage();
loadMain();
LOGGER.log(Level.INFO, "User successfully logged in {}", uname);
}
else {
username.getStyleClass().add("wrong-credentials");
password.getStyleClass().add("wrong-credentials");
LOGGER.log(Level.INFO, "Admin account created and logged in: {}", uname);
} else {
if (uname.equals(preference.getUsername()) && pword.equals(preference.getPassword())) {
closeStage();
loadMain();
LOGGER.log(Level.INFO, "User successfully logged in {}", uname);
} else {
username.getStyleClass().add("wrong-credentials");
password.getStyleClass().add("wrong-credentials");
}
}
}

Expand Down
8 changes: 7 additions & 1 deletion src/library/assistant/ui/login/login.fxml
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,17 @@
<?import com.jfoenix.controls.JFXPasswordField?>
<?import com.jfoenix.controls.JFXTextField?>
<?import de.jensd.fx.glyphs.fontawesome.FontAwesomeIconView?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>

<AnchorPane id="AnchorPane" prefHeight="367.0" prefWidth="419.0" stylesheets="@../../../../resources/dark-theme.css" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1" fx:controller="library.assistant.ui.login.LoginController">
<children>
<Label fx:id="titleLabel" alignment="CENTER" layoutY="85.0" prefHeight="25.0" prefWidth="419.0" textFill="WHITE">
<font>
<Font size="16.0"/>
</font>
</Label>
<JFXTextField fx:id="username" labelFloat="true" layoutX="95.0" layoutY="122.0" prefHeight="33.0" prefWidth="259.0" promptText="Username">
<font>
<Font size="16.0" />
Expand All @@ -19,7 +25,7 @@
<Font size="17.0" />
</font>
</JFXPasswordField>
<JFXButton layoutX="106.0" layoutY="278.0" onAction="#handleLoginButtonAction" prefHeight="40.0" prefWidth="90.0" styleClass="login-button" text="Login" />
<JFXButton fx:id="loginButton" layoutX="106.0" layoutY="278.0" onAction="#handleLoginButtonAction" prefHeight="40.0" prefWidth="90.0" styleClass="login-button" text="Login" />
<JFXButton layoutX="239.0" layoutY="278.0" onAction="#handleCancelButtonAction" prefHeight="40.0" prefWidth="90.0" styleClass="login-button" text="Cancel" />
<FontAwesomeIconView glyphName="LOCK" layoutX="200.0" layoutY="79.0" size="55" />
<FontAwesomeIconView glyphName="USER" layoutX="64.0" layoutY="149.0" size="25" />
Expand Down
2 changes: 0 additions & 2 deletions src/library/assistant/ui/settings/Preferences.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ public class Preferences {
public Preferences() {
nDaysWithoutFine = 14;
finePerDay = 2;
username = "admin";
setPassword("admin");
}

public int getnDaysWithoutFine() {
Expand Down
35 changes: 35 additions & 0 deletions test/library/assistant/ui/settings/PreferencesTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package library.assistant.ui.settings;

import org.junit.Test;
import static org.junit.Assert.*;
import org.apache.commons.codec.digest.DigestUtils;

public class PreferencesTest {

@Test
public void testDefaultConstructorNoCredentials() {
Preferences prefs = new Preferences();
assertNull("Username should be null by default", prefs.getUsername());
assertNull("Password should be null by default", prefs.getPassword());
}

@Test
public void testSetPasswordHashing() {
Preferences prefs = new Preferences();
String password = "short";
prefs.setPassword(password);

String expectedHash = DigestUtils.shaHex(password);
assertEquals("Short password should be hashed", expectedHash, prefs.getPassword());
}

@Test
public void testSetPasswordNoHashing() {
Preferences prefs = new Preferences();
// A string that is already a hash (>= 16 chars)
String longPassword = "thisisalongpasswordthatshouldnotbehashed";
prefs.setPassword(longPassword);

assertEquals("Long password should not be hashed again", longPassword, prefs.getPassword());
}
}
21 changes: 21 additions & 0 deletions test/stubs/library/assistant/alert/AlertMaker.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package library.assistant.alert;

import java.util.List;

public class AlertMaker {

public static void showSimpleAlert(String title, String content) {
}

public static void showErrorMessage(String title, String content) {
}

public static void showErrorMessage(Exception ex) {
}

public static void showErrorMessage(Exception ex, String title, String content) {
}

public static void showTrayMessage(String title, String message) {
}
}