Skip to content

Commit

Permalink
[IMP] mail_environment: Keep existing settings at install of module
Browse files Browse the repository at this point in the history
  • Loading branch information
rven committed Jan 2, 2025
1 parent 3a6f9fe commit ff73fec
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 0 deletions.
1 change: 1 addition & 0 deletions mail_environment/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
from . import models
from .hooks import post_init_hook, pre_init_hook
2 changes: 2 additions & 0 deletions mail_environment/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
}
75 changes: 75 additions & 0 deletions mail_environment/hooks.py
Original file line number Diff line number Diff line change
@@ -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]
1 change: 1 addition & 0 deletions mail_environment/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
from . import test_existing_mail_server_config
from . import test_mail_environment
50 changes: 50 additions & 0 deletions mail_environment/tests/test_existing_mail_server_config.py
Original file line number Diff line number Diff line change
@@ -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")

0 comments on commit ff73fec

Please sign in to comment.