Skip to content

Commit

Permalink
issue #61 end registration related tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Tazoeur committed Mar 23, 2020
1 parent 1a51e7c commit 8c6771d
Showing 1 changed file with 121 additions and 0 deletions.
121 changes: 121 additions & 0 deletions tests/auth/register.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,124 @@ fn register_full_address() {
// check that this address is the one we just added
assert_eq!(tab_address[0].street, "rue grandganage");
}

#[test]
fn register_address_wrong_type() {
let client = init::clean_client();
let conn = init::database_connection();

// check that there is no user in database already done in init::clean()

// prepare a user
let test_user = "{
\"email\":\"guillaume.latour@student.unamur.be\",
\"password\":\"mysuperpassword\",
\"firstname\": \"Guillaume\",
\"lastname\":\"Latour\",
\"phone\":\"+32 471 85 85 85\",
\"address\":{
\"street\":\"rue grandganage\",
\"number\":21,
\"city\":\"Namur\",
\"zipcode\":\"cinq mille\",
\"country\":\"Belgique\"
}
}";

// request the application on the route /api/register
let req = client
.post("/api/auth/register/")
.header(ContentType::JSON)
.body(test_user);
let response = req.dispatch();

// check that the response is OK
assert_eq!(response.status(), Status::UnprocessableEntity);

// check that there is no new user or address in database
assert_eq!(users.load::<User>(&conn).unwrap().len(), 0);
assert_eq!(addresses.load::<Address>(&conn).unwrap().len(), 0);
}

#[test]
fn register_incomplete_address() {
let client = init::clean_client();
let conn = init::database_connection();

// check that there is no user in database already done in init::clean()

// prepare a user
let test_user = "{
\"email\":\"guillaume.latour@student.unamur.be\",
\"password\":\"mysuperpassword\",
\"firstname\": \"Guillaume\",
\"lastname\":\"Latour\",
\"phone\":\"+32 471 85 85 85\",
\"address\":{
\"street\":\"rue grandganage\",
\"number\":21,
\"country\":\"Belgique\"
}
}";

// request the application on the route /api/register
let req = client
.post("/api/auth/register/")
.header(ContentType::JSON)
.body(test_user);
let response = req.dispatch();

// check that the response is OK
assert_eq!(response.status(), Status::UnprocessableEntity);

// check that there is no new user or address in database
assert_eq!(users.load::<User>(&conn).unwrap().len(), 0);
assert_eq!(addresses.load::<Address>(&conn).unwrap().len(), 0);
}

#[test]
fn register_with_existing_user() {
let client = init::clean_client();
let conn = init::database_connection();

// check that there is no user in database already done in init::clean()

// prepare a user
let test_user = "{
\"email\":\"guillaume.latour@student.unamur.be\",
\"password\":\"mysuperpassword\",
\"firstname\": \"Guillaume\",
\"lastname\":\"Latour\"
}";

// request the application on the route /api/register
let req = client
.post("/api/auth/register/")
.header(ContentType::JSON)
.body(test_user);
let response = req.dispatch();

// check that the response is OK
assert_eq!(response.status(), Status::Ok);

// load users present in database
let tab = users.load::<User>(&conn).unwrap();

// check that there is only one user in database
assert_eq!(tab.len(), 1);

// check that this user is the one we just added
assert_eq!(tab[0].email, "guillaume.latour@student.unamur.be");
// and there is nothing in the address table
assert_eq!(addresses.load::<Address>(&conn).unwrap().len(), 0);

let req2 = client
.post("/api/auth/register/")
.header(ContentType::JSON)
.body(test_user);
let response2 = req2.dispatch();
assert_eq!(response2.status(), Status::Conflict);

// check that there is still one user in database
assert_eq!(tab.len(), 1);
}

0 comments on commit 8c6771d

Please sign in to comment.