diff --git a/src/app/Cli.java b/src/app/Cli.java index d6bb4c5..41e58c6 100644 --- a/src/app/Cli.java +++ b/src/app/Cli.java @@ -92,12 +92,15 @@ public Result logout( String session_id ) { public Result signup( String email ) { Email e = null; try { + //check if email exists e = new Email(Entity.findExistingNode(LabelDef.EMAIL, Email.EMAIL_KEY, email)); }catch(IllegalStateException ise) { + //email doesn't exist, create and return claimtoken e = new Email(email); return (new Result(true, e.getClaimToken().toString())); } + //check if email has been registered if(e.getClaimToken() == null) return (new Result(false, "email claimed")); else @@ -108,22 +111,30 @@ public Result signup( String email ) { public Result register( String ct, String name, String pass, String passVer ) { GraphDatabaseService graphDB = GraphDatabase.get(); try(Transaction tx = graphDB.beginTx()) { - //ResourceIterable email_nodes = graphDB.findNodesByLabelAndProperty(LabelDef.EMAIL, Email.CLAIM_TOKEN, ct); - - Email email = null; + + //check if passwords match + if(!pass.equals(passVer)) + return (new Result(false, "passwords do not match")); + + Email e = null; try { - email = new Email(Entity.findExistingNode(LabelDef.EMAIL, Email.CLAIM_TOKEN, ct)); + //find email by claimtoken + e = new Email(Entity.findExistingNode(LabelDef.EMAIL, Email.CLAIM_TOKEN, ct)); }catch(IllegalStateException ise) { + //no email found, bad claimtoken return (new Result(false, "bad claimtoken")); } + //create or find user and set password and email User u = new User(); u.setPassword(pass); - u.addEmail(email); - email.clearClaimToken(); + u.addEmail(e); + + //clear claimToken(should do this on its own!!) + e.clearClaimToken(); tx.success(); - return (new Result(true, "added email to user?")); + return (new Result(true, "registration complete")); } } diff --git a/src/test/integrate/Registration.java b/src/test/integrate/Registration.java new file mode 100644 index 0000000..68f1072 --- /dev/null +++ b/src/test/integrate/Registration.java @@ -0,0 +1,133 @@ +package test.integrate; + +import static org.junit.Assert.*; +import org.neo4j.graphdb.*; +import org.neo4j.graphdb.GraphDatabaseService; +import org.junit.*; + +/** + * Unit test for {@link app.User}. + */ +public class Registration { + + @After + @Before + public void setup() { + app.GraphDatabase.clearDb(); + } + + @Test + public void check_signup() { + final String email_address = "test@integration.com"; + + app.Email e = null; + try { + //check if email already exists + e = new app.Email(app.Entity.findExistingNode(app.LabelDef.EMAIL, app.Email.EMAIL_KEY, email_address)); + }catch(IllegalStateException ise) { + //email does not exist, therefore create a new one w/ claimtoken + e = new app.Email(email_address); + } + + assertEquals(e.getAddress(), email_address); + } + + @Test + public void check_multiple_signup() { + final String email_address = "test@integration.com"; + app.ClaimToken ct = null; + + for(int i = 0; i < 10; i++) { + app.Email e = null; + try { + //check if email already exists + e = new app.Email(app.Entity.findExistingNode(app.LabelDef.EMAIL, app.Email.EMAIL_KEY, email_address)); + }catch(IllegalStateException ise) { + //email does not exist, therefore create a new one w/ claimtoken + e = new app.Email(email_address); + } + + if(i > 0) + assertEquals(e.getClaimToken().toString(), ct.toString()); + + ct = e.getClaimToken(); + + assertEquals(e.getAddress(), email_address); + } + } + + @Test + public void check_taken_email() { + app.Email e = new app.Email("test@integration.com"); + + assertNull(e.getUser()); + + GraphDatabaseService graphDB = app.GraphDatabase.get(); + try(Transaction tx = graphDB.beginTx()) { + + app.User u = new app.User(); + u.setPassword("password"); + u.addEmail(e); + + e.clearClaimToken(); + + assertNotNull(e.getUser()); + + app.Email f = new app.Email(app.Entity.findExistingNode(app.LabelDef.EMAIL, app.Email.EMAIL_KEY, "test@integration.com")); + + //make sure the email has been found + assertNotNull(f.getUser()); + + //confirm that both email objects are indeed the same, restricting users from using a single email for multiple accounts + assertEquals(e.getAddress(), f.getAddress()); + assertNotEquals(e.getAddress() + "fart", f.getAddress() + "traf"); + assertEquals(e.getClaimToken(), f.getClaimToken()); + + + tx.success(); + } + } + + @Test + public void check_register() { + + String name = "whocares"; + String pass = "nobody"; + String verify = "nobody"; + + assertTrue(pass.equals(verify)); + + app.Email e = new app.Email("test@integration.com"); + assertNotNull(e.getClaimToken()); + String ct = e.getClaimToken().toString(); + assertNull(e.getUser()); + + app.Email f = null; + GraphDatabaseService graphDB = app.GraphDatabase.get(); + try(Transaction tx = graphDB.beginTx()) { + + f = new app.Email(app.Entity.findExistingNode(app.LabelDef.EMAIL, app.Email.CLAIM_TOKEN, ct)); + assertNotNull(f); + + tx.success(); + } + + assertTrue(ct.equals(f.getClaimToken().toString())); + + try(Transaction tx = graphDB.beginTx()) { + app.User u = new app.User(); + u.setPassword(pass); + u.addEmail(f); + + f.clearClaimToken(); + + assertNotNull(f.getUser()); + assertNull(f.getClaimToken()); + assertEquals(u.getPassword(), pass); + + tx.success(); + + } + + } +} diff --git a/src/test/unit/Cli.java b/src/test/unit/Cli.java deleted file mode 100644 index db27ce1..0000000 --- a/src/test/unit/Cli.java +++ /dev/null @@ -1,23 +0,0 @@ -package test.unit; - -import static org.junit.Assert.*; -import org.junit.*; - -/** - * Unit test for {@link app.User}. - */ -public class Cli { - app.Cli c; - - @Before - public void setup() { - app.GraphDatabase.clearDb(); - c = new app.Cli(); - } - - @Test - public void testSignup() { - String email_address = "email@domain.com"; - c.signup(email_address); - } -}