diff options
Diffstat (limited to 'src/config.rs')
| -rw-r--r-- | src/config.rs | 111 |
1 files changed, 65 insertions, 46 deletions
diff --git a/src/config.rs b/src/config.rs index b26dea0..f410934 100644 --- a/src/config.rs +++ b/src/config.rs | |||
| @@ -4,6 +4,7 @@ use anyhow::{anyhow, Context, Result}; | |||
| 4 | use directories::ProjectDirs; | 4 | use directories::ProjectDirs; |
| 5 | #[cfg(test)] | 5 | #[cfg(test)] |
| 6 | use mockall::*; | 6 | use mockall::*; |
| 7 | use nostr::secp256k1::XOnlyPublicKey; | ||
| 7 | use serde::{self, Deserialize, Serialize}; | 8 | use serde::{self, Deserialize, Serialize}; |
| 8 | 9 | ||
| 9 | #[derive(Default)] | 10 | #[derive(Default)] |
| @@ -59,7 +60,7 @@ impl ConfigManagement for ConfigManager { | |||
| 59 | } | 60 | } |
| 60 | } | 61 | } |
| 61 | 62 | ||
| 62 | #[derive(Serialize, Deserialize, Clone, Default, Debug, PartialEq, Eq)] | 63 | #[derive(Serialize, Deserialize, Default, Clone, Debug, PartialEq, Eq)] |
| 63 | #[allow(clippy::module_name_repetitions)] | 64 | #[allow(clippy::module_name_repetitions)] |
| 64 | pub struct MyConfig { | 65 | pub struct MyConfig { |
| 65 | pub version: u8, | 66 | pub version: u8, |
| @@ -68,44 +69,64 @@ pub struct MyConfig { | |||
| 68 | 69 | ||
| 69 | #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)] | 70 | #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)] |
| 70 | pub struct UserRef { | 71 | pub struct UserRef { |
| 71 | pub nsec: String, | 72 | pub public_key: XOnlyPublicKey, |
| 73 | pub encrypted_key: String, | ||
| 72 | } | 74 | } |
| 73 | 75 | ||
| 74 | #[cfg(test)] | 76 | #[cfg(test)] |
| 75 | mod tests { | 77 | mod tests { |
| 76 | use anyhow::Result; | 78 | use anyhow::Result; |
| 77 | use serial_test::serial; | 79 | use serial_test::serial; |
| 78 | use test_utils::*; | ||
| 79 | 80 | ||
| 80 | use super::*; | 81 | use super::*; |
| 81 | 82 | ||
| 83 | fn backup_existing_config() -> Result<()> { | ||
| 84 | let config_path = get_dirs()?.config_dir().join("config.json"); | ||
| 85 | let backup_config_path = get_dirs()?.config_dir().join("config-backup.json"); | ||
| 86 | if config_path.exists() { | ||
| 87 | std::fs::rename(config_path, backup_config_path)?; | ||
| 88 | } | ||
| 89 | Ok(()) | ||
| 90 | } | ||
| 91 | |||
| 92 | fn restore_config_backup() -> Result<()> { | ||
| 93 | let config_path = get_dirs()?.config_dir().join("config.json"); | ||
| 94 | let backup_config_path = get_dirs()?.config_dir().join("config-backup.json"); | ||
| 95 | if config_path.exists() { | ||
| 96 | std::fs::remove_file(&config_path)?; | ||
| 97 | } | ||
| 98 | if backup_config_path.exists() { | ||
| 99 | std::fs::rename(backup_config_path, config_path)?; | ||
| 100 | } | ||
| 101 | Ok(()) | ||
| 102 | } | ||
| 103 | |||
| 82 | mod load { | 104 | mod load { |
| 83 | use super::*; | 105 | use super::*; |
| 84 | 106 | ||
| 85 | #[test] | 107 | #[test] |
| 86 | #[serial] | 108 | #[serial] |
| 87 | fn when_config_file_doesnt_exist_defaults_are_returned() -> Result<()> { | 109 | fn when_config_file_doesnt_exist_defaults_are_returned() -> Result<()> { |
| 88 | with_fresh_config(|| { | 110 | backup_existing_config()?; |
| 89 | assert_eq!(ConfigManager.load()?, MyConfig::default()); | 111 | let c = ConfigManager; |
| 90 | 112 | assert_eq!(c.load()?, MyConfig::default()); | |
| 91 | Ok(()) | 113 | restore_config_backup()?; |
| 92 | }) | 114 | Ok(()) |
| 93 | } | 115 | } |
| 94 | 116 | ||
| 95 | #[test] | 117 | #[test] |
| 96 | #[serial] | 118 | #[serial] |
| 97 | fn when_config_file_exists_it_is_returned() -> Result<()> { | 119 | fn when_config_file_exists_it_is_returned() -> Result<()> { |
| 98 | with_fresh_config(|| { | 120 | backup_existing_config()?; |
| 99 | let c = ConfigManager; | 121 | let c = ConfigManager; |
| 100 | let new_config = MyConfig { | 122 | let new_config = MyConfig { |
| 101 | version: 255, | 123 | version: 255, |
| 102 | ..MyConfig::default() | 124 | ..MyConfig::default() |
| 103 | }; | 125 | }; |
| 104 | c.save(&new_config)?; | 126 | c.save(&new_config)?; |
| 105 | assert_eq!(c.load()?, new_config); | 127 | assert_eq!(c.load()?, new_config); |
| 106 | 128 | restore_config_backup()?; | |
| 107 | Ok(()) | 129 | Ok(()) |
| 108 | }) | ||
| 109 | } | 130 | } |
| 110 | } | 131 | } |
| 111 | 132 | ||
| @@ -115,38 +136,36 @@ mod tests { | |||
| 115 | #[test] | 136 | #[test] |
| 116 | #[serial] | 137 | #[serial] |
| 117 | fn when_config_file_doesnt_config_is_saved() -> Result<()> { | 138 | fn when_config_file_doesnt_config_is_saved() -> Result<()> { |
| 118 | with_fresh_config(|| { | 139 | backup_existing_config()?; |
| 119 | let c = ConfigManager; | 140 | let c = ConfigManager; |
| 120 | let new_config = MyConfig { | 141 | let new_config = MyConfig { |
| 121 | version: 255, | 142 | version: 255, |
| 122 | ..MyConfig::default() | 143 | ..MyConfig::default() |
| 123 | }; | 144 | }; |
| 124 | c.save(&new_config)?; | 145 | c.save(&new_config)?; |
| 125 | assert_eq!(c.load()?, new_config); | 146 | assert_eq!(c.load().unwrap(), new_config); |
| 126 | 147 | restore_config_backup()?; | |
| 127 | Ok(()) | 148 | Ok(()) |
| 128 | }) | ||
| 129 | } | 149 | } |
| 130 | 150 | ||
| 131 | #[test] | 151 | #[test] |
| 132 | #[serial] | 152 | #[serial] |
| 133 | fn when_config_file_exists_new_config_is_saved() -> Result<()> { | 153 | fn when_config_file_exists_new_config_is_saved() -> Result<()> { |
| 134 | with_fresh_config(|| { | 154 | backup_existing_config()?; |
| 135 | let c = ConfigManager; | 155 | let c = ConfigManager; |
| 136 | let config = MyConfig { | 156 | let config = MyConfig { |
| 137 | version: 255, | 157 | version: 255, |
| 138 | ..MyConfig::default() | 158 | ..MyConfig::default() |
| 139 | }; | 159 | }; |
| 140 | c.save(&config)?; | 160 | c.save(&config)?; |
| 141 | let new_config = MyConfig { | 161 | let new_config = MyConfig { |
| 142 | version: 254, | 162 | version: 254, |
| 143 | ..MyConfig::default() | 163 | ..MyConfig::default() |
| 144 | }; | 164 | }; |
| 145 | c.save(&new_config)?; | 165 | c.save(&new_config)?; |
| 146 | assert_eq!(c.load()?, new_config); | 166 | assert_eq!(c.load().unwrap(), new_config); |
| 147 | 167 | restore_config_backup()?; | |
| 148 | Ok(()) | 168 | Ok(()) |
| 149 | }) | ||
| 150 | } | 169 | } |
| 151 | } | 170 | } |
| 152 | } | 171 | } |