Add a HTTP test for recipe edit (WIP)
This commit is contained in:
parent
a4e321e66d
commit
9c368e82ab
4 changed files with 197 additions and 42 deletions
|
|
@ -56,4 +56,4 @@ axum-test = "17"
|
|||
cookie = "0.18"
|
||||
scraper = "0.23"
|
||||
mockall = "0.13"
|
||||
scanf = "1.2"
|
||||
sscanf = "0.4"
|
||||
|
|
|
|||
|
|
@ -13,7 +13,6 @@ use crate::{
|
|||
data::db,
|
||||
html_templates::*,
|
||||
log::Log,
|
||||
ron_utils,
|
||||
translation::Sentence,
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -1,11 +1,14 @@
|
|||
use std::{error::Error, sync::Arc};
|
||||
|
||||
use axum::http;
|
||||
use axum_test::TestServer;
|
||||
use common::ron_api;
|
||||
use cookie::Cookie;
|
||||
use scraper::{ElementRef, Html, Selector};
|
||||
use serde::Serialize;
|
||||
use sscanf::sscanf;
|
||||
|
||||
use recipes::{app, email};
|
||||
use serde::Serialize;
|
||||
|
||||
mod utils;
|
||||
|
||||
|
|
@ -109,7 +112,6 @@ pub struct SignUpFormData {
|
|||
|
||||
#[tokio::test]
|
||||
async fn sign_up() -> Result<(), Box<dyn Error>> {
|
||||
use scanf::sscanf;
|
||||
use std::{cell::RefCell, rc::Rc};
|
||||
|
||||
// Arrange.
|
||||
|
|
@ -124,12 +126,12 @@ async fn sign_up() -> Result<(), Box<dyn Error>> {
|
|||
})
|
||||
.times(1)
|
||||
.returning_st(move |_email_sender, _email_receiver, _title, message| {
|
||||
sscanf!(
|
||||
let url = sscanf!(
|
||||
message,
|
||||
"Follow this link to confirm your inscription, http://127.0.0.1:8000{}",
|
||||
*validation_url_clone.borrow_mut()
|
||||
"Follow this link to confirm your inscription, http://127.0.0.1:8000{String}"
|
||||
)
|
||||
.unwrap();
|
||||
*validation_url_clone.borrow_mut() = url;
|
||||
Ok(())
|
||||
});
|
||||
|
||||
|
|
@ -219,8 +221,113 @@ async fn sign_in() -> Result<(), Box<dyn Error>> {
|
|||
// Assert.
|
||||
response.assert_status_see_other(); // Redirection after successful sign in.
|
||||
response.assert_text("");
|
||||
// English is the default language.
|
||||
response.assert_header("location", "/?user_message=16&user_message_icon=0");
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn create_recipe_and_edit_it() -> Result<(), Box<dyn Error>> {
|
||||
// Arrange.
|
||||
let state = utils::common_state().await?;
|
||||
let _user_id = utils::create_user(
|
||||
&state.db_connection,
|
||||
"president@spaceball.planet",
|
||||
"12345678",
|
||||
)
|
||||
.await?;
|
||||
let token = utils::sign_in(
|
||||
&state.db_connection,
|
||||
"president@spaceball.planet",
|
||||
"12345678",
|
||||
)
|
||||
.await?;
|
||||
let server = TestServer::new(app::make_service(state))?;
|
||||
|
||||
// Act.
|
||||
let cookie = Cookie::new("auth_token", token);
|
||||
|
||||
let response = server.get("/recipe/new").add_cookie(cookie.clone()).await;
|
||||
response.assert_status_see_other();
|
||||
|
||||
let location = response.header("location").to_str().unwrap().to_string();
|
||||
let recipe_id = sscanf!(&location, "/en/recipe/edit/{i64}").unwrap();
|
||||
|
||||
let response = server.get(&location).add_cookie(cookie.clone()).await;
|
||||
response.assert_status_ok();
|
||||
|
||||
let response = server
|
||||
.patch("/ron-api/recipe/title")
|
||||
.add_cookie(cookie.clone())
|
||||
.add_header(http::header::CONTENT_TYPE, common::consts::MIME_TYPE_RON)
|
||||
.bytes(
|
||||
ron_api::to_string(ron_api::SetRecipeTitle {
|
||||
recipe_id,
|
||||
title: "AAA".into(),
|
||||
})
|
||||
.unwrap()
|
||||
.into(),
|
||||
)
|
||||
.await;
|
||||
response.assert_status_ok();
|
||||
|
||||
let response = server
|
||||
.patch("/ron-api/recipe/description")
|
||||
.add_cookie(cookie.clone())
|
||||
.add_header(http::header::CONTENT_TYPE, common::consts::MIME_TYPE_RON)
|
||||
.bytes(
|
||||
ron_api::to_string(ron_api::SetRecipeDescription {
|
||||
recipe_id,
|
||||
description: "BBB".into(),
|
||||
})
|
||||
.unwrap()
|
||||
.into(),
|
||||
)
|
||||
.await;
|
||||
response.assert_status_ok();
|
||||
|
||||
let response = server
|
||||
.patch("/ron-api/recipe/servings")
|
||||
.add_cookie(cookie.clone())
|
||||
.add_header(http::header::CONTENT_TYPE, common::consts::MIME_TYPE_RON)
|
||||
.bytes(
|
||||
ron_api::to_string(ron_api::SetRecipeServings {
|
||||
recipe_id,
|
||||
servings: Some(42),
|
||||
})
|
||||
.unwrap()
|
||||
.into(),
|
||||
)
|
||||
.await;
|
||||
response.assert_status_ok();
|
||||
|
||||
// TODO: Set other recipe fields.
|
||||
|
||||
// Assert.
|
||||
let response = server
|
||||
.get(&format!("/recipe/edit/{}", recipe_id))
|
||||
.add_cookie(cookie.clone())
|
||||
.await;
|
||||
response.assert_status_ok();
|
||||
|
||||
let document = Html::parse_document(&response.text());
|
||||
if !document.errors.is_empty() {
|
||||
panic!("{:?}", document.errors);
|
||||
}
|
||||
|
||||
let title_selector = Selector::parse("#input-title").unwrap();
|
||||
let element_title = document.select(&title_selector).next().unwrap();
|
||||
assert_eq!(element_title.attr("value").unwrap(), "AAA");
|
||||
|
||||
let description_selector = Selector::parse("#text-area-description").unwrap();
|
||||
let element_description = document.select(&description_selector).next().unwrap();
|
||||
assert_eq!(element_description.inner_html(), "BBB");
|
||||
|
||||
let servings_selector = Selector::parse("#input-servings").unwrap();
|
||||
let element_servings = document.select(&servings_selector).next().unwrap();
|
||||
assert_eq!(element_servings.attr("value").unwrap(), "42");
|
||||
|
||||
// dbg!(response);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue