From ff73fec46aa471a0701136712c0d11596936ec2e Mon Sep 17 00:00:00 2001 From: Raf Ven Date: Thu, 2 Jan 2025 11:13:36 +0100 Subject: [PATCH] [IMP] mail_environment: Keep existing settings at install of module --- mail_environment/__init__.py | 1 + mail_environment/__manifest__.py | 2 + mail_environment/hooks.py | 75 +++++++++++++++++++ mail_environment/tests/__init__.py | 1 + .../tests/test_existing_mail_server_config.py | 50 +++++++++++++ 5 files changed, 129 insertions(+) create mode 100644 mail_environment/hooks.py create mode 100644 mail_environment/tests/test_existing_mail_server_config.py diff --git a/mail_environment/__init__.py b/mail_environment/__init__.py index 0650744f6..f69c7729c 100644 --- a/mail_environment/__init__.py +++ b/mail_environment/__init__.py @@ -1 +1,2 @@ from . import models +from .hooks import post_init_hook, pre_init_hook diff --git a/mail_environment/__manifest__.py b/mail_environment/__manifest__.py index 2ef4c4396..784f522f5 100644 --- a/mail_environment/__manifest__.py +++ b/mail_environment/__manifest__.py @@ -10,4 +10,6 @@ "license": "AGPL-3", "website": "https://github.com/OCA/server-env", "depends": ["mail", "server_environment"], + "pre_init_hook": "pre_init_hook", + "post_init_hook": "post_init_hook", } diff --git a/mail_environment/hooks.py b/mail_environment/hooks.py new file mode 100644 index 000000000..6e0ba77ca --- /dev/null +++ b/mail_environment/hooks.py @@ -0,0 +1,75 @@ +from odoo.tools import config + + +def pre_init_hook(env): + if config["test_enable"]: + env["ir.mail_server"].create( + { + "name": "Test Outgoing Mail Server", + "smtp_host": "localhost", + "smtp_port": 25, + "smtp_user": "test", + "smtp_pass": "test123", + "active": False, + } + ) + env.cr.execute( + "SELECT * FROM ir_mail_server where name = 'Test Outgoing Mail Server'" + ) + mail_server = env.cr.dictfetchone() + assert mail_server + assert mail_server["smtp_host"] == "localhost" + assert mail_server["smtp_port"] == 25 + assert mail_server["smtp_user"] == "test" + assert mail_server["smtp_pass"] == "test123" + env["fetchmail.server"].create( + { + "name": "Test Incoming Mail Server", + "server": "localhost", + "port": 143, + "user": "test", + "password": "test123", + "active": False, + } + ) + env.cr.execute( + "SELECT * FROM fetchmail_server where name = 'Test Incoming Mail Server'" + ) + mail_server = env.cr.dictfetchone() + assert mail_server + assert mail_server["server"] == "localhost" + assert mail_server["port"] == 143 + assert mail_server["user"] == "test" + assert mail_server["password"] == "test123" + + +def post_init_hook(env): + # Migrate Outgoing Mail Server data + env.cr.execute("SELECT * FROM ir_mail_server") + for row in env.cr.dictfetchall(): + mail_server = ( + env["ir.mail_server"] + .with_context(active_test=False) + .search([("name", "=", row["name"])]) + ) + if mail_server: + for field_name, _options in env[ + "ir.mail_server" + ]._server_env_fields.items(): + if field_name in row: + mail_server[field_name] = row[field_name] + + # Migrate Incoming Mail Server data + env.cr.execute("SELECT * FROM fetchmail_server") + for row in env.cr.dictfetchall(): + mail_server = ( + env["fetchmail.server"] + .with_context(active_test=False) + .search([("name", "=", row["name"])]) + ) + if mail_server: + for field_name, _options in env[ + "fetchmail.server" + ]._server_env_fields.items(): + if field_name in row: + mail_server[field_name] = row[field_name] diff --git a/mail_environment/tests/__init__.py b/mail_environment/tests/__init__.py index 82ffeb745..417aa55d3 100644 --- a/mail_environment/tests/__init__.py +++ b/mail_environment/tests/__init__.py @@ -1 +1,2 @@ +from . import test_existing_mail_server_config from . import test_mail_environment diff --git a/mail_environment/tests/test_existing_mail_server_config.py b/mail_environment/tests/test_existing_mail_server_config.py new file mode 100644 index 000000000..156e95df6 --- /dev/null +++ b/mail_environment/tests/test_existing_mail_server_config.py @@ -0,0 +1,50 @@ +import json + +from odoo.tests import tagged + +from odoo.addons.server_environment.tests.common import ServerEnvironmentCase + + +@tagged("post_install", "-at_install") +class TestMailEnvironment(ServerEnvironmentCase): + def test_outgoing_mail_server(self): + mail_server = ( + self.env["ir.mail_server"] + .with_context(active_test=False) + .search([("name", "=", "Test Outgoing Mail Server")]) + ) + self.assertTrue(mail_server) + self.assertEqual(mail_server.smtp_host, "localhost") + self.assertEqual(mail_server.smtp_port, 25) + self.assertEqual(mail_server.smtp_user, "test") + self.assertEqual(mail_server.smtp_pass, "test123") + self.env.cr.execute( + "SELECT * FROM ir_mail_server where name = 'Test Outgoing Mail Server'" + ) + mail_server = self.env.cr.dictfetchone() + server_env_defaults = json.loads(mail_server["server_env_defaults"]) + self.assertEqual(server_env_defaults["x_smtp_host_env_default"], "localhost") + self.assertEqual(server_env_defaults["x_smtp_port_env_default"], 25) + self.assertEqual(server_env_defaults["x_smtp_user_env_default"], "test") + self.assertEqual(server_env_defaults["x_smtp_pass_env_default"], "test123") + + def test_incoming_mail_server(self): + mail_server = ( + self.env["fetchmail.server"] + .with_context(active_test=False) + .search([("name", "=", "Test Incoming Mail Server")]) + ) + self.assertTrue(mail_server) + self.assertEqual(mail_server.server, "localhost") + self.assertEqual(mail_server.port, 143) + self.assertEqual(mail_server.user, "test") + self.assertEqual(mail_server.password, "test123") + self.env.cr.execute( + "SELECT * FROM fetchmail_server where name = 'Test Incoming Mail Server'" + ) + mail_server = self.env.cr.dictfetchone() + server_env_defaults = json.loads(mail_server["server_env_defaults"]) + self.assertEqual(server_env_defaults["x_server_env_default"], "localhost") + self.assertEqual(server_env_defaults["x_port_env_default"], 143) + self.assertEqual(server_env_defaults["x_user_env_default"], "test") + self.assertEqual(server_env_defaults["x_password_env_default"], "test123")