chorus/tests/auth.rs

225 lines
7.0 KiB
Rust
Raw Permalink Normal View History

2024-01-30 17:19:34 +01:00
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
use std::{os::unix::fs::chroot, str::FromStr};
use chorus::{instance::ChorusUser, types::{AuthenticatorType, LoginSchema, MfaVerifySchema, RegisterSchema, SendMfaSmsSchema}};
2023-11-20 14:03:06 +01:00
#[cfg(target_arch = "wasm32")]
use wasm_bindgen_test::*;
#[cfg(target_arch = "wasm32")]
wasm_bindgen_test_configure!(run_in_browser);
2023-05-27 22:46:50 +02:00
use chrono::NaiveDate;
mod common;
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
#[cfg_attr(not(target_arch = "wasm32"), tokio::test)]
2023-05-27 22:46:50 +02:00
async fn test_registration() {
2023-11-22 18:48:44 +01:00
let mut bundle = common::setup().await;
2023-06-25 11:33:50 +02:00
let reg = RegisterSchema {
username: "Hiiii".into(),
date_of_birth: Some(NaiveDate::from_str("2000-01-01").unwrap()),
2023-06-25 11:33:50 +02:00
consent: true,
..Default::default()
};
2023-11-22 18:48:44 +01:00
bundle.instance.register_account(reg).await.unwrap();
common::teardown(bundle).await;
}
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
#[cfg_attr(not(target_arch = "wasm32"), tokio::test)]
async fn test_login() {
let mut bundle = common::setup().await;
let reg = RegisterSchema {
username: "Hiiii".into(),
email: Some("testuser1@integrationtesting.xyz".into()),
password: Some("Correct-Horse-Battery-Staple1".into()),
date_of_birth: Some(NaiveDate::from_str("2000-01-01").unwrap()),
2023-11-22 18:48:44 +01:00
consent: true,
..Default::default()
};
bundle.instance.register_account(reg).await.unwrap();
let login = LoginSchema {
login: "testuser1@integrationtesting.xyz".into(),
password: "Correct-Horse-Battery-Staple1".into(),
..Default::default()
};
bundle.instance.login_account(login).await.unwrap();
common::teardown(bundle).await;
}
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
#[cfg_attr(not(target_arch = "wasm32"), tokio::test)]
async fn test_wrong_login() {
let mut bundle = common::setup().await;
let reg = RegisterSchema {
username: "Hiiii".into(),
email: Some("testuser2@integrationtesting.xyz".into()),
password: Some("Correct-Horse-Battery-Staple1".into()),
date_of_birth: Some(NaiveDate::from_str("2000-01-01").unwrap()),
2023-11-22 18:48:44 +01:00
consent: true,
..Default::default()
};
bundle.instance.register_account(reg).await.unwrap();
let login = LoginSchema {
login: "testuser2@integrationtesting.xyz".into(),
password: "Correct-Horse-Battery-Staple2".into(),
..Default::default()
};
let res = bundle.instance.login_account(login).await;
assert!(res.is_err());
2023-05-27 22:46:50 +02:00
common::teardown(bundle).await;
}
2023-11-22 18:38:23 +01:00
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
#[cfg_attr(not(target_arch = "wasm32"), tokio::test)]
async fn test_login_with_token() {
let mut bundle = common::setup().await;
let token = &bundle.user.token;
let other_user = bundle
.instance
.login_with_token(token.clone())
.await
.unwrap();
assert_eq!(
bundle.user.object.as_ref().unwrap().read().unwrap().id,
other_user.object.unwrap().read().unwrap().id
2023-11-22 18:38:23 +01:00
);
assert_eq!(bundle.user.token, other_user.token);
common::teardown(bundle).await;
}
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
#[cfg_attr(not(target_arch = "wasm32"), tokio::test)]
async fn test_login_with_invalid_token() {
let mut bundle = common::setup().await;
let token = "invalid token lalalalala".to_string();
let other_user = bundle.instance.login_with_token(token.clone()).await;
assert!(other_user.is_err());
common::teardown(bundle).await;
}
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
#[cfg_attr(not(target_arch = "wasm32"), tokio::test)]
async fn test_complete_mfa_challenge_totp() {
let mut bundle = common::setup().await;
let token = "".to_string();
let mut chorus_user = bundle.instance.login_with_token(token).await
.unwrap();
let schema = MfaVerifySchema {
ticket: "".to_string(),
mfa_type: AuthenticatorType::TOTP,
data: "".to_string(),
};
let result = chorus_user.complete_mfa_challenge(schema)
.await;
assert!(result.is_ok())
}
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
#[cfg_attr(not(target_arch = "wasm32"), tokio::test)]
async fn test_complete_mfa_challenge_sms() {
let mut bundle = common::setup().await;
let token = "".to_string();
let mut chorus_user = bundle.instance.login_with_token(token).await
.unwrap();
let schema = MfaVerifySchema {
ticket: "".to_string(),
mfa_type: AuthenticatorType::SMS,
data: "".to_string(),
};
let result = chorus_user.complete_mfa_challenge(schema)
.await;
assert!(result.is_ok())
}
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
#[cfg_attr(not(target_arch = "wasm32"), tokio::test)]
async fn test_verify_mfa_login_webauthn() {
let mut bundle = common::setup().await;
let token = "".to_string();
let mut chorus_user = bundle.instance.login_with_token(token).await
.unwrap();
let schema = MfaVerifySchema {
ticket: "".to_string(),
mfa_type: AuthenticatorType::SMS,
data: "".to_string(),
};
let result = chorus_user.complete_mfa_challenge(schema)
.await;
assert!(result.is_ok())
}
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
#[cfg_attr(not(target_arch = "wasm32"), tokio::test)]
async fn test_complete_mfa_challenge_backup() {
let mut bundle = common::setup().await;
let token = "".to_string();
let mut chorus_user = bundle.instance.login_with_token(token).await
.unwrap();
let schema = MfaVerifySchema {
ticket: "".to_string(),
mfa_type: AuthenticatorType::Backup,
data: "".to_string(),
};
let result = chorus_user.complete_mfa_challenge(schema)
.await;
assert!(result.is_ok())
}
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
#[cfg_attr(not(target_arch = "wasm32"), tokio::test)]
async fn test_complete_mfa_challenge_password() {
let mut bundle = common::setup().await;
let token = "".to_string();
let mut chorus_user = bundle.instance.login_with_token(token).await
.unwrap();
let schema = MfaVerifySchema {
ticket: "".to_string(),
mfa_type: AuthenticatorType::Password,
data: "".to_string(),
};
let result = chorus_user.complete_mfa_challenge(schema)
.await;
assert!(result.is_ok())
}
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
#[cfg_attr(not(target_arch = "wasm32"), tokio::test)]
async fn test_send_mfa_sms() {
let mut bundle = common::setup().await;
let schema = SendMfaSmsSchema { ticket: "".to_string() };
let result = bundle.instance.send_mfa_sms(schema).await;
assert!(result.is_ok())
}