//! The other side reviews a draft (phase 13): a version sent on a link, //! marked up there and sent back, read as a diff and taken change by //! change into the next version; every step in the journal. use axum::http::StatusCode; use sqlx::PgPool; use crate::common::{App, local}; const SAME: &[(&str, &str)] = &[("sec-fetch-site", "same-origin")]; /// The value of the input `name` in a page. fn input(page: &str, name: &str) -> String { let at = page .find(&format!("name=\"{name}\"")) .unwrap_or_else(|| panic!("no {name} in {page}")); let rest = &page[at..]; let v = rest.find("value=\"").unwrap() + 7; rest[v..v + rest[v..].find('"').unwrap()].to_string() } async fn performance(pool: &PgPool) -> (i64, i64, String) { sqlx::query_as( "SELECT c.id, p.id, v.body FROM contracts c \ JOIN contract_parties p ON p.contract_id = c.id \ JOIN contract_versions v ON v.contract_id = c.id \ WHERE c.title LIKE 'Performance agreement%' AND v.body <> '' \ ORDER BY v.version DESC LIMIT 1", ) .fetch_one(pool) .await .unwrap() } async fn send(app: &App, contract: i64, party: i64) -> String { let r = app .post( &format!("/contracts/{contract}/reviews"), &[ ("review_party", &party.to_string()), ("review_version", "1"), ("review_message", "Please look at the fee."), ], ) .await; assert_eq!(r.status, StatusCode::OK, "{}", r.text()); r.text() } #[sqlx::test(migrator = "visa::db::MIGRATOR")] async fn the_other_side_marks_a_version_up_and_we_take_their_changes(pool: PgPool) { let app = local(pool.clone()); let today = visa::dates::today(); visa::seed::demo(&pool, today).await.unwrap(); visa::seed::legal::demo(&pool, None, today).await.unwrap(); let (contract, party, body) = performance(&pool).await; // the contract page offers the review; sending makes a link shown once let t = app.get(&format!("/contracts/{contract}")).await.text(); assert!( t.contains("id=\"reviews\"") && t.contains("id=\"send-form\""), "{t}" ); let made = send(&app, contract, party).await; let at = made.find("/r/").expect("the link is shown"); let token: String = made[at + 3..] .chars() .take_while(|c| c.is_ascii_alphanumeric() || *c == '-' || *c == '_') .collect(); assert_eq!(token.len(), 43); let path = format!("/r/{token}"); // their page: the text in the editor, nothing of the staff's app let r = app.get(&path).await; assert_eq!(r.status, StatusCode::OK, "{}", r.text()); let page = r.text(); assert!(page.contains("id=\"rv-editor\"") && page.contains("Please look at the fee.")); assert!(page.contains("review-editor.js")); assert!(!page.contains("/desk"), "no link into the back office"); let csrf = input(&page, "csrf"); let round = input(&page, "round"); // their marks: the fee changed, the travel highlighted, a line added let (head, rest) = body.split_once("45,000").unwrap(); let (mid, tail) = rest.split_once("flight from Berlin").unwrap(); let pieces = serde_json::json!([ ["same", head, ""], ["del", "45,000", ""], ["ins", "60,000", ""], ["same", mid, ""], ["mark", "flight from Berlin", "Business class, please."], ["same", tail, ""], [ "ins", "\n\n# 4. Sound\nThe Festival provides two CDJ-3000 and a DJM-A9.", "" ] ]) .to_string(); let post = |pieces: String, name: &'static str, csrf: String| { let path = path.clone(); let round = round.clone(); let app = &app; async move { app.post_raw( &path, &[ ("csrf", &csrf), ("round", &round), ("pieces", &pieces), ("name", name), ("message", "Our counter offer."), ], SAME, ) .await } }; // a change to our text without a mark is refused let sneaky = serde_json::json!([["same", body.replace("45,000", "90,000"), ""]]).to_string(); let r = post(sneaky, "Ann", csrf.clone()).await; assert_eq!(r.status, StatusCode::UNPROCESSABLE_ENTITY); assert!(r.text().contains("does not match the version we sent")); // without a name the marks come back in the editor let r = post(pieces.clone(), "", csrf.clone()).await; assert_eq!(r.status, StatusCode::UNPROCESSABLE_ENTITY); let t = r.text(); assert!( t.contains("Say who sends this back") && t.contains("60,000"), "{t}" ); // the staff's token is no token here let r = post(pieces.clone(), "Ann", "x".repeat(43)).await; assert_eq!(r.status, StatusCode::FORBIDDEN); let r = post(pieces.clone(), "Ann Festival", csrf.clone()).await; assert_eq!(r.status, StatusCode::SEE_OTHER, "{}", r.text()); // once a round let r = post(pieces.clone(), "Ann Festival", csrf.clone()).await; assert_eq!(r.status, StatusCode::CONFLICT); let t = app.get(&path).await.text(); assert!( t.contains("Thank you") && t.contains("2 changes and 1 highlight"), "{t}" ); // the journal has both, with the fingerprint of their text let events: Vec<(String, String)> = sqlx::query_as( "SELECT event, detail FROM journal WHERE contract_id = $1 AND event IN \ ('sent for review', 'review returned') ORDER BY seq", ) .bind(contract) .fetch_all(&pool) .await .unwrap(); assert_eq!(events.len(), 2, "{events:?}"); assert!( events[1] .1 .contains("(Ann Festival) sent version 1 back with 2 changes and 1 highlight") ); let sha: String = sqlx::query_scalar("SELECT reply_sha256 FROM review_rounds WHERE id = $1") .bind(round.parse::().unwrap()) .fetch_one(&pool) .await .unwrap(); assert!(events[1].1.contains(&sha)); assert!(visa::journal::verify(&pool).await.unwrap().whole()); // our side: the changes, the highlight and the git view let review = format!("/contracts/{contract}/reviews/{round}"); let t = app.get(&review).await.text(); assert!(t.contains("id=\"take-form\"") && t.contains("Business class, please.")); assert!(t.contains("class=\"gitdiff\"") && t.contains("Our counter offer.")); // take only the fee: the version form starts with it, without the sound let r = app.post(&format!("{review}/take"), &[("take", "1")]).await; assert_eq!(r.status, StatusCode::OK); let t = r.text(); assert!(t.contains("60,000 THB") && !t.contains("CDJ-3000"), "{t}"); assert!(t.contains("1 of 2 changes by")); // every ticked box counts let t = app .post(&format!("{review}/take"), &[("take", "1"), ("take", "2")]) .await .text(); assert!(t.contains("60,000 THB") && t.contains("CDJ-3000")); assert!( t.contains("Ann Festival") || t.contains("changes of"), "{t}" ); // the next version goes on the same link, as a new round let made = send(&app, contract, party).await; assert!( made.contains("on the link") && !made.contains("/r/"), "{made}" ); let t = app.get(&path).await.text(); assert!(t.contains("id=\"rv-editor\"") && t.contains("id=\"earlier\"")); // a reply to the old round is refused let r = post(pieces, "Ann Festival", csrf).await; assert_eq!(r.status, StatusCode::CONFLICT); // the database keeps what they sent assert!( sqlx::query("UPDATE review_rounds SET reply_body = 'other' WHERE id = $1") .bind(round.parse::().unwrap()) .execute(&pool) .await .is_err() ); assert!( sqlx::query("DELETE FROM review_rounds") .execute(&pool) .await .is_err() ); // closing the link ends it let link: i64 = sqlx::query_scalar("SELECT id FROM review_links WHERE party_id = $1") .bind(party) .fetch_one(&pool) .await .unwrap(); let r = app .post( &format!("/contracts/{contract}/review-links/{link}/close"), &[], ) .await; assert_eq!(r.status, StatusCode::SEE_OTHER); assert_eq!(app.get(&path).await.status, StatusCode::GONE); assert_eq!( app.get(&format!("/r/{}", "a".repeat(43))).await.status, StatusCode::NOT_FOUND ); } #[sqlx::test(migrator = "visa::db::MIGRATOR")] async fn only_a_live_party_and_a_version_with_a_text_can_be_sent(pool: PgPool) { let app = local(pool.clone()); let today = visa::dates::today(); visa::seed::demo(&pool, today).await.unwrap(); visa::seed::legal::demo(&pool, None, today).await.unwrap(); let (contract, party, _) = performance(&pool).await; let other: i64 = sqlx::query_scalar("SELECT id FROM contract_parties WHERE contract_id <> $1 LIMIT 1") .bind(contract) .fetch_one(&pool) .await .unwrap(); let to = format!("/contracts/{contract}/reviews"); for form in [ vec![ ("review_party", other.to_string()), ("review_version", "1".to_string()), ], vec![ ("review_party", party.to_string()), ("review_version", "9".to_string()), ], ] { let pairs: Vec<(&str, &str)> = form.iter().map(|(a, b)| (*a, b.as_str())).collect(); assert_eq!( app.post(&to, &pairs).await.status, StatusCode::UNPROCESSABLE_ENTITY ); } // the demo's label review reads as a diff with its four changes let (c, round): (i64, i64) = sqlx::query_as( "SELECT l.contract_id, r.id FROM review_rounds r JOIN review_links l ON l.id = r.link_id \ WHERE r.replied_at IS NOT NULL LIMIT 1", ) .fetch_one(&pool) .await .unwrap(); let t = app .get(&format!("/contracts/{c}/reviews/{round}")) .await .text(); assert!( t.contains("id=\"change-4\"") && !t.contains("id=\"change-5\""), "{t}" ); assert!(t.contains("We need fifteen for the catalogue.")); let t = app.get(&format!("/contracts/{c}")).await.text(); assert!(t.contains("sent back") && t.contains("4 changes")); }