Skip to content

Commit

Permalink
don't update recipe image when updating recipe data
Browse files Browse the repository at this point in the history
  • Loading branch information
Austionian committed Dec 8, 2023
1 parent 31ed8b8 commit cb1a0eb
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 8 deletions.
6 changes: 2 additions & 4 deletions src/routes/admin/recipe/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,12 @@ async fn update_recipe_db(
SET
name = $1,
ingredients = $2,
steps = $3,
image_url = $4
WHERE id = $5
steps = $3
WHERE id = $4
"#,
data.name,
&data.ingredients,
&data.steps,
data.image_url,
recipe_uuid
)
.execute(db_pool)
Expand Down
25 changes: 21 additions & 4 deletions tests/api/admin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,10 @@ async fn admin_users_can_crud_recipes() {
assert_eq!(recipe.ingredients.unwrap().len(), 1);
assert_eq!(recipe.image_url.unwrap(), image_url);

let new_image_url = "https:://new_fake_url.com";

// Part Two: Update the recipe
let body = serde_json::json!({
"name": name,
"image_url": new_image_url,
"image_url": "",
"steps": [
"step",
"step2"
Expand All @@ -57,7 +55,7 @@ async fn admin_users_can_crud_recipes() {
assert_eq!(recipe.name, name.to_string());
assert_eq!(recipe.steps.unwrap().len(), 2);
assert_eq!(recipe.ingredients.unwrap().len(), 0);
assert_eq!(recipe.image_url.unwrap(), new_image_url);
assert_eq!(recipe.image_url.unwrap(), image_url);

// Part Three: Delete the recipe
let response = app.delete_recipe(&recipe.id.to_string()).await;
Expand All @@ -72,6 +70,25 @@ async fn admin_users_can_crud_recipes() {
assert_eq!(recipes.len(), 0);
}

#[tokio::test]
async fn admin_users_should_be_able_to_update_recipe_images() {
let app = spawn_app().await;
let new_url = "https://www.new_fake_url.com";
let body = serde_json::json!({
"image_url": new_url,
});
let response = app.update_recipe_image(&body).await;

assert_eq!(response.status().as_u16(), 200);

let recipe = sqlx::query!("SELECT * FROM recipe WHERE id = $1", app.recipe.id)
.fetch_one(&app.db_pool)
.await
.expect("Failed to get the updated recipe.");

assert_eq!(recipe.image_url.unwrap(), new_url);
}

#[tokio::test]
async fn admin_users_can_crud_fish_types() {
// Part One: Create new fish type and a fish instance of the type
Expand Down
22 changes: 22 additions & 0 deletions tests/api/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,26 @@ impl TestApp {
.expect("Failed to post recipe update.")
}

pub async fn update_recipe_image<Body>(&self, body: &Body) -> reqwest::Response
where
Body: serde::Serialize,
{
self.api_client
.put(format!(
"{}/v1/admin/recipe/{}/image",
&self.address, &self.recipe.id
))
.json(body)
.header(
"Cookie",
&format!("user_id={}", &self.admin_user.user_id.to_string()),
)
.header("Authorization", &format!("Bearer {}", &self.api_key))
.send()
.await
.expect("Failed to post recipe image update.")
}

pub async fn delete_recipe(&self, recipe_id: &str) -> reqwest::Response {
self.api_client
.delete(format!("{}/v1/admin/recipe/{}", &self.address, recipe_id))
Expand Down Expand Up @@ -695,6 +715,7 @@ impl Fish {
pub struct Recipe {
pub id: Uuid,
pub name: String,
pub image_url: Option<String>,
pub ingredients: Option<Vec<String>>,
pub steps: Option<Vec<String>>,
}
Expand All @@ -704,6 +725,7 @@ impl Recipe {
Self {
id: uuid::Uuid::new_v4(),
name: uuid::Uuid::new_v4().to_string(),
image_url: None,
ingredients: None,
steps: None,
}
Expand Down

0 comments on commit cb1a0eb

Please sign in to comment.