Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/caiyo/WaterCooler.git
Browse files Browse the repository at this point in the history
  • Loading branch information
caiyo committed Jun 14, 2015
2 parents dda1272 + d82bac7 commit 43d6206
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 48 deletions.
73 changes: 43 additions & 30 deletions app/models/User.java
Original file line number Diff line number Diff line change
@@ -1,35 +1,32 @@
package models;

import java.util.Date;

import javax.persistence.AttributeOverride;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NoResultException;
import javax.persistence.Table;
import javax.persistence.Transient;

import com.fasterxml.jackson.annotation.JsonIgnore;
import util.Utility;

import play.db.jpa.JPA;

import com.fasterxml.jackson.annotation.JsonIgnore;

/**
* @author kylewong
*
*/
@Entity
@Table(name="user")
@AttributeOverride(name = "id", column = @Column(name = "user_id"))
public class User extends AbstractModel{
public class User extends AbstractModel {

private static final int SALT_SIZE = 32;
private static final int MINIMUM_PASSWORD_SIZE = 6;

@Column(unique=true)
private String username;

private String name;

@JsonIgnore
private String password;

Expand All @@ -40,16 +37,13 @@ public class User extends AbstractModel{
private String salt;

public User(){}
public User(String username, String name){
public User(String username) {
this.username=username;
this.name=name;
}


public User(String username, String name, String password,
public User(String username,String password,
String confirmPassword) {
this.username = username;
this.name = name;
this.password = password;
this.confirmPassword = confirmPassword;
}
Expand All @@ -60,41 +54,63 @@ public User(String username, String name, String password,
public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

public String getConfirmPassword() {
return confirmPassword;
}

public void setConfirmPassword(String confirmPassword) {
this.confirmPassword = confirmPassword;
}

public String getSalt() {
return salt;
}

public void setSalt(String salt) {
this.salt = salt;
}

public boolean validateUserForCreation() {
if (getPassword() == null || getPassword().length() < MINIMUM_PASSWORD_SIZE) {
return false;
} else if (getConfirmPassword() == null || !getPassword().equals(getConfirmPassword())) {
return false;
}
return true;
}

public boolean validateForLogin(String submittedPassword) {
String hash = Utility.hashString(submittedPassword, getSalt());
return hash.equalsIgnoreCase(getPassword());
}

public void createUserCredentials() {
if (getPassword() != null) {
setSalt(Utility.generateHexString(SALT_SIZE));
setPassword(Utility.hashString(getPassword(), getSalt()));
}
}
/*
* STATIC METHODS
*/
public static User findUserById(long id){
public static User findUserById(long id) {
return JPA.em().find(User.class, id);
}

public static User findUserByUsername(String username){
public static User findUserByUsername(String username) {
User u;
try{
u = JPA.em().
Expand All @@ -107,8 +123,8 @@ public static User findUserByUsername(String username){
}
return u;
}
public static User createUser(User user){

public static User createUser(User user) {
try{
JPA.em().persist(user);
}catch(Exception e){
Expand All @@ -121,15 +137,12 @@ public static User createUser(User user){

//Not necessarily needed since dirty checking is enabled by default for hibernate
//created just in case
public static User updateUser(User updatedUser){
public static User updateUser(User updatedUser) {
JPA.em().merge(updatedUser);
return updatedUser;
}

public static void deleteUser(User user){
public static void deleteUser(User user) {
JPA.em().remove(user);
}



}
2 changes: 1 addition & 1 deletion test/models/CommentTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public void createObjects(){
JPA.withTransaction(new play.libs.F.Callback0() {
@Override
public void invoke() throws Throwable {
User u = User.createUser(new User("caiyo", "Kyle"));
User u = User.createUser(new User("caiyo"));
Post.createPost(new Post(u, "this is post 1"));
}
});
Expand Down
2 changes: 1 addition & 1 deletion test/models/PostTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public void createObjects(){
JPA.withTransaction(new play.libs.F.Callback0() {
@Override
public void invoke() throws Throwable {
User u = User.createUser(new User("caiyo", "Kyle"));
User u = User.createUser(new User("caiyo"));
Post.createPost(new Post(u, "this is post 1"));
}
});
Expand Down
75 changes: 59 additions & 16 deletions test/models/UserTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package models;

import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;

import org.junit.Before;
import org.junit.Test;
Expand All @@ -13,75 +17,77 @@ public void createUser(){
JPA.withTransaction(new play.libs.F.Callback0() {
@Override
public void invoke() throws Throwable {
User.createUser(new User("caiyo", "Kyle"));
User.createUser(new User("caiyo", "", ""));
}
});
}

@Test
public void testUserCreation(){
public void testUserCreation() {
JPA.withTransaction(new play.libs.F.Callback0() {
@Override
public void invoke() throws Throwable {
User u =User.createUser(new User("test", "Kyle"));
User u = User.createUser(new User("test"));
assertNotNull(u);

}
});

}

@Test
public void testUserNotCreated(){
public void testUserNotCreated() {
JPA.withTransaction(new play.libs.F.Callback0() {
@Override
public void invoke() throws Throwable {
User u =User.createUser(new User("caiyo", "Kyle"));
User u = User.createUser(new User("caiyo"));
assertNull(u);

}
});

}

@Test
public void testFindUserById(){
public void testFindUserById() {
JPA.withTransaction(new play.libs.F.Callback0() {
@Override
public void invoke() throws Throwable {
User u =User.findUserById(1);
User u = User.findUserById(1);
assertNotNull(u);

}
});
}

@Test
public void testFindUserByUsername(){
public void testFindUserByUsername() {
JPA.withTransaction(new play.libs.F.Callback0() {
@Override
public void invoke() throws Throwable {
User u =User.findUserByUsername("caiyo");
User u = User.findUserByUsername("caiyo");
assertNotNull(u);
assertEquals(u.getUsername(), "caiyo");

}
});
}

@Test
public void testUserUpdate(){
public void testUserUpdate() {
JPA.withTransaction(new play.libs.F.Callback0() {
@Override
public void invoke() throws Throwable {
User u =User.findUserById(1);
u.setName("Shaun");
User u = User.findUserById(1);
User.updateUser(u);

User updated = User.findUserById(1);
assertNotNull(updated);
assertEquals(updated.getName(), "Shaun");
}
});
}

@Test
public void testUserDelete(){
public void testUserDelete() {
JPA.withTransaction(new play.libs.F.Callback0() {
@Override
public void invoke() throws Throwable {
Expand All @@ -95,4 +101,41 @@ public void invoke() throws Throwable {
}
});
}

@Test
public void testUserConstructor() {
User u = new User("testuser", "abc", "abc");
assertNotNull(u.getUsername());
assertNotNull(u.getPassword());
assertNotNull(u.getConfirmPassword());
}

@Test
public void testValidateUserShortPassword() {
User u = new User("testuser", "abc", "abc");
assertFalse(u.validateUserForCreation());
}

@Test
public void testValidateUserWrongConfirmPassword() {
User u = new User("testuser", "password", "123456");
assertFalse(u.validateUserForCreation());
}

@Test
public void testValidateUserNullConfirmPassword() {
User u = new User("testuser", "password", null);
assertFalse(u.validateUserForCreation());
}

@Test
public void testCreateUserCredentials() {
User u = new User("testuser", "password", "password");
String oldPassword = u.getPassword();
assertNull(u.getSalt());
u.createUserCredentials();
assertFalse(oldPassword.equals(u.getPassword()));
assertTrue(u.getPassword().length() == 64); //SHA-256 String length;
assertNotNull(u.getSalt());
}
}
5 changes: 5 additions & 0 deletions test/util/UtilityTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package util;

public class UtilityTest {

}

0 comments on commit 43d6206

Please sign in to comment.