Skip to content

Commit

Permalink
test: refactor code structure
Browse files Browse the repository at this point in the history
  • Loading branch information
Atheer2104 committed Mar 17, 2024
1 parent 2c50f06 commit 9fb204f
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 58 deletions.
58 changes: 0 additions & 58 deletions auth/tests/api/auth.rs

This file was deleted.

85 changes: 85 additions & 0 deletions auth/tests/api/auth/login.rs
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());
}
13 changes: 13 additions & 0 deletions auth/tests/api/auth/mod.rs
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;
}
34 changes: 34 additions & 0 deletions auth/tests/api/auth/register.rs
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");
}

0 comments on commit 9fb204f

Please sign in to comment.