Review fixes: no duplicate invoices, safer logins and redirects, one charge per click
CI / test (push) Successful in 1m38s
CI / build (push) Successful in 5m50s
CI / deploy (push) Skipped

Invoicing: the invoice request is marked before it goes to Billingo; a
retry after a lost answer looks the invoice up under the invoice's own
partner instead of issuing a second one. An invoice issued but never
e-mailed counts as issued (downloadable). A Hungarian company with only
an EU VAT number is invoiced without a domestic tax number, the EU
number in the comment.

Security: the login redirect takes only plain local paths (no
backslash); the waitlist rate limit and share visits use X-Real-IP,
which Caddy sets from Cloudflare's address, not the spoofable first
X-Forwarded-For entry; a Google/Apple sign-in to an unconfirmed
password account removes its password and sessions; a person's birth
data can no longer be published through a share.

Credits and jobs: analysis requests are serialized per user, so a
double click queues and pays once; a forecast analysis also fixes a
person's birth data; admins can restart stuck tarot readings; a worker
forgets an attempt an admin restarted, so a stop does not requeue it;
two people accepting one comparison invite at once get one comparison.
Expired invitations are no longer accepted by e-mail, and unsubscribing
revokes the invitation's promo codes too.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012vLHzh5qUhFtmJhzkcTbXL
This commit is contained in:
feco
2026-11-20 20:17:13 +00:00
co-authored by Claude Opus 5.5
parent 1b768d558d
commit 4676f7860f
17 changed files with 335 additions and 67 deletions
+69 -4
View File
@@ -430,14 +430,31 @@ async fn google_login_creates_links_and_rejects(pool: PgPool) {
.await;
assert_eq!((r.status, &r.body["created"]), (StatusCode::OK, &json!(false)));
// an existing password account with the same address gets Google linked
// an existing, confirmed password account with the same address gets Google linked
let token = app.register("anna@example.com", "Anna").await;
sqlx::query("UPDATE users SET email_verified_at = now() WHERE email = 'anna@example.com'")
.execute(&app.pool)
.await
.unwrap();
let r = app
.post("/v1/auth/google", None, json!({"id_token": google_token("g-2", "anna@example.com", GOOGLE_CLIENT)}))
.await;
assert_eq!((r.status, &r.body["created"]), (StatusCode::OK, &json!(false)));
assert_eq!(app.get("/v1/me", Some(&token)).await.body["providers"], json!(["password", "google"]));
// an unconfirmed one may be someone else's: its password and sessions go
let squatter = app.register("victim@example.com", "Valaki").await;
let r = app
.post("/v1/auth/google", None, json!({"id_token": google_token("g-4", "victim@example.com", GOOGLE_CLIENT)}))
.await;
assert_eq!(r.status, StatusCode::OK, "{}", r.body);
let owner = r.body["session"]["token"].as_str().unwrap().to_string();
assert_eq!(app.get("/v1/me", Some(&squatter)).await.status, StatusCode::UNAUTHORIZED);
assert_eq!(app.get("/v1/me", Some(&owner)).await.body["providers"], json!(["google"]));
let r =
app.post("/v1/auth/login", None, json!({"email": "victim@example.com", "password": "titkos-jelszo-123"})).await;
assert_eq!(r.status, StatusCode::UNAUTHORIZED);
let r = app
.post("/v1/auth/google", None, json!({"id_token": google_token("g-3", "x@gmail.com", "other-client")}))
.await;
@@ -890,7 +907,7 @@ async fn a_visit_by_someone_else_earns_one_question_once(pool: PgPool) {
let anna = app.register("anna@example.com", "Anna").await;
let chart = app.chart(&anna, "Saját", true, "1990-03-15").await;
let share = json!({"kind": "chart", "target_id": chart["id"]});
let home = ("x-forwarded-for", "203.0.113.7");
let home = ("x-real-ip", "203.0.113.7");
let r = app.call_with("POST", "/v1/shares", Some(&anna), Some(share), &[home]).await;
assert_eq!(r.status, StatusCode::CREATED, "{}", r.body);
let visit = format!("/v1/public/shares/{}/visit", r.body["slug"].as_str().unwrap());
@@ -899,7 +916,9 @@ async fn a_visit_by_someone_else_earns_one_question_once(pool: PgPool) {
let before = me().await["analyses_left"]["question"].as_i64().unwrap();
// robots, a missing user agent, the sharer's address and the sharer do not count
let elsewhere = ("x-forwarded-for", "198.51.100.1, 172.70.1.1");
// A browser cannot pose as the sharer through X-Forwarded-For: only X-Real-IP
// (set by Caddy) counts.
let elsewhere = ("x-real-ip", "198.51.100.1");
for (token, headers) in [
(None, vec![("user-agent", "facebookexternalhit/1.1"), elsewhere]),
(None, vec![elsewhere]),
@@ -1968,6 +1987,18 @@ async fn admins_restart_open_generations_with_the_current_models(pool: PgPool) {
(status.as_str(), model.as_str(), fallback, started, attempts),
("pending", "anthropic/claude-test", None, None, 0)
);
// a stuck tarot reading restarts too
app.add_own_units(&admin, "tarot", 1).await;
let reading = app.post("/v1/tarot/readings", Some(&admin), json!({"spread": "daily"})).await;
let reading_id = reading.body["id"].as_str().unwrap().to_string();
sqlx::query("UPDATE analyses SET status = 'running', started_at = now() WHERE id = $1::uuid")
.bind(&reading_id)
.execute(&pool)
.await
.unwrap();
let r = app.post("/v1/admin/jobs/restart", Some(&admin), json!({ "ids": [reading_id] })).await;
assert_eq!(r.body["restarted"], 1, "{}", r.body);
// finished ones are left alone
sqlx::query("UPDATE analyses SET status = 'done', content = '{}'").execute(&pool).await.unwrap();
assert_eq!(app.post("/v1/admin/jobs/restart", Some(&admin), json!({})).await.body["restarted"], 0);
@@ -2174,7 +2205,7 @@ async fn an_invitation_brings_its_package_and_codes_fill_up(pool: PgPool) {
async fn the_waitlist_confirms_and_can_be_left(pool: PgPool) {
let app = TestApp::with_config(pool.clone(), |c| c.signup_mode = crate::config::SignupMode::Invite).await;
let admin = app.register("admin@example.com", "Admin").await;
let from = |ip: &'static str| [("x-forwarded-for", ip)];
let from = |ip: &'static str| [("x-real-ip", ip)];
// one confirmation per address, even when sent again
for _ in 0..2 {
@@ -2742,3 +2773,37 @@ async fn apple_login_can_be_off(pool: PgPool) {
.await;
assert_eq!(r.status, StatusCode::FORBIDDEN);
}
#[sqlx::test(migrator = "db::MIGRATOR")]
async fn a_double_click_queues_and_pays_one_analysis(pool: PgPool) {
let app = TestApp::new(pool.clone()).await;
let anna = app.register("anna@example.com", "Anna").await;
let chart = app.chart(&anna, "Saját", true, "1990-03-15").await;
grant_units(&app, &anna, "natal", 2).await;
let url = format!("/v1/charts/{}/analyses", chart["id"].as_str().unwrap());
let (a, b) = tokio::join!(app.call("POST", &url, Some(&anna), None), app.call("POST", &url, Some(&anna), None));
assert!(a.status.is_success() && b.status.is_success(), "{} {}", a.body, b.body);
assert_eq!(a.body["id"], b.body["id"]);
let count: i64 =
sqlx::query_scalar("SELECT count(*) FROM analyses WHERE kind = 'natal'").fetch_one(&pool).await.unwrap();
assert_eq!(count, 1);
assert_eq!(app.get("/v1/me", Some(&anna)).await.body["analyses_left"]["natal"], 1);
}
#[sqlx::test(migrator = "db::MIGRATOR")]
async fn a_paid_forecast_fixes_the_person(pool: PgPool) {
let app = TestApp::new(pool).await;
let anna = app.register("anna@example.com", "Anna").await;
app.chart(&anna, "Saját", true, "1990-03-15").await;
let bela = app.chart(&anna, "Béla", false, "1985-08-02").await;
let bela_id = bela["id"].as_str().unwrap();
let forecast = app.post(&format!("/v1/charts/{bela_id}/forecasts"), Some(&anna), json!({})).await;
assert_eq!(forecast.status, StatusCode::CREATED, "{}", forecast.body);
grant_units(&app, &anna, "forecast", 1).await;
let id = forecast.body["id"].as_str().unwrap();
let r = app.call("POST", &format!("/v1/forecasts/{id}/analyses"), Some(&anna), None).await;
assert_eq!(r.status, StatusCode::ACCEPTED, "{}", r.body);
let corrected = json!({"birth": {"date": "1985-08-03", "time": "10:00", "country": "HU", "place": "Szeged"}});
let r = app.call("PATCH", &format!("/v1/charts/{bela_id}"), Some(&anna), Some(corrected)).await;
assert_eq!((r.status, r.body["error"]["code"].as_str()), (StatusCode::CONFLICT, Some("person_analysed")));
}