One shared draft a contract, locked when all agree (phase 13, step 2)
Until a contract is set, it is one open text for every side, as Alex asked: our team edits it in the app, each other party on its link. Edits are tracked changes in the author's color (crossed out, added, highlighted with a comment) until the other side accepts or rejects them. When no change or highlight is open and Lanna Desk and every party agree, the text becomes a contract version that never changes, and the draft locks. - One side edits at a time: Edit takes the draft for 45 seconds, which every save renews; Done editing, leaving the page or silence gives it up. The editor saves 0.7 s after the last keystroke; the other pages ask for news every 3 s, so each side sees edits, who edits, agreements and the lock without reloading. - A save may only touch the saver's own marks (reviews::check_save); accept, reject (or withdraw) and resolve are their own steps, journaled with the text concerned. A later change voids the agreements given. - Notices: the other side's edits (one a half hour of editing), settled changes, agreements, the lock. The bell in the top bar counts the unseen (every 15 s); Notices lists them and marks them seen. - The draft as a diff: the standing text against every open change taken. - Migration 0015: contract_drafts, draft_saves and draft_agreements (both kept unchanged by a trigger), notices, notice_reads; drops step 1's review_rounds (only the demo held any). The review links stay as each party's way in. - Property tests for the save rule and settling, the fuzz text target runs the parser, the save check and settling; integration tests play both sides to the lock. The demo's recording agreement has an open draft with the label's changes and a notice. - PLAN: phase 13 rewritten; step 3 is the PDF and signing.
This commit is contained in:
1 parent
a4155548b8
commit
3dd9ab60e8
36 files changed
+3812
-2161
No files matched your search
+353
@@ -0,0 +1,353 @@
|
||||
//! The shared draft (phase 13): opened from a version, edited with tracked
|
||||
//! changes by our team and a party on its link, one editor at a time;
|
||||
//! changes accepted, agreement voided by a later change, and the text
|
||||
//! locked as a version once every side agrees. The other side's work
|
||||
//! shows on the bell.
|
||||
|
||||
use axum::http::StatusCode;
|
||||
use serde_json::Value;
|
||||
use sqlx::PgPool;
|
||||
|
||||
use crate::common::{App, local};
|
||||
|
||||
const SAME: &[(&str, &str)] = &[("sec-fetch-site", "same-origin")];
|
||||
|
||||
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 AND p.removed_at IS NULL \
|
||||
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()
|
||||
}
|
||||
|
||||
fn json(text: &str) -> Value {
|
||||
serde_json::from_str(text).unwrap_or_else(|e| panic!("{e}: {text}"))
|
||||
}
|
||||
|
||||
/// The JSON in the editor's data block.
|
||||
fn data_of(page: &str) -> Value {
|
||||
let at = page.find("id=\"dr-data\">").expect("the editor's data") + 13;
|
||||
let end = at + page[at..].find("</script>").unwrap();
|
||||
json(&page[at..end])
|
||||
}
|
||||
|
||||
struct Party<'a> {
|
||||
app: &'a App,
|
||||
path: String,
|
||||
csrf: String,
|
||||
}
|
||||
|
||||
impl Party<'_> {
|
||||
async fn act(&self, action: &str, fields: &[(&str, &str)]) -> (StatusCode, Value) {
|
||||
let mut pairs = vec![("csrf", self.csrf.as_str())];
|
||||
pairs.extend_from_slice(fields);
|
||||
let r = self
|
||||
.app
|
||||
.post_raw(&format!("{}/{action}", self.path), &pairs, SAME)
|
||||
.await;
|
||||
(r.status, json(&r.text()))
|
||||
}
|
||||
}
|
||||
|
||||
async fn staff(app: &App, c: i64, action: &str, fields: &[(&str, &str)]) -> (StatusCode, Value) {
|
||||
let r = app
|
||||
.post(&format!("/contracts/{c}/draft/{action}"), fields)
|
||||
.await;
|
||||
(r.status, json(&r.text()))
|
||||
}
|
||||
|
||||
fn rev(v: &Value) -> String {
|
||||
v["state"]["rev"]
|
||||
.as_i64()
|
||||
.or(v["rev"].as_i64())
|
||||
.unwrap()
|
||||
.to_string()
|
||||
}
|
||||
|
||||
#[sqlx::test(migrator = "visa::db::MIGRATOR")]
|
||||
async fn both_sides_edit_one_draft_until_all_agree_and_it_locks(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 (c, party, body) = performance(&pool).await;
|
||||
let versions_before: i64 =
|
||||
sqlx::query_scalar("SELECT count(*) FROM contract_versions WHERE contract_id = $1")
|
||||
.bind(c)
|
||||
.fetch_one(&pool)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
// the demo's own notice is seen already
|
||||
app.get("/notices").await;
|
||||
|
||||
// the contract page offers the draft; it opens from version 1
|
||||
let t = app.get(&format!("/contracts/{c}")).await.text();
|
||||
assert!(t.contains("id=\"draft-open-form\""), "{t}");
|
||||
let r = app
|
||||
.post(
|
||||
&format!("/contracts/{c}/draft/open"),
|
||||
&[("draft_version", "1")],
|
||||
)
|
||||
.await;
|
||||
assert_eq!(r.location(), format!("/contracts/{c}/draft"));
|
||||
let page = app.get(&format!("/contracts/{c}/draft")).await.text();
|
||||
let state = &data_of(&page)["state"];
|
||||
assert_eq!(state["state"], "open");
|
||||
assert_eq!(state["need"], 2, "Lanna Desk and the festival");
|
||||
|
||||
// the festival's link
|
||||
let made = app
|
||||
.post(
|
||||
&format!("/contracts/{c}/invites"),
|
||||
&[("party", &party.to_string())],
|
||||
)
|
||||
.await
|
||||
.text();
|
||||
let at = made.find("/r/").expect("the link is shown");
|
||||
let token: String = made[at + 3..]
|
||||
.chars()
|
||||
.take_while(|ch| ch.is_ascii_alphanumeric() || *ch == '-' || *ch == '_')
|
||||
.collect();
|
||||
let path = format!("/r/{token}");
|
||||
let r = app.get(&path).await;
|
||||
assert_eq!(r.status, StatusCode::OK, "{}", r.text());
|
||||
let page = r.text();
|
||||
assert!(!page.contains("/desk"), "nothing of the back office");
|
||||
let data = data_of(&page);
|
||||
let them = Party {
|
||||
app: &app,
|
||||
path: path.clone(),
|
||||
csrf: data["csrf"].as_str().unwrap().to_string(),
|
||||
};
|
||||
let mut now = data["state"]["rev"].as_i64().unwrap().to_string();
|
||||
|
||||
// one editor at a time
|
||||
let (s, _) = staff(&app, c, "take", &[("rev", &now)]).await;
|
||||
assert_eq!(s, StatusCode::OK);
|
||||
let (s, v) = them.act("take", &[("rev", &now)]).await;
|
||||
assert_eq!(s, StatusCode::CONFLICT);
|
||||
assert!(v["error"].as_str().unwrap().contains("is editing"), "{v}");
|
||||
staff(&app, c, "release", &[("rev", &now)]).await;
|
||||
let (s, _) = them.act("take", &[("rev", &now)]).await;
|
||||
assert_eq!(s, StatusCode::OK);
|
||||
|
||||
// the festival's tracked changes: the fee, and a highlight
|
||||
let side = format!("party:{party}");
|
||||
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", "", side],
|
||||
["ins", "60,000", "", side],
|
||||
["same", mid, "", ""],
|
||||
[
|
||||
"mark",
|
||||
"flight from Berlin",
|
||||
"Business class, please.",
|
||||
side
|
||||
],
|
||||
["same", tail, "", ""]
|
||||
])
|
||||
.to_string();
|
||||
// a change to the text as it stands is refused
|
||||
let sneaky =
|
||||
serde_json::json!([["same", body.replace("45,000", "90,000"), "", ""]]).to_string();
|
||||
let (s, _) = them
|
||||
.act("save", &[("rev", &now), ("pieces", &sneaky)])
|
||||
.await;
|
||||
assert_eq!(s, StatusCode::UNPROCESSABLE_ENTITY);
|
||||
// marks made in our name too
|
||||
let forged = pieces.replace(&side, "us");
|
||||
let (s, _) = them
|
||||
.act("save", &[("rev", &now), ("pieces", &forged)])
|
||||
.await;
|
||||
assert_eq!(s, StatusCode::UNPROCESSABLE_ENTITY);
|
||||
let (s, v) = them
|
||||
.act("save", &[("rev", &now), ("pieces", &pieces)])
|
||||
.await;
|
||||
assert_eq!(s, StatusCode::OK, "{v}");
|
||||
now = rev(&v);
|
||||
// we cannot save while they edit
|
||||
let (s, _) = staff(&app, c, "save", &[("rev", &now), ("pieces", &pieces)]).await;
|
||||
assert_eq!(s, StatusCode::CONFLICT);
|
||||
them.act("release", &[("rev", &now)]).await;
|
||||
|
||||
// the bell rings for us, and the list marks it seen
|
||||
let n = json(&app.get("/notices/count").await.text());
|
||||
assert_eq!(n["unread"], 1, "{n}");
|
||||
let t = app.get("/notices").await.text();
|
||||
assert!(t.contains("changed the draft of"), "{t}");
|
||||
assert_eq!(json(&app.get("/notices/count").await.text())["unread"], 0);
|
||||
|
||||
// our view has their change; we take it in
|
||||
let st = json(&app.get(&format!("/contracts/{c}/draft/state")).await.text());
|
||||
assert_eq!(st["rev"].as_i64().unwrap().to_string(), now);
|
||||
let (s, v) = staff(&app, c, "accept", &[("rev", &now), ("n", "1")]).await;
|
||||
assert_eq!(s, StatusCode::OK, "{v}");
|
||||
now = rev(&v);
|
||||
// they cannot accept their own; nothing is left to accept anyway
|
||||
let (s, _) = them.act("accept", &[("rev", &now), ("n", "1")]).await;
|
||||
assert_eq!(s, StatusCode::CONFLICT);
|
||||
|
||||
// an open highlight blocks an agreement; resolved, we agree
|
||||
let (s, _) = staff(&app, c, "agree", &[("rev", &now)]).await;
|
||||
assert_eq!(s, StatusCode::UNPROCESSABLE_ENTITY);
|
||||
let (_, v) = staff(&app, c, "resolve", &[("rev", &now), ("k", "1")]).await;
|
||||
now = rev(&v);
|
||||
// we agree; their next edit voids it
|
||||
let (s, v) = staff(&app, c, "agree", &[("rev", &now)]).await;
|
||||
assert_eq!(s, StatusCode::OK, "{v}");
|
||||
assert_eq!(v["state"]["have"], 1);
|
||||
them.act("take", &[("rev", &now)]).await;
|
||||
let added = format!(
|
||||
"{},[\"ins\",\"\\nSound check at 18:00.\",\"\",\"{side}\"]]",
|
||||
&pieces[..pieces.len() - 1]
|
||||
);
|
||||
let accepted = added
|
||||
.replace(&format!("[\"del\",\"45,000\",\"\",\"{side}\"],"), "")
|
||||
.replace(
|
||||
&format!("[\"ins\",\"60,000\",\"\",\"{side}\"]"),
|
||||
"[\"same\",\"60,000\",\"\",\"\"]",
|
||||
);
|
||||
let (s, v) = them
|
||||
.act("save", &[("rev", &now), ("pieces", &accepted)])
|
||||
.await;
|
||||
assert_eq!(s, StatusCode::OK, "{v}");
|
||||
now = rev(&v);
|
||||
assert_eq!(v["state"]["have"], 0, "a change asks everyone again");
|
||||
them.act("release", &[("rev", &now)]).await;
|
||||
// open changes block an agreement
|
||||
let (s, _) = them.act("agree", &[("rev", &now), ("name", "Ann")]).await;
|
||||
assert_eq!(s, StatusCode::UNPROCESSABLE_ENTITY);
|
||||
let (_, v) = staff(&app, c, "reject", &[("rev", &now), ("n", "1")]).await;
|
||||
now = rev(&v);
|
||||
let (_, v) = them.act("resolve", &[("rev", &now), ("k", "1")]).await;
|
||||
now = rev(&v);
|
||||
|
||||
// both agree: the text locks as a new version
|
||||
let (s, _) = staff(&app, c, "agree", &[("rev", &now)]).await;
|
||||
assert_eq!(s, StatusCode::OK);
|
||||
let (s, _) = them.act("agree", &[("rev", &now), ("name", "")]).await;
|
||||
assert_eq!(s, StatusCode::UNPROCESSABLE_ENTITY, "a name goes with it");
|
||||
let (s, v) = them
|
||||
.act("agree", &[("rev", &now), ("name", "Ann Festival")])
|
||||
.await;
|
||||
assert_eq!(s, StatusCode::OK, "{v}");
|
||||
assert_eq!(v["state"]["state"], "locked");
|
||||
let (count, text): (i64, String) = sqlx::query_as(
|
||||
"SELECT (SELECT count(*) FROM contract_versions WHERE contract_id = $1), \
|
||||
(SELECT body FROM contract_versions WHERE contract_id = $1 ORDER BY version DESC LIMIT 1)",
|
||||
)
|
||||
.bind(c)
|
||||
.fetch_one(&pool)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(count, versions_before + 1);
|
||||
assert!(text.contains("60,000") && !text.contains("45,000") && !text.contains("Sound check"));
|
||||
let events: Vec<String> = sqlx::query_scalar(
|
||||
"SELECT event FROM journal WHERE contract_id = $1 AND seq > 0 ORDER BY seq",
|
||||
)
|
||||
.bind(c)
|
||||
.fetch_all(&pool)
|
||||
.await
|
||||
.unwrap();
|
||||
for e in [
|
||||
"draft opened",
|
||||
"draft link made",
|
||||
"change accepted",
|
||||
"change rejected",
|
||||
"highlight resolved",
|
||||
"text agreed",
|
||||
"draft locked",
|
||||
] {
|
||||
assert!(events.iter().any(|x| x == e), "{e} missing in {events:?}");
|
||||
}
|
||||
assert!(visa::journal::verify(&pool).await.unwrap().whole());
|
||||
let t = app.get(&format!("/contracts/{c}")).await.text();
|
||||
assert!(t.contains("Every side agreed") && t.contains("Start a new draft"));
|
||||
|
||||
// locked is locked
|
||||
let (s, _) = staff(&app, c, "take", &[("rev", &now)]).await;
|
||||
assert_eq!(s, StatusCode::CONFLICT);
|
||||
assert!(
|
||||
sqlx::query("UPDATE contract_drafts SET pieces = '[]' WHERE contract_id = $1")
|
||||
.bind(c)
|
||||
.execute(&pool)
|
||||
.await
|
||||
.is_err()
|
||||
);
|
||||
assert!(
|
||||
sqlx::query("DELETE FROM draft_saves")
|
||||
.execute(&pool)
|
||||
.await
|
||||
.is_err()
|
||||
);
|
||||
assert!(
|
||||
sqlx::query("DELETE FROM draft_agreements")
|
||||
.execute(&pool)
|
||||
.await
|
||||
.is_err()
|
||||
);
|
||||
|
||||
// the link's form token is its own, and a closed link ends
|
||||
let r = app
|
||||
.post_raw(
|
||||
&format!("{path}/take"),
|
||||
&[("csrf", &"x".repeat(43)), ("rev", &now)],
|
||||
SAME,
|
||||
)
|
||||
.await;
|
||||
assert_eq!(r.status, StatusCode::FORBIDDEN);
|
||||
let link: i64 =
|
||||
sqlx::query_scalar("SELECT id FROM review_links WHERE party_id = $1 AND closed_at IS NULL")
|
||||
.bind(party)
|
||||
.fetch_one(&pool)
|
||||
.await
|
||||
.unwrap();
|
||||
app.post(&format!("/contracts/{c}/review-links/{link}/close"), &[])
|
||||
.await;
|
||||
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 the_demo_draft_shows_the_labels_changes_as_a_diff(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 c: i64 = sqlx::query_scalar("SELECT contract_id FROM contract_drafts LIMIT 1")
|
||||
.fetch_one(&pool)
|
||||
.await
|
||||
.unwrap();
|
||||
let st = json(&app.get(&format!("/contracts/{c}/draft/state")).await.text());
|
||||
let pieces = st["pieces"].as_array().unwrap();
|
||||
let changes = pieces
|
||||
.windows(2)
|
||||
.filter(|w| {
|
||||
let k = |p: &Value| p[0].as_str().unwrap().to_string();
|
||||
(k(&w[1]) == "ins" || k(&w[1]) == "del") && !(k(&w[0]) == "ins" || k(&w[0]) == "del")
|
||||
})
|
||||
.count();
|
||||
assert_eq!(changes, 4, "{st}");
|
||||
let t = app.get(&format!("/contracts/{c}/draft/diff")).await.text();
|
||||
assert!(
|
||||
t.contains("class=\"gitdiff\"") && t.contains("id=\"change-4\""),
|
||||
"{t}"
|
||||
);
|
||||
let t = app.get(&format!("/contracts/{c}")).await.text();
|
||||
assert!(
|
||||
t.contains("4 open changes") && t.contains("0 of 2 sides agree"),
|
||||
"{t}"
|
||||
);
|
||||
assert_eq!(json(&app.get("/notices/count").await.text())["unread"], 1);
|
||||
}
|
||||
+1
-1
@@ -11,6 +11,7 @@ mod companies;
|
||||
mod deadlines;
|
||||
mod demo;
|
||||
mod documents;
|
||||
mod drafts;
|
||||
mod fuzz_cases;
|
||||
mod garage;
|
||||
mod legal;
|
||||
@@ -22,7 +23,6 @@ mod pages;
|
||||
mod papers;
|
||||
mod people;
|
||||
mod records;
|
||||
mod reviews;
|
||||
|
||||
#[test]
|
||||
fn every_test_file_is_a_module() {
|
||||
|
||||
+14
-12
@@ -101,24 +101,26 @@ async fn every_page_renders_with_demo_data(pool: PgPool) {
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
// the label's review of the recording agreement (phase 13)
|
||||
let (review_contract, review_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();
|
||||
// the shared draft of the recording agreement (phase 13)
|
||||
let draft_contract: i64 = sqlx::query_scalar("SELECT contract_id FROM contract_drafts LIMIT 1")
|
||||
.fetch_one(&pool)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let mut pages = vec![
|
||||
(
|
||||
format!("/contracts/{review_contract}/reviews/{review_round}"),
|
||||
"As a diff".to_string(),
|
||||
format!("/contracts/{draft_contract}/draft"),
|
||||
"The shared draft for".to_string(),
|
||||
),
|
||||
(
|
||||
format!("/contracts/{review_contract}"),
|
||||
"Review by the other side".to_string(),
|
||||
format!("/contracts/{draft_contract}/draft/diff"),
|
||||
"The draft as a diff".to_string(),
|
||||
),
|
||||
(
|
||||
format!("/contracts/{draft_contract}"),
|
||||
"Shared draft".to_string(),
|
||||
),
|
||||
("/notices".to_string(), "changed the draft".to_string()),
|
||||
(
|
||||
"/originals".to_string(),
|
||||
"Chiang Mai Immigration".to_string(),
|
||||
|
||||
@@ -1,287 +0,0 @@
|
||||
//! 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::<i64>().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::<i64>().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"));
|
||||
}
|
||||
Reference in new issue
Block a user