Initial commit

This commit is contained in:
2021-12-23 10:10:59 +00:00
commit d0693b71ec
11 changed files with 2908 additions and 0 deletions

16
tests/health_check.rs Normal file
View File

@@ -0,0 +1,16 @@
pub mod test_helper;
#[actix_rt::test]
async fn health_check_works() {
let address = test_helper::spawn_app();
let client = reqwest::Client::new();
let response = client
.get(&format!("{}/ping", &address))
.send()
.await
.expect("Failed to execute request.");
assert!(response.status().is_success());
assert_eq!(Some(0), response.content_length());
}

27
tests/task.rs Normal file
View File

@@ -0,0 +1,27 @@
pub mod test_helper;
use contextswitch::taskwarrior;
use contextswitch::Task;
#[actix_rt::test]
async fn list_tasks() {
let task_data_path = test_helper::setup_tasks();
let address = test_helper::spawn_app();
let client = reqwest::Client::new();
taskwarrior::add(vec!["test1", "contextswitch:'{\"test\": 1}'"]).unwrap();
let response = client
.get(&format!("{}/tasks?filter=ls", &address))
.send()
.await
.expect("Failed to execute request.");
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");
let cs_metadata = tasks[0].contextswitch.as_ref().unwrap();
assert_eq!(cs_metadata.test, 1);
}

45
tests/test_helper.rs Normal file
View File

@@ -0,0 +1,45 @@
use configparser::ini::Ini;
use mktemp::Temp;
use std::env;
use std::fs;
use std::net::TcpListener;
use std::path::PathBuf;
pub fn spawn_app() -> String {
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::run(listener).expect("Failed to bind address");
let _ = tokio::spawn(server);
format!("http://127.0.0.1:{}", port)
}
pub fn setup_tasks() -> PathBuf {
let mut config = Ini::new();
config.setstr("default", "uda.contextswitch.type", Some("string"));
config.setstr(
"default",
"uda.contextswitch.label",
Some("Context Switch metadata"),
);
config.setstr("default", "uda.contextswitch.default", Some("{}"));
let tmp_dir = Temp::new_dir().unwrap();
let task_data_path = tmp_dir.to_path_buf();
config.setstr(
"default",
"data.location",
Some(task_data_path.to_str().unwrap()),
);
let taskrc_path = task_data_path.join(".taskrc");
config.write(taskrc_path.as_path()).unwrap();
env::set_var("TASKRC", taskrc_path.to_str().unwrap());
tmp_dir.release();
return task_data_path;
}
pub fn clear_tasks(task_data_path: PathBuf) {
fs::remove_dir_all(task_data_path).unwrap();
}