Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
robatipoor committed Dec 12, 2023
1 parent d73dca5 commit b5fef18
Show file tree
Hide file tree
Showing 10 changed files with 23 additions and 27 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# RUSTfulapi (Please refrain from using this for production purposes at the moment as it is currently undergoing significant development.)
Reusable template for building REST Web Services in Rust. Uses [Axum](https://github.com/tokio-rs/axum) HTTP web framework and [SeaORM](https://github.com/SeaQL/sea-orm) Toolkit and [PostgreSQL](https://www.postgresql.org/).
Reusable template for building REST Web Services in Rust. Uses [Axum](https://github.com/tokio-rs/axum) HTTP web framework and [SeaORM](https://github.com/SeaQL/sea-orm) ORM and [PostgreSQL](https://www.postgresql.org/).

![License](https://img.shields.io/github/license/robatipoor/rustfulapi)
![Lines of code](https://img.shields.io/tokei/lines/github/robatipoor/rustfulapi)
Expand Down Expand Up @@ -44,7 +44,7 @@ To use this template as your project starting point, click "Use this template" a
```bash
./run.sh
# open swagger panel
xdg-open http://127.0.0.1:8080/api/v1/swagger-ui/
xdg-open http://127.0.0.1:8080/swagger-ui/
# manually testing your API routes with curl commands
curl -X GET http://127.0.0.1:8080/api/v1/server/health_check
```
Expand Down
5 changes: 3 additions & 2 deletions src/client/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,12 @@ pub async fn migrate_database(db: &DatabaseConnection) -> AppResult {

#[cfg(test)]
mod tests {
use crate::constant::DATABASE;
use super::*;
use crate::constant::CONFIG;

#[tokio::test]
async fn test_ping_database() {
DATABASE()
DatabaseClient::build_from_config(&CONFIG)
.await
.unwrap()
.ping()
Expand Down
2 changes: 1 addition & 1 deletion src/client/email.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ impl ClientBuilder for EmailClient {
impl EmailClientExt for EmailClient {
async fn send_email(&self, email: &Email) -> AppResult {
let resp = self.send(Message::try_from(email)?).await?;
info!("sent email successfully code: {:?}", resp.code());
info!("Sent email successfully code: {:?}.", resp.code());
Ok(())
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/configure/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ mod tests {

#[test]
pub fn test_profile_to_string() {
let profile: Profile = Profile::try_from("Dev").unwrap();
let profile: Profile = Profile::try_from("dev").unwrap();
assert_eq!(profile, Profile::Dev)
}
}
6 changes: 0 additions & 6 deletions src/constant/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,6 @@ pub static ACCESS_TOKEN_DECODE_KEY: Lazy<DecodingKey> = Lazy::new(|| {
let key = CONFIG.secret.read_public_access_key().unwrap();
DecodingKey::from_rsa_pem(key.as_bytes()).unwrap()
});
#[allow(non_snake_case)]
pub async fn DATABASE() -> AppResult<&'static DatabaseClient> {
static DB: tokio::sync::OnceCell<DatabaseClient> = tokio::sync::OnceCell::const_new();
DB.get_or_try_init(|| async { DatabaseClient::build_from_config(&CONFIG).await })
.await
}
pub static API_DOC: Lazy<utoipa::openapi::OpenApi> = Lazy::new(ApiDoc::openapi);
pub static TEMPLATE_ENGIN: Lazy<TemplateEngine> = Lazy::new(|| {
let path = util::dir::root_dir("static/template/**/*")
Expand Down
10 changes: 7 additions & 3 deletions src/entity/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ use sea_orm::{DatabaseTransaction, TransactionTrait};
use test_context::AsyncTestContext;
use tracing::info;

use crate::error::ResourceType;
use crate::{
client::database::{DatabaseClient, DatabaseClientExt},
constant::CONFIG,
error::ResourceType,
};

pub mod message;
pub mod role;
Expand All @@ -20,9 +24,9 @@ pub struct TransactionTestContext {
impl AsyncTestContext for TransactionTestContext {
async fn setup() -> Self {
info!("Setup database for the test.");
let conn = crate::constant::DATABASE().await.unwrap();
let db = DatabaseClient::build_from_config(&CONFIG).await.unwrap();
Self {
tx: conn.begin().await.unwrap(),
tx: db.begin().await.unwrap(),
}
}

Expand Down
12 changes: 5 additions & 7 deletions src/handler/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,17 +172,15 @@ pub async fn logout(
pub async fn forget_password(
State(state): State<AppState>,
Query(param): Query<ForgetPasswordQueryParam>,
) -> AppResult<Json<MessageResponse>> {
) -> AppResult<Json<ForgetPasswordResponse>> {
info!("Forget password user query parameter: {param:?}");
match service::user::forget_password(&state, param).await {
Ok(_) => {
info!("success forget password user response");
Ok(Json(MessageResponse {
message: "Please check your email".to_string(),
}))
Ok(resp) => {
info!("Success forget password user response.");
Ok(Json(resp))
}
Err(e) => {
warn!("unsuccessful forget password user: {e:?}");
warn!("Unsuccessful forget password user: {e:?}.");
Err(e)
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/repo/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ pub async fn update_password(
) -> AppResult<()> {
entity::user::Entity::update_many()
.col_expr(entity::user::Column::Password, Expr::value(password))
.filter(entity::user::Column::Id.contains(user_id))
.filter(entity::user::Column::Id.eq(user_id))
.exec(db)
.await?;
Ok(())
Expand Down
3 changes: 2 additions & 1 deletion tests/api/helper/user.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::collections::HashMap;

use chrono::Utc;
use fake::faker::internet::en::FreeEmail;
use fake::{Fake, Faker};
use rustfulapi::{entity, util};
use rustfulapi::{entity::role::RoleUser, error::AppResult};
Expand All @@ -23,7 +24,7 @@ impl TestUser {
password: Set(util::password::hash(password.clone()).await?),
id: Set(Uuid::new_v4()),
username: Set(Faker.fake::<String>()),
email: Set(Faker.fake::<String>()),
email: Set(FreeEmail().fake::<String>()),
role: Set(role),
is_active: Set(true),
is_2fa: Set(false),
Expand Down
4 changes: 1 addition & 3 deletions tests/api/user_endpoint_tests/test_user_logout.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
use crate::{assert_err, assert_ok, context::seeder::SeedDbTestContext, unwrap};
use reqwest::StatusCode;
use crate::{assert_ok, context::seeder::SeedDbTestContext, unwrap};
use rustfulapi::{
dto::{LoginRequest, LoginResponse},
entity::role::RoleUser,
error::AppResponseError,
};
use test_context::test_context;

Expand Down

0 comments on commit b5fef18

Please sign in to comment.