Refactor the integration test suite

This commit is contained in:
2022-02-19 14:00:07 +01:00
parent 2c359e7d7a
commit a5c74d7e4c
4 changed files with 5 additions and 10 deletions

15
tests/api/health_check.rs Normal file
View File

@@ -0,0 +1,15 @@
use crate::helpers::app_address;
use rstest::*;
#[rstest]
#[tokio::test]
async fn health_check_works(app_address: &str) {
let response = reqwest::Client::new()
.get(&format!("{}/ping", &app_address))
.send()
.await
.expect("Failed to execute request.");
assert!(response.status().is_success());
assert_eq!(Some(0), response.content_length());
}

39
tests/api/helpers.rs Normal file
View File

@@ -0,0 +1,39 @@
use contextswitch_api::contextswitch::taskwarrior;
use contextswitch_api::observability::{get_subscriber, init_subscriber};
use mktemp::Temp;
use rstest::*;
use std::net::TcpListener;
use tracing::info;
fn setup_tracing() {
info!("Setting up tracing");
let subscriber = get_subscriber("debug".to_string());
init_subscriber(subscriber);
}
fn setup_server() -> String {
info!("Setting up server");
let listener = TcpListener::bind("127.0.0.1:0").expect("Failed to bind random port");
let port = listener.local_addr().unwrap().port();
let server = contextswitch_api::run(listener).expect("Failed to bind address");
let _ = tokio::spawn(server);
format!("http://127.0.0.1:{}", port)
}
fn setup_taskwarrior() -> String {
info!("Setting up TW");
let tmp_dir = Temp::new_dir().unwrap();
let task_data_location = taskwarrior::load_config(tmp_dir.to_str());
tmp_dir.release();
task_data_location
}
#[fixture]
#[once]
pub fn app_address() -> String {
setup_tracing();
setup_taskwarrior();
setup_server()
}

3
tests/api/main.rs Normal file
View File

@@ -0,0 +1,3 @@
mod health_check;
mod helpers;
mod task;

126
tests/api/task.rs Normal file
View File

@@ -0,0 +1,126 @@
use crate::helpers::app_address;
use contextswitch_api::contextswitch;
use contextswitch_types::{Bookmark, ContextswitchData, NewTask, Task, TaskId};
use http::uri::Uri;
use rstest::*;
use uuid::Uuid;
#[rstest]
#[tokio::test]
async fn list_tasks(app_address: &str) {
let task = contextswitch::add_task(vec![
"test",
"list_tasks",
"contextswitch:'{\"bookmarks\":[{\"uri\":\"https://example.com/path?filter=1\"}]}'",
])
.await
.unwrap();
let tasks: Vec<Task> = reqwest::Client::new()
.get(&format!("{}/tasks?filter={}", &app_address, task.id))
.send()
.await
.expect("Failed to execute request")
.json()
.await
.expect("Cannot parse JSON result");
assert_eq!(tasks.len(), 1);
assert_eq!(tasks[0].description, "test list_tasks");
let cs_data = tasks[0].contextswitch.as_ref().unwrap();
assert_eq!(cs_data.bookmarks.len(), 1);
assert_eq!(cs_data.bookmarks[0].content, None);
assert_eq!(
cs_data.bookmarks[0].uri,
"https://example.com/path?filter=1".parse::<Uri>().unwrap()
);
}
#[rstest]
#[tokio::test]
async fn list_tasks_with_unknown_contextswitch_data(app_address: &str) {
let task = contextswitch::add_task(vec![
"test",
"list_tasks_with_unknown_contextswitch_data",
"contextswitch:'{\"unknown\": 1}'",
])
.await
.unwrap();
let tasks: Vec<Task> = reqwest::Client::new()
.get(&format!("{}/tasks?filter={}", &app_address, task.id))
.send()
.await
.expect("Failed to execute request")
.json()
.await
.expect("Cannot parse JSON result");
assert_eq!(tasks.len(), 1);
assert_eq!(
tasks[0].description,
"test list_tasks_with_unknown_contextswitch_data"
);
assert!(tasks[0].contextswitch.is_none());
}
#[rstest]
#[tokio::test]
async fn list_tasks_with_invalid_contextswitch_data(app_address: &str) {
let task = contextswitch::add_task(vec![
"test",
"list_tasks_with_invalid_contextswitch_data",
"contextswitch:'}'",
])
.await
.unwrap();
let tasks: Vec<Task> = reqwest::Client::new()
.get(&format!("{}/tasks?filter={}", &app_address, task.id))
.send()
.await
.expect("Failed to execute request")
.json()
.await
.expect("Cannot parse JSON result");
assert_eq!(tasks.len(), 1);
assert_eq!(
tasks[0].description,
"test list_tasks_with_invalid_contextswitch_data"
);
assert!(tasks[0].contextswitch.is_none());
}
#[rstest]
#[tokio::test]
async fn add_task(app_address: &str) {
let response: serde_json::Value = reqwest::Client::new()
.post(&format!("{}/tasks", &app_address))
.json(&NewTask {
definition:
"test add_task contextswitch:{\"bookmarks\":[{\"uri\":\"https://example.com/path?filter=1\"}]}"
.to_string(),
})
.send()
.await
.expect("Failed to execute request")
.json()
.await
.expect("Cannot parse JSON result");
let new_task_id = TaskId(Uuid::parse_str(response["id"].as_str().unwrap()).unwrap());
let tasks = contextswitch::list_tasks(vec![&new_task_id.to_string()]).unwrap();
assert_eq!(tasks.len(), 1);
assert_eq!(tasks[0].id, new_task_id);
assert_eq!(tasks[0].description, "test add_task");
assert_eq!(
tasks[0].contextswitch.as_ref().unwrap(),
&ContextswitchData {
bookmarks: vec![Bookmark {
uri: "https://example.com/path?filter=1".parse::<Uri>().unwrap(),
content: None
}]
}
);
}