vintasend-fastapi-mail 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- vintasend_fastapi_mail-0.1.0/LICENSE.txt +21 -0
- vintasend_fastapi_mail-0.1.0/PKG-INFO +20 -0
- vintasend_fastapi_mail-0.1.0/README.md +3 -0
- vintasend_fastapi_mail-0.1.0/pyproject.toml +153 -0
- vintasend_fastapi_mail-0.1.0/vintasend_fastapi_mail/__init__.py +0 -0
- vintasend_fastapi_mail-0.1.0/vintasend_fastapi_mail/services/__init__.py +0 -0
- vintasend_fastapi_mail-0.1.0/vintasend_fastapi_mail/services/notification_adapters/__init__.py +0 -0
- vintasend_fastapi_mail-0.1.0/vintasend_fastapi_mail/services/notification_adapters/fastapi_mail.py +63 -0
- vintasend_fastapi_mail-0.1.0/vintasend_fastapi_mail/services/tests/__init__.py +0 -0
- vintasend_fastapi_mail-0.1.0/vintasend_fastapi_mail/services/tests/test_adapters.py +105 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2017 Vinta Serviços e Soluções Tecnológicas Ltda
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
Metadata-Version: 2.3
|
|
2
|
+
Name: vintasend-fastapi-mail
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: FastAPI Mail adapter for VintaSend
|
|
5
|
+
License: MIT
|
|
6
|
+
Author: Hugo bessa
|
|
7
|
+
Author-email: hugo@vinta.com.br
|
|
8
|
+
Requires-Python: >=3.10,<3.13
|
|
9
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
10
|
+
Classifier: Programming Language :: Python :: 3
|
|
11
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
14
|
+
Requires-Dist: fastapi-mail (>=1.4.2,<2.0.0)
|
|
15
|
+
Requires-Dist: vintasend
|
|
16
|
+
Description-Content-Type: text/markdown
|
|
17
|
+
|
|
18
|
+
# VintaSend FastAPI Mail
|
|
19
|
+
|
|
20
|
+
FastAPI Mail adapter for VintaSend
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
[tool.poetry]
|
|
2
|
+
name = "vintasend-fastapi-mail"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
description = "FastAPI Mail adapter for VintaSend"
|
|
5
|
+
authors = ["Hugo bessa <hugo@vinta.com.br>"]
|
|
6
|
+
license = "MIT"
|
|
7
|
+
readme = "README.md"
|
|
8
|
+
|
|
9
|
+
[tool.poetry.dependencies]
|
|
10
|
+
python = "<3.13,>=3.10"
|
|
11
|
+
vintasend = "*"
|
|
12
|
+
fastapi-mail = "^1.4.2"
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
[tool.poetry.group.dev.dependencies]
|
|
16
|
+
coverage = "^7.6.4"
|
|
17
|
+
tox = "^4.23.2"
|
|
18
|
+
pytest = "^8.3.3"
|
|
19
|
+
pytest-xdist = {version = "^3.6.1", extras=["psutil"]}
|
|
20
|
+
coveralls = "^4.0.1"
|
|
21
|
+
pytest-cov = "^6.0.0"
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
[build-system]
|
|
25
|
+
requires = ["poetry-core"]
|
|
26
|
+
build-backend = "poetry.core.masonry.api"
|
|
27
|
+
|
|
28
|
+
[tool.ruff]
|
|
29
|
+
select = [
|
|
30
|
+
# pycodestyle
|
|
31
|
+
"E",
|
|
32
|
+
# Pyflakes
|
|
33
|
+
"F",
|
|
34
|
+
# pep8-naming
|
|
35
|
+
"N",
|
|
36
|
+
# pyupgrade
|
|
37
|
+
"UP",
|
|
38
|
+
# flake8-bugbear
|
|
39
|
+
"B",
|
|
40
|
+
# flake8-bandit
|
|
41
|
+
"S",
|
|
42
|
+
# flake8-blind-except
|
|
43
|
+
"BLE",
|
|
44
|
+
# flake8-builtins
|
|
45
|
+
"A",
|
|
46
|
+
# isort
|
|
47
|
+
"I",
|
|
48
|
+
# flake8-logging-format
|
|
49
|
+
"G",
|
|
50
|
+
# flake8-no-pep420
|
|
51
|
+
"INP",
|
|
52
|
+
# Ruff-specific rules
|
|
53
|
+
"RUF",
|
|
54
|
+
]
|
|
55
|
+
exclude = [
|
|
56
|
+
".bzr",
|
|
57
|
+
".direnv",
|
|
58
|
+
".eggs",
|
|
59
|
+
".git",
|
|
60
|
+
".git-rewrite",
|
|
61
|
+
".hg",
|
|
62
|
+
".mypy_cache",
|
|
63
|
+
".nox",
|
|
64
|
+
".pants.d",
|
|
65
|
+
".pytype",
|
|
66
|
+
".ruff_cache",
|
|
67
|
+
".svn",
|
|
68
|
+
".tox",
|
|
69
|
+
".venv",
|
|
70
|
+
"__pypackages__",
|
|
71
|
+
"_build",
|
|
72
|
+
"buck-out",
|
|
73
|
+
"build",
|
|
74
|
+
"dist",
|
|
75
|
+
"node_modules",
|
|
76
|
+
"venv",
|
|
77
|
+
"virtualenvs",
|
|
78
|
+
"*/migrations/*",
|
|
79
|
+
]
|
|
80
|
+
ignore = [
|
|
81
|
+
# Disable eradicate (commented code removal)
|
|
82
|
+
"ERA001",
|
|
83
|
+
# Disable Conflicting lint rules,
|
|
84
|
+
# see https://docs.astral.sh/ruff/formatter/#conflicting-lint-rules
|
|
85
|
+
"W191",
|
|
86
|
+
"E501",
|
|
87
|
+
"E111",
|
|
88
|
+
"E117",
|
|
89
|
+
"D206",
|
|
90
|
+
"D300",
|
|
91
|
+
"Q000",
|
|
92
|
+
"Q001",
|
|
93
|
+
"Q002",
|
|
94
|
+
"Q003",
|
|
95
|
+
"COM812",
|
|
96
|
+
"COM819",
|
|
97
|
+
"ISC001",
|
|
98
|
+
"ISC002",
|
|
99
|
+
# Allow `except Exception`:
|
|
100
|
+
"BLE001",
|
|
101
|
+
# Disable unused `noqa` directive
|
|
102
|
+
"RUF100",
|
|
103
|
+
# Disable pyupgrade UP rules that conflict with django-ninja
|
|
104
|
+
"UP006",
|
|
105
|
+
"UP035",
|
|
106
|
+
"UP037",
|
|
107
|
+
"UP040",
|
|
108
|
+
]
|
|
109
|
+
line-length = 100
|
|
110
|
+
indent-width = 4
|
|
111
|
+
target-version = "py312"
|
|
112
|
+
# Allow unused variables when underscore-prefixed:
|
|
113
|
+
dummy-variable-rgx = "^(_+|(_+[a-zA-Z0-9_]*[a-zA-Z0-9]+?))$"
|
|
114
|
+
|
|
115
|
+
[tool.ruff.pycodestyle]
|
|
116
|
+
ignore-overlong-task-comments = true
|
|
117
|
+
|
|
118
|
+
[tool.ruff.lint.isort]
|
|
119
|
+
section-order = [
|
|
120
|
+
"future",
|
|
121
|
+
"standard-library",
|
|
122
|
+
"third-party",
|
|
123
|
+
"first-party",
|
|
124
|
+
"local-folder",
|
|
125
|
+
]
|
|
126
|
+
lines-after-imports = 2
|
|
127
|
+
|
|
128
|
+
[tool.ruff.per-file-ignores]
|
|
129
|
+
# Ignore "E402", "F403", "F405" (import violations) in __init__.py files.
|
|
130
|
+
# Ignore "S" (flake8-bandit) and "N802" (function name should be lowercase) in tests and docs.
|
|
131
|
+
# Ignore "RUF" (Ruff-specific rules) and "I" (isort) in migrations.
|
|
132
|
+
"__init__.py" = ["E402", "F403", "F405"]
|
|
133
|
+
"**/{tests,docs}/*" = ["E402", "F403", "F405", "S", "N802"]
|
|
134
|
+
"**/*test*.py" = ["E402", "F403", "F405", "S", "N802"]
|
|
135
|
+
"**/{settings}/*" = ["E402", "F403", "F405"]
|
|
136
|
+
"**/migrations/*" = ["RUF", "I"]
|
|
137
|
+
|
|
138
|
+
[tool.coverage.run]
|
|
139
|
+
branch = true
|
|
140
|
+
source = ["backend"]
|
|
141
|
+
omit = [
|
|
142
|
+
"**/venv/*",
|
|
143
|
+
"**/env/*",
|
|
144
|
+
"**/virtualenvs/*",
|
|
145
|
+
"**/node_modules/*",
|
|
146
|
+
"**/migrations/*",
|
|
147
|
+
"**/settings/*",
|
|
148
|
+
"**/tests/*",
|
|
149
|
+
]
|
|
150
|
+
|
|
151
|
+
[tool.pytest.ini_options]
|
|
152
|
+
python_files = ["test_*.py"]
|
|
153
|
+
addopts = "--dist=loadscope"
|
|
File without changes
|
|
File without changes
|
vintasend_fastapi_mail-0.1.0/vintasend_fastapi_mail/services/notification_adapters/__init__.py
ADDED
|
File without changes
|
vintasend_fastapi_mail-0.1.0/vintasend_fastapi_mail/services/notification_adapters/fastapi_mail.py
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
from typing import TYPE_CHECKING, Generic, TypeVar
|
|
2
|
+
|
|
3
|
+
from fastapi_mail import FastMail, MessageSchema, ConnectionConfig
|
|
4
|
+
|
|
5
|
+
from vintasend.constants import NotificationTypes
|
|
6
|
+
|
|
7
|
+
from vintasend.services.dataclasses import Notification
|
|
8
|
+
from vintasend.services.notification_backends.asyncio_base import AsyncIOBaseNotificationBackend
|
|
9
|
+
from vintasend.services.notification_adapters.asyncio_base import AsyncIOBaseNotificationAdapter
|
|
10
|
+
from vintasend.services.notification_template_renderers.base_templated_email_renderer import BaseTemplatedEmailRenderer
|
|
11
|
+
from vintasend.app_settings import NotificationSettings
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
if TYPE_CHECKING:
|
|
15
|
+
from vintasend.services.notification_service import NotificationContextDict
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
B = TypeVar("B", bound=AsyncIOBaseNotificationBackend)
|
|
19
|
+
T = TypeVar("T", bound=BaseTemplatedEmailRenderer)
|
|
20
|
+
|
|
21
|
+
class FastAPIMailNotificationAdapter(Generic[B, T], AsyncIOBaseNotificationAdapter[B, T]):
|
|
22
|
+
notification_type = NotificationTypes.EMAIL
|
|
23
|
+
config: ConnectionConfig
|
|
24
|
+
fm: FastMail
|
|
25
|
+
|
|
26
|
+
def __init__(
|
|
27
|
+
self, *args, **kwargs
|
|
28
|
+
) -> None:
|
|
29
|
+
super().__init__(*args, **kwargs)
|
|
30
|
+
self.fm = FastMail(self.config)
|
|
31
|
+
|
|
32
|
+
async def send(
|
|
33
|
+
self,
|
|
34
|
+
notification: Notification,
|
|
35
|
+
context: "NotificationContextDict",
|
|
36
|
+
headers: dict[str, str] | None = None,
|
|
37
|
+
) -> None:
|
|
38
|
+
"""
|
|
39
|
+
Send the notification to the user through email.
|
|
40
|
+
|
|
41
|
+
:param notification: The notification to send.
|
|
42
|
+
:param context: The context to render the notification templates.
|
|
43
|
+
"""
|
|
44
|
+
notification_settings = NotificationSettings()
|
|
45
|
+
|
|
46
|
+
user_email = await self.backend.get_user_email_from_notification(notification.id)
|
|
47
|
+
to = [user_email]
|
|
48
|
+
bcc = [email for email in notification_settings.NOTIFICATION_DEFAULT_BCC_EMAILS] or []
|
|
49
|
+
|
|
50
|
+
context_with_base_url: "NotificationContextDict" = context.copy()
|
|
51
|
+
context_with_base_url["base_url"] = f"{notification_settings.NOTIFICATION_DEFAULT_BASE_URL_PROTOCOL}://{notification_settings.NOTIFICATION_DEFAULT_BASE_URL_DOMAIN}"
|
|
52
|
+
|
|
53
|
+
template = self.template_renderer.render(notification, context_with_base_url)
|
|
54
|
+
|
|
55
|
+
message = MessageSchema(
|
|
56
|
+
subject=template.subject.strip(),
|
|
57
|
+
recipients=to,
|
|
58
|
+
body=template.body,
|
|
59
|
+
subtype="html",
|
|
60
|
+
bcc=bcc,
|
|
61
|
+
headers=headers,
|
|
62
|
+
)
|
|
63
|
+
await self.fm.send_message(message)
|
|
File without changes
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import uuid
|
|
2
|
+
import pytest
|
|
3
|
+
import base64
|
|
4
|
+
from unittest import IsolatedAsyncioTestCase
|
|
5
|
+
|
|
6
|
+
from fastapi_mail import ConnectionConfig
|
|
7
|
+
|
|
8
|
+
from vintasend.constants import NotificationStatus, NotificationTypes
|
|
9
|
+
from vintasend.exceptions import (
|
|
10
|
+
NotificationTemplateRenderingError,
|
|
11
|
+
)
|
|
12
|
+
from vintasend.services.dataclasses import Notification
|
|
13
|
+
from vintasend.services.notification_backends.stubs.fake_backend import FakeAsyncIOFileBackend, FakeFileBackend
|
|
14
|
+
from vintasend_fastapi_mail.services.notification_adapters.fastapi_mail import FastAPIMailNotificationAdapter
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
class FastAPIMailNotificationAdapterTestCase(IsolatedAsyncioTestCase):
|
|
19
|
+
def setup_method(self, method) -> None:
|
|
20
|
+
self.config = ConnectionConfig(
|
|
21
|
+
MAIL_USERNAME="test",
|
|
22
|
+
MAIL_PASSWORD="test", # type: ignore
|
|
23
|
+
MAIL_FROM="foo@example.com",
|
|
24
|
+
MAIL_PORT=587,
|
|
25
|
+
MAIL_SERVER="smtp.example.com",
|
|
26
|
+
MAIL_STARTTLS=False,
|
|
27
|
+
MAIL_SSL_TLS=False,
|
|
28
|
+
TEMPLATE_FOLDER=None,
|
|
29
|
+
SUPPRESS_SEND=True,
|
|
30
|
+
)
|
|
31
|
+
|
|
32
|
+
super().setUp()
|
|
33
|
+
|
|
34
|
+
def teardown_method(self, method) -> None:
|
|
35
|
+
FakeFileBackend(database_file_name="fastapi-mail-adapter-test-notifications.json").clear()
|
|
36
|
+
|
|
37
|
+
def teardown_class(self) -> None:
|
|
38
|
+
FakeFileBackend(database_file_name="fastapi-mail-adapter-test-notifications.json").clear()
|
|
39
|
+
|
|
40
|
+
def create_notification(self):
|
|
41
|
+
return Notification(
|
|
42
|
+
id=uuid.uuid4(),
|
|
43
|
+
user_id=uuid.uuid4(),
|
|
44
|
+
notification_type=NotificationTypes.EMAIL.value,
|
|
45
|
+
title="Test Notification",
|
|
46
|
+
body_template="Test Body",
|
|
47
|
+
context_name="test_context",
|
|
48
|
+
context_kwargs={"test": "test"},
|
|
49
|
+
send_after=None,
|
|
50
|
+
subject_template="Test Subject",
|
|
51
|
+
preheader_template="Test Preheader",
|
|
52
|
+
status=NotificationStatus.PENDING_SEND.value,
|
|
53
|
+
)
|
|
54
|
+
|
|
55
|
+
def create_notification_context(self):
|
|
56
|
+
return {"foo": "bar"}
|
|
57
|
+
|
|
58
|
+
@pytest.mark.asyncio
|
|
59
|
+
async def test_send_notification(self):
|
|
60
|
+
notification = self.create_notification()
|
|
61
|
+
context = self.create_notification_context()
|
|
62
|
+
|
|
63
|
+
backend = FakeAsyncIOFileBackend(database_file_name="fastapi-mail-adapter-test-notifications.json")
|
|
64
|
+
backend.notifications.append(notification)
|
|
65
|
+
await backend._store_notifications()
|
|
66
|
+
|
|
67
|
+
adapter = FastAPIMailNotificationAdapter(
|
|
68
|
+
"vintasend.services.notification_template_renderers.stubs.fake_templated_email_renderer.FakeTemplateRenderer",
|
|
69
|
+
"vintasend.services.notification_backends.stubs.fake_backend.FakeAsyncIOFileBackend",
|
|
70
|
+
backend_kwargs={"database_file_name": "fastapi-mail-adapter-test-notifications.json"},
|
|
71
|
+
config=self.config
|
|
72
|
+
)
|
|
73
|
+
|
|
74
|
+
with adapter.fm.record_messages() as outbox:
|
|
75
|
+
await adapter.send(notification, context)
|
|
76
|
+
|
|
77
|
+
assert len(outbox) == 1
|
|
78
|
+
email = outbox[0]
|
|
79
|
+
payload = email.get_payload(0).get_payload()
|
|
80
|
+
email_body = base64.b64decode(payload).decode("utf-8")
|
|
81
|
+
assert email["Subject"] == notification.subject_template
|
|
82
|
+
assert email_body == notification.body_template
|
|
83
|
+
assert email["To"] == "testemail@example.com" # This is the email that the FakeFileBackend returns
|
|
84
|
+
assert email["From"] == "foo@example.com" # This is the email that the FakeFileBackend returns
|
|
85
|
+
|
|
86
|
+
@pytest.mark.asyncio
|
|
87
|
+
async def test_send_notification_with_render_error(self):
|
|
88
|
+
notification = self.create_notification()
|
|
89
|
+
context = self.create_notification_context()
|
|
90
|
+
|
|
91
|
+
backend = FakeAsyncIOFileBackend(database_file_name="fastapi-mail-adapter-test-notifications.json")
|
|
92
|
+
backend.notifications.append(notification)
|
|
93
|
+
await backend._store_notifications()
|
|
94
|
+
|
|
95
|
+
adapter = FastAPIMailNotificationAdapter(
|
|
96
|
+
"vintasend.services.notification_template_renderers.stubs.fake_templated_email_renderer.FakeTemplateRendererWithException",
|
|
97
|
+
"vintasend.services.notification_backends.stubs.fake_backend.FakeAsyncIOFileBackend",
|
|
98
|
+
backend_kwargs={"database_file_name": "fastapi-mail-adapter-test-notifications.json"},
|
|
99
|
+
config=self.config
|
|
100
|
+
)
|
|
101
|
+
with adapter.fm.record_messages() as outbox:
|
|
102
|
+
with pytest.raises(NotificationTemplateRenderingError):
|
|
103
|
+
await adapter.send(notification, context)
|
|
104
|
+
|
|
105
|
+
assert len(outbox) == 0
|