| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
use crate::harness::TestHarness; |
| 5 |
use serde_json::{json, Value}; |
| 6 |
|
| 7 |
|
| 8 |
async fn setup_paid_item(h: &mut TestHarness, price_cents: i64) -> (String, String) { |
| 9 |
let creator_id = h.signup("seller", "seller@test.com", "password123").await; |
| 10 |
h.grant_creator(creator_id).await; |
| 11 |
|
| 12 |
sqlx::query("UPDATE users SET stripe_account_id = 'acct_mock_seller', stripe_charges_enabled = true WHERE id = $1") |
| 13 |
.bind(creator_id) |
| 14 |
.execute(&h.db) |
| 15 |
.await |
| 16 |
.unwrap(); |
| 17 |
h.client.post_form("/logout", "").await; |
| 18 |
h.login("seller", "password123").await; |
| 19 |
|
| 20 |
let resp = h |
| 21 |
.client |
| 22 |
.post_form("/api/projects", "slug=shop&title=Shop") |
| 23 |
.await; |
| 24 |
let project: Value = resp.json(); |
| 25 |
let project_id = project["id"].as_str().unwrap().to_string(); |
| 26 |
|
| 27 |
let resp = h |
| 28 |
.client |
| 29 |
.post_form( |
| 30 |
&format!("/api/projects/{}/items", project_id), |
| 31 |
&format!("title=My+Item&price_cents={}&item_type=audio", price_cents), |
| 32 |
) |
| 33 |
.await; |
| 34 |
let item: Value = resp.json(); |
| 35 |
let item_id = item["id"].as_str().unwrap().to_string(); |
| 36 |
|
| 37 |
|
| 38 |
h.client |
| 39 |
.put_json(&format!("/api/projects/{}", project_id), r#"{"is_public": true}"#) |
| 40 |
.await; |
| 41 |
h.client |
| 42 |
.put_form(&format!("/api/items/{}", item_id), "is_public=true") |
| 43 |
.await; |
| 44 |
|
| 45 |
|
| 46 |
h.client.post_form("/logout", "").await; |
| 47 |
|
| 48 |
(creator_id.to_string(), item_id) |
| 49 |
} |
| 50 |
|
| 51 |
#[tokio::test] |
| 52 |
async fn guest_checkout_creates_session() { |
| 53 |
let mut h = TestHarness::with_mocks().await; |
| 54 |
let (_, item_id) = setup_paid_item(&mut h, 999).await; |
| 55 |
|
| 56 |
let body = json!({}).to_string(); |
| 57 |
let resp = h.client.post_json(&format!("/api/checkout/guest/{}", item_id), &body).await; |
| 58 |
|
| 59 |
assert!(resp.status.is_success(), "Guest checkout failed: {} {}", resp.status, resp.text); |
| 60 |
|
| 61 |
let data: Value = resp.json(); |
| 62 |
assert!(data["checkout_url"].is_string(), "Missing checkout_url: {:?}", data); |
| 63 |
assert!(data["checkout_url"].as_str().unwrap().contains("http"), "Invalid checkout_url"); |
| 64 |
} |
| 65 |
|
| 66 |
#[tokio::test] |
| 67 |
async fn guest_checkout_private_item_404() { |
| 68 |
let mut h = TestHarness::new().await; |
| 69 |
let creator_id = h.signup("seller", "seller@test.com", "password123").await; |
| 70 |
h.grant_creator(creator_id).await; |
| 71 |
sqlx::query("UPDATE users SET stripe_account_id = 'acct_mock_seller', stripe_charges_enabled = true WHERE id = $1") |
| 72 |
.bind(creator_id) |
| 73 |
.execute(&h.db) |
| 74 |
.await |
| 75 |
.unwrap(); |
| 76 |
h.client.post_form("/logout", "").await; |
| 77 |
h.login("seller", "password123").await; |
| 78 |
|
| 79 |
let resp = h.client.post_form("/api/projects", "slug=shop&title=Shop").await; |
| 80 |
let project: Value = resp.json(); |
| 81 |
let project_id = project["id"].as_str().unwrap(); |
| 82 |
|
| 83 |
let resp = h.client.post_form( |
| 84 |
&format!("/api/projects/{}/items", project_id), |
| 85 |
"title=Private+Item&price_cents=500&item_type=audio", |
| 86 |
).await; |
| 87 |
let item: Value = resp.json(); |
| 88 |
let item_id = item["id"].as_str().unwrap(); |
| 89 |
|
| 90 |
|
| 91 |
h.client |
| 92 |
.put_form(&format!("/api/items/{}", item_id), "is_public=false") |
| 93 |
.await; |
| 94 |
h.client.post_form("/logout", "").await; |
| 95 |
|
| 96 |
let body = json!({}).to_string(); |
| 97 |
let resp = h.client.post_json(&format!("/api/checkout/guest/{}", item_id), &body).await; |
| 98 |
assert_eq!(resp.status.as_u16(), 404); |
| 99 |
} |
| 100 |
|
| 101 |
#[tokio::test] |
| 102 |
async fn guest_checkout_free_item_rejected() { |
| 103 |
let mut h = TestHarness::new().await; |
| 104 |
let (_, item_id) = setup_paid_item(&mut h, 0).await; |
| 105 |
|
| 106 |
let body = json!({}).to_string(); |
| 107 |
let resp = h.client.post_json(&format!("/api/checkout/guest/{}", item_id), &body).await; |
| 108 |
assert_eq!(resp.status.as_u16(), 400, "Free items should be rejected by paid checkout"); |
| 109 |
} |
| 110 |
|
| 111 |
#[tokio::test] |
| 112 |
async fn guest_free_claim_sends_download() { |
| 113 |
let mut h = TestHarness::new().await; |
| 114 |
let (_, item_id) = setup_paid_item(&mut h, 0).await; |
| 115 |
|
| 116 |
let body = json!({ "email": "fan@example.com" }).to_string(); |
| 117 |
let resp = h.client.post_json(&format!("/api/checkout/guest-free/{}", item_id), &body).await; |
| 118 |
|
| 119 |
assert!(resp.status.is_success(), "Free claim failed: {} {}", resp.status, resp.text); |
| 120 |
|
| 121 |
let data: Value = resp.json(); |
| 122 |
assert_eq!(data["status"], "claimed"); |
| 123 |
assert!(data["download_url"].as_str().unwrap().contains("/download/")); |
| 124 |
} |
| 125 |
|
| 126 |
#[tokio::test] |
| 127 |
async fn guest_free_claim_invalid_email() { |
| 128 |
let mut h = TestHarness::new().await; |
| 129 |
let (_, item_id) = setup_paid_item(&mut h, 0).await; |
| 130 |
|
| 131 |
let body = json!({ "email": "bad" }).to_string(); |
| 132 |
let resp = h.client.post_json(&format!("/api/checkout/guest-free/{}", item_id), &body).await; |
| 133 |
assert_eq!(resp.status.as_u16(), 400); |
| 134 |
} |
| 135 |
|
| 136 |
#[tokio::test] |
| 137 |
async fn buy_page_renders() { |
| 138 |
let mut h = TestHarness::new().await; |
| 139 |
let (_, item_id) = setup_paid_item(&mut h, 1500).await; |
| 140 |
|
| 141 |
let resp = h.client.get(&format!("/buy/{}", item_id)).await; |
| 142 |
assert!(resp.status.is_success(), "Buy page failed: {} {}", resp.status, resp.text); |
| 143 |
assert!(resp.text.contains("Buy Now"), "Missing buy button"); |
| 144 |
assert!(resp.text.contains("makenot.work"), "Missing footer branding"); |
| 145 |
} |
| 146 |
|
| 147 |
#[tokio::test] |
| 148 |
async fn buy_page_private_item_404() { |
| 149 |
let mut h = TestHarness::new().await; |
| 150 |
let creator_id = h.signup("seller", "seller@test.com", "password123").await; |
| 151 |
h.grant_creator(creator_id).await; |
| 152 |
h.client.post_form("/logout", "").await; |
| 153 |
h.login("seller", "password123").await; |
| 154 |
|
| 155 |
let resp = h.client.post_form("/api/projects", "slug=shop&title=Shop").await; |
| 156 |
let project: Value = resp.json(); |
| 157 |
let project_id = project["id"].as_str().unwrap(); |
| 158 |
|
| 159 |
let resp = h.client.post_form( |
| 160 |
&format!("/api/projects/{}/items", project_id), |
| 161 |
"title=Secret&price_cents=500&item_type=audio", |
| 162 |
).await; |
| 163 |
let item: Value = resp.json(); |
| 164 |
let item_id = item["id"].as_str().unwrap(); |
| 165 |
|
| 166 |
|
| 167 |
h.client |
| 168 |
.put_form(&format!("/api/items/{}", item_id), "is_public=false") |
| 169 |
.await; |
| 170 |
|
| 171 |
h.client.post_form("/logout", "").await; |
| 172 |
let resp = h.client.get(&format!("/buy/{}", item_id)).await; |
| 173 |
assert_eq!(resp.status.as_u16(), 404); |
| 174 |
} |
| 175 |
|
| 176 |
#[tokio::test] |
| 177 |
async fn claim_token_attaches_purchase() { |
| 178 |
let mut h = TestHarness::new().await; |
| 179 |
let (_, item_id) = setup_paid_item(&mut h, 0).await; |
| 180 |
|
| 181 |
|
| 182 |
let body = json!({ "email": "claimer@example.com" }).to_string(); |
| 183 |
let resp = h.client.post_json(&format!("/api/checkout/guest-free/{}", item_id), &body).await; |
| 184 |
assert!(resp.status.is_success()); |
| 185 |
|
| 186 |
|
| 187 |
let data: Value = resp.json(); |
| 188 |
assert!(data["download_url"].as_str().unwrap().contains("/download/")); |
| 189 |
|
| 190 |
|
| 191 |
let user_id = h.signup("claimer", "claimer@example.com", "password123").await; |
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
let _ = user_id; |
| 199 |
} |
| 200 |
|