Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_attachment_tilde_path(tmpdir):
"""Attachment with home directory tilde notation file path."""
template_path = Path(tmpdir/"template.txt")
template_path.write_text(textwrap.dedent(u"""\
TO: to@test.com
FROM: from@test.com
ATTACHMENT: ~/attachment.txt
Hello world
"""))
# Render will throw an error because we didn't create a file in the
# user's home directory. We'll just check the filename.
template_message = TemplateMessage(template_path)
with pytest.raises(MailmergeError) as err:
template_message.render({})
correct_path = Path.home() / "attachment.txt"
assert str(correct_path) in str(err)
def test_attachment_not_found(tmpdir):
"""Attachment file not found."""
# Template specifying an attachment that doesn't exist
template_path = Path(tmpdir/"template.txt")
template_path.write_text(textwrap.dedent(u"""\
TO: to@test.com
FROM: from@test.com
ATTACHMENT: attachment.txt
Hello world
"""))
# Render in tmpdir, which lacks attachment.txt
template_message = TemplateMessage(template_path)
with pytest.raises(MailmergeError):
with tmpdir.as_cwd():
template_message.render({})
except jinja2.exceptions.TemplateError as err:
sys.exit(">>> Error in Jinja2 template: {}".format(err))
except csv.Error as err:
sys.exit(">>> Error reading CSV file: {}".format(err))
except smtplib.SMTPAuthenticationError as err:
sys.exit(">>> Authentication error: {}".format(err))
except configparser.Error as err:
sys.exit(
">>> Error reading config file {filename}: {message}"
.format(filename=config_path, message=err)
)
except smtplib.SMTPException as err:
sys.exit(">>> Error sending message", err, sep=' ', file=sys.stderr)
except socket.error:
sys.exit(">>> Error connecting to server")
except MailmergeError as err:
sys.exit(">>> {}".format(err))
# Hints for user
if not no_limit:
print(
">>> Limit was {limit} message{pluralizer}. "
"To remove the limit, use the --no-limit option."
.format(limit=limit, pluralizer=("" if limit == 1 else "s"))
)
if dry_run:
print(
">>> This was a dry run. "
"To send messages, use the --no-dry-run option."