Skip to content

Commit

Permalink
Merge 5f16b9e into 8f8a7e1
Browse files Browse the repository at this point in the history
  • Loading branch information
dpnova committed Jan 7, 2016
2 parents 8f8a7e1 + 5f16b9e commit 3871682
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 5 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
language: python
python:
- "2.6"
- "2.7"
- "pypy"

Expand Down
16 changes: 13 additions & 3 deletions cyclone/mail.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import os.path

from cStringIO import StringIO
from OpenSSL.SSL import SSLv3_METHOD
from OpenSSL.SSL import OP_NO_SSLv3

from email import Encoders
from email.MIMEText import MIMEText
Expand All @@ -40,6 +40,17 @@
from twisted.mail.smtp import ESMTPSenderFactory, quoteaddr


class ContextFactory(ClientContextFactory):

"""Context factory that disables SSLv3 (POODLE attack)."""

def getContext(self):
"""Get the parent context but disable SSLv3."""
ctx = ClientContextFactory.getContext(self)
ctx.set_options(OP_NO_SSLv3)
return ctx


class Message(object):
"""Create new e-mail messages.
Expand Down Expand Up @@ -170,8 +181,7 @@ def sendmail(mailconf, message):

if use_tls:
port = mailconf.get("port", 587)
contextFactory = ClientContextFactory()
contextFactory.method = SSLv3_METHOD
contextFactory = ContextFactory()
else:
port = mailconf.get("port", 25)
contextFactory = None
Expand Down
69 changes: 69 additions & 0 deletions cyclone/tests/test_mail.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#
# Copyright 2014 David Novakovic
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from twisted.trial import unittest
from cyclone.mail import ContextFactory, ClientContextFactory, Message
from cyclone.mail import sendmail
from mock import Mock, patch
import types


class ContextFactoryTest(unittest.TestCase):
def test_no_sslv3(self):
"""We must disable ssl v3."""
ClientContextFactory.getContext = Mock()
cf = ContextFactory()
ctx = cf.getContext()
ctx.set_options.assert_called_with(33554432)


class MessageTest(unittest.TestCase):
def setUp(self):
self.message = Message(
"foo@example.com",
["bar@example.com"],
"hi thar",
"This is a message."
)

def test_init(self):
self.assertTrue(self.message.message)
str(self.message)

def test_init_single_addr(self):
message = Message(
"foo@example.com",
"bar@example.com",
"hi thar",
"This is a message."
)
self.assertTrue(isinstance(message.to_addrs, types.ListType))

def test_attach(self):
open("foo.txt", "w").write("sometext")
self.message.attach("foo.txt")
self.assertTrue(self.message.msg)

def test_render(self):
self.message.add_header("X-MailTag", "foobar")
sio = self.message.render()
self.assertTrue("foo@example.com" in sio.getvalue())

@patch("cyclone.mail.reactor.connectTCP")
def test_sendmail(self, conn):
sendmail(
{"host": "localhost", "tls": True},
self.message
)
self.assertTrue(conn.call_count)
3 changes: 2 additions & 1 deletion cyclone/tests/test_requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
mock
twisted>=12.0
twisted>=12.0
pyopenssl

0 comments on commit 3871682

Please sign in to comment.