Skip to content

Commit

Permalink
Added Unit Tests for the admin features
Browse files Browse the repository at this point in the history
  • Loading branch information
trufflepirate authored Oct 15, 2018
2 parents 075dc20 + 17e0a57 commit 6422de8
Show file tree
Hide file tree
Showing 22 changed files with 1,522 additions and 56 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ classes/
/data/
src/main/resources/docs/
out/
config.json
preferences.json

config\.json
preferences\.json
Binary file modified docs/diagrams/ModelComponentClassDiagram.pptx
Binary file not shown.
Binary file modified docs/diagrams/StorageComponentClassDiagram.pptx
Binary file not shown.
Binary file modified docs/images/ModelClassDiagram.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/images/StorageClassDiagram.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 4 additions & 4 deletions preferences.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"guiSettings" : {
"windowWidth" : 1551.4285888671875,
"windowHeight" : 788.0,
"windowWidth" : 1550.4000244140625,
"windowHeight" : 838.4000244140625,
"windowCoordinates" : {
"x" : 1,
"y" : 1
"x" : -7,
"y" : -7
}
},
"addressBookFilePath" : "data\\addressbook.xml",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package seedu.address.logic.commands.admin;

import static java.util.Objects.requireNonNull;

import seedu.address.logic.CommandHistory;
import seedu.address.logic.commands.Command;
import seedu.address.logic.commands.CommandResult;
Expand All @@ -9,7 +11,6 @@
import seedu.address.model.admin.Password;
import seedu.address.model.admin.Username;


/**
* Lets one admin add another admin
*/
Expand All @@ -21,13 +22,19 @@ public class AddAdminCommand extends Command {
public static final String MESSAGE_USAGE = COMMAND_WORD + "Used to add another admin.\n"
+ "Example: addAdmin USERNAME PASSWORD PASSWORD\n";
public static final String MESSAGE_PASSWORDS_DONT_MATCH = "The two password fields don't match! Please try again.";
public static final String MESSAGE_NOT_VALID_PASSWORD = "Password not valid! You need at least 8 chars, "
+ "where you have at least 1 smaller case, 1 bigger case, 1 symbol, 1 number and no whitespace";

private final Username username;
private final Password password;
private final Password passwordVerify;
private final Admin toAddIn;

public AddAdminCommand(Username username, Password password, Password passwordVerify) {
requireNonNull(username);
requireNonNull(password);
requireNonNull(passwordVerify);

this.username = username;
this.password = password;
this.passwordVerify = passwordVerify;
Expand All @@ -36,6 +43,8 @@ public AddAdminCommand(Username username, Password password, Password passwordVe

@Override
public CommandResult execute(Model model, CommandHistory history) throws CommandException {
requireNonNull(model);

if (!model.isLoggedIn()) {
throw new CommandException(MESSAGE_NO_ACCESS);
}
Expand All @@ -48,6 +57,12 @@ public CommandResult execute(Model model, CommandHistory history) throws Command
throw new CommandException(MESSAGE_ADMIN_ALREADY_EXISTS);
}

PasswordValidator pwVal = new PasswordValidator();

if (!pwVal.isValidPassword(this.password)) {
throw new CommandException(MESSAGE_NOT_VALID_PASSWORD);
}

model.addAdmin(toAddIn);
model.commitAddressBook(); //TODO: not sure what this does;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package seedu.address.logic.commands.admin;

import static java.util.Objects.requireNonNull;

import org.mindrot.jbcrypt.BCrypt;

import seedu.address.commons.core.EventsCenter;
Expand Down Expand Up @@ -31,12 +33,15 @@ public class LoginCommand extends Command {
private final Password password;

public LoginCommand(Username username, Password password) {
requireNonNull(username);
requireNonNull(password);
this.username = username;
this.password = password;
}

@Override
public CommandResult execute(Model model, CommandHistory history) throws CommandException {
requireNonNull(model);
if (model.isLoggedIn()) {
throw new CommandException(MESSAGE_ALREADY_LOGGED_IN);
} else if (model.findAdmin(username) == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class LogoutCommand extends Command {

public static final String COMMAND_WORD = "logout";
public static final String MESSAGE_SUCCESS = "logout successful!";
private static final String MESSAGE_NO_CURRENT_SESSION = "Logout failed! No one is currently logged in.";
public static final String MESSAGE_NO_CURRENT_SESSION = "Logout failed! No one is currently logged in.";

public static final String MESSAGE_USAGE = COMMAND_WORD + "Logout used to get out of admin mode.\n"
+ "Example: logout\n";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package seedu.address.logic.commands.admin;

import seedu.address.model.admin.Password;

/**
* Used for Validation on AddAdmin and UpdateAdmin
* Credits to this answer: https://stackoverflow.com/a/8148112
*/
public class PasswordValidator {
/**
* Returns whether the password provided is strong enough to be accepted
* @param password
*/
public boolean isValidPassword(Password password) {
String pwd = password.toString();
String pattern = "(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%^&+=])(?=\\S+$).{8,}";
if (pwd.matches(pattern)) {
return true;
} else {
return false;
}

}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package seedu.address.logic.commands.admin;

import static java.util.Objects.requireNonNull;

import seedu.address.logic.CommandHistory;
import seedu.address.logic.commands.Command;
import seedu.address.logic.commands.CommandResult;
Expand All @@ -16,13 +18,14 @@ public class RemoveAdminCommand extends Command {
public static final String MESSAGE_NO_ACCESS = "You must be logged in to remove another admin!";
public static final String MESSAGE_SUCCESS = "Admin removed successfully!";
public static final String MESSAGE_NO_SUCH_ADMIN = "No such admins exist. Have you typed the username correctly?";
private static final String MESSAGE_CANT_DELETE_LAST_ADMIN = "You can't delete the last admin.";
public static final String MESSAGE_CANT_DELETE_LAST_ADMIN = "You can't delete the last admin.";
public static final String MESSAGE_USAGE = COMMAND_WORD + "Used to remove another admin.\n"
+ "Example: removeAdmin USERNAME\n";

private final Username username;

public RemoveAdminCommand(Username username) {
requireNonNull(username);
this.username = username;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package seedu.address.logic.commands.admin;

import static java.util.Objects.requireNonNull;

import org.mindrot.jbcrypt.BCrypt;

import seedu.address.logic.CommandHistory;
import seedu.address.logic.commands.Command;
import seedu.address.logic.commands.CommandResult;
Expand All @@ -20,26 +24,32 @@ public class UpdatePasswordCommand extends Command {
+ "Example: udpatePassword USERNAME OLD_PW NEW_PW NEW_PW_VERIFY\n";
public static final String MESSAGE_ONLY_CHANGE_YOUR_OWN_PW = "You can only change your own password.";
public static final String MESSAGE_PASSWORDS_DONT_MATCH = "The two password fields don't match! Please try again.";
public static final String MESSAGE_WRONG_OLD_DETAILS = "Your old password doesn't match.";
public static final String MESSAGE_NOT_VALID_PASSWORD = "Password not valid! You need at least 8 chars, "
+ "where you have at least 1 smaller case, 1 bigger case, 1 symbol, 1 number and no whitespace";

private final Username username;
private final Password oldPassword;
private final Password newPassword;
private final Password passwordVerify;
private final Admin toUpdate;
private final Admin updatedAdmin;

public UpdatePasswordCommand(Username username, Password oldPassword,
Password newPassword, Password passwordVerify) {
requireNonNull(username);
requireNonNull(oldPassword);
requireNonNull(newPassword);
requireNonNull(passwordVerify);

this.username = username;
this.oldPassword = oldPassword;
this.newPassword = newPassword;
this.passwordVerify = passwordVerify;
this.toUpdate = new Admin(username, oldPassword);
this.updatedAdmin = new Admin(username, newPassword);
}

@Override
public CommandResult execute(Model model, CommandHistory history) throws CommandException {
requireNonNull(model);

if (!model.isLoggedIn()) {
throw new CommandException(MESSAGE_NO_ACCESS);
}
Expand All @@ -52,6 +62,24 @@ public CommandResult execute(Model model, CommandHistory history) throws Command
throw new CommandException(MESSAGE_ONLY_CHANGE_YOUR_OWN_PW);
}

PasswordValidator pwVal = new PasswordValidator();

if (!pwVal.isValidPassword(this.newPassword)) {
throw new CommandException(MESSAGE_NOT_VALID_PASSWORD);
}

final Admin toUpdate;
final Admin updatedAdmin;

if (!BCrypt.checkpw(oldPassword.toString(), model.findAdmin(username).getPassword().toString())) {
throw new CommandException(MESSAGE_WRONG_OLD_DETAILS);
} else {
toUpdate = new Admin(username, model.findAdmin(username).getPassword());
updatedAdmin = new Admin(username, newPassword);
}



model.updateAdmin(toUpdate, updatedAdmin);
model.commitAddressBook(); //TODO: not sure what this does;

Expand Down
10 changes: 1 addition & 9 deletions src/main/java/seedu/address/model/AddressBook.java
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ public void addAdmin(Admin toAdd) {
* The admin must not already exist in the address book.
*/
public void addAdminWithoutRehash(Admin toAdd) {
//TODO: Not idiot proof
admins.add(toAdd);
}

Expand All @@ -168,14 +169,6 @@ public void removeAdmin(Admin toRemove) {
admins.remove(toRemove);
}

/**
* updates an admin in the address book.
*/
public void updateAdmin(Admin toRemove, Admin toAdd) {
admins.remove(toRemove);
admins.add(toAdd);
}

public boolean hasAdmin(Admin admin) {
return admins.contains(admin);
}
Expand All @@ -201,7 +194,6 @@ private Admin encryptedAdmin(Admin rawAdmin) {
return protectedAdmin;
}


//======================== machine methods ================================//

/**
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/seedu/address/model/ModelManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,8 @@ public void removeAdmin(Admin admin) {
//TODO: add tests
@Override
public void updateAdmin(Admin admin, Admin updatedAdmin) {
versionedAddressBook.updateAdmin(admin, updatedAdmin);
versionedAddressBook.addAdmin(updatedAdmin);
versionedAddressBook.removeAdmin(admin);
indicateAdminListChanged();
}

Expand Down
3 changes: 3 additions & 0 deletions src/main/java/seedu/address/model/admin/Password.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package seedu.address.model.admin;

import static java.util.Objects.requireNonNull;

/**
* Represents the hash of the password of an admin
*/
Expand All @@ -8,6 +10,7 @@ public class Password {
private String value;

public Password(String password) {
requireNonNull(password);
this.value = password;
}

Expand Down
3 changes: 3 additions & 0 deletions src/main/java/seedu/address/model/admin/Username.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package seedu.address.model.admin;

import static java.util.Objects.requireNonNull;

/**
* Represents the Username of an admin
*/
Expand All @@ -8,6 +10,7 @@ public class Username {
private String value;

public Username(String username) {
requireNonNull(username);
this.value = username;
}

Expand Down
33 changes: 0 additions & 33 deletions src/main/java/seedu/address/model/job/JobOwner.java

This file was deleted.

Loading

0 comments on commit 6422de8

Please sign in to comment.