| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
use crate::harness::TestHarness; |
| 8 |
use makenotwork::db; |
| 9 |
use serde_json::Value; |
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
#[tokio::test] |
| 17 |
async fn suspended_user_checkout_blocked() { |
| 18 |
let mut h = TestHarness::new().await; |
| 19 |
|
| 20 |
|
| 21 |
let setup = h.create_creator_with_item("chkseller", "digital", 1000).await; |
| 22 |
h.publish_project_and_item(&setup.project_id, &setup.item_id).await; |
| 23 |
|
| 24 |
|
| 25 |
h.client.post_form("/logout", "").await; |
| 26 |
let buyer_id = h.signup("chkbuyer", "chkbuyer@test.com", "password123").await; |
| 27 |
|
| 28 |
|
| 29 |
db::users::suspend_user(&h.db, buyer_id, "test suspension").await.unwrap(); |
| 30 |
|
| 31 |
|
| 32 |
h.client.post_form("/logout", "").await; |
| 33 |
h.login("chkbuyer", "password123").await; |
| 34 |
|
| 35 |
|
| 36 |
let resp = h |
| 37 |
.client |
| 38 |
.post_form( |
| 39 |
&format!("/stripe/checkout/{}", setup.item_id), |
| 40 |
"", |
| 41 |
) |
| 42 |
.await; |
| 43 |
assert_eq!( |
| 44 |
resp.status, 403, |
| 45 |
"Suspended user should not be able to checkout: {} {}", |
| 46 |
resp.status, resp.text |
| 47 |
); |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
#[tokio::test] |
| 52 |
async fn active_user_checkout_proceeds() { |
| 53 |
let mut h = TestHarness::new().await; |
| 54 |
|
| 55 |
|
| 56 |
let setup = h.create_creator_with_item("chkseller2", "digital", 1000).await; |
| 57 |
h.publish_project_and_item(&setup.project_id, &setup.item_id).await; |
| 58 |
|
| 59 |
|
| 60 |
h.client.post_form("/logout", "").await; |
| 61 |
let _buyer_id = h.signup("chkbuyer2", "chkbuyer2@test.com", "password123").await; |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
let resp = h |
| 66 |
.client |
| 67 |
.post_form( |
| 68 |
&format!("/stripe/checkout/{}", setup.item_id), |
| 69 |
"", |
| 70 |
) |
| 71 |
.await; |
| 72 |
assert_ne!( |
| 73 |
resp.status, 403, |
| 74 |
"Active user should not get 403 on checkout: {} {}", |
| 75 |
resp.status, resp.text |
| 76 |
); |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
#[tokio::test] |
| 85 |
async fn suspended_user_create_promo_code_blocked() { |
| 86 |
let mut h = TestHarness::new().await; |
| 87 |
|
| 88 |
|
| 89 |
let creator_id = h.create_creator("promoseller").await; |
| 90 |
|
| 91 |
|
| 92 |
db::users::suspend_user(&h.db, creator_id, "test suspension").await.unwrap(); |
| 93 |
|
| 94 |
|
| 95 |
h.client.post_form("/logout", "").await; |
| 96 |
h.login("promoseller", "password123").await; |
| 97 |
|
| 98 |
|
| 99 |
let resp = h |
| 100 |
.client |
| 101 |
.post_form( |
| 102 |
"/api/promo-codes", |
| 103 |
"code=TESTCODE&code_purpose=discount&discount_type=percentage&discount_value=50", |
| 104 |
) |
| 105 |
.await; |
| 106 |
assert_eq!( |
| 107 |
resp.status, 403, |
| 108 |
"Suspended user should not create promo codes: {} {}", |
| 109 |
resp.status, resp.text |
| 110 |
); |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
#[tokio::test] |
| 115 |
async fn suspended_user_claim_promo_code_blocked() { |
| 116 |
let mut h = TestHarness::new().await; |
| 117 |
|
| 118 |
|
| 119 |
let _creator_id = h.create_creator("claimseller").await; |
| 120 |
let resp = h |
| 121 |
.client |
| 122 |
.post_form("/api/projects", "slug=claim-shop&title=Claim+Shop") |
| 123 |
.await; |
| 124 |
assert!(resp.status.is_success()); |
| 125 |
let project: Value = resp.json(); |
| 126 |
let project_id = project["id"].as_str().unwrap(); |
| 127 |
|
| 128 |
let resp = h |
| 129 |
.client |
| 130 |
.post_form( |
| 131 |
&format!("/api/projects/{}/items", project_id), |
| 132 |
"title=Claim+Item&item_type=digital&price_cents=0", |
| 133 |
) |
| 134 |
.await; |
| 135 |
assert!(resp.status.is_success()); |
| 136 |
let item: Value = resp.json(); |
| 137 |
let item_id = item["id"].as_str().unwrap(); |
| 138 |
|
| 139 |
h.client |
| 140 |
.put_json( |
| 141 |
&format!("/api/projects/{}", project_id), |
| 142 |
r#"{"is_public": true}"#, |
| 143 |
) |
| 144 |
.await; |
| 145 |
h.client |
| 146 |
.put_form(&format!("/api/items/{}", item_id), "is_public=true") |
| 147 |
.await; |
| 148 |
|
| 149 |
let resp = h |
| 150 |
.client |
| 151 |
.post_form( |
| 152 |
"/api/promo-codes", |
| 153 |
&format!("code_purpose=free_access&item_id={}", item_id), |
| 154 |
) |
| 155 |
.await; |
| 156 |
assert!(resp.status.is_success()); |
| 157 |
let code: Value = resp.json(); |
| 158 |
let key_code = code["code"].as_str().unwrap().to_string(); |
| 159 |
|
| 160 |
|
| 161 |
h.client.post_form("/logout", "").await; |
| 162 |
let buyer_id = h.signup("claimbuyer", "claimbuyer@test.com", "password123").await; |
| 163 |
db::users::suspend_user(&h.db, buyer_id, "test suspension").await.unwrap(); |
| 164 |
h.client.post_form("/logout", "").await; |
| 165 |
h.login("claimbuyer", "password123").await; |
| 166 |
|
| 167 |
|
| 168 |
let resp = h |
| 169 |
.client |
| 170 |
.post_form("/api/promo-codes/claim", &format!("code={}", key_code)) |
| 171 |
.await; |
| 172 |
assert_eq!( |
| 173 |
resp.status, 403, |
| 174 |
"Suspended user should not claim promo codes: {} {}", |
| 175 |
resp.status, resp.text |
| 176 |
); |
| 177 |
} |
| 178 |
|
| 179 |
|
| 180 |
#[tokio::test] |
| 181 |
async fn suspended_user_delete_promo_code_blocked() { |
| 182 |
let mut h = TestHarness::new().await; |
| 183 |
|
| 184 |
|
| 185 |
let creator_id = h.create_creator("delseller").await; |
| 186 |
|
| 187 |
let resp = h |
| 188 |
.client |
| 189 |
.post_form( |
| 190 |
"/api/promo-codes", |
| 191 |
"code=DELCODE&code_purpose=discount&discount_type=percentage&discount_value=50", |
| 192 |
) |
| 193 |
.await; |
| 194 |
assert!(resp.status.is_success()); |
| 195 |
let code: Value = resp.json(); |
| 196 |
let code_id = code["id"].as_str().unwrap().to_string(); |
| 197 |
|
| 198 |
|
| 199 |
db::users::suspend_user(&h.db, creator_id, "test suspension").await.unwrap(); |
| 200 |
h.client.post_form("/logout", "").await; |
| 201 |
h.login("delseller", "password123").await; |
| 202 |
|
| 203 |
|
| 204 |
let resp = h |
| 205 |
.client |
| 206 |
.delete(&format!("/api/promo-codes/{}", code_id)) |
| 207 |
.await; |
| 208 |
assert_eq!( |
| 209 |
resp.status, 403, |
| 210 |
"Suspended user should not delete promo codes: {} {}", |
| 211 |
resp.status, resp.text |
| 212 |
); |
| 213 |
} |
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
#[tokio::test] |
| 221 |
async fn suspended_user_generate_license_key_blocked() { |
| 222 |
let mut h = TestHarness::new().await; |
| 223 |
|
| 224 |
|
| 225 |
let setup = h.create_creator_with_item("lkseller", "digital", 1000).await; |
| 226 |
h.publish_project_and_item(&setup.project_id, &setup.item_id).await; |
| 227 |
|
| 228 |
|
| 229 |
let resp = h |
| 230 |
.client |
| 231 |
.put_form( |
| 232 |
&format!("/api/items/{}/license-settings", setup.item_id), |
| 233 |
"enable_license_keys=true", |
| 234 |
) |
| 235 |
.await; |
| 236 |
assert!( |
| 237 |
resp.status.is_success() || resp.status == 204, |
| 238 |
"Enable license keys failed: {} {}", |
| 239 |
resp.status, resp.text |
| 240 |
); |
| 241 |
|
| 242 |
|
| 243 |
db::users::suspend_user(&h.db, setup.user_id, "test suspension").await.unwrap(); |
| 244 |
h.client.post_form("/logout", "").await; |
| 245 |
h.login("lkseller", "password123").await; |
| 246 |
|
| 247 |
|
| 248 |
let resp = h |
| 249 |
.client |
| 250 |
.post_form( |
| 251 |
&format!("/api/items/{}/keys", setup.item_id), |
| 252 |
"", |
| 253 |
) |
| 254 |
.await; |
| 255 |
assert_eq!( |
| 256 |
resp.status, 403, |
| 257 |
"Suspended user should not generate license keys: {} {}", |
| 258 |
resp.status, resp.text |
| 259 |
); |
| 260 |
} |
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
#[tokio::test] |
| 268 |
async fn suspension_after_login_takes_effect() { |
| 269 |
let mut h = TestHarness::new().await; |
| 270 |
|
| 271 |
|
| 272 |
let creator_id = h.create_creator("stalesuspend").await; |
| 273 |
|
| 274 |
|
| 275 |
let resp = h |
| 276 |
.client |
| 277 |
.post_form("/api/projects", "slug=stale-shop&title=Stale+Shop") |
| 278 |
.await; |
| 279 |
assert!( |
| 280 |
resp.status.is_success(), |
| 281 |
"Active user should create project: {} {}", |
| 282 |
resp.status, resp.text |
| 283 |
); |
| 284 |
let project: Value = resp.json(); |
| 285 |
let project_id = project["id"].as_str().unwrap(); |
| 286 |
|
| 287 |
|
| 288 |
db::users::suspend_user(&h.db, creator_id, "admin suspension").await.unwrap(); |
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
{ |
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
} |
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
h.client.post_form("/logout", "").await; |
| 309 |
h.login("stalesuspend", "password123").await; |
| 310 |
|
| 311 |
|
| 312 |
let resp = h |
| 313 |
.client |
| 314 |
.put_json( |
| 315 |
&format!("/api/projects/{}", project_id), |
| 316 |
r#"{"title": "Should Fail"}"#, |
| 317 |
) |
| 318 |
.await; |
| 319 |
assert_eq!( |
| 320 |
resp.status, 403, |
| 321 |
"Suspended user (post-login) should be blocked: {} {}", |
| 322 |
resp.status, resp.text |
| 323 |
); |
| 324 |
} |
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
#[tokio::test] |
| 332 |
async fn webhook_stale_timestamp_rejected() { |
| 333 |
use crate::harness::stripe::{sign_webhook_payload_with_timestamp, TEST_WEBHOOK_SECRET_V2}; |
| 334 |
use std::time::{SystemTime, UNIX_EPOCH}; |
| 335 |
|
| 336 |
let mut h = TestHarness::with_stripe().await; |
| 337 |
|
| 338 |
let payload = r#"{"id":"evt_stale","type":"v2.core.event_destination.ping"}"#; |
| 339 |
let stale_ts = SystemTime::now() |
| 340 |
.duration_since(UNIX_EPOCH) |
| 341 |
.unwrap() |
| 342 |
.as_secs() |
| 343 |
- 600; |
| 344 |
let signature = sign_webhook_payload_with_timestamp(payload, TEST_WEBHOOK_SECRET_V2, stale_ts); |
| 345 |
|
| 346 |
let resp = h |
| 347 |
.client |
| 348 |
.request_with_headers( |
| 349 |
"POST", |
| 350 |
"/stripe/webhook/v2", |
| 351 |
Some(payload), |
| 352 |
&[ |
| 353 |
("stripe-signature", &signature), |
| 354 |
("content-type", "application/json"), |
| 355 |
], |
| 356 |
) |
| 357 |
.await; |
| 358 |
assert_eq!( |
| 359 |
resp.status.as_u16(), |
| 360 |
400, |
| 361 |
"Stale webhook timestamp should be rejected: {} {}", |
| 362 |
resp.status, |
| 363 |
resp.text |
| 364 |
); |
| 365 |
} |
| 366 |
|
| 367 |
|
| 368 |
#[tokio::test] |
| 369 |
async fn webhook_current_timestamp_accepted() { |
| 370 |
use crate::harness::stripe::{sign_webhook_payload, TEST_WEBHOOK_SECRET_V2}; |
| 371 |
|
| 372 |
let mut h = TestHarness::with_stripe().await; |
| 373 |
|
| 374 |
let payload = r#"{"id":"evt_fresh","type":"v2.core.event_destination.ping"}"#; |
| 375 |
let signature = sign_webhook_payload(payload, TEST_WEBHOOK_SECRET_V2); |
| 376 |
|
| 377 |
let resp = h |
| 378 |
.client |
| 379 |
.request_with_headers( |
| 380 |
"POST", |
| 381 |
"/stripe/webhook/v2", |
| 382 |
Some(payload), |
| 383 |
&[ |
| 384 |
("stripe-signature", &signature), |
| 385 |
("content-type", "application/json"), |
| 386 |
], |
| 387 |
) |
| 388 |
.await; |
| 389 |
|
| 390 |
|
| 391 |
assert_ne!( |
| 392 |
resp.status.as_u16(), |
| 393 |
400, |
| 394 |
"Valid webhook signature should not be rejected: {} {}", |
| 395 |
resp.status, |
| 396 |
resp.text |
| 397 |
); |
| 398 |
} |
| 399 |
|
| 400 |
|
| 401 |
#[tokio::test] |
| 402 |
async fn webhook_recent_timestamp_accepted() { |
| 403 |
use crate::harness::stripe::{sign_webhook_payload_with_timestamp, TEST_WEBHOOK_SECRET_V2}; |
| 404 |
use std::time::{SystemTime, UNIX_EPOCH}; |
| 405 |
|
| 406 |
let mut h = TestHarness::with_stripe().await; |
| 407 |
|
| 408 |
let payload = r#"{"id":"evt_recent","type":"v2.core.event_destination.ping"}"#; |
| 409 |
let recent_ts = SystemTime::now() |
| 410 |
.duration_since(UNIX_EPOCH) |
| 411 |
.unwrap() |
| 412 |
.as_secs() |
| 413 |
- 1; |
| 414 |
let signature = sign_webhook_payload_with_timestamp(payload, TEST_WEBHOOK_SECRET_V2, recent_ts); |
| 415 |
|
| 416 |
let resp = h |
| 417 |
.client |
| 418 |
.request_with_headers( |
| 419 |
"POST", |
| 420 |
"/stripe/webhook/v2", |
| 421 |
Some(payload), |
| 422 |
&[ |
| 423 |
("stripe-signature", &signature), |
| 424 |
("content-type", "application/json"), |
| 425 |
], |
| 426 |
) |
| 427 |
.await; |
| 428 |
assert_ne!( |
| 429 |
resp.status.as_u16(), |
| 430 |
400, |
| 431 |
"Recent webhook timestamp should not be rejected: {} {}", |
| 432 |
resp.status, |
| 433 |
resp.text |
| 434 |
); |
| 435 |
} |
| 436 |
|