|
| 1 | +use insta::{Settings, assert_debug_snapshot, with_settings}; |
| 2 | +use rstest::rstest; |
| 3 | +use serial_test::serial; |
| 4 | + |
| 5 | +macro_rules! configure_insta { |
| 6 | + ($(expr:expr),*) => { |
| 7 | + let mut settings = Settings::clone_current(); |
| 8 | + settings.set_prepend_module_to_snapshot(false); |
| 9 | + settings.set_snapshot_path("snapshots/auth"); |
| 10 | + settings.set_snapshot_suffix("auth"); |
| 11 | + let _guard = settings.bind_to_scope(); |
| 12 | + }; |
| 13 | +} |
| 14 | + |
| 15 | +#[rstest] |
| 16 | +#[case( |
| 17 | + "can_successfully_register_user", |
| 18 | + serde_json::json!({ |
| 19 | + "email": "test1@example.com", |
| 20 | + "username": "test_user1", |
| 21 | + "password": "SafePassWord11!", |
| 22 | + "confirmPassword": "SafePassWord11!" |
| 23 | + }) |
| 24 | +)] |
| 25 | +#[case( |
| 26 | + "when_passwords_do_not_match_registration_fails", |
| 27 | + serde_json::json!({ |
| 28 | + "email": "test1@example.com", |
| 29 | + "username": "test_user1", |
| 30 | + "password": "SafePassWord11!", |
| 31 | + "confirmPassword": "SafePassWord11" |
| 32 | + }) |
| 33 | +)] |
| 34 | +#[case( |
| 35 | + "when_password_is_under_eight_chars_registration_fails", |
| 36 | + serde_json::json!({ |
| 37 | + "email": "test1@example.com", |
| 38 | + "username": "test_user1", |
| 39 | + "password": "Safe", |
| 40 | + "confirmPassword": "Safe" |
| 41 | + }) |
| 42 | +)] |
| 43 | +#[case( |
| 44 | + "when_password_contains_whitespace_char_registration_fails", |
| 45 | + serde_json::json!({ |
| 46 | + "email": "test1@example.com", |
| 47 | + "username": "test_user1", |
| 48 | + "password": "ChangeMe 123!", |
| 49 | + "confirmPassword": "ChangeMe 123!" |
| 50 | + }) |
| 51 | +)] |
| 52 | +#[case( |
| 53 | + "when_password_contains_comma_char_registration_fails", |
| 54 | + serde_json::json!({ |
| 55 | + "email": "test1@example.com", |
| 56 | + "username": "test_user1", |
| 57 | + "password": "ChangeMe,123!", |
| 58 | + "confirmPassword": "ChangeMe,123!" |
| 59 | + }) |
| 60 | +)] |
| 61 | +#[case( |
| 62 | + "when_email_is_not_an_email_registration_fails", |
| 63 | + serde_json::json!({ |
| 64 | + "email": "test1:example.com", |
| 65 | + "username": "test_user1", |
| 66 | + "password": "Password123", |
| 67 | + "confirmPassword": "Password123" |
| 68 | + }) |
| 69 | +)] |
| 70 | +#[case( |
| 71 | + "when_username_is_under_six_chars_registration_fails", |
| 72 | + serde_json::json!({ |
| 73 | + "email": "test1@example.com", |
| 74 | + "username": "test", |
| 75 | + "password": "Password123", |
| 76 | + "confirmPassword": "Password123" |
| 77 | + }) |
| 78 | +)] |
| 79 | +#[case( |
| 80 | + "when_username_contains_non_alphanumeric_chars_registration_fails", |
| 81 | + serde_json::json!({ |
| 82 | + "email": "test1@example.com", |
| 83 | + "username": "test user;+", |
| 84 | + "password": "Password123", |
| 85 | + "confirmPassword": "Password123" |
| 86 | + }) |
| 87 | +)] |
| 88 | +#[tokio::test] |
| 89 | +#[serial] |
| 90 | +async fn can_register_user(#[case] test_name: &str, #[case] params: serde_json::Value) { |
| 91 | + crate::request(|server, ctx| async move { |
| 92 | + configure_insta!(); |
| 93 | + |
| 94 | + crate::seed_data(ctx.db()) |
| 95 | + .await |
| 96 | + .expect("Failed to seed data"); |
| 97 | + |
| 98 | + let response = server.post("/auth/sign-up").json(¶ms).await; |
| 99 | + |
| 100 | + with_settings!({ |
| 101 | + filters => { |
| 102 | + let mut filters = crate::cleanup_date().to_vec(); |
| 103 | + filters.extend(crate::cleanup_uuid().to_vec()); |
| 104 | + filters |
| 105 | + } |
| 106 | + }, { |
| 107 | + assert_debug_snapshot!(test_name, (response.status_code(), response.text())) |
| 108 | + }) |
| 109 | + }) |
| 110 | + .await; |
| 111 | +} |
0 commit comments