Integration tests: homepage (WIP)
This commit is contained in:
parent
fb62288161
commit
1485110204
6 changed files with 556 additions and 41 deletions
78
backend/tests/http.rs
Normal file
78
backend/tests/http.rs
Normal file
|
|
@ -0,0 +1,78 @@
|
|||
use std::error::Error;
|
||||
|
||||
use axum_test::TestServer;
|
||||
use scraper::Html;
|
||||
|
||||
use recipes::{app, config, data::db, log};
|
||||
|
||||
#[tokio::test]
|
||||
async fn homepage() -> Result<(), Box<dyn Error>> {
|
||||
// Arrange.
|
||||
let state = common_state().await?;
|
||||
let user_id = create_user(&state.db_connection, "president@spaceball.planet", "12345").await?;
|
||||
let _recipe_id = create_recipe(&state.db_connection, user_id, "spaghetti").await?;
|
||||
let server = TestServer::new(app::make_service(state))?;
|
||||
|
||||
// Act.
|
||||
let response = server.get("/").await;
|
||||
|
||||
// Assert.
|
||||
response.assert_status_ok();
|
||||
// TODO: check if 'spaghetti' is in the list.
|
||||
let document = Html::parse_document(&response.text());
|
||||
assert_eq!(document.errors.len(), 0);
|
||||
|
||||
// println!("{:?}", document.errors);
|
||||
// println!("{:?}", response);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn create_user(
|
||||
db_connection: &db::Connection,
|
||||
email: &str,
|
||||
password: &str,
|
||||
) -> Result<i64, Box<dyn Error>> {
|
||||
if let db::user::SignUpResult::UserCreatedWaitingForValidation(token) = db_connection
|
||||
.sign_up(email, password, chrono::Weekday::Mon)
|
||||
.await?
|
||||
{
|
||||
if let db::user::ValidationResult::Ok(_, user_id) = db_connection
|
||||
.validation(
|
||||
&token,
|
||||
chrono::Duration::hours(1),
|
||||
"127.0.0.1",
|
||||
"Mozilla/5.0",
|
||||
)
|
||||
.await?
|
||||
{
|
||||
Ok(user_id)
|
||||
} else {
|
||||
Err(Box::<dyn Error>::from("Unable to validate user"))
|
||||
}
|
||||
} else {
|
||||
Err(Box::<dyn Error>::from("Unable to sign up"))
|
||||
}
|
||||
}
|
||||
|
||||
async fn create_recipe(
|
||||
db_connection: &db::Connection,
|
||||
user_id: i64,
|
||||
title: &str,
|
||||
) -> Result<i64, Box<dyn Error>> {
|
||||
let recipe_id = db_connection.create_recipe(user_id).await?;
|
||||
db_connection.set_recipe_title(recipe_id, title).await?;
|
||||
db_connection.set_recipe_is_public(recipe_id, true).await?;
|
||||
Ok(recipe_id)
|
||||
}
|
||||
|
||||
async fn common_state() -> Result<app::AppState, Box<dyn Error>> {
|
||||
let db_connection = db::Connection::new_in_memory().await?;
|
||||
let config = config::Config::default();
|
||||
let log = log::Log::new_stdout_only();
|
||||
Ok(app::AppState {
|
||||
config,
|
||||
db_connection,
|
||||
log,
|
||||
})
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue