| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | mod backup; |
| 18 | mod reset_password; |
| 19 | mod restore; |
| 20 | |
| 21 | pub use backup::run_backup; |
| 22 | pub use reset_password::print_reset_link; |
| 23 | pub use restore::run_restore; |
| 24 | |
| 25 | use std::path::PathBuf; |
| 26 | |
| 27 | use crate::application::RestoreMode; |
| 28 | |
| 29 | |
| 30 | |
| 31 | |
| 32 | |
| 33 | |
| 34 | |
| 35 | |
| 36 | pub(crate) const BACKUP_WORKSPACE: &str = ".backup"; |
| 37 | |
| 38 | |
| 39 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 40 | pub enum Command { |
| 41 | |
| 42 | Serve, |
| 43 | |
| 44 | Backup { destination: PathBuf }, |
| 45 | |
| 46 | |
| 47 | Restore { archive: PathBuf, mode: RestoreMode }, |
| 48 | |
| 49 | |
| 50 | ResetPassword { handle: String }, |
| 51 | } |
| 52 | |
| 53 | pub const USAGE: &str = "usage: |
| 54 | steid serve |
| 55 | steid backup <destination> write one archive of the database and data directory |
| 56 | steid restore <archive> restore an archive into an empty instance |
| 57 | steid restore --replace <a> replace everything this instance holds with the archive |
| 58 | steid reset-password <handle> print a one-time password reset link |
| 59 | "; |
| 60 | |
| 61 | impl Command { |
| 62 | |
| 63 | pub fn parse<I, S>(args: I) -> Result<Self, String> |
| 64 | where |
| 65 | I: IntoIterator<Item = S>, |
| 66 | S: Into<String>, |
| 67 | { |
| 68 | let args: Vec<String> = args.into_iter().map(Into::into).skip(1).collect(); |
| 69 | match args |
| 70 | .iter() |
| 71 | .map(String::as_str) |
| 72 | .collect::<Vec<_>>() |
| 73 | .as_slice() |
| 74 | { |
| 75 | [] => Ok(Self::Serve), |
| 76 | ["backup", destination] => Ok(Self::Backup { |
| 77 | destination: PathBuf::from(destination), |
| 78 | }), |
| 79 | |
| 80 | |
| 81 | ["restore", "--replace", archive] | ["restore", archive, "--replace"] => { |
| 82 | Ok(Self::Restore { |
| 83 | archive: PathBuf::from(archive), |
| 84 | mode: RestoreMode::Replace, |
| 85 | }) |
| 86 | } |
| 87 | |
| 88 | |
| 89 | ["restore", archive] if !archive.starts_with('-') => Ok(Self::Restore { |
| 90 | archive: PathBuf::from(archive), |
| 91 | mode: RestoreMode::OnlyIfEmpty, |
| 92 | }), |
| 93 | ["reset-password", handle] => Ok(Self::ResetPassword { |
| 94 | handle: (*handle).to_owned(), |
| 95 | }), |
| 96 | ["-h" | "--help" | "help", ..] => Err(USAGE.to_owned()), |
| 97 | other => Err(format!("steid: unrecognised arguments {other:?}\n{USAGE}")), |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | #[cfg(test)] |
| 103 | mod tests { |
| 104 | use super::*; |
| 105 | |
| 106 | #[test] |
| 107 | fn a_bare_invocation_serves() { |
| 108 | assert_eq!(Command::parse(["steid"]), Ok(Command::Serve)); |
| 109 | } |
| 110 | |
| 111 | #[test] |
| 112 | fn each_command_takes_exactly_one_argument() { |
| 113 | assert_eq!( |
| 114 | Command::parse(["steid", "backup", "/tmp/out"]), |
| 115 | Ok(Command::Backup { |
| 116 | destination: PathBuf::from("/tmp/out") |
| 117 | }) |
| 118 | ); |
| 119 | assert_eq!( |
| 120 | Command::parse(["steid", "restore", "a.tar.gz"]), |
| 121 | Ok(Command::Restore { |
| 122 | archive: PathBuf::from("a.tar.gz"), |
| 123 | mode: RestoreMode::OnlyIfEmpty, |
| 124 | }) |
| 125 | ); |
| 126 | assert_eq!( |
| 127 | Command::parse(["steid", "reset-password", "ada"]), |
| 128 | Ok(Command::ResetPassword { |
| 129 | handle: "ada".to_owned() |
| 130 | }) |
| 131 | ); |
| 132 | assert!(Command::parse(["steid", "backup"]).is_err()); |
| 133 | assert!(Command::parse(["steid", "frobnicate"]).is_err()); |
| 134 | } |
| 135 | |
| 136 | #[test] |
| 137 | fn replace_is_opt_in_and_reads_from_either_side_of_the_path() { |
| 138 | |
| 139 | |
| 140 | |
| 141 | for args in [ |
| 142 | ["steid", "restore", "--replace", "a.tar.gz"], |
| 143 | ["steid", "restore", "a.tar.gz", "--replace"], |
| 144 | ] { |
| 145 | assert_eq!( |
| 146 | Command::parse(args), |
| 147 | Ok(Command::Restore { |
| 148 | archive: PathBuf::from("a.tar.gz"), |
| 149 | mode: RestoreMode::Replace, |
| 150 | }), |
| 151 | "{args:?}" |
| 152 | ); |
| 153 | } |
| 154 | |
| 155 | assert!(Command::parse(["steid", "restore", "--replace"]).is_err()); |
| 156 | assert!(Command::parse(["steid", "restore", "--force", "a.tar.gz"]).is_err()); |
| 157 | } |
| 158 | |
| 159 | #[test] |
| 160 | fn the_usage_says_what_replace_does() { |
| 161 | let text = Command::parse(["steid", "--help"]).unwrap_err(); |
| 162 | |
| 163 | assert!( |
| 164 | text.contains("replace everything this instance holds with the archive"), |
| 165 | "{text}" |
| 166 | ); |
| 167 | } |
| 168 | |
| 169 | #[test] |
| 170 | fn help_is_an_error_carrying_the_usage() { |
| 171 | let text = Command::parse(["steid", "--help"]).unwrap_err(); |
| 172 | assert!(text.contains("steid backup <destination>")); |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | |
| 177 | |
| 178 | |
| 179 | |
| 180 | |
| 181 | |
| 182 | #[cfg(test)] |
| 183 | pub(crate) mod fixture { |
| 184 | use sqlx::SqlitePool; |
| 185 | use tempfile::TempDir; |
| 186 | |
| 187 | use crate::{ |
| 188 | application::{AppConfig, OwnerSpec, claim_instance}, |
| 189 | domain::SetupToken, |
| 190 | infrastructure::{ |
| 191 | database, |
| 192 | password::Argon2Hasher, |
| 193 | repository::{SqliteMembershipRepo, SqliteOrgRepo, SqliteUserRepo}, |
| 194 | }, |
| 195 | }; |
| 196 | |
| 197 | |
| 198 | |
| 199 | |
| 200 | |
| 201 | pub(crate) struct Instance { |
| 202 | pub(crate) dir: TempDir, |
| 203 | pub(crate) config: AppConfig, |
| 204 | pub(crate) pool: SqlitePool, |
| 205 | } |
| 206 | |
| 207 | |
| 208 | pub(crate) async fn empty() -> Instance { |
| 209 | let dir = tempfile::tempdir().expect("a temp dir"); |
| 210 | let data_dir = dir.path().join("data"); |
| 211 | std::fs::create_dir_all(&data_dir).expect("mkdir"); |
| 212 | |
| 213 | let database_url = format!("sqlite:{}?mode=rwc", dir.path().join("steid.db").display()); |
| 214 | let pool = database::connect(&database_url) |
| 215 | .await |
| 216 | .expect("the database opens"); |
| 217 | |
| 218 | Instance { |
| 219 | config: AppConfig { |
| 220 | database_url, |
| 221 | data_dir, |
| 222 | insecure_cookies: false, |
| 223 | allow_private_webhooks: false, |
| 224 | trusted_proxy: false, |
| 225 | operator_token: None, |
| 226 | setup_token: None, |
| 227 | backup_marker: None, |
| 228 | smtp_url: None, |
| 229 | mail_from: None, |
| 230 | owner_email: None, |
| 231 | owner_handle: None, |
| 232 | public_url: None, |
| 233 | }, |
| 234 | pool, |
| 235 | dir, |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | |
| 240 | |
| 241 | pub(crate) async fn claimed(handle: &str) -> Instance { |
| 242 | let instance = empty().await; |
| 243 | let token = SetupToken::generate(); |
| 244 | let revealed = token.reveal().to_owned(); |
| 245 | |
| 246 | claim_instance( |
| 247 | &revealed, |
| 248 | &token, |
| 249 | &OwnerSpec { |
| 250 | handle: handle.to_owned(), |
| 251 | email: format!("{handle}@example.com"), |
| 252 | password: "a-long-enough-password".to_owned(), |
| 253 | }, |
| 254 | &SqliteUserRepo::new(instance.pool.clone()), |
| 255 | &SqliteOrgRepo::new(instance.pool.clone()), |
| 256 | &SqliteMembershipRepo::new(instance.pool.clone()), |
| 257 | &Argon2Hasher::new(), |
| 258 | ) |
| 259 | .await |
| 260 | .expect("the claim succeeds"); |
| 261 | |
| 262 | instance |
| 263 | } |
| 264 | } |