Skip to content

Commit

Permalink
fix: httptest does not work on wasm
Browse files Browse the repository at this point in the history
  • Loading branch information
kozabrada123 committed Oct 9, 2024
1 parent ad0b5fd commit 7a392d3
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 11 deletions.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ lazy_static = "1.5.0"
wasm-bindgen-test = "0.3.43"
wasm-bindgen = "0.2.93"
simple_logger = { version = "5.0.0", default-features = false }

[target.'cfg(not(target_arch = "wasm32"))'.dev-dependencies]
httptest = "0.16.1"

[lints.rust]
Expand Down
15 changes: 9 additions & 6 deletions tests/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@ use std::str::FromStr;
use chorus::types::{
AuthenticatorType, LoginSchema, MfaVerifySchema, RegisterSchema, SendMfaSmsSchema,
};

#[cfg(not(target_arch = "wasm32"))]
use httptest::{
matchers::{all_of, contains, eq, json_decoded, request},
responders::json_encoded,
Expectation,
};

use serde_json::json;
#[cfg(target_arch = "wasm32")]
use wasm_bindgen_test::*;
Expand Down Expand Up @@ -110,8 +113,8 @@ async fn test_login_with_invalid_token() {
common::teardown(bundle).await;
}

#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
#[cfg_attr(not(target_arch = "wasm32"), tokio::test)]
#[cfg(not(target_arch = "wasm32"))]
async fn test_complete_mfa_challenge_totp() {
let server = common::create_mock_server();
let mut bundle = common::setup_with_mock_server(&server).await;
Expand Down Expand Up @@ -140,8 +143,8 @@ async fn test_complete_mfa_challenge_totp() {
assert_eq!(bundle.user.mfa_token.unwrap().token, "testtoken".to_string());
}

#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
#[cfg_attr(not(target_arch = "wasm32"), tokio::test)]
#[cfg(not(target_arch = "wasm32"))]
async fn test_complete_mfa_challenge_sms() {
let server = common::create_mock_server();
let mut bundle = common::setup_with_mock_server(&server).await;
Expand Down Expand Up @@ -170,8 +173,8 @@ async fn test_complete_mfa_challenge_sms() {
assert_eq!(bundle.user.mfa_token.unwrap().token, "testtoken".to_string());
}

#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
#[cfg_attr(not(target_arch = "wasm32"), tokio::test)]
#[cfg(not(target_arch = "wasm32"))]
async fn test_verify_mfa_login_webauthn() {
let server = common::create_mock_server();
let mut bundle = common::setup_with_mock_server(&server).await;
Expand Down Expand Up @@ -200,8 +203,8 @@ async fn test_verify_mfa_login_webauthn() {
assert_eq!(bundle.user.mfa_token.unwrap().token, "testtoken".to_string());
}

#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
#[cfg_attr(not(target_arch = "wasm32"), tokio::test)]
#[cfg(not(target_arch = "wasm32"))]
async fn test_complete_mfa_challenge_backup() {
let server = common::create_mock_server();
let mut bundle = common::setup_with_mock_server(&server).await;
Expand Down Expand Up @@ -230,8 +233,8 @@ async fn test_complete_mfa_challenge_backup() {
assert_eq!(bundle.user.mfa_token.unwrap().token, "testtoken".to_string());
}

#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
#[cfg_attr(not(target_arch = "wasm32"), tokio::test)]
#[cfg(not(target_arch = "wasm32"))]
async fn test_complete_mfa_challenge_password() {
let server = common::create_mock_server();
let mut bundle = common::setup_with_mock_server(&server).await;
Expand Down Expand Up @@ -259,8 +262,8 @@ async fn test_complete_mfa_challenge_password() {
assert!(result.is_ok())
}

#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
#[cfg_attr(not(target_arch = "wasm32"), tokio::test)]
#[cfg(not(target_arch = "wasm32"))]
async fn test_send_mfa_sms() {
let server = common::create_mock_server();
let mut bundle = common::setup_with_mock_server(&server).await;
Expand Down
25 changes: 20 additions & 5 deletions tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,13 @@ use chorus::{
};

use chrono::NaiveDate;
use httptest::matchers::{all_of, contains, request};
use httptest::responders::{json_encoded, status_code};
use httptest::Expectation;

#[cfg(not(target_arch = "wasm32"))]
use httptest::{
matchers::{all_of, contains, request},
responders::{json_encoded, status_code},
Expectation,
};

#[allow(dead_code)]
#[derive(Debug)]
Expand Down Expand Up @@ -63,6 +67,7 @@ impl TestBundle {

/// Set up a test by creating an [Instance] and a User for a real,
/// running server at localhost:3001. Reduces Test boilerplate.
#[allow(dead_code)]
pub(crate) async fn setup() -> TestBundle {
// So we can get logs when tests fail
let _ = simple_logger::SimpleLogger::with_level(
Expand Down Expand Up @@ -146,11 +151,15 @@ pub(crate) async fn setup() -> TestBundle {
}
}

/// Set up a test by "creating" an [Instance] and a User for a mocked
/// Set up a test by creating an [Instance] and a User for a mocked
/// server with httptest. Reduces Test boilerplate.
///
/// Note: httptest does not work on wasm!
///
/// This test server will always provide snowflake ids as 123456789101112131
/// and auth tokens as "faketoken"
#[allow(dead_code)]
#[cfg(not(target_arch = "wasm32"))]
pub(crate) async fn setup_with_mock_server(server: &httptest::Server) -> TestBundle {
// So we can get logs when tests fail
let _ = simple_logger::SimpleLogger::with_level(
Expand All @@ -159,7 +168,9 @@ pub(crate) async fn setup_with_mock_server(server: &httptest::Server) -> TestBun
)
.init();

let instance = Instance::new(server.url_str("/api").as_str(), None).await.unwrap();
let instance = Instance::new(server.url_str("/api").as_str(), None)
.await
.unwrap();

// Requires the existence of the below user.
let reg = RegisterSchema {
Expand Down Expand Up @@ -223,8 +234,12 @@ pub(crate) async fn teardown(mut bundle: TestBundle) {
/// Creates a mock http server at localhost:3001 with the basic routes
/// needed to run TestBundle setup and teardown
///
/// Note: httptest does not work on wasm!
///
/// This test server will always provide snowflake ids as 123456789101112131
/// and auth tokens as "faketoken"
#[allow(dead_code)]
#[cfg(not(target_arch = "wasm32"))]
pub(crate) fn create_mock_server() -> httptest::Server {
let server = httptest::Server::run();

Expand Down

0 comments on commit 7a392d3

Please sign in to comment.