Skip to content

Commit

Permalink
Merge pull request #15 from abmack/software-testing
Browse files Browse the repository at this point in the history
Adding integration tests for registration
  • Loading branch information
James Miller V committed Sep 20, 2013
2 parents 148e93c + ff9851e commit d986139
Show file tree
Hide file tree
Showing 3 changed files with 151 additions and 30 deletions.
25 changes: 18 additions & 7 deletions src/app/Cli.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<Node> 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"));

}
}
Expand Down
133 changes: 133 additions & 0 deletions src/test/integrate/Registration.java
Original file line number Diff line number Diff line change
@@ -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 = "[email protected]";

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 = "[email protected]";
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("[email protected]");

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, "[email protected]"));

//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("[email protected]");
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();

}

}
}
23 changes: 0 additions & 23 deletions src/test/unit/Cli.java

This file was deleted.

0 comments on commit d986139

Please sign in to comment.