Skip to content

Commit

Permalink
Partially complete tests for relying on the client configuration file…
Browse files Browse the repository at this point in the history
… for URLs and tokens
  • Loading branch information
marcua committed Jan 7, 2024
1 parent 74e8b7d commit c269279
Showing 1 changed file with 52 additions and 36 deletions.
88 changes: 52 additions & 36 deletions tests/e2e.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,11 @@ impl Drop for SmtpServer {
}

fn create_database(
server_url: &str,
config: &str,
api_key: &str,
result: &str,
) -> Result<(), Box<dyn std::error::Error>> {
let cmd = ayb_assert_cmd!("client", "--url", server_url, "create_database", "e2e-first/test.sqlite", "sqlite"; {
let cmd = ayb_assert_cmd!("client", "--config", config, "create_database", "e2e-first/test.sqlite", "sqlite"; {
"AYB_API_TOKEN" => api_key,
});

Expand All @@ -94,13 +94,13 @@ fn create_database(
}

fn query(
server_url: &str,
config: &str,
api_key: &str,
query: &str,
format: &str,
result: &str,
) -> Result<(), Box<dyn std::error::Error>> {
let cmd = ayb_assert_cmd!("client", "--url", server_url, "query", "e2e-first/test.sqlite", "--format", format, query; {
let cmd = ayb_assert_cmd!("client", "--config", config, "query", "e2e-first/test.sqlite", "--format", format, query; {
"AYB_API_TOKEN" => api_key,
});

Expand All @@ -109,12 +109,14 @@ fn query(
}

fn register(
config: &str,
server_url: &str,
slug: &str,
email: &str,
result: &str,
) -> Result<(), Box<dyn std::error::Error>> {
let cmd = ayb_assert_cmd!("client", "register", slug, email; {
"AYB_CLIENT_CONFIG_FILE" => config,
"AYB_SERVER_URL" => server_url,
});

Expand All @@ -123,13 +125,13 @@ fn register(
}

fn list_databases(
server_url: &str,
config: &str,
api_key: &str,
entity: &str,
format: &str,
result: &str,
) -> Result<(), Box<dyn std::error::Error>> {
let cmd = ayb_assert_cmd!("client", "--url", server_url, "list", entity, "--format", format; {
let cmd = ayb_assert_cmd!("client", "--config", config, "list", entity, "--format", format; {
"AYB_API_TOKEN" => api_key,
});

Expand All @@ -138,13 +140,13 @@ fn list_databases(
}

fn profile(
server_url: &str,
config: &str,
api_key: &str,
entity: &str,
format: &str,
result: &str,
) -> Result<(), Box<dyn std::error::Error>> {
let cmd = ayb_assert_cmd!("client", "--url", server_url, "profile", entity, "--format", format; {
let cmd = ayb_assert_cmd!("client", "--config", config, "profile", entity, "--format", format; {
"AYB_API_TOKEN" => api_key,
});

Expand All @@ -153,7 +155,7 @@ fn profile(
}

fn update_profile(
server_url: &str,
config: &str,
api_key: &str,
entity: &str,
display_name: Option<&str>,
Expand All @@ -164,7 +166,7 @@ fn update_profile(
result: &str,
) -> Result<(), Box<dyn std::error::Error>> {
let mut cmd = Command::cargo_bin("ayb")?;
cmd.args(["client", "--url", server_url, "update_profile", entity])
cmd.args(["client", "--config", config, "update_profile", entity])
.env("AYB_API_TOKEN", api_key);

if let Some(display_name) = display_name {
Expand Down Expand Up @@ -236,6 +238,7 @@ fn client_server_integration(
server_url: &str,
smtp_port: u16,
) -> Result<(), Box<dyn std::error::Error>> {
let config_path = format!("tests/ayb_data_{}/ayb.json", db_type);
let _cleanup = Cleanup;

Command::new(format!("tests/reset_db_{}.sh", db_type))
Expand All @@ -253,16 +256,27 @@ fn client_server_integration(

let first_entity_0 = "e2e-first";

// Before running commands, we have no configuration file
assert_eq!(
fs::read_to_string(&config_path).unwrap_err().kind(),
std::io::ErrorKind::NotFound
);

// Register an entity.
register(
&config_path,
server_url,
first_entity_0,
"e2e@example.org",
"Check your email to finish registering e2e-first",
)?;

// The configuration file should register the server URL.
assert_eq!(fs::read_to_string(&config_path).unwrap(), "{}");

// Register the same entity with the same email address.
register(
&config_path,
server_url,
first_entity_0,
"e2e@example.org",
Expand All @@ -272,6 +286,7 @@ fn client_server_integration(
// Can start to register an entity twice with different email
// addresses as long as you don't complete the process.
register(
&config_path,
server_url,
first_entity_0,
"e2e-another@example.org",
Expand All @@ -282,6 +297,7 @@ fn client_server_integration(

// Start the registration process for a second user (e2e-second)
register(
&config_path,
server_url,
second_entity_0,
"e2e-another@example.org",
Expand All @@ -303,7 +319,7 @@ fn client_server_integration(

// Using a bad token (appending a letter) doesn't work.
let cmd = ayb_assert_cmd!("client", "confirm", &format!("{}a", first_token0); {
"AYB_SERVER_URL" => server_url,
"AYB_CLIENT_CONFIG_FILE" => config_path.clone(),
});
cmd.stdout("Error: Invalid or expired token\n");

Expand All @@ -314,31 +330,31 @@ fn client_server_integration(
// same account, won't work now that there's already a confirmed
// email address on the account..
let cmd = ayb_assert_cmd!("client", "confirm", &first_token0; {
"AYB_SERVER_URL" => server_url,
"AYB_CLIENT_CONFIG_FILE" => config_path.clone(),
});
let first_api_key0 = utils::extract_api_key(cmd.get_output())?;

let cmd = ayb_assert_cmd!("client", "confirm", &first_token1; {
"AYB_SERVER_URL" => server_url,
"AYB_CLIENT_CONFIG_FILE" => config_path.clone(),
});
let first_api_key1 = utils::extract_api_key(cmd.get_output())?;

let cmd = ayb_assert_cmd!("client", "confirm", &first_token2; {
"AYB_SERVER_URL" => server_url,
"AYB_CLIENT_CONFIG_FILE" => config_path.clone(),
});
cmd.stdout("Error: e2e-first has already been registered\n");

// And for the second account, we can still confirm using the only
// authentication token we've requested so far.
let cmd = ayb_assert_cmd!("client", "confirm", &second_token0; {
"AYB_SERVER_URL" => server_url,
"AYB_CLIENT_CONFIG_FILE" => config_path.clone(),
});
let second_api_key0 = utils::extract_api_key(cmd.get_output())?;

// Logging in as the user emails the first email address, which
// can confirm using the token it received.
let cmd = ayb_assert_cmd!("client", "log_in", "e2e-first"; {
"AYB_SERVER_URL" => server_url,
"AYB_CLIENT_CONFIG_FILE" => config_path.clone(),
});

cmd.stdout("Check your email to finish logging in e2e-first\n");
Expand All @@ -348,7 +364,7 @@ fn client_server_integration(
let login_token = utils::extract_token(&entries[2])?;

let cmd = ayb_assert_cmd!("client", "confirm", &login_token; {
"AYB_SERVER_URL" => server_url,
"AYB_CLIENT_CONFIG_FILE" => config_path.clone(),
});
let first_api_key2 = utils::extract_api_key(cmd.get_output())?;

Expand All @@ -359,35 +375,35 @@ fn client_server_integration(

// Can't create database on e2e-first with e2e-second's token.
create_database(
server_url,
&config_path,
&second_api_key0,
"Error: Authenticated entity e2e-second can not create a database for entity e2e-first",
)?;

// Can't create database on e2e-first with invalid token.
create_database(
server_url,
&config_path,
&format!("{}bad", first_api_key0),
"Error: Invalid API token",
)?;

// Create a database with the appropriate user/key pair.
create_database(
server_url,
&config_path,
&first_api_key0,
"Successfully created e2e-first/test.sqlite",
)?;

// Can't create a database twice.
create_database(
server_url,
&config_path,
&first_api_key0,
"Error: Database already exists",
)?;

// Can't query database with second account's API key
query(
server_url,
&config_path,
&second_api_key0,
"CREATE TABLE test_table(fname varchar, lname varchar);",
"table",
Expand All @@ -396,7 +412,7 @@ fn client_server_integration(

// Can't query database with bad API key.
query(
server_url,
&config_path,
&format!("{}bad", first_api_key0),
"CREATE TABLE test_table(fname varchar, lname varchar);",
"table",
Expand All @@ -406,34 +422,34 @@ fn client_server_integration(
// Populate and query database. Alternate through the three API
// keys for the first account to ensure they all work.
query(
server_url,
&config_path,
&first_api_key0,
"CREATE TABLE test_table(fname varchar, lname varchar);",
"table",
"\nRows: 0",
)?;
query(
server_url,
&config_path,
&first_api_key1,
"INSERT INTO test_table (fname, lname) VALUES (\"the first\", \"the last\");",
"table",
"\nRows: 0",
)?;
query(
server_url,
&config_path,
&first_api_key2,
"INSERT INTO test_table (fname, lname) VALUES (\"the first2\", \"the last2\");",
"table",
"\nRows: 0",
)?;
query(
server_url,
&config_path,
&first_api_key0,
"SELECT * FROM test_table;",
"table",
" fname | lname \n------------+-----------\n the first | the last \n the first2 | the last2 \n\nRows: 2")?;
query(
server_url,
&config_path,
&first_api_key0,
"SELECT * FROM test_table;",
"csv",
Expand All @@ -442,7 +458,7 @@ fn client_server_integration(

// List databases from first account using its API key
list_databases(
server_url,
&config_path,
&first_api_key0,
first_entity_0,
"csv",
Expand All @@ -451,15 +467,15 @@ fn client_server_integration(

// List databases from first account using the API key of the second account
list_databases(
server_url,
&config_path,
&second_api_key0,
first_entity_0,
"csv",
&format!("No queryable databases owned by {}", first_entity_0),
)?;

update_profile(
server_url,
&config_path,
&first_api_key0,
first_entity_0,
Some("Entity 0"),
Expand All @@ -471,23 +487,23 @@ fn client_server_integration(
)?;

profile(
server_url,
&config_path,
&first_api_key0,
first_entity_0,
"csv",
"Display name,Description,Organization,Location,Links\nEntity 0,Entity 0 description,null,null,"
)?;

profile(
server_url,
&config_path,
&second_api_key0,
first_entity_0,
"csv",
"Display name,Description,Organization,Location,Links\nEntity 0,Entity 0 description,null,null,"
)?;

update_profile(
server_url,
&config_path,
&first_api_key0,
first_entity_0,
None,
Expand All @@ -499,15 +515,15 @@ fn client_server_integration(
)?;

profile(
server_url,
&config_path,
&first_api_key0,
first_entity_0,
"csv",
"Display name,Description,Organization,Location,Links\nEntity 0,Entity 0 NEW description,Entity 0 organization,null,\"http://ayb.host/,http://ayb2.host\""
)?;

profile(
server_url,
&config_path,
&second_api_key0,
first_entity_0,
"csv",
Expand Down

0 comments on commit c269279

Please sign in to comment.