-
-
Notifications
You must be signed in to change notification settings - Fork 163
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[IMP] mail_environment: Keep existing settings at install of module
- Loading branch information
Showing
5 changed files
with
129 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
50
mail_environment/tests/test_existing_mail_server_config.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |