From 40401472be111621218f124c34913f407e39e389 Mon Sep 17 00:00:00 2001 From: Holger Frey Date: Sun, 22 Oct 2017 11:50:47 +0200 Subject: [PATCH] added events for user notification --- ordr2/events.py | 7 +++-- ordr2/templates/emails/registration.jinja2 | 24 ++++++++++++++++ tests/events.py | 33 ++++++++++++++++++++++ 3 files changed, 61 insertions(+), 3 deletions(-) create mode 100755 ordr2/templates/emails/registration.jinja2 create mode 100644 tests/events.py diff --git a/ordr2/events.py b/ordr2/events.py index 1be7429..3fb29c1 100644 --- a/ordr2/events.py +++ b/ordr2/events.py @@ -2,6 +2,7 @@ from pyramid.events import subscriber from pyramid.renderers import render +from pyramid_mailer import get_mailer from pyramid_mailer.message import Message @@ -36,7 +37,7 @@ class UserNotification(object): class CompleteRegistration(UserNotification): ''' user notification for account activation ''' - subject='[ordr] Your account was activated' + subject='[ordr] Please verify your email address' template = 'ordr2:templates/emails/registration.jinja2' @@ -63,7 +64,6 @@ class OrderStatusChange(UserNotification): @subscriber(UserNotification) def notify_user(event): ''' notify a user about an event ''' - body = render( event.template, {'user': event.account, 'data': event.data}, @@ -75,4 +75,5 @@ def notify_user(event): recipients=[event.account.email], html=body ) - event.request.mailer.send(message) + mailer = get_mailer(event.request.registry) + mailer.send(message) diff --git a/ordr2/templates/emails/registration.jinja2 b/ordr2/templates/emails/registration.jinja2 new file mode 100755 index 0000000..f50fb91 --- /dev/null +++ b/ordr2/templates/emails/registration.jinja2 @@ -0,0 +1,24 @@ + + + + + [ordr] verify your email address + + + +

Hi there!

+

+ Please verify your email address for the account "{{ user.user_name }}" by following this link + {{ request.resource_url(context, data.hash) }} +

+

+ Regards, +
+ ordr +

+

+ Please don't respont to this email! This is an automatically generated notification by the system. + +

+ + diff --git a/tests/events.py b/tests/events.py new file mode 100644 index 0000000..adce141 --- /dev/null +++ b/tests/events.py @@ -0,0 +1,33 @@ +''' Tests for ordr2.security ''' + +import pytest + +from pyramid.testing import DummyRequest +from pyramid_mailer import get_mailer + +from . import app_config, get_user + + +def test_user_notification_init(): + ''' test creation of user notification events ''' + from ordr2.events import UserNotification + + notification = UserNotification('a', 'b', 'c') + + assert notification.request == 'a' + assert notification.account == 'b' + assert notification.data == 'c' + + +def test_notify_user(app_config): + from ordr2.events import CompleteRegistration, notify_user + + request = DummyRequest() + user = get_user('user') + notification = CompleteRegistration(request, user, {'hash': 'something'}) + notify_user(notification) + mailer = get_mailer(request.registry) + last_mail = mailer.outbox[-1] + + assert 'Please verify your email address ' in last_mail.html + assert 'http://example.com//something' in last_mail.html