summaryrefslogtreecommitdiff
path: root/tests/integration.rs
diff options
context:
space:
mode:
Diffstat (limited to 'tests/integration.rs')
-rw-r--r--tests/integration.rs70
1 files changed, 46 insertions, 24 deletions
diff --git a/tests/integration.rs b/tests/integration.rs
index afded52..1866702 100644
--- a/tests/integration.rs
+++ b/tests/integration.rs
@@ -16,31 +16,53 @@ use rocket::{
extern crate rocket;
extern crate anyhow;
-async fn run_pytest(markers: &'static str, invite: bool) -> anyhow::Result<()> {
+async fn run_pytest(
+ markers: &'static str,
+ port_offset: u16,
+ invite_admin: Option<&'static str>,
+) -> anyhow::Result<()> {
dotenv::dotenv().ok();
// at this point this is only a test runner to be used by the nix build.
env::set_var("ROCKET_LOG_LEVEL", "off");
+ // allow multiple runs of the server, with different port offsets.
+ let port = 8000 + port_offset;
+ let mail_port = 2525 + port_offset;
let (tx, rx) = channel();
- let rocket = build().await?.attach(AdHoc::on_liftoff("notify startup", move |rocket| {
- Box::pin(async move {
- // add an invite code as-if generated during startup and move it to an
- // env var, emulating the user looking at the logs and copying the code.
- // invite_only needs this to function.
- if invite {
- let db = rocket.state::<Db>().unwrap();
- let tx = db.begin().await.unwrap();
- let mut code = [0; 32];
- OsRng.fill_bytes(&mut code);
- let code = base64::encode_config(code, URL_SAFE_NO_PAD);
- tx.add_invite_code(&code, Utc::now() + Duration::minutes(5)).await.unwrap();
- tx.commit().await.unwrap();
- env::set_var("INVITE_CODE", code);
- }
+ let rocket = rocket::build();
+ let figment = rocket
+ .figment()
+ .clone()
+ .merge((rocket::Config::PORT, port))
+ .merge(("location", format!("http://localhost:{}", port)))
+ .merge(("mail_port", mail_port));
+ let figment = if let Some(admin) = invite_admin {
+ figment.merge(("invite_only", true)).merge(("invite_admin_address", admin))
+ } else {
+ figment.merge(("invite_only", false))
+ };
+ let rocket = build(rocket.configure(figment)).await?.attach(AdHoc::on_liftoff(
+ "notify startup",
+ move |rocket| {
+ Box::pin(async move {
+ // add an invite code as-if generated during startup and move it to an
+ // env var, emulating the user looking at the logs and copying the code.
+ // invite_only needs this to function.
+ if invite_admin.is_some() {
+ let db = rocket.state::<Db>().unwrap();
+ let tx = db.begin().await.unwrap();
+ let mut code = [0; 32];
+ OsRng.fill_bytes(&mut code);
+ let code = base64::encode_config(code, URL_SAFE_NO_PAD);
+ tx.add_invite_code(&code, Utc::now() + Duration::minutes(5)).await.unwrap();
+ tx.commit().await.unwrap();
+ env::set_var("INVITE_CODE", code);
+ }
- tx.send(()).unwrap();
- })
- }));
+ tx.send(()).unwrap();
+ })
+ },
+ ));
spawn(async move { rocket.launch().await });
let test = spawn(async move {
@@ -49,6 +71,9 @@ async fn run_pytest(markers: &'static str, invite: bool) -> anyhow::Result<()> {
.arg("-vvv")
.arg("-m")
.arg(markers)
+ .env("API_PORT", port.to_string())
+ .env("MAIL_PORT", mail_port.to_string())
+ .kill_on_drop(true)
.spawn()
.expect("failed to spawn");
child.wait().await
@@ -61,13 +86,10 @@ async fn run_pytest(markers: &'static str, invite: bool) -> anyhow::Result<()> {
#[async_test]
async fn open() -> anyhow::Result<()> {
- env::set_var("ROCKET_INVITE_ONLY", "false");
- run_pytest("not invite", false).await
+ run_pytest("not invite", 100, None).await
}
#[async_test]
async fn invite_only() -> anyhow::Result<()> {
- env::set_var("ROCKET_INVITE_ONLY", "true");
- env::set_var("ROCKET_INVITE_ADMIN_ADDRESS", "test.account@test-auth");
- run_pytest("invite", true).await
+ run_pytest("invite", 200, Some("test.account.invite@test-auth")).await
}