-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix #61 : write all auth related tests
- Loading branch information
Showing
6 changed files
with
286 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,167 @@ | ||
//! # Activation | ||
//! | ||
//! Here are grouped the registration tests | ||
//! Those tests attack the `/api/auth/activate` route. | ||
/************************* REQUIRE *******************************************/ | ||
|
||
use rocket::http::{ContentType, Status}; | ||
|
||
use unanimitylibrary::database::models::user::User; | ||
|
||
use super::super::init; | ||
|
||
const ROUTE: &'static str = "/api/auth/activate/"; | ||
|
||
/**************************** TESTS ******************************************/ | ||
|
||
#[test] | ||
fn activation_good_id_good_token() { | ||
let client = init::clean_client(); | ||
let (user, _passwd) = init::get_user(false); | ||
let connection = init::database_connection(); | ||
|
||
// assert the user is inactive | ||
assert!(!user.active); | ||
|
||
let data = format!( | ||
"{{\"id\":{}, \"token\":\"{}\"}}", | ||
user.id, | ||
user.token.unwrap() | ||
); | ||
|
||
let request = client.post(ROUTE).header(ContentType::JSON).body(data); | ||
|
||
let response = request.dispatch(); | ||
|
||
assert_eq!(response.status(), Status::Ok); | ||
|
||
let activated_user = User::by_email(&connection, &user.email).unwrap(); | ||
|
||
assert!(activated_user.active); | ||
assert!(activated_user.token.is_none()); | ||
} | ||
|
||
#[test] | ||
fn activation_wrong_id_good_token() { | ||
let client = init::clean_client(); | ||
let (user, _passwd) = init::get_user(false); | ||
let connection = init::database_connection(); | ||
|
||
// assert the user is inactive | ||
assert!(!user.active); | ||
|
||
let mut fake_id = 12; | ||
while fake_id == user.id { | ||
fake_id += 1; | ||
} | ||
|
||
let data = format!( | ||
"{{\"id\":{}, \"token\":\"{}\"}}", | ||
fake_id, | ||
user.token.unwrap() | ||
); | ||
|
||
let request = client.post(ROUTE).header(ContentType::JSON).body(data); | ||
|
||
let response = request.dispatch(); | ||
|
||
assert_eq!(response.status(), Status::Forbidden); | ||
|
||
let not_so_activated_user = User::by_email(&connection, &user.email).unwrap(); | ||
|
||
assert!(!not_so_activated_user.active); | ||
assert!(not_so_activated_user.token.is_some()); | ||
} | ||
|
||
#[test] | ||
fn activation_good_id_wrong_token() { | ||
let client = init::clean_client(); | ||
let (user, _passwd) = init::get_user(false); | ||
let connection = init::database_connection(); | ||
|
||
// assert the user is inactive | ||
assert!(!user.active); | ||
|
||
let data = format!( | ||
"{{\"id\":{}, \"token\":\"{}\"}}", | ||
user.id, "thisisafaketoken" | ||
); | ||
|
||
let request = client.post(ROUTE).header(ContentType::JSON).body(data); | ||
|
||
let response = request.dispatch(); | ||
|
||
assert_eq!(response.status(), Status::Forbidden); | ||
|
||
let not_so_activated_user = User::by_email(&connection, &user.email).unwrap(); | ||
|
||
assert!(!not_so_activated_user.active); | ||
assert!(not_so_activated_user.token.is_some()); | ||
} | ||
|
||
#[test] | ||
fn activation_wrong_id_wrong_token() { | ||
let client = init::clean_client(); | ||
let (user, _passwd) = init::get_user(false); | ||
let connection = init::database_connection(); | ||
|
||
// assert the user is inactive | ||
assert!(!user.active); | ||
|
||
let mut fake_id = 12; | ||
while fake_id == user.id { | ||
fake_id += 1; | ||
} | ||
|
||
let data = format!( | ||
"{{\"id\":{}, \"token\":\"{}\"}}", | ||
fake_id, "thisisafaketoken" | ||
); | ||
|
||
let request = client.post(ROUTE).header(ContentType::JSON).body(data); | ||
|
||
let response = request.dispatch(); | ||
|
||
assert_eq!(response.status(), Status::Forbidden); | ||
|
||
let not_so_activated_user = User::by_email(&connection, &user.email).unwrap(); | ||
|
||
assert!(!not_so_activated_user.active); | ||
assert!(not_so_activated_user.token.is_some()); | ||
} | ||
|
||
#[test] | ||
fn double_activation() { | ||
let client = init::clean_client(); | ||
let (user, _passwd) = init::get_user(false); | ||
let connection = init::database_connection(); | ||
|
||
// assert the user is inactive | ||
assert!(!user.active); | ||
|
||
let data = format!( | ||
"{{\"id\":{}, \"token\":\"{}\"}}", | ||
user.id, | ||
user.token.unwrap() | ||
); | ||
|
||
let request = client.post(ROUTE).header(ContentType::JSON).body(&data); | ||
let response = request.dispatch(); | ||
|
||
assert_eq!(response.status(), Status::Ok); | ||
|
||
let activated_user = User::by_email(&connection, &user.email).unwrap(); | ||
|
||
assert!(activated_user.active); | ||
assert!(activated_user.token.is_none()); | ||
|
||
let request_bis = client.post(ROUTE).header(ContentType::JSON).body(&data); | ||
let response_bis = request_bis.dispatch(); | ||
|
||
assert_eq!(response_bis.status(), Status::Forbidden); | ||
|
||
// the request failed, but the user is still activated | ||
assert!(activated_user.active); | ||
assert!(activated_user.token.is_none()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
//! # Login | ||
//! | ||
//! Here are grouped the login tests | ||
//! Those tests attack the `/api/auth/login` route. | ||
/************************* REQUIRE *******************************************/ | ||
|
||
use rocket::http::{ContentType, Status}; | ||
|
||
use super::super::init; | ||
|
||
const ROUTE: &'static str = "/api/auth/login/"; | ||
|
||
/**************************** TESTS ******************************************/ | ||
|
||
#[test] | ||
fn login_user_activated_correct_credentials() { | ||
let client = init::clean_client(); | ||
let (user, password) = init::get_user(true); | ||
|
||
let data = format!( | ||
"{{\"email\":\"{}\", \"password\":\"{}\"}}", | ||
user.email, password | ||
); | ||
|
||
let request = client.post(ROUTE).header(ContentType::JSON).body(data); | ||
|
||
let response = request.dispatch(); | ||
|
||
assert_eq!(response.status(), Status::Ok); | ||
} | ||
|
||
#[test] | ||
fn login_user_activated_wrong_credentials() { | ||
let client = init::clean_client(); | ||
let (user, _password) = init::get_user(true); | ||
|
||
let data = format!( | ||
"{{\"email\":\"{}\", \"password\":\"{}\"}}", | ||
user.email, "thisisnotacorrectpassword" | ||
); | ||
|
||
let request = client.post(ROUTE).header(ContentType::JSON).body(data); | ||
|
||
let response = request.dispatch(); | ||
|
||
assert_eq!(response.status(), Status::Unauthorized); | ||
} | ||
|
||
#[test] | ||
fn login_user_not_activated_correct_credentials() { | ||
let client = init::clean_client(); | ||
let (user, password) = init::get_user(false); | ||
|
||
let data = format!( | ||
"{{\"email\":\"{}\", \"password\":\"{}\"}}", | ||
user.email, password | ||
); | ||
|
||
let request = client.post(ROUTE).header(ContentType::JSON).body(data); | ||
|
||
let response = request.dispatch(); | ||
|
||
assert_eq!(response.status(), Status::Forbidden); | ||
} | ||
|
||
#[test] | ||
fn login_user_not_activated_wrong_credentials() { | ||
let client = init::clean_client(); | ||
let (user, _password) = init::get_user(false); | ||
|
||
let data = format!( | ||
"{{\"email\":\"{}\", \"password\":\"{}\"}}", | ||
user.email, "thisisnotacorrectpassword" | ||
); | ||
|
||
let request = client.post(ROUTE).header(ContentType::JSON).body(data); | ||
|
||
let response = request.dispatch(); | ||
|
||
assert_eq!(response.status(), Status::Unauthorized); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters