Add new task endpoint

This commit is contained in:
2022-01-13 21:47:19 +01:00
parent b359b5123e
commit 46b29c28e4
7 changed files with 109 additions and 31 deletions

View File

@@ -1,27 +1,53 @@
pub mod test_helper;
use contextswitch_api::taskwarrior;
use contextswitch_api::{taskwarrior, TaskDefinition};
use contextswitch_types::Task;
#[tokio::test]
async fn list_tasks() {
let address = test_helper::spawn_app();
let task_data_path = test_helper::setup_tasks();
let client = reqwest::Client::new();
taskwarrior::add(vec!["test1", "contextswitch:'{\"test\": 1}'"]).unwrap();
let task_id =
taskwarrior::add(vec!["test", "list_tasks", "contextswitch:'{\"test\": 1}'"]).unwrap();
let response: reqwest::Response = client
.get(&format!("{}/tasks", &address))
let tasks: Vec<Task> = reqwest::Client::new()
.get(&format!("{}/tasks?filter={}", &address, task_id))
.send()
.await
.expect("Failed to execute request.");
.expect("Failed to execute request")
.json()
.await
.expect("Cannot parse JSON result");
test_helper::clear_tasks(task_data_path);
let text_body = response.text_with_charset("utf-8").await.unwrap();
let tasks: Vec<Task> = serde_json::from_str(&text_body).unwrap();
assert_eq!(tasks.len(), 1);
assert_eq!(tasks[0].description, "test1");
assert_eq!(tasks[0].description, "test list_tasks");
let cs_metadata = tasks[0].contextswitch.as_ref().unwrap();
assert_eq!(cs_metadata.test, 1);
}
#[tokio::test]
async fn add_task() {
let address = test_helper::spawn_app();
println!("add_task address: {}", address);
let response: serde_json::Value = reqwest::Client::new()
.post(&format!("{}/tasks", &address))
.json(&TaskDefinition {
definition: "test add_task contextswitch:{\"test\":1}".to_string(),
})
.send()
.await
.expect("Failed to execute request")
.json()
.await
.expect("Cannot parse JSON result");
let new_task_id = response["id"].as_u64().unwrap();
let tasks = taskwarrior::export(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(),
&"{\"test\":1}".to_string()
);
}

View File

@@ -10,23 +10,31 @@ static TRACING: Lazy<()> = Lazy::new(|| {
init_subscriber(subscriber);
});
pub fn spawn_app() -> String {
Lazy::force(&TRACING);
static SERVER_ADDRESS: Lazy<String> = Lazy::new(|| {
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)
}
});
pub fn setup_tasks() -> String {
static TASK_DATA_LOCATION: Lazy<String> = Lazy::new(|| {
let tmp_dir = Temp::new_dir().unwrap();
let task_data_location = taskwarrior::load_config(tmp_dir.to_str());
tmp_dir.release();
return task_data_location;
task_data_location
});
pub fn spawn_app() -> String {
Lazy::force(&TRACING);
setup_tasks();
Lazy::force(&SERVER_ADDRESS).to_string()
}
pub fn setup_tasks() -> String {
Lazy::force(&TASK_DATA_LOCATION).to_string()
}
pub fn clear_tasks(task_data_location: String) {