-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2c50f06
commit 9fb204f
Showing
4 changed files
with
132 additions
and
58 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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,85 @@ | ||
use auth::proto::auth::{LoginRequest, RegisterRequest}; | ||
use rand::{thread_rng, Rng}; | ||
use tonic_types::StatusExt; | ||
|
||
use super::{sleep, spawn_app, Code, Request}; | ||
|
||
#[tokio::test] | ||
async fn missing_field_in_login_request() { | ||
let app = spawn_app().await; | ||
let mut rng = thread_rng(); | ||
sleep(rng.gen_range(100..200)).await; | ||
|
||
let test_cases = vec![("", "password"), ("user", "")]; | ||
|
||
for (username, password) in test_cases { | ||
let response = app | ||
.login(Request::new(LoginRequest { | ||
username: username.into(), | ||
password: password.into(), | ||
})) | ||
.await; | ||
|
||
let error = response.expect_err("The response was not a error"); | ||
|
||
println!("ERROR (MISSING_FIELDS): {:?}", error); | ||
|
||
// check the code for status is a invalid argument | ||
assert!(error.code() == Code::InvalidArgument); | ||
|
||
// check that status contains bad request violations | ||
assert!(error.get_error_details().has_bad_request_violations()); | ||
} | ||
} | ||
|
||
#[tokio::test] | ||
async fn login_as_non_existing_user() { | ||
let app = spawn_app().await; | ||
let mut rng = thread_rng(); | ||
sleep(rng.gen_range(100..200)).await; | ||
|
||
let response = app | ||
.login(Request::new(LoginRequest { | ||
username: "ABCDEFG".into(), | ||
password: "123456789".into(), | ||
})) | ||
.await; | ||
|
||
let error = response.expect_err("The response was not a error"); | ||
|
||
println!("ERROR (NON_EXISTING_USER): {:?}", error); | ||
|
||
assert!(error.code() == Code::Unauthenticated); | ||
} | ||
|
||
#[tokio::test] | ||
async fn login_as_a_registered_user() { | ||
let app = spawn_app().await; | ||
let mut rng = thread_rng(); | ||
sleep(rng.gen_range(100..200)).await; | ||
|
||
let register_response = app | ||
.register(Request::new(RegisterRequest { | ||
firstname: "atheer".into(), | ||
lastname: "ABC".into(), | ||
username: "atheer2104".into(), | ||
email: "atheer@gmail.com".into(), | ||
password: "strong password".into(), | ||
})) | ||
.await; | ||
|
||
println!("REGISTER RESPONSE: {:?}", register_response); | ||
|
||
assert!(register_response.is_ok()); | ||
|
||
let login_request = app | ||
.login(Request::new(LoginRequest { | ||
username: "atheer2104".into(), | ||
password: "strong password".into(), | ||
})) | ||
.await; | ||
|
||
println!("LOGIN REQUEST: {:?}", login_request); | ||
|
||
assert!(login_request.is_ok()); | ||
} |
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,13 @@ | ||
mod login; | ||
mod register; | ||
|
||
use std::time::Duration; | ||
use tonic::{Code, Request}; | ||
|
||
use crate::helpers::spawn_app; | ||
|
||
pub async fn sleep(duration: u64) { | ||
// we sleep to make sure the server is up and running, here we should use the health check service on and essentially poll it until | ||
// the servcie is ready and then we can start to make the request | ||
tokio::time::sleep(Duration::from_millis(duration)).await; | ||
} |
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,34 @@ | ||
use auth::proto::auth::RegisterRequest; | ||
use rand::{thread_rng, Rng}; | ||
|
||
use super::{sleep, spawn_app, Request}; | ||
|
||
#[tokio::test] | ||
async fn register_user_dont_check_token() { | ||
let app = spawn_app().await; | ||
let mut rng = thread_rng(); | ||
sleep(rng.gen_range(100..200)).await; | ||
|
||
let response = app | ||
.register(Request::new(RegisterRequest { | ||
firstname: "atheer".into(), | ||
lastname: "ABC".into(), | ||
username: "atheer2104".into(), | ||
email: "atheer@gmail.com".into(), | ||
password: "strong password".into(), | ||
})) | ||
.await; | ||
|
||
assert!(response.is_ok()); | ||
|
||
let saved = sqlx::query!(r#"SELECT * FROM account"#) | ||
.fetch_one(&app.db_pool) | ||
.await | ||
.expect("Failed to fetch from db"); | ||
|
||
assert_eq!(saved.firstname, "atheer"); | ||
assert_eq!(saved.lastname, "ABC"); | ||
assert_eq!(saved.username, "atheer2104"); | ||
assert_eq!(saved.email, "atheer@gmail.com"); | ||
assert_eq!(saved.password, "strong password"); | ||
} |