From 8c512e33cef07711b1bc59d4478170758cbb2292 Mon Sep 17 00:00:00 2001 From: Lance Edgar Date: Tue, 14 Jan 2025 17:28:36 -0600 Subject: [PATCH] fix: add `wutta.purge_email_settings()` for cloning prod DB to test --- docs/api/wuttamess.wutta.rst | 6 ++++ docs/index.rst | 1 + src/wuttamess/wutta.py | 57 ++++++++++++++++++++++++++++++++++++ tests/test_wutta.py | 26 ++++++++++++++++ 4 files changed, 90 insertions(+) create mode 100644 docs/api/wuttamess.wutta.rst create mode 100644 src/wuttamess/wutta.py create mode 100644 tests/test_wutta.py diff --git a/docs/api/wuttamess.wutta.rst b/docs/api/wuttamess.wutta.rst new file mode 100644 index 0000000..ecb58a5 --- /dev/null +++ b/docs/api/wuttamess.wutta.rst @@ -0,0 +1,6 @@ + +``wuttamess.wutta`` +=================== + +.. automodule:: wuttamess.wutta + :members: diff --git a/docs/index.rst b/docs/index.rst index 6571493..6aec06a 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -36,3 +36,4 @@ project. api/wuttamess.ssh api/wuttamess.sync api/wuttamess.util + api/wuttamess.wutta diff --git a/src/wuttamess/wutta.py b/src/wuttamess/wutta.py new file mode 100644 index 0000000..0145542 --- /dev/null +++ b/src/wuttamess/wutta.py @@ -0,0 +1,57 @@ +# -*- coding: utf-8; -*- +################################################################################ +# +# WuttaMess -- Fabric Automation Helpers +# Copyright © 2024-2025 Lance Edgar +# +# This file is part of Wutta Framework. +# +# Wutta Framework 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. +# +# Wutta Framework 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 +# Wutta Framework. If not, see . +# +################################################################################ +""" +Utilities for Wutta Framework +""" + +from wuttamess import postgres + + +def purge_email_settings(c, dbname, appname='wutta'): + """ + Purge production email settings for a database. + + This can be used when cloning a production app DB to a test + server. The general pattern is: + + * setup test app on test server + * config file should specify test email settings + * clone the production DB to test server + * call this function to purge email settings from test DB + + So the end result should be, the test server app can run and send + emails safely using only what is specified in config file(s), + since none of the production email settings remain in the test DB. + + :param dbname: Name of the database to be updated. + + :param appname: The ``appname`` used to determine setting names. + """ + postgres.sql(c, f"delete from setting where name like '{appname}.email.%.sender';", + database=dbname) + postgres.sql(c, f"delete from setting where name like '{appname}.email.%.to';", + database=dbname) + postgres.sql(c, f"delete from setting where name like '{appname}.email.%.cc';", + database=dbname) + postgres.sql(c, f"delete from setting where name like '{appname}.email.%.bcc';", + database=dbname) diff --git a/tests/test_wutta.py b/tests/test_wutta.py new file mode 100644 index 0000000..1b8436c --- /dev/null +++ b/tests/test_wutta.py @@ -0,0 +1,26 @@ +# -*- coding: utf-8; -*- + +from unittest import TestCase +from unittest.mock import MagicMock, patch, call + +from wuttamess import wutta as mod + + +class TestPurgeEmailSettings(TestCase): + + def test_basic(self): + c = MagicMock() + sql = MagicMock() + postgres = MagicMock(sql=sql) + with patch.object(mod, 'postgres', new=postgres): + mod.purge_email_settings(c, 'testy', appname='wuttatest') + sql.assert_has_calls([ + call(c, "delete from setting where name like 'wuttatest.email.%.sender';", + database='testy'), + call(c, "delete from setting where name like 'wuttatest.email.%.to';", + database='testy'), + call(c, "delete from setting where name like 'wuttatest.email.%.cc';", + database='testy'), + call(c, "delete from setting where name like 'wuttatest.email.%.bcc';", + database='testy'), + ])