1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
|
mod utils;
use std::collections::BTreeSet;
use chrono::Utc;
use nethsm_sdk_rs::{
apis::{configuration::Configuration, default_api, Error},
models::{
BackupPassphraseConfig, KeyGenerateRequestData, KeyMechanism, KeyType,
ProvisionRequestData, RestoreRequestArguments, SystemState, UnlockRequestData,
UserPostData, UserRole,
},
};
#[tokio::test]
async fn test_health_state() {
utils::with_container(|config| {
let result = default_api::health_state_get(&config);
assert!(result.is_ok(), "{result:?}");
})
.await
}
#[tokio::test]
async fn test_error_with_body() {
utils::with_container(|config| {
let err = default_api::keys_get(&config, None).err().unwrap();
match err {
Error::ResponseError(content) => {
assert_eq!(content.status, 412);
let err = String::from_utf8_lossy(&content.content);
assert!(
err.contains("Service not available"),
"unexpected error message: {err}"
);
match content.entity {
default_api::KeysGetError::Status412() => {}
err => panic!("Unexpected error variant: {err:?}"),
}
}
_ => {
panic!("Unexpected error variant: {err:?}");
}
}
})
.await
}
#[tokio::test]
async fn test_namespaces() {
let admin_passphrase = "adminadmin";
let n_admin_passphrase = "admin2admin2";
let unlock_passphrase = "unlockunlock";
utils::with_container(|mut config| {
let request = ProvisionRequestData::new(
unlock_passphrase.to_owned(),
admin_passphrase.to_owned(),
Utc::now().to_rfc3339(),
);
default_api::provision_post(&config, request).unwrap();
config.basic_auth = Some(("admin".to_owned(), Some(admin_passphrase.to_owned())));
let request = UserPostData::new(
"N-Admin".to_owned(),
UserRole::Administrator,
n_admin_passphrase.to_owned(),
);
let user_id = default_api::users_user_id_post(&config, "mynamespace~", request)
.unwrap()
.entity
.id;
assert!(user_id.starts_with("mynamespace~"));
assert_eq!(list_namespaces(&config), BTreeSet::new());
default_api::namespaces_namespace_id_put(&config, "mynamespace").unwrap();
assert_eq!(
list_namespaces(&config),
["mynamespace".to_owned()].into_iter().collect()
);
config.basic_auth = Some((user_id, Some(n_admin_passphrase.to_owned())));
let mut request =
KeyGenerateRequestData::new(vec![KeyMechanism::RsaDecryptionRaw], KeyType::Rsa);
request.length = Some(2048);
let key_id = default_api::keys_generate_post(&config, request)
.unwrap()
.entity
.id;
let keys = BTreeSet::from([key_id.clone()]);
assert_eq!(list_keys(&config), keys);
config.basic_auth = Some(("admin".to_owned(), Some(admin_passphrase.to_owned())));
assert_eq!(list_keys(&config), BTreeSet::new());
default_api::namespaces_namespace_id_delete(&config, "mynamespace").unwrap();
assert_eq!(list_namespaces(&config), BTreeSet::new());
})
.await
}
#[tokio::test]
async fn test_restore() {
let admin_passphrase = "adminadmin";
let backup_passphrase = "backupbackup";
let unlock_passphrase = "unlockunlock";
let (generated_keys, backup) = utils::with_container(|mut config| {
let request = ProvisionRequestData::new(
unlock_passphrase.to_owned(),
admin_passphrase.to_owned(),
Utc::now().to_rfc3339(),
);
default_api::provision_post(&config, request).unwrap();
config.basic_auth = Some(("admin".to_owned(), Some(admin_passphrase.to_owned())));
let mut request =
KeyGenerateRequestData::new(vec![KeyMechanism::RsaDecryptionRaw], KeyType::Rsa);
request.length = Some(2048);
let key_id = default_api::keys_generate_post(&config, request)
.unwrap()
.entity
.id;
let keys = BTreeSet::from([key_id.clone()]);
assert_eq!(list_keys(&config), keys);
let request = BackupPassphraseConfig::new(backup_passphrase.to_owned(), String::new());
default_api::config_backup_passphrase_put(&config, request).unwrap();
let request = UserPostData::new(
"Backup User".to_owned(),
UserRole::Backup,
backup_passphrase.to_owned(),
);
default_api::users_user_id_put(&config, "backup", request).unwrap();
config.basic_auth = Some(("backup".to_owned(), Some(backup_passphrase.to_owned())));
let backup = default_api::system_backup_post(&config).unwrap().entity;
config.basic_auth = Some(("admin".to_owned(), Some(admin_passphrase.to_owned())));
default_api::keys_key_id_delete(&config, &key_id).unwrap();
assert_eq!(list_keys(&config), BTreeSet::default());
let mut request = RestoreRequestArguments::new();
request.backup_passphrase = Some(backup_passphrase.to_owned());
request.system_time = Some(Utc::now().to_rfc3339());
default_api::system_restore_post(&config, Some(request), Some(backup.clone())).unwrap();
assert_eq!(list_keys(&config), keys);
(keys, backup)
})
.await;
let restored_keys = utils::with_container(|mut config| {
let state = default_api::health_state_get(&config).unwrap().entity.state;
assert_eq!(state, SystemState::Unprovisioned);
let mut request = RestoreRequestArguments::new();
request.backup_passphrase = Some(backup_passphrase.to_owned());
request.system_time = Some(Utc::now().to_rfc3339());
default_api::system_restore_post(&config, Some(request), Some(backup)).unwrap();
let state = default_api::health_state_get(&config).unwrap().entity.state;
assert_eq!(state, SystemState::Locked);
let request = UnlockRequestData::new(unlock_passphrase.to_owned());
default_api::unlock_post(&config, request).unwrap();
config.basic_auth = Some(("admin".to_owned(), Some(admin_passphrase.to_owned())));
list_keys(&config)
})
.await;
assert_eq!(generated_keys, restored_keys);
}
fn list_keys(config: &Configuration) -> BTreeSet<String> {
default_api::keys_get(config, None)
.unwrap()
.entity
.into_iter()
.map(|item| item.id)
.collect()
}
fn list_namespaces(config: &Configuration) -> BTreeSet<String> {
default_api::namespaces_get(config)
.unwrap()
.entity
.into_iter()
.map(|item| item.id)
.collect()
}
|