def test_dummy_send_to_queue(self): from pyramid_mailer.mailer import DummyMailer from pyramid_mailer.message import Message mailer = DummyMailer() msg = Message(subject="testing", sender="*****@*****.**", recipients=["*****@*****.**"], body="test") mailer.send_to_queue(msg) self.assertEqual(len(mailer.queue), 1)
def test_dummy_send_immediately_and_fail_silently(self): from pyramid_mailer.mailer import DummyMailer from pyramid_mailer.message import Message mailer = DummyMailer() msg = Message(subject="testing", sender="*****@*****.**", recipients=["*****@*****.**"], body="test") mailer.send_immediately(msg, True) self.assertEqual(len(mailer.outbox), 1)
def dummy_test_send_immediately(self): from pyramid_mailer.mailer import DummyMailer from pyramid_mailer.message import Message mailer = DummyMailer() msg = Message(subject="testing", sender="*****@*****.**", recipients=["*****@*****.**"], body="test") mailer.send_immediately(msg) self.assert_(len(mailer.outbox)) == 1
def test_dummy_send_immediately_and_fail_silently(self): from pyramid_mailer.mailer import DummyMailer from pyramid_mailer.message import Message mailer = DummyMailer() msg = Message(subject="testing", sender="*****@*****.**", recipients=["*****@*****.**"], body="test") mailer.send_immediately(msg, True) self.assert_(len(mailer.outbox)) == 1
def test_dummy_send_to_queue(self): from pyramid_mailer.mailer import DummyMailer from pyramid_mailer.message import Message mailer = DummyMailer() msg = Message(subject="testing", sender="*****@*****.**", recipients=["*****@*****.**"], body="test") mailer.send_to_queue(msg) self.assert_(len(mailer.queue)) == 1
def test_forgot_password_invalid_password(self): from horus.views import ForgotPasswordController from pyramid_mailer.interfaces import IMailer from pyramid_mailer.mailer import DummyMailer from horus.interfaces import IUserClass from horus.tests.models import User self.config.registry.registerUtility(User, IUserClass) self.config.add_route('index', '/') self.config.include('horus') self.config.registry.registerUtility(DummyMailer(), IMailer) user = User(username='******', password='******', email='*****@*****.**') user.password = '******' self.session.add(user) self.session.flush() request = self.get_csrf_request(post={ 'email': 'sontek' }, request_method='POST') request.user = None view = ForgotPasswordController(request) response = view.forgot_password() assert len(response['errors']) == 1
def test_forgot_password_valid_user(self): from horus.views import ForgotPasswordController from pyramid_mailer.interfaces import IMailer from pyramid_mailer.mailer import DummyMailer from horus.interfaces import IUserClass from horus.tests.models import User self.config.registry.registerUtility(User, IUserClass) self.config.add_route('index', '/') self.config.include('horus') self.config.registry.registerUtility(DummyMailer(), IMailer) user = User(username='******', password='******', email='*****@*****.**') user.password = '******' self.session.add(user) self.session.flush() request = self.get_csrf_request(post={ 'email': '*****@*****.**' }, request_method='POST') request.user = None view = ForgotPasswordController(request) with patch('horus.views.FlashMessage') as FlashMessage: response = view.forgot_password() FlashMessage.assert_called_with(request, view.Str.reset_password_email_sent, kind="success") assert response.status_int == 302
def test_activate_invalid(self): from horus.views import RegisterController from pyramid_mailer.interfaces import IMailer from pyramid_mailer.mailer import DummyMailer from horus.interfaces import IUserClass from horus.tests.models import User from horus.interfaces import IActivationClass from horus.tests.models import Activation self.config.registry.registerUtility(Activation, IActivationClass) self.config.registry.registerUtility(User, IUserClass) self.config.include('horus') self.config.add_route('index', '/') self.config.registry.registerUtility(DummyMailer(), IMailer) user = User(username='******', email='*****@*****.**') user.password = '******' user.activation = Activation() self.session.add(user) self.session.flush() request = testing.DummyRequest() request.matchdict = Mock() get = Mock() get.return_value = 'invalid' request.matchdict.get = get controller = RegisterController(request) response = controller.activate() user = User.get_by_username(request, 'sontek') assert not user.is_activated assert response.status_int == 404
def test_register_existing_user(self): from horus.views import RegisterController from pyramid_mailer.mailer import DummyMailer from pyramid_mailer.interfaces import IMailer from horus.interfaces import IUserClass from horus.tests.models import User from horus.interfaces import IActivationClass from horus.tests.models import Activation self.config.registry.registerUtility(Activation, IActivationClass) self.config.registry.registerUtility(User, IUserClass) self.config.include('horus') self.config.registry.registerUtility(DummyMailer(), IMailer) self.config.add_route('index', '/') admin = User(username='******', email='*****@*****.**') admin.password = '******' self.session.add(admin) self.session.flush() request = self.get_csrf_request(post={ 'username': '******', 'password': { 'password': '******', 'password-confirm': 'test123', }, 'email': '*****@*****.**' }, request_method='POST') view = RegisterController(request) adict = view.register() assert isinstance(adict, dict) assert adict['errors']
def test_register_validation(self): from horus.views import RegisterController from pyramid_mailer.mailer import DummyMailer from pyramid_mailer.interfaces import IMailer from horus.interfaces import IUserClass from horus.tests.models import User from horus.interfaces import IActivationClass from horus.tests.models import Activation self.config.registry.registerUtility(Activation, IActivationClass) self.config.registry.registerUtility(User, IUserClass) self.config.include('horus') self.config.registry.registerUtility(DummyMailer(), IMailer) self.config.add_route('index', '/') request = self.get_csrf_request(request_method='POST') request.user = Mock() controller = RegisterController(request) response = controller.register() assert len(response['errors']) == 3 assert 'There was a problem with your submission' in response['form']
def test_register_controller_extensions_with_mail(self): from pyramid_mailer.mailer import DummyMailer from pyramid_mailer.interfaces import IMailer from pyramid_signup.views import RegisterController from pyramid_signup.interfaces import ISURegisterSchema from pyramid_signup.interfaces import ISURegisterForm self.config.add_route('index', '/') request = testing.DummyRequest() getUtility = Mock() getUtility.return_value = True schema = Mock() form = Mock() self.config.registry.registerUtility(schema, ISURegisterSchema) self.config.registry.registerUtility(form, ISURegisterForm) self.config.registry.registerUtility(DummyMailer(), IMailer) with patch('pyramid_signup.views.get_mailer') as get_mailer: RegisterController(request) assert get_mailer.called assert schema.called assert form.called
def registerDummyMailer(): from pyramid_mailer.mailer import DummyMailer from kotti.message import _inject_mailer mailer = DummyMailer() _inject_mailer.append(mailer) return mailer
def test_activate_invalid(self): from pyramid_signup.views import RegisterController from pyramid_signup.models import User from pyramid_signup.models import Activation from pyramid_mailer.interfaces import IMailer from pyramid_mailer.mailer import DummyMailer from pyramid_signup.managers import UserManager self.config.include('pyramid_signup') self.config.add_route('index', '/') self.config.registry.registerUtility(DummyMailer(), IMailer) user = User(username='******', password='******') user.activation = Activation() self.session.add(user) self.session.flush() request = testing.DummyRequest() request.matchdict = Mock() get = Mock() get.return_value = 'invalid' request.matchdict.get = get controller = RegisterController(request) response = controller.activate() mgr = UserManager(request) user = mgr.get_by_username('sontek') assert not user.activated assert response.status_int == 404
def test_register_redirects_if_logged_in(self): from horus.views import RegisterController from pyramid_mailer.mailer import DummyMailer from pyramid_mailer.interfaces import IMailer from horus.interfaces import IUserClass from horus.tests.models import User from horus.interfaces import IActivationClass from horus.tests.models import Activation self.config.registry.registerUtility(Activation, IActivationClass) self.config.registry.registerUtility(User, IUserClass) self.config.include('horus') self.config.registry.registerUtility(DummyMailer(), IMailer) self.config.add_route('index', '/') self.config.registry.settings['horus.login_redirect'] = 'index' self.config.registry.settings['horus.logout_redirect'] = 'index' request = testing.DummyRequest() request.user = Mock() controller = RegisterController(request) response = controller.register() assert response.status_int == 302
def test_send(self): mailer = self._makeOne() msg = _makeMessage() smtp_mailer = DummyMailer() mailer.smtp_mailer = smtp_mailer mailer.send(msg) self.assertEqual(len(smtp_mailer.out), 0)
def test_register_existing_user(self): from pyramid_signup.views import RegisterController from pyramid_mailer.mailer import DummyMailer from pyramid_mailer.interfaces import IMailer from pyramid_signup.models import User self.config.include('pyramid_signup') self.config.registry.registerUtility(DummyMailer(), IMailer) self.config.add_route('index', '/') admin = User(username='******', password='******') self.session.add(admin) self.session.flush() request = self.get_csrf_request(post={ 'Username': '******', 'Password': { 'value': 'test123', 'confirm': 'test123', }, 'Email': '*****@*****.**' }, request_method='POST') flash = Mock() request.session.flash = flash controller = RegisterController(request) controller.post() flash.assert_called_with(u'That username is already used.', 'error')
def test_register_creates_user(self): from pyramid_signup.views import RegisterController from pyramid_mailer.mailer import DummyMailer from pyramid_mailer.interfaces import IMailer from pyramid_signup.managers import UserManager self.config.include('pyramid_signup') self.config.registry.registerUtility(DummyMailer(), IMailer) self.config.add_route('index', '/') request = self.get_csrf_request(post={ 'Username': '******', 'Password': { 'value': 'test123', 'confirm': 'test123', }, 'Email': '*****@*****.**' }, request_method='POST') request.user = Mock() controller = RegisterController(request) response = controller.post() assert response.status_int == 302 mgr = UserManager(request) user = mgr.get_by_username('admin') assert user != None
def test_register_creates_user(self): from horus.views import RegisterController from pyramid_mailer.mailer import DummyMailer from pyramid_mailer.interfaces import IMailer from horus.interfaces import IUserClass from horus.tests.models import User from horus.interfaces import IActivationClass from horus.tests.models import Activation self.config.registry.registerUtility(Activation, IActivationClass) self.config.registry.registerUtility(User, IUserClass) self.config.include('horus') self.config.registry.registerUtility(DummyMailer(), IMailer) self.config.add_route('index', '/') request = self.get_csrf_request(post={ 'username': '******', 'password': { 'password': '******', 'password-confirm': 'test123', }, 'email': '*****@*****.**' }, request_method='POST') request.user = Mock() controller = RegisterController(request) response = controller.register() assert response.status_int == 302 user = User.get_by_username(request, 'admin') assert user != None
def test_send_immediately_sendmail_with_exc_fail_loudly(self): email_sender = "*****@*****.**" email_recipient = "*****@*****.**" mailer = self._makeOne() sendmail_mailer = DummyMailer(ValueError()) mailer.sendmail_mailer = sendmail_mailer msg = _makeMessage(subject="test_send_immediately_sendmail", body="body-test_send_immediately_sendmail") self.assertRaises(ValueError, mailer.send_immediately_sendmail, msg)
def test_send_to_queue(self): import os test_queue = os.path.join(self._makeTempdir(), 'test_queue') for dir in ('cur', 'new', 'tmp'): os.makedirs(os.path.join(test_queue, dir)) mailer = self._makeOne(queue_path=test_queue) msg = _makeMessage() queuedelivery = DummyMailer() mailer.queue_delivery = queuedelivery mailer.send_to_queue(msg) self.assertEqual(len(queuedelivery.out), 1)
def test_send_immediately_multipart(self): mailer = self._makeOne() utf_8_encoded = b'mo \xe2\x82\xac' utf_8 = utf_8_encoded.decode('utf_8') text_string = utf_8 html_string = '<p>' + utf_8 + '</p>' msg = _makeMessage(body=text_string, html=html_string) smtp_mailer = DummyMailer() mailer.smtp_mailer = smtp_mailer mailer.send_immediately(msg, True) self.assertEqual(len(smtp_mailer.out), 1)
def test_send_immediately_sendmail_with_exc_fail_silently(self): email_sender = "*****@*****.**" email_recipient = "*****@*****.**" mailer = self._makeOne() sendmail_mailer = DummyMailer(ValueError()) mailer.sendmail_mailer = sendmail_mailer msg = _makeMessage(subject="test_send_immediately_sendmail", body="body-test_send_immediately_sendmail") mailer.send_immediately_sendmail(msg, fail_silently=True) out = sendmail_mailer.out self.assertEqual(len(out), 0)
def test_reset_password_valid_user(self): from horus.views import ForgotPasswordController from hem.interfaces import IDBSession from horus.events import PasswordResetEvent from pyramid_mailer.interfaces import IMailer from pyramid_mailer.mailer import DummyMailer from horus.models import crypt from horus.interfaces import IUserClass from horus.interfaces import IActivationClass from horus.tests.models import User from horus.tests.models import Activation self.config.registry.registerUtility(User, IUserClass) self.config.registry.registerUtility(Activation, IActivationClass) self.config.add_route('index', '/') self.config.include('horus') self.config.registry.registerUtility(DummyMailer(), IMailer) user = User(username='******', email='*****@*****.**') user.password = '******' user.activation = Activation() self.session.add(user) self.session.flush() request = self.get_csrf_request(post={ 'password': { 'password': '******', 'password-confirm': 'test123', }, }, request_method='POST') request.matchdict = Mock() get = Mock() get.return_value = user.activation.code request.matchdict.get = get request.user = None def handle_password_reset(event): request = event.request session = request.registry.getUtility(IDBSession) session.commit() self.config.add_subscriber(handle_password_reset, PasswordResetEvent) view = ForgotPasswordController(request) response = view.reset_password() assert not crypt.check(user.password, 'temp' + user.salt) assert response.status_int == 302
def test_send_immediately_sendmail(self): email_sender = "*****@*****.**" email_recipient = "*****@*****.**" mailer = self._makeOne() sendmail_mailer = DummyMailer() mailer.sendmail_mailer = sendmail_mailer msg = _makeMessage(subject="test_send_immediately_sendmail", body="body-test_send_immediately_sendmail") mailer.send_immediately_sendmail(msg) out = sendmail_mailer.out self.assertEqual(len(out), 1) first = out[0] self.assertEqual(first[0], '*****@*****.**') self.assertEqual(first[1], set(['*****@*****.**']))
def test_send(self): mailer = DummyMailer() service = SMTPEmailSender(mailer, sender="DevPyPI <*****@*****.**>") service.send("a subject", "a body", recipient="*****@*****.**") assert len(mailer.outbox) == 1 msg = mailer.outbox[0] assert msg.subject == "a subject" assert msg.body == "a body" assert msg.recipients == ["*****@*****.**"] assert msg.sender == "DevPyPI <*****@*****.**>"
def test_activate_invalid_user(self): from horus.views import RegisterController from pyramid_mailer.interfaces import IMailer from pyramid_mailer.mailer import DummyMailer from horus.interfaces import IUserClass from horus.tests.models import User from horus.interfaces import IActivationClass from horus.tests.models import Activation self.config.registry.registerUtility(Activation, IActivationClass) self.config.registry.registerUtility(User, IUserClass) self.config.include('horus') self.config.add_route('index', '/') self.config.registry.registerUtility(DummyMailer(), IMailer) bad_act = Activation() user = User(username='******', email='*****@*****.**') user.activation = Activation() user.password = '******' user2 = User(username='******', email='*****@*****.**') user2.activation = bad_act user2.password = '******' self.session.add(user) self.session.add(user2) self.session.flush() request = testing.DummyRequest() request.matchdict = Mock() def get(val, ret): if val == 'code': return bad_act.code elif val == 'user_id': return user.id request.matchdict.get = get controller = RegisterController(request) response = controller.activate() new_user1 = User.get_by_username(request, 'sontek') new_user2 = User.get_by_username(request, 'jessie') assert not new_user1.is_activated assert not new_user2.is_activated assert response.status_int == 404
def test_register_redirects_if_logged_in(self): from pyramid_signup.views import RegisterController from pyramid_mailer.mailer import DummyMailer from pyramid_mailer.interfaces import IMailer self.config.include('pyramid_signup') self.config.registry.registerUtility(DummyMailer(), IMailer) self.config.add_route('index', '/') request = testing.DummyRequest() request.user = Mock() controller = RegisterController(request) response = controller.get() assert response.status_int == 302
def test_register_loads_not_logged_in(self): from pyramid_signup.views import RegisterController from pyramid_mailer.mailer import DummyMailer from pyramid_mailer.interfaces import IMailer self.config.include('pyramid_signup') self.config.registry.registerUtility(DummyMailer(), IMailer) self.config.add_route('index', '/') request = testing.DummyRequest() request.user = None controller = RegisterController(request) response = controller.get() assert response.get('form', None)
def setup_tests_registry(): """Initialize required adapters and utilities for testing""" config = setUp(hook_zca=True) registry = config.registry # register adapters registry.registerAdapter(I18nAttributeAdapter, (Interface, ), II18n) # register security factory register_factory(IRoleProtectedObject, RoleProtectedObject) # register notification components mailer = DummyMailer() register_factory(INotificationSettings, NotificationSettings) registry.registerUtility(mailer, provided=IMailer, name='mailer') registry.registerAdapter(security_notification_factory, (ISecurityManager, ), provided=INotificationSettings) return config
def test_activate_multiple_users(self): from horus.views import RegisterController from pyramid_mailer.interfaces import IMailer from pyramid_mailer.mailer import DummyMailer from horus.interfaces import IUserClass from horus.tests.models import User from horus.interfaces import IActivationClass from horus.tests.models import Activation self.config.registry.registerUtility(Activation, IActivationClass) self.config.registry.registerUtility(User, IUserClass) self.config.include('horus') self.config.add_route('index', '/') self.config.registry.registerUtility(DummyMailer(), IMailer) user = User(username='******', email='*****@*****.**') user.activation = Activation() user.password = '******' user1 = User(username='******', email='*****@*****.**') user1.activation = Activation() user1.password = '******' self.session.add(user) self.session.add(user1) self.session.flush() request = testing.DummyRequest() request.matchdict = Mock() def get(key, default): if key == 'code': return user1.activation.code else: return user1.id request.matchdict.get = get controller = RegisterController(request) response = controller.activate() user = User.get_by_username(request, 'sontek1') activations = Activation.get_all(request) assert len(activations.all()) == 1 assert user.is_activated assert response.status_int == 302
def test_register_no_email_validation(self): from horus.views import RegisterController from pyramid_mailer.mailer import DummyMailer from pyramid_mailer.interfaces import IMailer from hem.interfaces import IDBSession from horus.events import NewRegistrationEvent from horus.interfaces import IUserClass from horus.tests.models import User from horus.interfaces import IActivationClass from horus.tests.models import Activation self.config.registry.registerUtility(Activation, IActivationClass) self.config.registry.registerUtility(User, IUserClass) self.config.include('horus') self.config.registry.registerUtility(DummyMailer(), IMailer) self.config.add_route('index', '/') self.config.registry.settings['horus.require_activation'] = False def handle_registration(event): request = event.request session = request.registry.getUtility(IDBSession) session.commit() self.config.add_subscriber(handle_registration, NewRegistrationEvent) request = self.get_csrf_request(post={ 'username': '******', 'password': { 'password': '******', 'password-confirm': 'test123', }, 'email': '*****@*****.**' }, request_method='POST') request.user = Mock() view = RegisterController(request) with patch('horus.views.FlashMessage') as FlashMessage: response = view.register() FlashMessage.assert_called_with(request, view.Str.registration_done, kind="success") assert response.status_int == 302 user = User.get_by_username(request, 'admin') assert user.is_activated == True
def test_register_validation(self): from pyramid_signup.views import RegisterController from pyramid_mailer.mailer import DummyMailer from pyramid_mailer.interfaces import IMailer self.config.include('pyramid_signup') self.config.registry.registerUtility(DummyMailer(), IMailer) self.config.add_route('index', '/') request = self.get_csrf_request(request_method='POST') request.user = Mock() controller = RegisterController(request) response = controller.post() assert len(response['errors']) == 3 assert 'There was a problem with your submission' in response['form']
def test_reset_password_invalid_password(self): from horus.views import ForgotPasswordController from pyramid_mailer.interfaces import IMailer from pyramid_mailer.mailer import DummyMailer from horus.interfaces import IUserClass from horus.interfaces import IActivationClass from horus.tests.models import User from horus.tests.models import Activation self.config.registry.registerUtility(User, IUserClass) self.config.registry.registerUtility(Activation, IActivationClass) self.config.add_route('index', '/') self.config.include('horus') self.config.registry.registerUtility(DummyMailer(), IMailer) user = User(username='******', password='******', email='*****@*****.**') user.password = '******' user.activation = Activation() self.session.add(user) self.session.flush() request = self.get_csrf_request(post={ 'Password': { 'Password': '******', 'Password-confirm': 't', }, }, request_method='POST') request.matchdict = Mock() get = Mock() get.return_value = user.activation.code request.matchdict.get = get request.user = None view = ForgotPasswordController(request) response = view.reset_password() assert len(response['errors']) == 1