feat: sytests
This commit is contained in:
parent
8e041f90dd
commit
00a9424719
13 changed files with 1339 additions and 36 deletions
|
@ -66,7 +66,7 @@ pub fn get_register_available_route(
|
|||
) -> MatrixResult<get_username_availability::Response> {
|
||||
// Validate user id
|
||||
let user_id: UserId =
|
||||
match (*format!("@{}:{}", body.username.clone(), db.globals.hostname())).try_into() {
|
||||
match (*format!("@{}:{}", body.username.clone(), db.globals.server_name())).try_into() {
|
||||
Err(_) => {
|
||||
debug!("Username invalid");
|
||||
return MatrixResult(Err(Error {
|
||||
|
@ -117,7 +117,7 @@ pub fn register_route(
|
|||
body.username
|
||||
.clone()
|
||||
.unwrap_or_else(|| utils::random_string(GUEST_NAME_LENGTH)),
|
||||
db.globals.hostname()
|
||||
db.globals.server_name()
|
||||
))
|
||||
.try_into()
|
||||
{
|
||||
|
@ -229,7 +229,7 @@ pub fn login_route(
|
|||
(body.user.clone(), body.login_info.clone())
|
||||
{
|
||||
if !username.contains(':') {
|
||||
username = format!("@{}:{}", username, db.globals.hostname());
|
||||
username = format!("@{}:{}", username, db.globals.server_name());
|
||||
}
|
||||
if let Ok(user_id) = (*username).try_into() {
|
||||
if let Some(hash) = db.users.password_hash(&user_id).unwrap() {
|
||||
|
@ -288,7 +288,7 @@ pub fn login_route(
|
|||
MatrixResult(Ok(login::Response {
|
||||
user_id,
|
||||
access_token: token,
|
||||
home_server: Some(db.globals.hostname().to_owned()),
|
||||
home_server: Some(db.globals.server_name().to_owned()),
|
||||
device_id,
|
||||
well_known: None,
|
||||
}))
|
||||
|
@ -769,7 +769,7 @@ pub fn create_room_route(
|
|||
body: Ruma<create_room::Request>,
|
||||
) -> MatrixResult<create_room::Response> {
|
||||
// TODO: check if room is unique
|
||||
let room_id = RoomId::try_from(db.globals.hostname()).expect("host is valid");
|
||||
let room_id = RoomId::try_from(db.globals.server_name()).expect("host is valid");
|
||||
let user_id = body.user_id.as_ref().expect("user is authenticated");
|
||||
|
||||
db.rooms
|
||||
|
@ -858,7 +858,7 @@ pub fn get_alias_route(
|
|||
_room_alias: String,
|
||||
) -> MatrixResult<get_alias::Response> {
|
||||
// TODO
|
||||
let room_id = if body.room_alias.server_name() == db.globals.hostname() {
|
||||
let room_id = if body.room_alias.server_name() == db.globals.server_name() {
|
||||
match body.room_alias.alias() {
|
||||
"conduit" => "!lgOCCXQKtXOAPlAlG5:conduit.rs",
|
||||
_ => {
|
||||
|
@ -923,7 +923,7 @@ pub fn join_room_by_id_or_alias_route(
|
|||
let room_id = match RoomId::try_from(body.room_id_or_alias.clone()) {
|
||||
Ok(room_id) => room_id,
|
||||
Err(room_alias) => {
|
||||
if room_alias.server_name() == db.globals.hostname() {
|
||||
if room_alias.server_name() == db.globals.server_name() {
|
||||
return MatrixResult(Err(Error {
|
||||
kind: ErrorKind::NotFound,
|
||||
message: "Room alias not found.".to_owned(),
|
||||
|
|
|
@ -7,6 +7,8 @@ pub(self) mod users;
|
|||
use directories::ProjectDirs;
|
||||
use std::fs::remove_dir_all;
|
||||
|
||||
use rocket::Config;
|
||||
|
||||
pub struct Database {
|
||||
pub globals: globals::Globals,
|
||||
pub users: users::Users,
|
||||
|
@ -18,26 +20,38 @@ pub struct Database {
|
|||
|
||||
impl Database {
|
||||
/// Tries to remove the old database but ignores all errors.
|
||||
pub fn try_remove(hostname: &str) {
|
||||
pub fn try_remove(server_name: &str) {
|
||||
let mut path = ProjectDirs::from("xyz", "koesters", "conduit")
|
||||
.unwrap()
|
||||
.data_dir()
|
||||
.to_path_buf();
|
||||
path.push(hostname);
|
||||
path.push(server_name);
|
||||
let _ = remove_dir_all(path);
|
||||
}
|
||||
|
||||
/// Load an existing database or create a new one.
|
||||
pub fn load_or_create(hostname: &str) -> Self {
|
||||
let mut path = ProjectDirs::from("xyz", "koesters", "conduit")
|
||||
.unwrap()
|
||||
.data_dir()
|
||||
.to_path_buf();
|
||||
path.push(hostname);
|
||||
pub fn load_or_create(config: &Config) -> Self {
|
||||
let server_name = config.get_str("server_name").unwrap_or("localhost");
|
||||
|
||||
let path = config
|
||||
.get_str("database_path")
|
||||
.map(|x| x.to_owned())
|
||||
.unwrap_or_else(|_| {
|
||||
let path = ProjectDirs::from("xyz", "koesters", "conduit")
|
||||
.unwrap()
|
||||
.data_dir()
|
||||
.join(server_name);
|
||||
path.to_str().unwrap().to_owned()
|
||||
});
|
||||
|
||||
let db = sled::open(&path).unwrap();
|
||||
log::info!("Opened sled database at {}", path);
|
||||
|
||||
Self {
|
||||
globals: globals::Globals::load(db.open_tree("global").unwrap(), hostname.to_owned()),
|
||||
globals: globals::Globals::load(
|
||||
db.open_tree("global").unwrap(),
|
||||
server_name.to_owned(),
|
||||
),
|
||||
users: users::Users {
|
||||
userid_password: db.open_tree("userid_password").unwrap(),
|
||||
userdeviceids: db.open_tree("userdeviceids").unwrap(),
|
||||
|
|
|
@ -4,13 +4,13 @@ pub const COUNTER: &str = "c";
|
|||
|
||||
pub struct Globals {
|
||||
pub(super) globals: sled::Tree,
|
||||
hostname: String,
|
||||
server_name: String,
|
||||
keypair: ruma_signatures::Ed25519KeyPair,
|
||||
reqwest_client: reqwest::Client,
|
||||
}
|
||||
|
||||
impl Globals {
|
||||
pub fn load(globals: sled::Tree, hostname: String) -> Self {
|
||||
pub fn load(globals: sled::Tree, server_name: String) -> Self {
|
||||
let keypair = ruma_signatures::Ed25519KeyPair::new(
|
||||
&*globals
|
||||
.update_and_fetch("keypair", utils::generate_keypair)
|
||||
|
@ -22,15 +22,15 @@ impl Globals {
|
|||
|
||||
Self {
|
||||
globals,
|
||||
hostname,
|
||||
server_name,
|
||||
keypair,
|
||||
reqwest_client: reqwest::Client::new(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the hostname of the server.
|
||||
pub fn hostname(&self) -> &str {
|
||||
&self.hostname
|
||||
/// Returns the server_name of the server.
|
||||
pub fn server_name(&self) -> &str {
|
||||
&self.server_name
|
||||
}
|
||||
|
||||
/// Returns this server's keypair.
|
||||
|
|
|
@ -216,7 +216,7 @@ impl Rooms {
|
|||
event_id: EventId::try_from("$thiswillbefilledinlater").expect("we know this is valid"),
|
||||
room_id: room_id.clone(),
|
||||
sender: sender.clone(),
|
||||
origin: globals.hostname().to_owned(),
|
||||
origin: globals.server_name().to_owned(),
|
||||
origin_server_ts: utils::millis_since_unix_epoch()
|
||||
.try_into()
|
||||
.expect("this only fails many years in the future"),
|
||||
|
@ -245,8 +245,12 @@ impl Rooms {
|
|||
.expect("ruma's reference hashes are correct");
|
||||
|
||||
let mut pdu_json = serde_json::to_value(&pdu)?;
|
||||
ruma_signatures::hash_and_sign_event(globals.hostname(), globals.keypair(), &mut pdu_json)
|
||||
.expect("our new event can be hashed and signed");
|
||||
ruma_signatures::hash_and_sign_event(
|
||||
globals.server_name(),
|
||||
globals.keypair(),
|
||||
&mut pdu_json,
|
||||
)
|
||||
.expect("our new event can be hashed and signed");
|
||||
|
||||
self.replace_pdu_leaves(&room_id, &pdu.event_id)?;
|
||||
|
||||
|
|
|
@ -75,8 +75,7 @@ fn setup_rocket() -> rocket::Rocket {
|
|||
],
|
||||
)
|
||||
.attach(AdHoc::on_attach("Config", |rocket| {
|
||||
let hostname = rocket.config().get_str("hostname").unwrap_or("localhost");
|
||||
let data = Database::load_or_create(&hostname);
|
||||
let data = Database::load_or_create(&rocket.config());
|
||||
|
||||
Ok(rocket.manage(data))
|
||||
}))
|
||||
|
|
|
@ -55,12 +55,12 @@ pub async fn send_request<T: Endpoint>(
|
|||
|
||||
request_map.insert("method".to_owned(), T::METADATA.method.to_string().into());
|
||||
request_map.insert("uri".to_owned(), T::METADATA.path.into());
|
||||
request_map.insert("origin".to_owned(), db.globals.hostname().into());
|
||||
request_map.insert("origin".to_owned(), db.globals.server_name().into());
|
||||
request_map.insert("destination".to_owned(), destination.into());
|
||||
|
||||
let mut request_json = request_map.into();
|
||||
ruma_signatures::sign_json(
|
||||
db.globals.hostname(),
|
||||
db.globals.server_name(),
|
||||
db.globals.keypair(),
|
||||
&mut request_json,
|
||||
)
|
||||
|
@ -82,7 +82,7 @@ pub async fn send_request<T: Endpoint>(
|
|||
AUTHORIZATION,
|
||||
HeaderValue::from_str(&format!(
|
||||
"X-Matrix origin={},key=\"{}\",sig=\"{}\"",
|
||||
db.globals.hostname(),
|
||||
db.globals.server_name(),
|
||||
s.0,
|
||||
s.1
|
||||
))
|
||||
|
@ -156,7 +156,7 @@ pub fn get_server_keys(db: State<'_, Database>) -> Json<String> {
|
|||
);
|
||||
let mut response = serde_json::from_slice(
|
||||
http::Response::try_from(get_server_keys::Response {
|
||||
server_name: db.globals.hostname().to_owned(),
|
||||
server_name: db.globals.server_name().to_owned(),
|
||||
verify_keys,
|
||||
old_verify_keys: BTreeMap::new(),
|
||||
signatures: BTreeMap::new(),
|
||||
|
@ -166,7 +166,7 @@ pub fn get_server_keys(db: State<'_, Database>) -> Json<String> {
|
|||
.body(),
|
||||
)
|
||||
.unwrap();
|
||||
ruma_signatures::sign_json(db.globals.hostname(), db.globals.keypair(), &mut response).unwrap();
|
||||
ruma_signatures::sign_json(db.globals.server_name(), db.globals.keypair(), &mut response).unwrap();
|
||||
Json(response.to_string())
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue