Skip to content

Commit

Permalink
fix: update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ParzivalEugene committed Aug 9, 2024
1 parent 2d9c5d8 commit a3857ac
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 52 deletions.
73 changes: 53 additions & 20 deletions src/tests/e2e/auth/sign_in.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,27 +125,60 @@ mod test {
#[tokio::test]
async fn wrong_password() {
let state = AppState::default();
let app = app_router(state.clone()).with_state(state);
let mut app = app_router(state.clone()).with_state(state).into_service();

let response = app
.oneshot(
Request::builder()
.method(http::Method::POST)
.uri(SIGN_IN)
.header(http::header::CONTENT_TYPE, mime::APPLICATION_JSON.as_ref())
.body(Body::from(
json!({
"email": "[email protected]",
"password": "wrong_password",
"device": {
"name": "android 1111",
"os": "Android"
}
})
.to_string(),
))
.unwrap(),
)
let request = Request::builder()
.method(http::Method::POST)
.uri(SIGN_UP)
.header(http::header::CONTENT_TYPE, mime::APPLICATION_JSON.as_ref())
.body(Body::from(
json!({
"email": "[email protected]",
"password": "1234",
"device": {
"name": "android 1111",
"os": "Android"
}
})
.to_string(),
))
.unwrap();
let response = ServiceExt::<Request<Body>>::ready(&mut app)
.await
.unwrap()
.call(request)
.await
.unwrap();

assert_eq!(response.status(), StatusCode::CREATED);

let body = response.into_body().collect().await.unwrap().to_bytes();
let body: Value = serde_json::from_slice(&body).unwrap();

assert!(body["data"].is_object());
assert!(body["data"]["access_token"].is_string());
assert!(body["data"]["refresh_token"].is_string());

let request = Request::builder()
.method(http::Method::POST)
.uri(SIGN_IN)
.header(http::header::CONTENT_TYPE, mime::APPLICATION_JSON.as_ref())
.body(Body::from(
json!({
"email": "[email protected]",
"password": "wrong_password",
"device": {
"name": "android 1111",
"os": "Android"
}
})
.to_string(),
))
.unwrap();
let response = ServiceExt::<Request<Body>>::ready(&mut app)
.await
.unwrap()
.call(request)
.await
.unwrap();

Expand Down
75 changes: 54 additions & 21 deletions src/tests/e2e/auth/sign_up.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ mod test {
};
use http_body_util::BodyExt;
use serde_json::{json, Value};
use tower::ServiceExt;
use tower::{Service, ServiceExt};

use crate::{
enums::errors::external::{AuthError, ExternalError},
Expand Down Expand Up @@ -56,27 +56,60 @@ mod test {
#[tokio::test]
async fn user_already_exists() {
let state = AppState::default();
let app = app_router(state.clone()).with_state(state);
let mut app = app_router(state.clone()).with_state(state).into_service();

let request = Request::builder()
.method(http::Method::POST)
.uri(SIGN_UP)
.header(http::header::CONTENT_TYPE, mime::APPLICATION_JSON.as_ref())
.body(Body::from(
json!({
"email": "[email protected]",
"password": "1234",
"device": {
"name": "android 1111",
"os": "Android"
}
})
.to_string(),
))
.unwrap();
let response = ServiceExt::<Request<Body>>::ready(&mut app)
.await
.unwrap()
.call(request)
.await
.unwrap();

let response = app
.oneshot(
Request::builder()
.method(http::Method::POST)
.uri(SIGN_UP)
.header(http::header::CONTENT_TYPE, mime::APPLICATION_JSON.as_ref())
.body(Body::from(
json!({
"email": "[email protected]",
"password": "1234",
"device": {
"name": "android 1111",
"os": "Android"
}
})
.to_string(),
))
.unwrap(),
)
assert_eq!(response.status(), StatusCode::CREATED);

let body = response.into_body().collect().await.unwrap().to_bytes();
let body: Value = serde_json::from_slice(&body).unwrap();

assert!(body["data"].is_object());
assert!(body["data"]["access_token"].is_string());
assert!(body["data"]["refresh_token"].is_string());

let request = Request::builder()
.method(http::Method::POST)
.uri(SIGN_UP)
.header(http::header::CONTENT_TYPE, mime::APPLICATION_JSON.as_ref())
.body(Body::from(
json!({
"email": "[email protected]",
"password": "1234",
"device": {
"name": "android 1111",
"os": "Android"
}
})
.to_string(),
))
.unwrap();
let response = ServiceExt::<Request<Body>>::ready(&mut app)
.await
.unwrap()
.call(request)
.await
.unwrap();

Expand Down
2 changes: 1 addition & 1 deletion src/tests/e2e/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
mod auth;
mod session;
mod session;
2 changes: 1 addition & 1 deletion src/tests/e2e/scripts/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ psql postgresql://helios:123456789@localhost:5555/heliosdb -f migrations/0000000
psql postgresql://helios:123456789@localhost:5555/heliosdb -f migrations/2024-08-02-114025_initial/up.sql
psql postgresql://helios:123456789@localhost:5555/heliosdb -f src/tests/e2e/sql/seed.sql

cargo test e2e -- --test-threads=1
cargo test e2e

docker compose -f docker-compose-test.yml down

Expand Down
4 changes: 3 additions & 1 deletion src/tests/e2e/session/create_session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,9 @@ mod test {
http::header::AUTHORIZATION,
format!("Bearer {}", body["data"]["access_token"].as_str().unwrap()),
)
.body(Body::from(json!({"country": "unknown country"}).to_string()))
.body(Body::from(
json!({"country": "unknown country"}).to_string(),
))
.unwrap();
let response = ServiceExt::<Request<Body>>::ready(&mut app)
.await
Expand Down
1 change: 1 addition & 0 deletions src/tests/e2e/session/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
mod close_session;
mod create_session;
8 changes: 0 additions & 8 deletions src/tests/unit/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +0,0 @@
#[cfg(test)]
mod test {
use crate::config::ENV;
#[test]
fn test() {
println!("{:?}", ENV.database_url);
}
}

0 comments on commit a3857ac

Please sign in to comment.