server: Initial stab at e2e tests (#534)
Initial end to end testing. generates unique names for user and groups, and all tests run serially
This commit is contained in:
27
server/tests/common/auth.rs
Normal file
27
server/tests/common/auth.rs
Normal file
@@ -0,0 +1,27 @@
|
||||
use crate::common::env;
|
||||
use reqwest::blocking::Client;
|
||||
|
||||
pub fn get_token(client: &Client) -> String {
|
||||
let username = env::admin_dn();
|
||||
let password = env::admin_password();
|
||||
let base_url = env::http_url();
|
||||
let response = client
|
||||
.post(format!("{base_url}/auth/simple/login"))
|
||||
.header(reqwest::header::CONTENT_TYPE, "application/json")
|
||||
.body(
|
||||
serde_json::to_string(&lldap_auth::login::ClientSimpleLoginRequest {
|
||||
username,
|
||||
password,
|
||||
})
|
||||
.expect("Failed to encode the username/password as json to log in"),
|
||||
)
|
||||
.send()
|
||||
.expect("Failed to send auth request")
|
||||
.error_for_status()
|
||||
.expect("Auth attempt failed");
|
||||
serde_json::from_str::<lldap_auth::login::ServerLoginResponse>(
|
||||
&response.text().expect("Failed to get response text"),
|
||||
)
|
||||
.expect("Failed to parse json")
|
||||
.token
|
||||
}
|
||||
37
server/tests/common/env.rs
Normal file
37
server/tests/common/env.rs
Normal file
@@ -0,0 +1,37 @@
|
||||
use std::env::var;
|
||||
|
||||
pub const DB_KEY: &str = "LLDAP_DATABASE_URL";
|
||||
|
||||
pub fn database_url() -> String {
|
||||
let url = var(DB_KEY).ok();
|
||||
url.unwrap_or("sqlite://e2e_test.db?mode=rwc".to_string())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub fn ldap_url() -> String {
|
||||
let port = var("LLDAP_LDAP_PORT").ok();
|
||||
let port = port.unwrap_or("3890".to_string());
|
||||
format!("ldap://localhost:{}", port)
|
||||
}
|
||||
|
||||
pub fn http_url() -> String {
|
||||
let port = var("LLDAP_HTTP_PORT").ok();
|
||||
let port = port.unwrap_or("17170".to_string());
|
||||
format!("http://localhost:{}", port)
|
||||
}
|
||||
|
||||
pub fn admin_dn() -> String {
|
||||
let user = var("LLDAP_LDAP_USER_DN").ok();
|
||||
user.unwrap_or("admin".to_string())
|
||||
}
|
||||
|
||||
pub fn admin_password() -> String {
|
||||
let pass = var("LLDAP_LDAP_USER_PASS").ok();
|
||||
pass.unwrap_or("password".to_string())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub fn base_dn() -> String {
|
||||
let dn = var("LLDAP_LDAP_BASE_DN").ok();
|
||||
dn.unwrap_or("dc=example,dc=com".to_string())
|
||||
}
|
||||
240
server/tests/common/fixture.rs
Normal file
240
server/tests/common/fixture.rs
Normal file
@@ -0,0 +1,240 @@
|
||||
use crate::common::{
|
||||
auth::get_token,
|
||||
env,
|
||||
graphql::{
|
||||
add_user_to_group, create_group, create_user, delete_group_query, delete_user_query, post,
|
||||
AddUserToGroup, CreateGroup, CreateUser, DeleteGroupQuery, DeleteUserQuery,
|
||||
},
|
||||
};
|
||||
use assert_cmd::prelude::*;
|
||||
use nix::{
|
||||
sys::signal::{self, Signal},
|
||||
unistd::Pid,
|
||||
};
|
||||
use reqwest::blocking::{Client, ClientBuilder};
|
||||
use std::collections::{HashMap, HashSet};
|
||||
use std::process::{Child as ChildProcess, Command};
|
||||
use std::{fs::canonicalize, thread, time::Duration};
|
||||
use uuid::Uuid;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct User {
|
||||
pub username: String,
|
||||
pub groups: Vec<String>,
|
||||
}
|
||||
|
||||
impl User {
|
||||
pub fn new(username: &str, groups: Vec<&str>) -> Self {
|
||||
let username = username.to_owned();
|
||||
let groups = groups.iter().map(|username| username.to_string()).collect();
|
||||
Self { username, groups }
|
||||
}
|
||||
}
|
||||
|
||||
pub struct LLDAPFixture {
|
||||
token: String,
|
||||
client: Client,
|
||||
child: ChildProcess,
|
||||
users: HashSet<String>,
|
||||
groups: HashMap<String, i64>,
|
||||
}
|
||||
|
||||
const MAX_HEALTHCHECK_ATTEMPS: u8 = 10;
|
||||
|
||||
impl LLDAPFixture {
|
||||
pub fn new() -> Self {
|
||||
let mut cmd = create_lldap_command();
|
||||
cmd.arg("run");
|
||||
cmd.arg("--verbose");
|
||||
let child = cmd.spawn().expect("Unable to start server");
|
||||
let mut started = false;
|
||||
for _ in 0..MAX_HEALTHCHECK_ATTEMPS {
|
||||
let status = create_lldap_command()
|
||||
.arg("healthcheck")
|
||||
.status()
|
||||
.expect("healthcheck fail");
|
||||
if status.success() {
|
||||
started = true;
|
||||
break;
|
||||
}
|
||||
thread::sleep(Duration::from_millis(1000));
|
||||
}
|
||||
assert!(started);
|
||||
let client = ClientBuilder::new()
|
||||
.connect_timeout(std::time::Duration::from_secs(2))
|
||||
.timeout(std::time::Duration::from_secs(5))
|
||||
.redirect(reqwest::redirect::Policy::none())
|
||||
.build()
|
||||
.expect("failed to make http client");
|
||||
let token = get_token(&client);
|
||||
Self {
|
||||
client,
|
||||
token,
|
||||
child,
|
||||
users: HashSet::new(),
|
||||
groups: HashMap::new(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn load_state(&mut self, state: &Vec<User>) {
|
||||
let mut users: HashSet<String> = HashSet::new();
|
||||
let mut groups: HashSet<String> = HashSet::new();
|
||||
for user in state {
|
||||
users.insert(user.username.clone());
|
||||
groups.extend(user.groups.clone());
|
||||
}
|
||||
for user in &users {
|
||||
self.add_user(user);
|
||||
}
|
||||
for group in &groups {
|
||||
self.add_group(group);
|
||||
}
|
||||
for User { username, groups } in state {
|
||||
for group in groups {
|
||||
self.add_user_to_group(username, group);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn add_user(&mut self, user: &String) {
|
||||
post::<CreateUser>(
|
||||
&self.client,
|
||||
&self.token,
|
||||
create_user::Variables {
|
||||
user: create_user::CreateUserInput {
|
||||
id: user.clone(),
|
||||
email: format!("{}@lldap.test", user),
|
||||
avatar: None,
|
||||
display_name: None,
|
||||
first_name: None,
|
||||
last_name: None,
|
||||
},
|
||||
},
|
||||
)
|
||||
.expect("failed to add user");
|
||||
self.users.insert(user.clone());
|
||||
}
|
||||
|
||||
fn add_group(&mut self, group: &str) {
|
||||
let id = post::<CreateGroup>(
|
||||
&self.client,
|
||||
&self.token,
|
||||
create_group::Variables {
|
||||
name: group.to_owned(),
|
||||
},
|
||||
)
|
||||
.expect("failed to add group")
|
||||
.create_group
|
||||
.id;
|
||||
self.groups.insert(group.to_owned(), id);
|
||||
}
|
||||
|
||||
fn delete_user(&mut self, user: &String) {
|
||||
post::<DeleteUserQuery>(
|
||||
&self.client,
|
||||
&self.token,
|
||||
delete_user_query::Variables { user: user.clone() },
|
||||
)
|
||||
.expect("failed to delete user");
|
||||
self.users.remove(user);
|
||||
}
|
||||
|
||||
fn delete_group(&mut self, group: &String) {
|
||||
let group_id = self.groups.get(group).unwrap();
|
||||
post::<DeleteGroupQuery>(
|
||||
&self.client,
|
||||
&self.token,
|
||||
delete_group_query::Variables {
|
||||
group_id: *group_id,
|
||||
},
|
||||
)
|
||||
.expect("failed to delete group");
|
||||
self.groups.remove(group);
|
||||
}
|
||||
|
||||
fn add_user_to_group(&mut self, user: &str, group: &String) {
|
||||
let group_id = self.groups.get(group).unwrap();
|
||||
post::<AddUserToGroup>(
|
||||
&self.client,
|
||||
&self.token,
|
||||
add_user_to_group::Variables {
|
||||
user: user.to_owned(),
|
||||
group: *group_id,
|
||||
},
|
||||
)
|
||||
.expect("failed to add user to group");
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for LLDAPFixture {
|
||||
fn drop(&mut self) {
|
||||
let users = self.users.clone();
|
||||
for user in users {
|
||||
self.delete_user(&user);
|
||||
}
|
||||
let groups = self.groups.clone();
|
||||
for group in groups.keys() {
|
||||
self.delete_group(group);
|
||||
}
|
||||
let result = signal::kill(
|
||||
Pid::from_raw(self.child.id().try_into().unwrap()),
|
||||
Signal::SIGTERM,
|
||||
);
|
||||
if let Err(err) = result {
|
||||
println!("Failed to send kill signal: {:?}", err);
|
||||
let _ = self
|
||||
.child
|
||||
.kill()
|
||||
.map_err(|err| println!("Failed to kill LLDAP: {:?}", err));
|
||||
return;
|
||||
}
|
||||
|
||||
for _ in 0..10 {
|
||||
let status = self.child.try_wait();
|
||||
if status.is_err() {}
|
||||
match status {
|
||||
Err(e) => {
|
||||
println!(
|
||||
"Failed to get status while waiting for graceful exit: {}",
|
||||
e
|
||||
);
|
||||
break;
|
||||
}
|
||||
Ok(None) => {
|
||||
println!("LLDAP still running, sleeping for 1 second.");
|
||||
}
|
||||
Ok(Some(status)) => {
|
||||
if !status.success() {
|
||||
println!("LLDAP exited with status {}", status)
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
thread::sleep(Duration::from_millis(1000));
|
||||
}
|
||||
println!("LLDAP alive after 10 seconds, forcing exit.");
|
||||
let _ = self
|
||||
.child
|
||||
.kill()
|
||||
.map_err(|err| println!("Failed to kill LLDAP: {:?}", err));
|
||||
}
|
||||
}
|
||||
|
||||
pub fn new_id(prefix: Option<&str>) -> String {
|
||||
let id = Uuid::new_v4();
|
||||
let id = format!("{}-lldap-test", id.to_simple());
|
||||
match prefix {
|
||||
Some(prefix) => format!("{}{}", prefix, id),
|
||||
None => id,
|
||||
}
|
||||
}
|
||||
|
||||
fn create_lldap_command() -> Command {
|
||||
let mut cmd = Command::cargo_bin(env!("CARGO_PKG_NAME")).expect("cargo bin not found");
|
||||
// This gives us the absolute path of the repo base instead of running it in server/
|
||||
let path = canonicalize("..").expect("canonical path");
|
||||
let db_url = env::database_url();
|
||||
cmd.current_dir(path);
|
||||
cmd.env(env::DB_KEY, db_url);
|
||||
cmd
|
||||
}
|
||||
121
server/tests/common/graphql.rs
Normal file
121
server/tests/common/graphql.rs
Normal file
@@ -0,0 +1,121 @@
|
||||
use crate::common::env;
|
||||
use anyhow::{anyhow, Context, Result};
|
||||
use graphql_client::GraphQLQuery;
|
||||
use reqwest::blocking::Client;
|
||||
|
||||
pub type DateTimeUtc = chrono::DateTime<chrono::Utc>;
|
||||
|
||||
#[derive(GraphQLQuery)]
|
||||
#[graphql(
|
||||
schema_path = "../schema.graphql",
|
||||
query_path = "tests/queries/add_user_to_group.graphql",
|
||||
response_derives = "Debug",
|
||||
variables_derives = "Debug,Clone",
|
||||
custom_scalars_module = "crate::common::graphql"
|
||||
)]
|
||||
pub struct AddUserToGroup;
|
||||
|
||||
#[derive(GraphQLQuery)]
|
||||
#[graphql(
|
||||
schema_path = "../schema.graphql",
|
||||
query_path = "tests/queries/create_user.graphql",
|
||||
response_derives = "Debug",
|
||||
variables_derives = "Debug,Clone",
|
||||
custom_scalars_module = "crate::common::graphql"
|
||||
)]
|
||||
pub struct CreateUser;
|
||||
|
||||
#[derive(GraphQLQuery)]
|
||||
#[graphql(
|
||||
schema_path = "../schema.graphql",
|
||||
query_path = "tests/queries/create_group.graphql",
|
||||
response_derives = "Debug",
|
||||
variables_derives = "Debug,Clone",
|
||||
custom_scalars_module = "crate::common::graphql"
|
||||
)]
|
||||
pub struct CreateGroup;
|
||||
|
||||
#[derive(GraphQLQuery)]
|
||||
#[graphql(
|
||||
schema_path = "../schema.graphql",
|
||||
query_path = "tests/queries/list_users.graphql",
|
||||
response_derives = "Debug",
|
||||
custom_scalars_module = "crate::common::graphql"
|
||||
)]
|
||||
pub struct ListUsers;
|
||||
|
||||
#[derive(GraphQLQuery)]
|
||||
#[graphql(
|
||||
schema_path = "../schema.graphql",
|
||||
query_path = "tests/queries/get_user_details.graphql",
|
||||
response_derives = "Debug",
|
||||
variables_derives = "Debug,Clone",
|
||||
custom_scalars_module = "crate::common::graphql"
|
||||
)]
|
||||
pub struct GetUserDetails;
|
||||
|
||||
#[derive(GraphQLQuery)]
|
||||
#[graphql(
|
||||
schema_path = "../schema.graphql",
|
||||
query_path = "tests/queries/list_groups.graphql",
|
||||
response_derives = "Debug",
|
||||
custom_scalars_module = "crate::common::graphql"
|
||||
)]
|
||||
pub struct ListGroups;
|
||||
|
||||
#[derive(GraphQLQuery)]
|
||||
#[graphql(
|
||||
schema_path = "../schema.graphql",
|
||||
query_path = "tests/queries/delete_group.graphql",
|
||||
response_derives = "Debug",
|
||||
custom_scalars_module = "crate::common::graphql"
|
||||
)]
|
||||
pub struct DeleteGroupQuery;
|
||||
|
||||
#[derive(GraphQLQuery)]
|
||||
#[graphql(
|
||||
schema_path = "../schema.graphql",
|
||||
query_path = "tests/queries/delete_user.graphql",
|
||||
response_derives = "Debug",
|
||||
custom_scalars_module = "crate::common::graphql"
|
||||
)]
|
||||
pub struct DeleteUserQuery;
|
||||
|
||||
pub fn post<QueryType>(
|
||||
client: &Client,
|
||||
token: &String,
|
||||
variables: QueryType::Variables,
|
||||
) -> Result<QueryType::ResponseData>
|
||||
where
|
||||
QueryType: GraphQLQuery + 'static,
|
||||
{
|
||||
let unwrap_graphql_response = |graphql_client::Response { data, errors, .. }| {
|
||||
data.ok_or_else(|| {
|
||||
anyhow!(
|
||||
"Errors: [{}]",
|
||||
errors
|
||||
.unwrap_or_default()
|
||||
.iter()
|
||||
.map(ToString::to_string)
|
||||
.collect::<Vec<_>>()
|
||||
.join(", ")
|
||||
)
|
||||
})
|
||||
};
|
||||
let url = env::http_url() + "/api/graphql";
|
||||
let auth_header = format!("Bearer {}", token);
|
||||
client
|
||||
.post(url)
|
||||
.header(reqwest::header::AUTHORIZATION, auth_header)
|
||||
// Request body.
|
||||
.json(&QueryType::build_query(variables))
|
||||
.send()
|
||||
.context("while sending a request to the LLDAP server")?
|
||||
.error_for_status()
|
||||
.context("error from an LLDAP response")?
|
||||
// Parse response as Json.
|
||||
.json::<graphql_client::Response<QueryType::ResponseData>>()
|
||||
.context("while parsing backend response")
|
||||
.and_then(unwrap_graphql_response)
|
||||
.context("GraphQL error from an LLDAP response")
|
||||
}
|
||||
4
server/tests/common/mod.rs
Normal file
4
server/tests/common/mod.rs
Normal file
@@ -0,0 +1,4 @@
|
||||
pub mod auth;
|
||||
pub mod env;
|
||||
pub mod fixture;
|
||||
pub mod graphql;
|
||||
Reference in New Issue
Block a user