-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #24 from snoonetIRC/master+ignore-invalid-addr
Ignore invalid email addresses when sending
- Loading branch information
Showing
5 changed files
with
184 additions
and
2 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
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,71 @@ | ||
import os | ||
import smtplib | ||
from pathlib import Path | ||
from tempfile import NamedTemporaryFile | ||
from textwrap import dedent | ||
|
||
import pytest | ||
from mock import patch | ||
from responses import RequestsMock | ||
|
||
from mailer.config import SMTPConfig | ||
|
||
|
||
class MockSMTPConfig(SMTPConfig): | ||
def send(self, message): | ||
raise smtplib.SMTPRecipientsRefused([message['To']]) | ||
|
||
|
||
@pytest.fixture() | ||
def mock_smtp_config(): | ||
with patch('mailer.config.SMTPConfig', MockSMTPConfig) as mocked: | ||
yield mocked | ||
|
||
|
||
@pytest.fixture() | ||
def mock_api(): | ||
with RequestsMock() as reqs: | ||
reqs.add( | ||
'GET', 'https://localhost:8888/api/user?name=nick', | ||
match_querystring=True, | ||
json={ | ||
'nc': { | ||
'REGSERVER': 'test.example.com', | ||
} | ||
} | ||
) | ||
yield reqs | ||
|
||
|
||
@pytest.fixture() | ||
def override_config(): | ||
old = os.getenv('MAILER_CONFIG') | ||
try: | ||
os.environ['MAILER_CONFIG'] = 'travis/config.tests.yaml' | ||
yield | ||
finally: | ||
if old is None: | ||
del os.environ['MAILER_CONFIG'] | ||
else: | ||
os.environ['MAILER_CONFIG'] = old | ||
|
||
|
||
def test_invalid_recipient(override_config, mock_smtp_config, mock_api): | ||
from mailer.daemon import Daemon | ||
daemon = Daemon() | ||
with patch.object(daemon.config, 'error_reporter') as reporter: | ||
with NamedTemporaryFile(delete=False) as tmp: | ||
tmp.write(dedent("""\ | ||
From: [email protected] | ||
To: "nick" <a.user@!somedomain.org> | ||
nick=nick | ||
type=registration | ||
code=1234 | ||
""").encode()) | ||
|
||
daemon.handle_email(Path(tmp.name)) | ||
|
||
reporter.report_warning.assert_called_with( | ||
"Invalid email recipient(s) ['nick <a.user@!somedomain.org>'], ignoring message" | ||
) |
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,104 @@ | ||
mail_dir: emails | ||
smtp: | ||
network1: | ||
server: smtp.example.com | ||
from_addr: [email protected] | ||
port: 587 | ||
tls: true | ||
user_name: '' | ||
password: '' | ||
reports: | ||
from_addr: [email protected] | ||
server: smtp-relay.gmail.com | ||
port: 587 | ||
tls: true | ||
smtp_helo: snoonet.org | ||
|
||
reports: | ||
default: | ||
to: [email protected] | ||
smtp: reports | ||
delay: 1h # By default send reports once an hour | ||
stats: | ||
# Statistics reports | ||
delay: 1d # Only send statistics once per day | ||
errors: | ||
# Error reports | ||
delay: 10m # Send an error summary at most once every 10 minutes | ||
|
||
# requires: | ||
# https://github.com/snoonetIRC/anope-modules/blob/master/modules/m_data_api.cpp | ||
# https://github.com/snoonetIRC/anope-modules/blob/master/modules/m_store_server.cpp | ||
api: https://localhost:8888 | ||
|
||
default_formats: | ||
registration: | ||
subject: Nickname registration for {nick} | ||
message: |- | ||
Hi, | ||
You have requested to register the nickname {nick} on {network}. | ||
Please type \" /msg NickServ CONFIRM {code} \" in your IRC Client to complete registration. | ||
If you don't know why this mail was sent to you, please ignore it silently. | ||
{network} administrators. | ||
reset: | ||
subject: Reset password request for {nick} | ||
message: |- | ||
Hi, | ||
You have requested to have the password for {nick} reset. | ||
To reset your password, type \" /msg NickServ CONFIRM {nick} {code} \" | ||
After you do this, you will be logged in to your account by anope. | ||
Then change your password with \" /msg NickServ SET PASSWORD newpasshere \". | ||
If you don't know why this mail was sent to you, please ignore it silently. | ||
{network} administrators. | ||
emailchange: | ||
subject: Email confirmation | ||
message: |- | ||
Hi, | ||
You have requested to change your email address to {newmail}. | ||
Please type \" /msg NickServ CONFIRM {code} \" in your IRC client to confirm this change. | ||
If you don't know why this mail was sent to you, please ignore it silently. | ||
{network} administrators. | ||
memo: | ||
subject: New memo | ||
message: |- | ||
Hi {nick}, | ||
You've just received a new memo from {sender}. This is memo number {memo_id}. | ||
Memo text: | ||
{text} | ||
Automated message from {network}. Please do not reply to this email. | ||
networks: | ||
- name: Net1 | ||
server: '*.example.com' | ||
smtp: network1 | ||
|
||
- name: Snoonet | ||
# Any server not already matched uses this config | ||
server: '*' | ||
smtp: snoonet | ||
formats: | ||
memo: | ||
subject: New memo from {sender} | ||
message: |- | ||
Hi {nick}, | ||
You've just received a new memo from {sender}. This is memo number {memo_id}. | ||
Memo text: | ||
{text} | ||
Automated message from {network}. Please do not reply to this email. |
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 |
---|---|---|
|
@@ -3,4 +3,5 @@ pytest-cov==2.7.1 | |
pytest-travis-fold==1.3.0 | ||
codecov==2.0.15 | ||
mock==3.0.5 | ||
responses==0.10.6 | ||
-r ../requirements.txt |