| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
use crate::harness::{BuildOptions, TestHarness}; |
| 5 |
use makenotwork::config::AccessGate; |
| 6 |
use sqlx::PgPool; |
| 7 |
|
| 8 |
fn location(resp: &crate::harness::client::TestResponse) -> String { |
| 9 |
resp.headers |
| 10 |
.get("location") |
| 11 |
.and_then(|v| v.to_str().ok()) |
| 12 |
.unwrap_or("") |
| 13 |
.to_string() |
| 14 |
} |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
async fn seed_user(pool: &PgPool, username: &str, can_create_projects: bool) { |
| 19 |
let hash = makenotwork::auth::hash_password("password123").expect("hash"); |
| 20 |
sqlx::query( |
| 21 |
"INSERT INTO users (username, email, password_hash, email_verified, can_create_projects) |
| 22 |
VALUES ($1, $2, $3, true, $4)", |
| 23 |
) |
| 24 |
.bind(username) |
| 25 |
.bind(format!("{username}@example.com")) |
| 26 |
.bind(&hash) |
| 27 |
.bind(can_create_projects) |
| 28 |
.execute(pool) |
| 29 |
.await |
| 30 |
.expect("seed user"); |
| 31 |
} |
| 32 |
|
| 33 |
#[tokio::test] |
| 34 |
async fn access_gate_restricts_to_fan_plus_or_creator() { |
| 35 |
let mut h = TestHarness::build(BuildOptions { |
| 36 |
access_gate: AccessGate::FanPlusOrCreator, |
| 37 |
..Default::default() |
| 38 |
}) |
| 39 |
.await; |
| 40 |
seed_user(&h.db, "gatecreator", true).await; |
| 41 |
seed_user(&h.db, "plainfan", false).await; |
| 42 |
|
| 43 |
|
| 44 |
let r = h.client.get("/").await; |
| 45 |
assert!(r.status.is_redirection(), "anon should be redirected, got {}", r.status); |
| 46 |
assert!(location(&r).starts_with("/login"), "anon should land on login, got {}", location(&r)); |
| 47 |
|
| 48 |
|
| 49 |
assert_eq!(h.client.get("/login").await.status.as_u16(), 200, "login page must be reachable"); |
| 50 |
assert_eq!(h.client.get("/health").await.status.as_u16(), 200, "health must be reachable"); |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
h.login("plainfan", "password123").await; |
| 55 |
let r = h.client.get("/library").await; |
| 56 |
assert!(r.status.is_redirection(), "plain fan should be gated, got {}", r.status); |
| 57 |
assert!(location(&r).starts_with("/login"), "plain fan should land on login"); |
| 58 |
|
| 59 |
|
| 60 |
h.client.post_form("/logout", "").await; |
| 61 |
h.login("gatecreator", "password123").await; |
| 62 |
let r = h.client.get("/library").await; |
| 63 |
assert_eq!(r.status.as_u16(), 200, "creator should pass the gate: {} {}", r.status, r.text); |
| 64 |
} |
| 65 |
|
| 66 |
#[tokio::test] |
| 67 |
async fn access_gate_open_serves_public_site() { |
| 68 |
|
| 69 |
|
| 70 |
let mut h = TestHarness::new().await; |
| 71 |
let r = h.client.get("/").await; |
| 72 |
assert_eq!(r.status.as_u16(), 200, "open site should serve landing to anon: {}", r.status); |
| 73 |
} |
| 74 |
|