| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
use crate::harness::TestHarness; |
| 8 |
use makenotwork::db; |
| 9 |
use serde_json::Value; |
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
async fn setup_seller_with_discountable_item(h: &mut TestHarness) -> (db::UserId, String) { |
| 14 |
let seller_id = h.signup("seller", "seller@test.com", "password123").await; |
| 15 |
h.grant_creator(seller_id).await; |
| 16 |
h.client.post_form("/logout", "").await; |
| 17 |
h.login("seller", "password123").await; |
| 18 |
|
| 19 |
let resp = h |
| 20 |
.client |
| 21 |
.post_form("/api/projects", "slug=shop&title=Shop") |
| 22 |
.await; |
| 23 |
let project: Value = resp.json(); |
| 24 |
let project_id = project["id"].as_str().unwrap(); |
| 25 |
|
| 26 |
let resp = h |
| 27 |
.client |
| 28 |
.post_form( |
| 29 |
&format!("/api/projects/{}/items", project_id), |
| 30 |
"title=Product&item_type=digital&price_cents=1000", |
| 31 |
) |
| 32 |
.await; |
| 33 |
assert!(resp.status.is_success(), "Create item failed: {} {}", resp.status, resp.text); |
| 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( |
| 40 |
&format!("/api/projects/{}", project_id), |
| 41 |
r#"{"is_public": true}"#, |
| 42 |
) |
| 43 |
.await; |
| 44 |
h.client |
| 45 |
.put_form(&format!("/api/items/{}", item_id), "is_public=true") |
| 46 |
.await; |
| 47 |
|
| 48 |
|
| 49 |
let resp = h.client.post_form( |
| 50 |
"/api/promo-codes", |
| 51 |
"code=FREE100&code_purpose=discount&discount_type=percentage&discount_value=100", |
| 52 |
).await; |
| 53 |
assert!(resp.status.is_success(), "Create promo code failed: {} {}", resp.status, resp.text); |
| 54 |
|
| 55 |
h.client.post_form("/logout", "").await; |
| 56 |
(seller_id, item_id) |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
async fn buyer_purchase_with_share_contact(h: &mut TestHarness, item_id: &str) { |
| 61 |
let resp = h |
| 62 |
.client |
| 63 |
.post_form( |
| 64 |
&format!("/stripe/checkout/{}", item_id), |
| 65 |
"promo_code=FREE100&share_contact=true", |
| 66 |
) |
| 67 |
.await; |
| 68 |
|
| 69 |
assert!( |
| 70 |
resp.status.is_redirection() || resp.status.is_success(), |
| 71 |
"Purchase with share_contact failed: {} {}", |
| 72 |
resp.status, |
| 73 |
resp.text |
| 74 |
); |
| 75 |
} |
| 76 |
|
| 77 |
#[tokio::test] |
| 78 |
async fn revoke_hides_contact_from_seller() { |
| 79 |
let mut h = TestHarness::new().await; |
| 80 |
|
| 81 |
let (seller_id, item_id) = setup_seller_with_discountable_item(&mut h).await; |
| 82 |
|
| 83 |
|
| 84 |
let _buyer_id = h.signup("buyer", "buyer@test.com", "password123").await; |
| 85 |
buyer_purchase_with_share_contact(&mut h, &item_id).await; |
| 86 |
|
| 87 |
|
| 88 |
let contacts = db::transactions::get_seller_contacts(&h.db, seller_id).await.unwrap(); |
| 89 |
assert_eq!(contacts.len(), 1, "Seller should see 1 contact before revocation"); |
| 90 |
assert_eq!(contacts[0].email, "buyer@test.com"); |
| 91 |
|
| 92 |
|
| 93 |
let resp = h |
| 94 |
.client |
| 95 |
.delete(&format!("/api/contacts/{}", *seller_id)) |
| 96 |
.await; |
| 97 |
assert_eq!(resp.status, 204, "Revoke should return 204, got {}", resp.status); |
| 98 |
|
| 99 |
|
| 100 |
let contacts = db::transactions::get_seller_contacts(&h.db, seller_id).await.unwrap(); |
| 101 |
assert!(contacts.is_empty(), "Seller should see 0 contacts after revocation"); |
| 102 |
} |
| 103 |
|
| 104 |
#[tokio::test] |
| 105 |
async fn repurchase_with_share_contact_clears_revocation() { |
| 106 |
let mut h = TestHarness::new().await; |
| 107 |
|
| 108 |
let (seller_id, item_id) = setup_seller_with_discountable_item(&mut h).await; |
| 109 |
|
| 110 |
|
| 111 |
let _buyer_id = h.signup("buyer2", "buyer2@test.com", "password123").await; |
| 112 |
buyer_purchase_with_share_contact(&mut h, &item_id).await; |
| 113 |
|
| 114 |
let resp = h |
| 115 |
.client |
| 116 |
.delete(&format!("/api/contacts/{}", *seller_id)) |
| 117 |
.await; |
| 118 |
assert_eq!(resp.status, 204); |
| 119 |
|
| 120 |
|
| 121 |
let contacts = db::transactions::get_seller_contacts(&h.db, seller_id).await.unwrap(); |
| 122 |
assert!(contacts.is_empty(), "Contact should be hidden after revocation"); |
| 123 |
|
| 124 |
|
| 125 |
h.client.post_form("/logout", "").await; |
| 126 |
h.login("seller", "password123").await; |
| 127 |
let project_id = { |
| 128 |
let resp = h.client.get("/api/projects").await; |
| 129 |
let projects: Value = resp.json(); |
| 130 |
projects["data"][0]["id"].as_str().unwrap().to_string() |
| 131 |
}; |
| 132 |
let resp = h.client.post_form( |
| 133 |
&format!("/api/projects/{}/items", project_id), |
| 134 |
"title=Product+2&item_type=digital&price_cents=1000", |
| 135 |
).await; |
| 136 |
assert!(resp.status.is_success(), "Create item 2 failed: {} {}", resp.status, resp.text); |
| 137 |
let item2: Value = resp.json(); |
| 138 |
let item2_id = item2["id"].as_str().unwrap().to_string(); |
| 139 |
h.client.put_form(&format!("/api/items/{}", item2_id), "is_public=true").await; |
| 140 |
|
| 141 |
|
| 142 |
h.client.post_form("/logout", "").await; |
| 143 |
h.login("buyer2", "password123").await; |
| 144 |
buyer_purchase_with_share_contact(&mut h, &item2_id).await; |
| 145 |
|
| 146 |
|
| 147 |
let contacts = db::transactions::get_seller_contacts(&h.db, seller_id).await.unwrap(); |
| 148 |
assert_eq!(contacts.len(), 1, "Contact should reappear after re-purchase with share_contact"); |
| 149 |
assert_eq!(contacts[0].email, "buyer2@test.com"); |
| 150 |
} |
| 151 |
|
| 152 |
#[tokio::test] |
| 153 |
async fn export_redacts_email_after_revocation() { |
| 154 |
let mut h = TestHarness::new().await; |
| 155 |
|
| 156 |
let (seller_id, item_id) = setup_seller_with_discountable_item(&mut h).await; |
| 157 |
|
| 158 |
|
| 159 |
let _buyer_id = h.signup("buyer3", "buyer3@test.com", "password123").await; |
| 160 |
buyer_purchase_with_share_contact(&mut h, &item_id).await; |
| 161 |
|
| 162 |
|
| 163 |
let rows = db::transactions::get_seller_transactions_for_export(&h.db, seller_id).await.unwrap(); |
| 164 |
assert!(!rows.is_empty(), "Export should have rows"); |
| 165 |
assert_eq!( |
| 166 |
rows[0].buyer_email.as_deref(), |
| 167 |
Some("buyer3@test.com"), |
| 168 |
"Export should show buyer email before revocation" |
| 169 |
); |
| 170 |
|
| 171 |
|
| 172 |
let resp = h |
| 173 |
.client |
| 174 |
.delete(&format!("/api/contacts/{}", *seller_id)) |
| 175 |
.await; |
| 176 |
assert_eq!(resp.status, 204); |
| 177 |
|
| 178 |
|
| 179 |
let rows = db::transactions::get_seller_transactions_for_export(&h.db, seller_id).await.unwrap(); |
| 180 |
assert!(!rows.is_empty(), "Export should still have rows after revocation"); |
| 181 |
assert_eq!( |
| 182 |
rows[0].buyer_email, None, |
| 183 |
"Export should hide buyer email after revocation" |
| 184 |
); |
| 185 |
} |
| 186 |
|
| 187 |
#[tokio::test] |
| 188 |
async fn revoke_is_idempotent() { |
| 189 |
let mut h = TestHarness::new().await; |
| 190 |
|
| 191 |
let seller_id = h.signup("seller4", "seller4@test.com", "password123").await; |
| 192 |
h.grant_creator(seller_id).await; |
| 193 |
h.client.post_form("/logout", "").await; |
| 194 |
let _buyer_id = h.signup("buyer4", "buyer4@test.com", "password123").await; |
| 195 |
|
| 196 |
|
| 197 |
let resp = h |
| 198 |
.client |
| 199 |
.delete(&format!("/api/contacts/{}", *seller_id)) |
| 200 |
.await; |
| 201 |
assert_eq!(resp.status, 204, "First revoke should return 204, got {}", resp.status); |
| 202 |
|
| 203 |
let resp = h |
| 204 |
.client |
| 205 |
.delete(&format!("/api/contacts/{}", *seller_id)) |
| 206 |
.await; |
| 207 |
assert_eq!(resp.status, 204, "Second revoke should return 204, got {}", resp.status); |
| 208 |
} |
| 209 |
|
| 210 |
#[tokio::test] |
| 211 |
async fn revoke_requires_auth() { |
| 212 |
let mut h = TestHarness::new().await; |
| 213 |
|
| 214 |
|
| 215 |
h.client.fetch_csrf_token().await; |
| 216 |
|
| 217 |
let fake_seller = uuid::Uuid::new_v4(); |
| 218 |
let resp = h |
| 219 |
.client |
| 220 |
.delete(&format!("/api/contacts/{}", fake_seller)) |
| 221 |
.await; |
| 222 |
assert!( |
| 223 |
resp.status == 401 || resp.status == 302 || resp.status == 303, |
| 224 |
"Unauthenticated revoke should be rejected, got {}", |
| 225 |
resp.status |
| 226 |
); |
| 227 |
} |
| 228 |
|