From 07c8a682dcd607956fddcb9f4aedc894d6b1f802 Mon Sep 17 00:00:00 2001 From: Manos Pitsidianakis Date: Sun, 1 Sep 2024 20:21:36 +0300 Subject: [PATCH] cli: add tests for message filter settings interface Signed-off-by: Manos Pitsidianakis --- mailpot-cli/tests/basic_interfaces.rs | 37 ++--- mailpot-cli/tests/message_filter_settings.rs | 155 +++++++++++++++++++ mailpot-cli/tests/out_queue_flush.rs | 37 ++--- mailpot-web/src/templates/css.html | 6 +- mailpot/build/make_migrations.rs | 8 +- mailpot/build/mod.rs | 2 +- mailpot/tests/migrations.rs | 2 +- mailpot/tests/settings_json.rs | 90 +++++------ 8 files changed, 244 insertions(+), 93 deletions(-) create mode 100644 mailpot-cli/tests/message_filter_settings.rs diff --git a/mailpot-cli/tests/basic_interfaces.rs b/mailpot-cli/tests/basic_interfaces.rs index 8e8a438..a91030c 100644 --- a/mailpot-cli/tests/basic_interfaces.rs +++ b/mailpot-cli/tests/basic_interfaces.rs @@ -1,23 +1,20 @@ -/* - * meli - email module - * - * Copyright 2019 Manos Pitsidianakis - * - * This file is part of meli. - * - * meli is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * meli is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with meli. If not, see . - */ +// +// This file is part of mailpot +// +// Copyright 2020 - Manos Pitsidianakis +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as +// published by the Free Software Foundation, either version 3 of the +// License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . #![deny(dead_code)] diff --git a/mailpot-cli/tests/message_filter_settings.rs b/mailpot-cli/tests/message_filter_settings.rs new file mode 100644 index 0000000..934824b --- /dev/null +++ b/mailpot-cli/tests/message_filter_settings.rs @@ -0,0 +1,155 @@ +// +// This file is part of mailpot +// +// Copyright 2020 - Manos Pitsidianakis +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as +// published by the Free Software Foundation, either version 3 of the +// License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + +use assert_cmd::assert::OutputAssertExt; +use mailpot::{Configuration, Connection, SendMail}; +use mailpot_tests::*; +use predicates::prelude::*; +use tempfile::TempDir; + +#[test] +fn test_message_filter_settings_command() { + use assert_cmd::Command; + + let tmp_dir = TempDir::new().unwrap(); + + let conf_path = tmp_dir.path().join("conf.toml"); + let db_path = tmp_dir.path().join("mpot.db"); + std::fs::copy("../mailpot-tests/for_testing.db", &db_path).unwrap(); + let mut perms = std::fs::metadata(&db_path).unwrap().permissions(); + #[allow(clippy::permissions_set_readonly_false)] + perms.set_readonly(false); + std::fs::set_permissions(&db_path, perms).unwrap(); + + let config = Configuration { + send_mail: SendMail::ShellCommand("/usr/bin/false".to_string()), + db_path, + data_path: tmp_dir.path().to_path_buf(), + administrators: vec![], + }; + let db = Connection::open_or_create_db(config.clone()) + .unwrap() + .trusted(); + let list = db.lists().unwrap().remove(0); + + let config_str = config.to_toml(); + + std::fs::write(&conf_path, config_str.as_bytes()).unwrap(); + + println!("Test print-message-filter-settings --show-available"); + let mut cmd = Command::cargo_bin("mpot").unwrap(); + let output = cmd + .arg("-vv") + .arg("-c") + .arg(&conf_path) + .arg("list") + .arg(&list.id) + .arg("print-message-filter-settings") + .arg("--show-available") + .output() + .unwrap() + .assert(); + output.code(0).stderr(predicates::str::is_empty()).stdout( + predicate::eq("AddSubjectTagPrefixSettings\nArchivedAtLinkSettings\nMimeRejectSettings") + .trim() + .normalize(), + ); + + println!("Testing that inserting settings works…"); + let mut cmd = Command::cargo_bin("mpot").unwrap(); + let output = cmd + .arg("-vv") + .arg("-c") + .arg(&conf_path) + .arg("list") + .arg(&list.id) + .arg("set-message-filter-setting") + .arg("--name") + .arg("ArchivedAtLinkSettings") + .arg("--value") + .arg( + serde_json::to_string_pretty(&serde_json::json!({ + "template": "https://www.example.com/{{msg_id}}.html", + "preserve_carets": false + })) + .unwrap(), + ) + .output() + .unwrap() + .assert(); + output.code(0).stderr(predicates::str::is_empty()).stdout( + predicate::eq("Successfully updated ArchivedAtLinkSettings value for list foobar chat.") + .trim() + .normalize(), + ); + + println!("Test print-message-filter-settings returns values"); + let mut cmd = Command::cargo_bin("mpot").unwrap(); + let output = cmd + .arg("-vv") + .arg("-c") + .arg(&conf_path) + .arg("list") + .arg(&list.id) + .arg("print-message-filter-settings") + .output() + .unwrap() + .assert(); + output.code(0).stderr(predicates::str::is_empty()).stdout( + predicate::eq("ArchivedAtLinkSettings: {\"preserve_carets\":false,\"template\":\"https://www.example.com/{{msg_id}}.html\"}") + .trim() + .normalize(), + ); + + println!("Test print-message-filter-settings returns filtered values"); + let mut cmd = Command::cargo_bin("mpot").unwrap(); + let output = cmd + .arg("-vv") + .arg("-c") + .arg(&conf_path) + .arg("list") + .arg(&list.id) + .arg("print-message-filter-settings") + .arg("--filter") + .arg("archived") + .output() + .unwrap() + .assert(); + output.code(0).stderr(predicates::str::is_empty()).stdout( + predicate::eq("ArchivedAtLinkSettings: {\"preserve_carets\":false,\"template\":\"https://www.example.com/{{msg_id}}.html\"}") + .trim() + .normalize(), + ); + let mut cmd = Command::cargo_bin("mpot").unwrap(); + let output = cmd + .arg("-vv") + .arg("-c") + .arg(&conf_path) + .arg("list") + .arg(&list.id) + .arg("print-message-filter-settings") + .arg("--filter") + .arg("aadfa") + .output() + .unwrap() + .assert(); + output + .code(0) + .stderr(predicates::str::is_empty()) + .stdout(predicate::eq("").trim().normalize()); +} diff --git a/mailpot-cli/tests/out_queue_flush.rs b/mailpot-cli/tests/out_queue_flush.rs index 5eb62b4..9daba6a 100644 --- a/mailpot-cli/tests/out_queue_flush.rs +++ b/mailpot-cli/tests/out_queue_flush.rs @@ -1,23 +1,20 @@ -/* - * meli - email module - * - * Copyright 2019 Manos Pitsidianakis - * - * This file is part of meli. - * - * meli is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * meli is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with meli. If not, see . - */ +// +// This file is part of mailpot +// +// Copyright 2020 - Manos Pitsidianakis +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as +// published by the Free Software Foundation, either version 3 of the +// License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . use assert_cmd::assert::OutputAssertExt; use mailpot::{ diff --git a/mailpot-web/src/templates/css.html b/mailpot-web/src/templates/css.html index 9688990..1bebbef 100644 --- a/mailpot-web/src/templates/css.html +++ b/mailpot-web/src/templates/css.html @@ -709,9 +709,9 @@ border-top: none; } div.post-reply-link { - padding-inline-start: 1em; - padding-block-end: 1em; - display: inline-block; + padding-inline-start: 1em; + padding-block-end: 1em; + display: inline-block; } td.message-id, span.message-id{ diff --git a/mailpot/build/make_migrations.rs b/mailpot/build/make_migrations.rs index 091eff6..0c20cd0 100644 --- a/mailpot/build/make_migrations.rs +++ b/mailpot/build/make_migrations.rs @@ -25,17 +25,19 @@ /// the schema file. /// /// Returns the current `user_version` PRAGMA value. -pub fn make_migrations, O: AsRef>( +pub fn make_migrations, O: AsRef<::std::path::Path>>( migrations_path: M, output_file: O, schema_file: &mut Vec, ) -> i32 { + use std::io::Write; + let migrations_folder_path = migrations_path.as_ref(); let output_file_path = output_file.as_ref(); let mut paths = vec![]; let mut undo_paths = vec![]; - for entry in read_dir(migrations_folder_path).unwrap() { + for entry in ::std::fs::read_dir(migrations_folder_path).unwrap() { let entry = entry.unwrap(); let path = entry.path(); if path.is_dir() || path.extension().map(|os| os.to_str().unwrap()) != Some("sql") { @@ -56,7 +58,7 @@ pub fn make_migrations, O: AsRef>( paths.sort(); undo_paths.sort(); - let mut migr_rs = OpenOptions::new() + let mut migr_rs = ::std::fs::OpenOptions::new() .write(true) .create(true) .truncate(true) diff --git a/mailpot/build/mod.rs b/mailpot/build/mod.rs index 98315e5..9f844e6 100644 --- a/mailpot/build/mod.rs +++ b/mailpot/build/mod.rs @@ -94,7 +94,7 @@ fn main() { let mut file = std::fs::File::create("./src/schema.sql").unwrap(); file.write_all(&output.stdout).unwrap(); file.write_all( - &format!("\n\n-- Set current schema version.\n\nPRAGMA user_version = {user_version};\n") + format!("\n\n-- Set current schema version.\n\nPRAGMA user_version = {user_version};\n") .as_bytes(), ) .unwrap(); diff --git a/mailpot/tests/migrations.rs b/mailpot/tests/migrations.rs index faeb476..5b1c621 100644 --- a/mailpot/tests/migrations.rs +++ b/mailpot/tests/migrations.rs @@ -17,7 +17,7 @@ * along with this program. If not, see . */ -use std::fs::{File, OpenOptions}; +use std::{fs::File, io::Write}; use mailpot::{Configuration, Connection, SendMail}; use mailpot_tests::init_stderr_logging; diff --git a/mailpot/tests/settings_json.rs b/mailpot/tests/settings_json.rs index 82d459d..e144f1e 100644 --- a/mailpot/tests/settings_json.rs +++ b/mailpot/tests/settings_json.rs @@ -24,7 +24,51 @@ use serde_json::{json, Value}; use tempfile::TempDir; #[test] -fn test_settings_json() { +fn test_settings_json_schemas_are_valid() { + init_stderr_logging(); + let tmp_dir = TempDir::new().unwrap(); + + let db_path = tmp_dir.path().join("mpot.db"); + std::fs::copy("../mailpot-tests/for_testing.db", &db_path).unwrap(); + let mut perms = std::fs::metadata(&db_path).unwrap().permissions(); + #[allow(clippy::permissions_set_readonly_false)] + perms.set_readonly(false); + std::fs::set_permissions(&db_path, perms).unwrap(); + + let config = Configuration { + send_mail: SendMail::ShellCommand("/usr/bin/false".to_string()), + db_path, + data_path: tmp_dir.path().to_path_buf(), + administrators: vec![], + }; + let db = Connection::open_or_create_db(config).unwrap().trusted(); + + let schemas: Vec = { + let mut stmt = db + .connection + .prepare("SELECT value FROM list_settings_json;") + .unwrap(); + let iter = stmt + .query_map([], |row| { + let value: String = row.get("value")?; + Ok(value) + }) + .unwrap(); + let mut ret = vec![]; + for item in iter { + ret.push(item.unwrap()); + } + ret + }; + println!("Testing that schemas are valid…"); + for schema in schemas { + let schema: Value = serde_json::from_str(&schema).unwrap(); + let _compiled = JSONSchema::compile(&schema).expect("A valid schema"); + } +} + +#[test] +fn test_settings_json_triggers() { init_stderr_logging(); let tmp_dir = TempDir::new().unwrap(); @@ -177,47 +221,3 @@ fn test_settings_json() { assert!(is_valid); assert!(new_last_modified != last_modified); } - -#[test] -fn test_settings_json_schemas() { - init_stderr_logging(); - let tmp_dir = TempDir::new().unwrap(); - - let db_path = tmp_dir.path().join("mpot.db"); - std::fs::copy("../mailpot-tests/for_testing.db", &db_path).unwrap(); - let mut perms = std::fs::metadata(&db_path).unwrap().permissions(); - #[allow(clippy::permissions_set_readonly_false)] - perms.set_readonly(false); - std::fs::set_permissions(&db_path, perms).unwrap(); - - let config = Configuration { - send_mail: SendMail::ShellCommand("/usr/bin/false".to_string()), - db_path, - data_path: tmp_dir.path().to_path_buf(), - administrators: vec![], - }; - let db = Connection::open_or_create_db(config).unwrap().trusted(); - - let schemas: Vec = { - let mut stmt = db - .connection - .prepare("SELECT value FROM list_settings_json;") - .unwrap(); - let iter = stmt - .query_map([], |row| { - let value: String = row.get("value")?; - Ok(value) - }) - .unwrap(); - let mut ret = vec![]; - for item in iter { - ret.push(item.unwrap()); - } - ret - }; - println!("Testing that schemas are valid…"); - for schema in schemas { - let schema: Value = serde_json::from_str(&schema).unwrap(); - let _compiled = JSONSchema::compile(&schema).expect("A valid schema"); - } -}