|
| 1 | +//! CLI auth command tests (`login` / `logout`) |
| 2 | +
|
| 3 | +use spacetimedb_smoketests::{require_local_server, Smoketest}; |
| 4 | +use std::fs; |
| 5 | +use std::process::Output; |
| 6 | + |
| 7 | +fn output_stdout(output: &Output) -> String { |
| 8 | + String::from_utf8_lossy(&output.stdout).to_string() |
| 9 | +} |
| 10 | + |
| 11 | +fn output_stderr(output: &Output) -> String { |
| 12 | + String::from_utf8_lossy(&output.stderr).to_string() |
| 13 | +} |
| 14 | + |
| 15 | +fn assert_success(output: &Output, context: &str) { |
| 16 | + assert!( |
| 17 | + output.status.success(), |
| 18 | + "{context} failed:\nstdout: {}\nstderr: {}", |
| 19 | + output_stdout(output), |
| 20 | + output_stderr(output), |
| 21 | + ); |
| 22 | +} |
| 23 | + |
| 24 | +fn read_config(test: &Smoketest) -> toml::Table { |
| 25 | + let raw = fs::read_to_string(&test.config_path).expect("Failed to read config"); |
| 26 | + raw.parse::<toml::Table>().expect("Failed to parse config") |
| 27 | +} |
| 28 | + |
| 29 | +fn write_config(test: &Smoketest, config: &toml::Table) { |
| 30 | + let raw = toml::to_string(config).expect("Failed to serialize config"); |
| 31 | + fs::write(&test.config_path, raw).expect("Failed to write config"); |
| 32 | +} |
| 33 | + |
| 34 | +#[test] |
| 35 | +fn cli_logout_removes_cached_tokens() { |
| 36 | + require_local_server!(); |
| 37 | + let test = Smoketest::builder().autopublish(false).build(); |
| 38 | + |
| 39 | + let login = test.spacetime_cmd(&["login", "--server-issued-login", &test.server_url]); |
| 40 | + assert_success(&login, "initial login"); |
| 41 | + |
| 42 | + // Simulate a cached web session token; logout should clear both token fields. |
| 43 | + let mut config = read_config(&test); |
| 44 | + config.insert( |
| 45 | + "web_session_token".to_string(), |
| 46 | + toml::Value::String("fake-web-session-token".to_string()), |
| 47 | + ); |
| 48 | + write_config(&test, &config); |
| 49 | + |
| 50 | + let logout = test.spacetime_cmd(&["logout"]); |
| 51 | + assert_success(&logout, "logout"); |
| 52 | + assert!( |
| 53 | + output_stdout(&logout).contains("Logged out (identity "), |
| 54 | + "logout stdout should include identity message:\n{}", |
| 55 | + output_stdout(&logout), |
| 56 | + ); |
| 57 | + |
| 58 | + let config_after = read_config(&test); |
| 59 | + assert!( |
| 60 | + config_after.get("spacetimedb_token").is_none(), |
| 61 | + "spacetimedb_token should be removed after logout: {:?}", |
| 62 | + config_after.get("spacetimedb_token") |
| 63 | + ); |
| 64 | + assert!( |
| 65 | + config_after.get("web_session_token").is_none(), |
| 66 | + "web_session_token should be removed after logout: {:?}", |
| 67 | + config_after.get("web_session_token") |
| 68 | + ); |
| 69 | +} |
| 70 | + |
| 71 | +#[test] |
| 72 | +// Even if there's no web session, logout still removes the SpacetimeDB token |
| 73 | +fn cli_logout_removes_cached_tokens_without_web_token() { |
| 74 | + require_local_server!(); |
| 75 | + let test = Smoketest::builder().autopublish(false).build(); |
| 76 | + |
| 77 | + let login = test.spacetime_cmd(&["login", "--server-issued-login", &test.server_url]); |
| 78 | + assert_success(&login, "initial login"); |
| 79 | + |
| 80 | + let logout = test.spacetime_cmd(&["logout"]); |
| 81 | + assert_success(&logout, "logout"); |
| 82 | + assert!( |
| 83 | + output_stdout(&logout).contains("Logged out (identity "), |
| 84 | + "logout stdout should include identity message:\n{}", |
| 85 | + output_stdout(&logout), |
| 86 | + ); |
| 87 | + |
| 88 | + let config_after = read_config(&test); |
| 89 | + assert!( |
| 90 | + config_after.get("spacetimedb_token").is_none(), |
| 91 | + "spacetimedb_token should be removed after logout: {:?}", |
| 92 | + config_after.get("spacetimedb_token") |
| 93 | + ); |
| 94 | + assert!( |
| 95 | + config_after.get("web_session_token").is_none(), |
| 96 | + "web_session_token should be removed after logout: {:?}", |
| 97 | + config_after.get("web_session_token") |
| 98 | + ); |
| 99 | +} |
| 100 | + |
| 101 | +#[test] |
| 102 | +fn cli_logout_is_idempotent() { |
| 103 | + require_local_server!(); |
| 104 | + let test = Smoketest::builder().autopublish(false).build(); |
| 105 | + |
| 106 | + let login = test.spacetime_cmd(&["login", "--server-issued-login", &test.server_url]); |
| 107 | + assert_success(&login, "initial login"); |
| 108 | + |
| 109 | + let first_logout = test.spacetime_cmd(&["logout"]); |
| 110 | + assert_success(&first_logout, "first logout"); |
| 111 | + assert!( |
| 112 | + output_stdout(&first_logout).contains("Logged out "), |
| 113 | + "first logout should report logged-out:\n{}", |
| 114 | + output_stdout(&first_logout) |
| 115 | + ); |
| 116 | + |
| 117 | + let second_logout = test.spacetime_cmd(&["logout"]); |
| 118 | + assert_success(&second_logout, "second logout"); |
| 119 | + assert!( |
| 120 | + output_stdout(&second_logout).contains("You are not logged in."), |
| 121 | + "second logout should report not logged in:\n{}", |
| 122 | + output_stdout(&second_logout) |
| 123 | + ); |
| 124 | +} |
| 125 | + |
| 126 | +#[test] |
| 127 | +fn cli_direct_login_works_and_shows_core_messages() { |
| 128 | + require_local_server!(); |
| 129 | + let test = Smoketest::builder().autopublish(false).build(); |
| 130 | + |
| 131 | + let login = test.spacetime_cmd(&["login", "--server-issued-login", &test.server_url]); |
| 132 | + assert_success(&login, "direct login"); |
| 133 | + |
| 134 | + let login_stdout = output_stdout(&login); |
| 135 | + assert!( |
| 136 | + login_stdout.contains("Logged in "), |
| 137 | + "direct login stdout missing confirmation:\n{}", |
| 138 | + login_stdout |
| 139 | + ); |
| 140 | + |
| 141 | + let show = test.spacetime_cmd(&["login", "show"]); |
| 142 | + assert_success(&show, "login show"); |
| 143 | + assert!( |
| 144 | + output_stdout(&show).contains("You are logged in as "), |
| 145 | + "login show should report current identity:\n{}", |
| 146 | + output_stdout(&show) |
| 147 | + ); |
| 148 | +} |
| 149 | + |
| 150 | +#[test] |
| 151 | +fn cli_logging_in_twice_works() { |
| 152 | + require_local_server!(); |
| 153 | + let test = Smoketest::builder().autopublish(false).build(); |
| 154 | + |
| 155 | + let first = test.spacetime_cmd(&["login", "--server-issued-login", &test.server_url]); |
| 156 | + assert_success(&first, "first login"); |
| 157 | + |
| 158 | + let second = test.spacetime_cmd(&["login", "--server-issued-login", &test.server_url]); |
| 159 | + assert_success(&second, "second login"); |
| 160 | + |
| 161 | + let second_stdout = output_stdout(&second); |
| 162 | + assert!( |
| 163 | + second_stdout.contains("Logged out (identity "), |
| 164 | + "second login should log out previous identity first:\n{}", |
| 165 | + second_stdout |
| 166 | + ); |
| 167 | + assert!( |
| 168 | + second_stdout.contains("Logged in with identity "), |
| 169 | + "second login should complete with a new login:\n{}", |
| 170 | + second_stdout |
| 171 | + ); |
| 172 | +} |
0 commit comments