Skip to content

Commit

Permalink
test設計がわかりやすいようにディレクトリ構造を変更
Browse files Browse the repository at this point in the history
  • Loading branch information
yokohama committed Jul 18, 2024
1 parent cfab8aa commit d3fb224
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 71 deletions.
2 changes: 1 addition & 1 deletion backend/bin/seed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ async fn run_migrations(pool: &PgPool) {
async fn run_seeds(pool: &PgPool) {
for i in 1..=5 {
let email = format!("hoge{}@example.com", i);
let new_user = models::user::NewUser {
let new_user = models::user::New {
email,
password: "passpass".to_string(),
};
Expand Down
25 changes: 14 additions & 11 deletions backend/src/controllers/user/point_conditions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,27 @@ use crate::{
};

#[derive(serde::Deserialize)]
pub struct NewPointConditionRequest {
pub struct NewRequest {
lat: f64,
lon: f64,
}

pub async fn index(
State(pool): State<PgPool>,
claims: auth::Claims,
) -> Result<Json<impl Serialize>, error::AppError> {
let current_user = claims.get_current_user(&pool).await?;
let point_conditions: Vec<_> = point_condition::find_by_user_id(
&pool,
current_user.id
).await?;
Ok(Json(point_conditions))
}

pub async fn create(
State(pool): State<PgPool>,
claims: auth::Claims,
Json(payload): Json<NewPointConditionRequest>,
Json(payload): Json<NewRequest>,
) -> Result<Json<impl Serialize>, error::AppError> {
let current_user = claims.get_current_user(&pool).await?;
let new = point_condition::New {
Expand All @@ -30,12 +42,3 @@ pub async fn create(
let created = point_condition::create(&pool, new).await?;
Ok(Json(created))
}

pub async fn index(
State(pool): State<PgPool>,
claims: auth::Claims,
) -> Result<Json<impl Serialize>, error::AppError> {
let current_user = claims.get_current_user(&pool).await?;
let point_conditions: Vec<point_condition::Entry> = point_condition::find_by_user_id(&pool, current_user.id).await?;
Ok(Json(point_conditions))
}
6 changes: 3 additions & 3 deletions backend/src/controllers/user/registration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@ use crate::{
};

#[derive(serde::Deserialize)]
pub struct NewUserRequest {
pub struct NewRequest {
email: String,
password: String,
}

pub async fn create(
State(pool): State<PgPool>,
Json(payload): Json<NewUserRequest>,
Json(payload): Json<NewRequest>,
) -> Result<Json<impl Serialize>, error::AppError> {
let new_user = user::NewUser {
let new_user = user::New {
email: payload.email,
password: payload.password,
};
Expand Down
4 changes: 2 additions & 2 deletions backend/src/midleware/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,12 @@ impl Claims {
pub async fn get_current_user(
&self,
pool: &PgPool
) -> Result<user::CurrentUser, error::AppError> {
) -> Result<user::Current, error::AppError> {
let user_id: i32 = self.sub.parse::<i32>().map_err(|e| {
error!("Failed to parse sub to i32: {:#?}", e);
error::AppError::InternalServerError(e.to_string())
})?;
let current_user = user::CurrentUser::new(&pool, user_id).await?;
let current_user = user::Current::new(&pool, user_id).await?;
Ok(current_user)
}
}
Expand Down
65 changes: 15 additions & 50 deletions backend/src/models/user.rs
Original file line number Diff line number Diff line change
@@ -1,51 +1,47 @@
use chrono::NaiveDateTime;

use serde::Serialize;
use sqlx::{
PgPool,
query_as,
FromRow,
};
use sqlx::{PgPool, query_as, FromRow};

use tracing::error;

use crate::midleware::{auth, error};

#[derive(Serialize, FromRow)]
pub struct User {
pub struct Entry {
pub id: i32,
pub name: Option<String>,
pub email: String,
pub created_at: NaiveDateTime,
pub updated_at: NaiveDateTime,
}

pub struct NewUser {
pub struct New {
pub email: String,
pub password: String,
}

#[derive(FromRow)]
struct UserExists {
struct Exists {
email: String,
}

#[derive(Debug, Serialize, FromRow)]
pub struct CreatedUser {
pub struct Created {
pub id: i32,
pub email: String,
pub created_at: NaiveDateTime,
}

#[derive(Serialize)]
pub struct CurrentUser {
pub struct Current {
pub id: i32,
pub name: Option<String>,
pub email: String,
pub created_at: NaiveDateTime,
pub updated_at: NaiveDateTime,
}
impl CurrentUser {
impl Current {
pub async fn new(
pool: &PgPool,
id: i32
Expand All @@ -65,12 +61,12 @@ impl CurrentUser {
pub async fn find_by_id(
pool: &PgPool,
id: i32
) -> Result<User, error::AppError> {
) -> Result<Entry, error::AppError> {
let sql = r#"
SELECT id, name, email, created_at, updated_at FROM users
WHERE id = $1
"#;
let user: User = query_as::<_, User>(sql)
let user: Entry = query_as::<_, Entry>(sql)
.bind(id)
.fetch_one(pool)
.await
Expand All @@ -81,11 +77,11 @@ pub async fn find_by_id(
Ok(user)
}

pub async fn all(pool: &PgPool) -> Result<Vec<User>, error::AppError> {
pub async fn all(pool: &PgPool) -> Result<Vec<Entry>, error::AppError> {
let sql = r#"
SELECT id, name, email, created_at, updated_at FROM users
"#;
let users: Vec<User> = query_as::<_, User>(sql)
let users: Vec<Entry> = query_as::<_, Entry>(sql)
.fetch_all(pool)
.await
.map_err(|e| {
Expand All @@ -98,15 +94,15 @@ pub async fn all(pool: &PgPool) -> Result<Vec<User>, error::AppError> {

pub async fn create(
pool: &PgPool,
new_user: NewUser
) -> Result<CreatedUser, error::AppError> {
new_user: New
) -> Result<Created, error::AppError> {
let sql = r#"
SELECT email
FROM users
WHERE email = $1
"#;

let user_exists = query_as::<_, UserExists>(sql)
let user_exists = query_as::<_, Exists>(sql)
.bind(&new_user.email)
.fetch_one(pool)
.await;
Expand Down Expand Up @@ -138,7 +134,7 @@ pub async fn create(
error::AppError::InternalServerError(e.to_string())
})?;

let created_user = query_as::<_, CreatedUser>(sql)
let created_user = query_as::<_, Created>(sql)
.bind(&new_user.email)
.bind(encrypted_password)
.fetch_one(pool)
Expand All @@ -156,34 +152,3 @@ pub async fn create(
}
}
}

/*
#[cfg(test)]
mod tests {
use super::*;
use crate::midleware;
#[tokio::test]
async fn test_create_user() {
let pool = midleware::db::get_db_pool().await;
let new_user = NewUser {
email: "[email protected]".to_string(),
password: "password".to_string(),
};
let result = create(&pool, new_user).await;
match result {
Ok(user) => {
assert_eq!(user.email, "[email protected]");
}
Err(e) => {
error!("{:#?}", e);
// panic!("Failed to create user");
}
}
}
}
*/
4 changes: 2 additions & 2 deletions backend/tests/admin_test.rs → backend/tests/e2e/admin.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
mod common;

use reqwest::Client;
use tokio;

use crate::common;

const AUTH_URL: &str = "http://localhost:3000/admin/session";
const EMAIL: &str = "[email protected]";
const PASSWORD: &str = "passpass";
Expand Down
2 changes: 2 additions & 0 deletions backend/tests/e2e/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod admin;
pub mod user;
4 changes: 2 additions & 2 deletions backend/tests/user_test.rs → backend/tests/e2e/user.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
mod common;

use pointbreak::models;
use reqwest::Client;
use reqwest::Response;
use tokio;

use crate::common;

const AUTH_URL: &str = "http://localhost:3000/user/session";
const EMAIL: &str = "[email protected]";
const PASSWORD: &str = "passpass";
Expand Down
2 changes: 2 additions & 0 deletions backend/tests/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod common;
pub mod e2e;

0 comments on commit d3fb224

Please sign in to comment.