Compare commits
5 Commits
eeaebdd2ba
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
fbc6da3e54
|
||
|
|
aa6a90d3f0
|
||
|
|
aafca8b714
|
||
|
|
055088d11e
|
||
|
|
190dad39b6
|
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1 +1,2 @@
|
||||
e2e/target
|
||||
tmp
|
||||
|
||||
5
.gitmodules
vendored
5
.gitmodules
vendored
@@ -3,4 +3,7 @@
|
||||
url = git@git.orionkindel.com:dnim/db
|
||||
[submodule "api"]
|
||||
path = api
|
||||
url = git@git.orionkindel.com:dnim/api
|
||||
url = ../api
|
||||
branch = main
|
||||
[submodule "git@git.orionkindel.com:dnim/api"]
|
||||
url = ../api
|
||||
|
||||
2
api
2
api
Submodule api updated: 568b248f3e...7e89edc30f
2
db
2
db
Submodule db updated: be07aca4c5...aad2e076fe
39
e2e/Cargo.lock
generated
39
e2e/Cargo.lock
generated
@@ -224,6 +224,15 @@ version = "0.1.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "00b0228411908ca8685dba7fc2cdd70ec9990a6e753e89b6ac91a84c40fbaf4b"
|
||||
|
||||
[[package]]
|
||||
name = "form_urlencoded"
|
||||
version = "1.2.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a62bc1cf6f830c2ec14a513a9fb124d0a213a629668a4186f329db21fe045652"
|
||||
dependencies = [
|
||||
"percent-encoding",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "futures-channel"
|
||||
version = "0.3.28"
|
||||
@@ -320,6 +329,16 @@ dependencies = [
|
||||
"digest",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "idna"
|
||||
version = "0.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7d20d6b07bfbc108882d88ed8e37d39636dcc260e15e30c45e6ba089610b917c"
|
||||
dependencies = [
|
||||
"unicode-bidi",
|
||||
"unicode-normalization",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "is-terminal"
|
||||
version = "0.4.9"
|
||||
@@ -945,9 +964,9 @@ checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20"
|
||||
|
||||
[[package]]
|
||||
name = "toad"
|
||||
version = "1.0.0-beta.4"
|
||||
version = "1.0.0-beta.8"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7680b4a4868e2527ee0cb58b3850179f9ad9a44532b13fb9e4487a176091b71a"
|
||||
checksum = "6b4d56ca31b3b83e311136e8e425dc62b8190ed72e2677485431a419493828f2"
|
||||
dependencies = [
|
||||
"embedded-time",
|
||||
"log",
|
||||
@@ -1039,9 +1058,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "toad-msg"
|
||||
version = "1.0.0-beta.2"
|
||||
version = "1.0.0-beta.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "63f5ff0cb4b95ec5eb83db517a2301d6a5a5917d94124140dff2fafa283a41f5"
|
||||
checksum = "219f76dce7b054dc71ff7dff5409b450f821b2bdeccf67d95d3577aa029fe737"
|
||||
dependencies = [
|
||||
"blake2",
|
||||
"tinyvec",
|
||||
@@ -1051,6 +1070,7 @@ dependencies = [
|
||||
"toad-len",
|
||||
"toad-macros",
|
||||
"toad-map",
|
||||
"url",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -1179,6 +1199,17 @@ dependencies = [
|
||||
"tinyvec",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "url"
|
||||
version = "2.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "50bff7831e19200a85b17131d085c25d7811bc4e186efdaf54bbd132994a88cb"
|
||||
dependencies = [
|
||||
"form_urlencoded",
|
||||
"idna",
|
||||
"percent-encoding",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "vcpkg"
|
||||
version = "0.2.15"
|
||||
|
||||
@@ -7,8 +7,8 @@ edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
postgres = {path = "../api/postgres/postgres"}
|
||||
toad = "1.0.0-beta.4"
|
||||
toad-msg = "1.0.0-beta.2"
|
||||
toad = "1.0.0-beta.8"
|
||||
toad-msg = "1.0.0-beta.5"
|
||||
nb = "1.1"
|
||||
simple_logger = "4.2"
|
||||
serde_json = "1"
|
||||
|
||||
159
e2e/src/lib.rs
159
e2e/src/lib.rs
@@ -8,48 +8,51 @@ mod __toad_aliases {
|
||||
|
||||
mod user_signup;
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::fs::OpenOptions;
|
||||
use std::io::{Read, Write};
|
||||
use std::net::SocketAddr;
|
||||
use std::net::{SocketAddr, UdpSocket};
|
||||
use std::ops::DerefMut;
|
||||
use std::process::{Command, Stdio};
|
||||
use std::sync::Once;
|
||||
use std::sync::{Once, Mutex, Barrier, Arc};
|
||||
use std::thread::JoinHandle;
|
||||
use std::time::Duration;
|
||||
|
||||
pub use __toad_aliases::Toad;
|
||||
use toad::net::Addrd;
|
||||
use toad::platform::Platform;
|
||||
use toad::resp::code;
|
||||
use toad_msg::alloc::Message;
|
||||
use toad_msg::{Code, Type, MessageOptions};
|
||||
|
||||
pub fn run<S, F, R>(script: S, init: R, mut fold_lines: F) -> R
|
||||
where S: AsRef<str>,
|
||||
F: FnMut(R, &str) -> R
|
||||
{
|
||||
let temppath = Command::new("mktemp").output().unwrap().stdout;
|
||||
let temppath = String::from_utf8_lossy(&temppath);
|
||||
let temppath = temppath.trim();
|
||||
|
||||
let mut child = Command::new("sh").args(["-c", script.as_ref()])
|
||||
.current_dir("../")
|
||||
.stdout(OpenOptions::new().read(true)
|
||||
.write(true)
|
||||
.open(temppath)
|
||||
.unwrap())
|
||||
.stderr(Stdio::piped())
|
||||
.spawn()
|
||||
.unwrap();
|
||||
let mut child = Command::new("bash").args(["-c", script.as_ref()])
|
||||
.current_dir("../")
|
||||
.stdin(Stdio::piped())
|
||||
.stdout(Stdio::piped())
|
||||
.stderr(Stdio::piped())
|
||||
.spawn()
|
||||
.unwrap();
|
||||
|
||||
let mut r: R = init;
|
||||
let mut line_ix = 0usize;
|
||||
let mut out = child.stdout.take().unwrap();
|
||||
let mut out_str = String::new();
|
||||
let mut seen = 0;
|
||||
|
||||
macro_rules! do_fold {
|
||||
() => {{
|
||||
let out = std::fs::read_to_string(temppath).unwrap();
|
||||
out.read_to_string(&mut out_str).unwrap();
|
||||
|
||||
#[allow(unused_assignments)]
|
||||
while let Some(line) = out.split('\n').nth(line_ix) {
|
||||
line_ix += 1;
|
||||
if !line.trim().is_empty() {
|
||||
r = fold_lines(r, line);
|
||||
std::io::stdout().flush().unwrap();
|
||||
}
|
||||
}
|
||||
r = out_str.split('\n').skip(seen).fold(r, |mut r, line| {
|
||||
if !line.trim().is_empty() {
|
||||
r = fold_lines(r, line);
|
||||
}
|
||||
seen += 1;
|
||||
r
|
||||
});
|
||||
}};
|
||||
}
|
||||
|
||||
@@ -60,8 +63,9 @@ pub fn run<S, F, R>(script: S, init: R, mut fold_lines: F) -> R
|
||||
|
||||
let exit = child.try_wait().unwrap();
|
||||
if exit.map(|e| e.success()) == Some(false) {
|
||||
let mut err = child.stderr.take().unwrap();
|
||||
let mut e = String::new();
|
||||
child.stderr.take().unwrap().read_to_string(&mut e).unwrap();
|
||||
err.read_to_string(&mut e).unwrap();
|
||||
panic!("{}", e);
|
||||
} else if exit.is_some() {
|
||||
do_fold!();
|
||||
@@ -75,45 +79,104 @@ pub fn run<S, F, R>(script: S, init: R, mut fold_lines: F) -> R
|
||||
pub struct Fixture {
|
||||
pub toad: &'static Toad,
|
||||
pub api_addr: no_std_net::SocketAddr,
|
||||
pub api_thread: JoinHandle<()>,
|
||||
pub pg: postgres::Client,
|
||||
}
|
||||
|
||||
impl Drop for Fixture {
|
||||
fn drop(&mut self) {
|
||||
let mut api_thread: JoinHandle<()> = std::thread::spawn(|| ());
|
||||
std::mem::swap(&mut self.api_thread, &mut api_thread);
|
||||
|
||||
let msg = Message::builder(Type::Con, Code::POST).path("debug/shutdown")
|
||||
.build();
|
||||
|
||||
let shutdown = Addrd(msg, self.api_addr);
|
||||
let rep = nb::block!({
|
||||
std::thread::sleep(std::time::Duration::from_millis(500));
|
||||
self.toad.send_req(&shutdown)
|
||||
}).unwrap();
|
||||
if rep.data().code != code::CREATED {
|
||||
eprintln!("{} != 2.01", String::from_iter(rep.data().code.to_human()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn init() -> Fixture {
|
||||
static mut SHARED: Option<Toad> = None;
|
||||
struct ApiThreadPorts(Vec<usize>);
|
||||
|
||||
static mut SHARED: Option<(Toad, Mutex<ApiThreadPorts>)> = None;
|
||||
|
||||
static INIT: Once = Once::new();
|
||||
INIT.call_once(|| {
|
||||
simple_logger::init().unwrap();
|
||||
|
||||
log::info!("[init] sync repos");
|
||||
run("git fetch --recurse-submodules 2>&1; git submodule update --init --recursive 2>&1",
|
||||
(),
|
||||
|_, _| print!("."));
|
||||
println!();
|
||||
simple_logger::init_with_level(log::Level::Info).unwrap();
|
||||
|
||||
log::info!("[init] build db");
|
||||
run("cd db; docker compose up -d 2>&1; ./scripts/build.sh 2>&1",
|
||||
(),
|
||||
|_, _| print!("."));
|
||||
run("cd api; cargo build", (), |_, _| ());
|
||||
|
||||
run("cd db; ./scripts/build.sh 2>&1", (), |_, _| {
|
||||
print!(".");
|
||||
std::io::stdout().flush().unwrap();
|
||||
});
|
||||
println!();
|
||||
|
||||
log::info!("[init] start api");
|
||||
std::thread::spawn(|| {
|
||||
run("cd api; cargo run", (), |_, line| println!("{line}"));
|
||||
});
|
||||
|
||||
let toad = Toad::try_new("127.0.0.1:4445".parse::<SocketAddr>().unwrap(),
|
||||
let toad = Toad::try_new("127.0.0.1:3999".parse::<SocketAddr>().unwrap(),
|
||||
Default::default()).unwrap();
|
||||
|
||||
let available_ports: Vec<usize> =
|
||||
(4000..5000).filter(|p| UdpSocket::bind(format!("127.0.0.1:{p}")).is_ok())
|
||||
.collect();
|
||||
log::info!("[init] {} ports available for api threads",
|
||||
available_ports.len());
|
||||
|
||||
unsafe {
|
||||
SHARED = Some(toad);
|
||||
SHARED = Some((toad,
|
||||
Mutex::new(ApiThreadPorts(available_ports))));
|
||||
}
|
||||
});
|
||||
|
||||
Fixture {
|
||||
toad: unsafe {SHARED.as_ref()}.unwrap(),
|
||||
api_addr: "127.0.0.1:4444".parse().unwrap(),
|
||||
pg: postgres::Client::connect("host=127.0.0.1 port=5432 dbname=dnim user=postgres password=password", postgres::NoTls).unwrap(),
|
||||
while unsafe { SHARED.as_ref() }.is_none() {
|
||||
std::thread::sleep(std::time::Duration::from_millis(500));
|
||||
}
|
||||
|
||||
log::info!("[init] start api");
|
||||
let mut ports = unsafe { SHARED.as_ref() }.unwrap().1.lock().unwrap();
|
||||
let port = ports.deref_mut().0.remove(0);
|
||||
drop(ports);
|
||||
|
||||
let env = HashMap::from([("POSTGRES_HOST", "127.0.0.1".to_string()),
|
||||
("POSTGRES_PORT", "5433".to_string()),
|
||||
("POSTGRES_USER", "postgres".to_string()),
|
||||
("POSTGRES_PASS", "password".to_string()),
|
||||
("ENVIRON", "debug".to_string()),
|
||||
("API_ADDR", format!("127.0.0.1:{port}"))]);
|
||||
|
||||
let env_sh = env.iter()
|
||||
.fold(String::new(), |b, (k, v)| format!("{b} {k}=\"{v}\""));
|
||||
|
||||
let api_thread = std::thread::spawn(move || {
|
||||
run(format!("echo '' > ./tmp/log.{port}; (cd api; {env_sh} target/debug/api 2>&1) 1>>./tmp/log.{port}"), (), |_, _| ());
|
||||
});
|
||||
|
||||
Fixture {
|
||||
toad: &unsafe {SHARED.as_ref()}.unwrap().0,
|
||||
api_addr: env.get("API_ADDR").unwrap().parse().unwrap(),
|
||||
api_thread,
|
||||
pg: postgres::Client::connect("host=127.0.0.1 port=5433 dbname=dnim user=postgres password=password", postgres::NoTls).unwrap(),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn run_works() {
|
||||
let s = run("echo foo; echo bar", String::new(), |b, a| {
|
||||
format!("{b}\n{a}")
|
||||
});
|
||||
assert_eq!(s, "\nfoo\nbar");
|
||||
|
||||
let s = run("nohup sleep 10 >/dev/null & echo $!",
|
||||
String::new(),
|
||||
|b, a| format!("{b}\n{a}"));
|
||||
usize::from_str_radix(&s.trim().split('\n').nth(0).unwrap(), 10).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
@@ -22,47 +22,77 @@ pub fn user_signup() {
|
||||
let msg = Addrd(msg, fx.api_addr);
|
||||
|
||||
let rep = nb::block!({
|
||||
std::thread::sleep(std::time::Duration::from_micros(500));
|
||||
std::thread::sleep(std::time::Duration::from_millis(100));
|
||||
fx.toad.send_req(&msg)
|
||||
}).unwrap();
|
||||
|
||||
assert_eq!(rep.data().code, toad::resp::code::CONTENT);
|
||||
assert_eq!(rep.data().code, toad::resp::code::CREATED);
|
||||
assert_eq!(rep.data().content_format(), Some(ContentFormat::Json));
|
||||
let rep_json =
|
||||
serde_json::from_slice::<serde_json::Value>(rep.data().payload().as_bytes()).unwrap();
|
||||
|
||||
let signup_link = rep_json.as_object()
|
||||
.unwrap()
|
||||
.get("links")
|
||||
.unwrap()
|
||||
.as_object()
|
||||
.unwrap()
|
||||
.get("user_signup")
|
||||
.unwrap()
|
||||
.as_str()
|
||||
.unwrap();
|
||||
let groups = rep_json.as_object()
|
||||
.unwrap()
|
||||
.get("links")
|
||||
.unwrap()
|
||||
.as_object()
|
||||
.unwrap()
|
||||
.get("groups")
|
||||
.unwrap()
|
||||
.as_str()
|
||||
.unwrap();
|
||||
|
||||
let login = rep_json.as_object()
|
||||
.unwrap()
|
||||
.get("links")
|
||||
.unwrap()
|
||||
.as_object()
|
||||
.unwrap()
|
||||
.get("login")
|
||||
.unwrap()
|
||||
.as_str()
|
||||
.unwrap();
|
||||
|
||||
let login =
|
||||
Message::builder(Type::Con, Code::POST).path(login)
|
||||
.accept(ContentFormat::Json)
|
||||
.content_format(ContentFormat::Json)
|
||||
.payload(serde_json::to_vec(&serde_json::json!({
|
||||
"tag": "foo",
|
||||
"password": "bingus"
|
||||
})).unwrap())
|
||||
.build();
|
||||
let login = Addrd(login, fx.api_addr);
|
||||
let rep = nb::block!({
|
||||
std::thread::sleep(std::time::Duration::from_millis(100));
|
||||
fx.toad.send_req(&login)
|
||||
}).unwrap();
|
||||
assert_eq!(rep.data().code, toad::resp::code::CREATED);
|
||||
assert_eq!(rep.data().content_format(), Some(ContentFormat::Json));
|
||||
let rep_json =
|
||||
serde_json::from_slice::<serde_json::Value>(rep.data().payload().as_bytes()).unwrap();
|
||||
|
||||
let session = rep_json.as_object()
|
||||
.unwrap()
|
||||
.get("session_id")
|
||||
.get("session")
|
||||
.unwrap()
|
||||
.as_str()
|
||||
.unwrap();
|
||||
|
||||
let get_signup =
|
||||
Message::builder(Type::Con, Code::GET).path(signup_link)
|
||||
let get_groups =
|
||||
Message::builder(Type::Con, Code::GET).path(groups)
|
||||
.accept(ContentFormat::Json)
|
||||
.content_format(ContentFormat::Json)
|
||||
.payload(serde_json::to_vec(&serde_json::json!({
|
||||
session: session
|
||||
"session": session
|
||||
})).unwrap())
|
||||
.build();
|
||||
|
||||
let get_signup = Addrd(get_signup, fx.api_addr);
|
||||
let get_groups = Addrd(get_groups, fx.api_addr);
|
||||
|
||||
let rep = nb::block!({
|
||||
std::thread::sleep(std::time::Duration::from_micros(500));
|
||||
fx.toad.send_req(&get_signup)
|
||||
std::thread::sleep(std::time::Duration::from_millis(100));
|
||||
fx.toad.send_req(&get_groups)
|
||||
}).unwrap();
|
||||
|
||||
assert_eq!(rep.data().code, toad::resp::code::CONTENT);
|
||||
@@ -70,11 +100,15 @@ pub fn user_signup() {
|
||||
let rep_json =
|
||||
serde_json::from_slice::<serde_json::Value>(rep.data().payload().as_bytes()).unwrap();
|
||||
|
||||
let status = rep_json.as_object()
|
||||
.unwrap()
|
||||
.get("status")
|
||||
.unwrap()
|
||||
.as_str()
|
||||
.unwrap();
|
||||
assert_eq!(status, "email_verification_pending");
|
||||
let groups =
|
||||
rep_json.as_array()
|
||||
.unwrap()
|
||||
.iter()
|
||||
.map(|g| g.as_object().unwrap().get("name").unwrap().as_str().unwrap()).collect::<Vec<_>>();
|
||||
|
||||
if !groups.contains(&"unverified_email") {
|
||||
panic!("expected {groups:?} to contain 'unverified_email'");
|
||||
}
|
||||
|
||||
drop(fx);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user