Use cargo workspaces

This commit is contained in:
2022-03-10 22:45:02 +01:00
parent b2bc58f2f6
commit 5958a3ed29
30 changed files with 936 additions and 368 deletions

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());
}

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

@@ -0,0 +1,43 @@
use contextswitch_api::configuration::Settings;
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(settings: &Settings) {
info!("Setting up tracing");
let subscriber = get_subscriber(&settings.application.log_directive);
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(mut settings: Settings) -> String {
info!("Setting up Taskwarrior");
let tmp_dir = Temp::new_dir().unwrap();
settings.taskwarrior.data_location = tmp_dir.to_str().map(String::from);
let task_data_location = taskwarrior::load_config(&settings.taskwarrior);
tmp_dir.release();
task_data_location
}
#[fixture]
#[once]
pub fn app_address() -> String {
let settings = Settings::new_from_file(Some("config/test".to_string()))
.expect("Cannot load test configuration");
setup_tracing(&settings);
setup_taskwarrior(settings);
setup_server()
}

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

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

178
api/tests/api/tasks.rs Normal file
View File

@@ -0,0 +1,178 @@
use crate::helpers::app_address;
use contextswitch::{Bookmark, ContextswitchData, NewTask, Task};
use contextswitch_api::contextswitch as cs;
use http::uri::Uri;
use rstest::*;
mod list_tasks {
use super::*;
#[rstest]
#[tokio::test]
async fn list_tasks(app_address: &str) {
let task = cs::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_cs_data(app_address: &str) {
let task = cs::add_task(vec![
"test",
"list_tasks_with_unknown_cs_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_cs_data");
assert!(tasks[0].contextswitch.is_none());
}
#[rstest]
#[tokio::test]
async fn list_tasks_with_invalid_cs_data(app_address: &str) {
let task = cs::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());
}
}
mod add_task {
use super::*;
#[rstest]
#[tokio::test]
async fn add_task(app_address: &str) {
let task: Task = 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");
assert_eq!(task.description, "test add_task");
assert_eq!(
task.contextswitch.as_ref().unwrap(),
&ContextswitchData {
bookmarks: vec![Bookmark {
uri: "https://example.com/path?filter=1".parse::<Uri>().unwrap(),
content: None
}]
}
);
}
}
mod update_task {
use super::*;
#[rstest]
#[tokio::test]
async fn update_task(app_address: &str) {
let mut task = cs::add_task(vec![
"test",
"update_task",
"contextswitch:'{\"bookmarks\":[{\"uri\":\"https://example.com/path?filter=1\"}]}'",
])
.await
.unwrap();
task.description = "updated task description".to_string();
let cs_data = task.contextswitch.as_mut().unwrap();
cs_data.bookmarks.push(Bookmark {
uri: "https://example.com/path2".parse::<Uri>().unwrap(),
content: None,
});
let updated_task: Task = reqwest::Client::new()
.put(&format!("{}/tasks/{}", &app_address, task.id))
.json(&task)
.send()
.await
.expect("Failed to execute request")
.json()
.await
.expect("Cannot parse JSON result");
assert_eq!(updated_task.description, "updated task description");
assert_eq!(
updated_task.contextswitch.as_ref().unwrap(),
&ContextswitchData {
bookmarks: vec![
Bookmark {
uri: "https://example.com/path?filter=1".parse::<Uri>().unwrap(),
content: None
},
Bookmark {
uri: "https://example.com/path2".parse::<Uri>().unwrap(),
content: None
}
]
}
);
}
// TODO : test incoherent task id
}