aboutsummaryrefslogtreecommitdiffstats
path: root/crates/pseudonyms/tests/helpers.rs
blob: 5e2545bb7be499bbf5049d2bd2679b61c9b7ea66 (plain)
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
use sqlx::PgPool;
use tokio::sync::oneshot;
use tonic::transport::Channel;
use warden_core::pseudonyms::transaction_relationship::mutate_pseudonym_client::MutatePseudonymClient;
use warden_pseudonyms::state::{AppHandle, AppState, Services};
use warden_stack::{Configuration, cache::RedisManager};

use std::sync::Arc;

pub struct TestApp {
    _state: AppHandle,
    pub mutate: MutatePseudonymClient<Channel>,
}

impl TestApp {
    pub async fn new(pool: PgPool) -> Self {
        let (tx, rx) = oneshot::channel();
        // Set port to 0 so tests can spawn multiple servers on OS assigned ports.
        //
        let config_path = "pseudonyms.toml";

        let config = config::Config::builder()
            .add_source(config::File::new(config_path, config::FileFormat::Toml))
            .build()
            .unwrap();


        let mut config = config.try_deserialize::<Configuration>().unwrap();
        config.application.port = 0;

        let cache = RedisManager::new(&config.cache).await.unwrap();

        let services = Services {
            postgres: pool,
            cache,
        };

        let state = AppHandle(Arc::new(AppState::new(services, config, None).unwrap()));

        dbg!(&state.addr.port());

        tokio::spawn(warden_pseudonyms::run(state.clone(), tx));
        let port = rx.await.expect("channel to be open");
        let addr = format!("http://[::1]:{port}");

        let mutation_client = MutatePseudonymClient::connect(addr.to_string())
            .await
            .expect("expect server to be running");

        Self {
            _state: state,
            mutate: mutation_client,
        }
    }
}