| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
use crate::harness::TestHarness; |
| 28 |
use crate::harness::faults::stripe_unavailable; |
| 29 |
use makenotwork::db::UserId; |
| 30 |
|
| 31 |
|
| 32 |
async fn stored_account(h: &TestHarness, user_id: UserId) -> Option<String> { |
| 33 |
sqlx::query_scalar::<_, Option<String>>("SELECT stripe_account_id FROM users WHERE id = $1") |
| 34 |
.bind(user_id) |
| 35 |
.fetch_one(&h.db) |
| 36 |
.await |
| 37 |
.expect("read stripe_account_id") |
| 38 |
} |
| 39 |
|
| 40 |
|
| 41 |
fn creations(h: &TestHarness) -> u32 { |
| 42 |
h.mock_stripe |
| 43 |
.as_ref() |
| 44 |
.expect("with_mocks provides a payment provider") |
| 45 |
.faults() |
| 46 |
.calls("create_connect_account") |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
fn onboarding_url(resp: &crate::harness::client::TestResponse) -> String { |
| 51 |
resp.json::<serde_json::Value>() |
| 52 |
.get("url") |
| 53 |
.and_then(|v| v.as_str()) |
| 54 |
.unwrap_or_else(|| panic!("proceed must answer an object with a url: {}", resp.text)) |
| 55 |
.to_string() |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
#[tokio::test] |
| 62 |
async fn proceed_answers_json_because_fetch_cannot_follow_stripes_redirect() { |
| 63 |
let mut h = TestHarness::with_mocks().await; |
| 64 |
h.signup("connectjson", "connectjson@test.com", "pass1234") |
| 65 |
.await; |
| 66 |
|
| 67 |
let resp = h.client.post_form("/stripe/connect/proceed", "").await; |
| 68 |
|
| 69 |
assert_eq!( |
| 70 |
resp.status.as_u16(), |
| 71 |
200, |
| 72 |
"a redirect cannot be followed by the fetch() that calls this: {}", |
| 73 |
resp.text |
| 74 |
); |
| 75 |
assert!( |
| 76 |
resp.header("content-type") |
| 77 |
.is_some_and(|c| c.starts_with("application/json")), |
| 78 |
"the page reads a JSON body, got {:?}", |
| 79 |
resp.header("content-type") |
| 80 |
); |
| 81 |
assert!( |
| 82 |
onboarding_url(&resp).starts_with("https://"), |
| 83 |
"the body must carry the provider's onboarding link" |
| 84 |
); |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
#[tokio::test] |
| 91 |
async fn a_second_proceed_reuses_the_claimed_account_rather_than_creating_another() { |
| 92 |
let mut h = TestHarness::with_mocks().await; |
| 93 |
let user_id = h |
| 94 |
.signup("connectagain", "connectagain@test.com", "pass1234") |
| 95 |
.await; |
| 96 |
|
| 97 |
let first = h.client.post_form("/stripe/connect/proceed", "").await; |
| 98 |
assert_eq!(first.status.as_u16(), 200, "first proceed: {}", first.text); |
| 99 |
let claimed = stored_account(&h, user_id) |
| 100 |
.await |
| 101 |
.expect("proceed claims an account id for the user"); |
| 102 |
|
| 103 |
let second = h.client.post_form("/stripe/connect/proceed", "").await; |
| 104 |
assert_eq!( |
| 105 |
second.status.as_u16(), |
| 106 |
200, |
| 107 |
"second proceed: {}", |
| 108 |
second.text |
| 109 |
); |
| 110 |
|
| 111 |
assert_eq!( |
| 112 |
creations(&h), |
| 113 |
1, |
| 114 |
"the second visit must reuse the claimed account, not create an orphan" |
| 115 |
); |
| 116 |
assert_eq!( |
| 117 |
stored_account(&h, user_id).await.as_deref(), |
| 118 |
Some(claimed.as_str()), |
| 119 |
"the stored account id must not move under a repeat visit" |
| 120 |
); |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
#[tokio::test] |
| 127 |
async fn proceed_stores_a_stripe_shaped_account_id() { |
| 128 |
let mut h = TestHarness::with_mocks().await; |
| 129 |
let user_id = h |
| 130 |
.signup("connectshape", "connectshape@test.com", "pass1234") |
| 131 |
.await; |
| 132 |
|
| 133 |
let resp = h.client.post_form("/stripe/connect/proceed", "").await; |
| 134 |
assert_eq!(resp.status.as_u16(), 200, "proceed: {}", resp.text); |
| 135 |
|
| 136 |
let stored = stored_account(&h, user_id) |
| 137 |
.await |
| 138 |
.expect("an account id is stored"); |
| 139 |
assert!( |
| 140 |
stored.starts_with("acct_"), |
| 141 |
"the column is Stripe-shaped and the check belongs here, got {stored}" |
| 142 |
); |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
#[tokio::test] |
| 149 |
async fn a_provider_outage_at_proceed_claims_nothing() { |
| 150 |
let mut h = TestHarness::with_mocks().await; |
| 151 |
let user_id = h |
| 152 |
.signup("connectdown", "connectdown@test.com", "pass1234") |
| 153 |
.await; |
| 154 |
|
| 155 |
h.mock_stripe |
| 156 |
.as_ref() |
| 157 |
.expect("with_mocks provides a payment provider") |
| 158 |
.faults() |
| 159 |
.fail_always("create_connect_account", stripe_unavailable); |
| 160 |
|
| 161 |
let resp = h.client.post_form("/stripe/connect/proceed", "").await; |
| 162 |
assert_eq!( |
| 163 |
resp.status.as_u16(), |
| 164 |
503, |
| 165 |
"an outage is surfaced as unavailable, not as the creator's mistake, got {}", |
| 166 |
resp.status |
| 167 |
); |
| 168 |
assert_eq!( |
| 169 |
stored_account(&h, user_id).await, |
| 170 |
None, |
| 171 |
"no account was created, so none may be claimed" |
| 172 |
); |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
#[tokio::test] |
| 178 |
async fn a_sandbox_account_cannot_start_onboarding() { |
| 179 |
let mut h = TestHarness::with_mocks().await; |
| 180 |
h.client.get("/sandbox").await; |
| 181 |
let created = h.client.post_form("/sandbox", "").await; |
| 182 |
assert!( |
| 183 |
created.status.is_redirection(), |
| 184 |
"sandbox signup should redirect, got {}", |
| 185 |
created.status |
| 186 |
); |
| 187 |
|
| 188 |
let resp = h.client.post_form("/stripe/connect/proceed", "").await; |
| 189 |
|
| 190 |
assert_eq!( |
| 191 |
resp.status.as_u16(), |
| 192 |
403, |
| 193 |
"sandbox is refused before Stripe is touched, got {}", |
| 194 |
resp.status |
| 195 |
); |
| 196 |
assert_eq!(creations(&h), 0, "and no account was created"); |
| 197 |
} |
| 198 |
|
| 199 |
|
| 200 |
#[tokio::test] |
| 201 |
async fn onboarding_is_closed_to_anonymous_callers() { |
| 202 |
let mut h = TestHarness::with_mocks().await; |
| 203 |
|
| 204 |
let disclaimer = h.client.get("/stripe/connect").await; |
| 205 |
assert_eq!( |
| 206 |
disclaimer.status.as_u16(), |
| 207 |
401, |
| 208 |
"the disclaimer is behind the session guard, got {}", |
| 209 |
disclaimer.status |
| 210 |
); |
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
let proceed = h.client.post_form("/stripe/connect/proceed", "").await; |
| 216 |
assert_eq!( |
| 217 |
proceed.status.as_u16(), |
| 218 |
403, |
| 219 |
"proceed is refused before the handler, got {}", |
| 220 |
proceed.status |
| 221 |
); |
| 222 |
assert_eq!(creations(&h), 0, "nothing was created for a stranger"); |
| 223 |
} |
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
#[tokio::test] |
| 230 |
async fn the_stripe_landing_pages_render_unauthenticated() { |
| 231 |
let mut h = TestHarness::with_mocks().await; |
| 232 |
|
| 233 |
let ret = h.client.get("/stripe/connect/return").await; |
| 234 |
assert_eq!( |
| 235 |
ret.status.as_u16(), |
| 236 |
200, |
| 237 |
"the return page arrives cross-site with no session, got {}", |
| 238 |
ret.status |
| 239 |
); |
| 240 |
assert!( |
| 241 |
ret.text.contains("/dashboard?tab=payments"), |
| 242 |
"the return page must land the creator on the payments tab" |
| 243 |
); |
| 244 |
|
| 245 |
let refresh = h.client.get("/stripe/connect/refresh").await; |
| 246 |
assert_eq!( |
| 247 |
refresh.status.as_u16(), |
| 248 |
200, |
| 249 |
"the refresh page arrives cross-site with no session, got {}", |
| 250 |
refresh.status |
| 251 |
); |
| 252 |
assert!( |
| 253 |
refresh.text.contains("/stripe/connect"), |
| 254 |
"an expired link must send the creator back to restart setup" |
| 255 |
); |
| 256 |
} |
| 257 |
|