Merge lp:~canonical-isd-hackers/canonical-identity-provider/minor-change-to-email into lp:canonical-identity-provider/release

Proposed by Łukasz Czyżykowski
Status: Merged
Approved by: David Owen
Approved revision: no longer in the source branch.
Merged at revision: 106
Proposed branch: lp:~canonical-isd-hackers/canonical-identity-provider/minor-change-to-email
Merge into: lp:canonical-identity-provider/release
Diff against target: 186 lines (+82/-24)
4 files modified
identityprovider/models/authtoken.py (+3/-3)
identityprovider/templates/ubuntu/email/api-email-validation-token.txt (+15/-0)
identityprovider/tests/test_models_authtoken.py (+53/-9)
identityprovider/webservice/models.py (+11/-12)
To merge this branch: bzr merge lp:~canonical-isd-hackers/canonical-identity-provider/minor-change-to-email
Reviewer Review Type Date Requested Status
David Owen (community) Approve
Review via email: mp+33377@code.launchpad.net

Description of the change

Changed the email which is sent to the user to validate email address.

Added two new tests to make sure that modified Token.sendEmailValidationToken works as expected. The behaviour of the api is checked by existing doctests (api-workflow.txt).

To post a comment you must log in.
Revision history for this message
David Owen (dsowen) wrote :

Nice refactor in the tests.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'identityprovider/models/authtoken.py'
--- identityprovider/models/authtoken.py 2010-07-19 17:34:01 +0000
+++ identityprovider/models/authtoken.py 2010-08-23 12:47:40 +0000
@@ -103,13 +103,13 @@
103 self._send_email(u"%s Email Validator" % current_brand.name, subject,103 self._send_email(u"%s Email Validator" % current_brand.name, subject,
104 message)104 message)
105105
106 def sendEmailValidationToken(self):106 def sendEmailValidationToken(self, template='email-validation-token.txt'):
107 replacements = {107 replacements = {
108 'token': self.token,108 'token': self.token,
109 'toaddress': self.email,109 'toaddress': self.email,
110 }110 }
111 message = render_to_string('%s/email/email-validation-token.txt' %111 template_path = '%s/email/%s' % (current_brand.template_dir, template)
112 current_brand.template_dir, replacements)112 message = render_to_string(template_path, replacements)
113 subject = u"%s: %s" % (current_brand.name,113 subject = u"%s: %s" % (current_brand.name,
114 _("Validate your email address"))114 _("Validate your email address"))
115 self._send_email(u"%s Email Validator" % current_brand.name, subject,115 self._send_email(u"%s Email Validator" % current_brand.name, subject,
116116
=== added file 'identityprovider/templates/ubuntu/email/api-email-validation-token.txt'
--- identityprovider/templates/ubuntu/email/api-email-validation-token.txt 1970-01-01 00:00:00 +0000
+++ identityprovider/templates/ubuntu/email/api-email-validation-token.txt 2010-08-23 12:47:40 +0000
@@ -0,0 +1,15 @@
1{% load i18n %}
2{% blocktrans %}Hello
3
4As a final step of the Ubuntu Single Sign On (SSO) account creation process, please validate the email address {{ toaddress }}. Ubuntu SSO enables convenient access to a variety of Ubuntu-related services like Ubuntu One with the same username and password.
5
6Copy and paste the confirmation code below into the desktop application.
7
8 {{ token }}
9
10If you don't know what this is about, then someone has probably entered your email address by mistake. Sorry about that. You don't need to do anything further. Just delete this message.
11
12Thank you,
13
14The Ubuntu Single Sign On team
15https://login.ubuntu.com/{% endblocktrans %}
016
=== modified file 'identityprovider/tests/test_models_authtoken.py'
--- identityprovider/tests/test_models_authtoken.py 2010-05-20 14:07:52 +0000
+++ identityprovider/tests/test_models_authtoken.py 2010-08-23 12:47:40 +0000
@@ -6,11 +6,25 @@
66
7from identityprovider.tests.utils import BasicAccountTestCase7from identityprovider.tests.utils import BasicAccountTestCase
8from identityprovider.models.account import Account8from identityprovider.models.account import Account
9from identityprovider.models import authtoken
9from identityprovider.models.authtoken import (AuthToken, AuthTokenFactory,10from identityprovider.models.authtoken import (AuthToken, AuthTokenFactory,
10 send_validation_email_request, create_unique_token_for_table, valid_email)11 send_validation_email_request, create_unique_token_for_table, valid_email)
11from identityprovider.models.const import EmailStatus, LoginTokenType12from identityprovider.models.const import EmailStatus, LoginTokenType
1213
1314
15class MockRenderToString(object):
16
17 def __init__(self, return_value=None):
18 self.template = None
19 self.context = None
20 self.return_value = None
21
22 def __call__(self, template, context):
23 self.template = template
24 self.context = context
25 return self.return_value
26
27
14class AuthTokenTestCase(BasicAccountTestCase):28class AuthTokenTestCase(BasicAccountTestCase):
15 def setUp(self):29 def setUp(self):
16 def mock_send_email(self, from_name, subject, message):30 def mock_send_email(self, from_name, subject, message):
@@ -28,9 +42,7 @@
28 super(AuthTokenTestCase, self).tearDown()42 super(AuthTokenTestCase, self).tearDown()
2943
30 def test_sendEmailValidationRequest(self):44 def test_sendEmailValidationRequest(self):
31 account = Account.objects.get_by_email('test@canonical.com')45 token = self._get_new_email_validation_token()
32 token = AuthTokenFactory().new_api_email_validation_token(
33 account, 'email@domain.com')
3446
35 self.assertFalse(hasattr(token, 'message'))47 self.assertFalse(hasattr(token, 'message'))
36 token.sendEmailValidationRequest()48 token.sendEmailValidationRequest()
@@ -38,18 +50,50 @@
38 self.assertTrue(settings.SUPPORT_FORM_URL in token.message)50 self.assertTrue(settings.SUPPORT_FORM_URL in token.message)
3951
40 def test_sendEmailValidationToken(self):52 def test_sendEmailValidationToken(self):
41 account = Account.objects.get_by_email('test@canonical.com')53 token = self._get_new_email_validation_token()
42 token = AuthTokenFactory().new_api_email_validation_token(
43 account, 'email@domain.com')
44 self.assertFalse(hasattr(token, 'message'))54 self.assertFalse(hasattr(token, 'message'))
45 token.sendEmailValidationToken()55 token.sendEmailValidationToken()
46 self.assertTrue(hasattr(token, 'message'))56 self.assertTrue(hasattr(token, 'message'))
47 self.assertTrue(token.token in token.message)57 self.assertTrue(token.token in token.message)
4858
59 def test_sendEmailValidationToken_uses_provided_template(self):
60 token = self._get_new_email_validation_token()
61 mock_rts, rts = self._setup_mock_rendert_to_string()
62
63 token.sendEmailValidationToken('new-template.txt')
64
65 self.assertTrue(mock_rts.template.endswith('new-template.txt'))
66
67 self._teardown_mock_render_to_string(rts)
68
69 def _teardown_mock_render_to_string(self, rts):
70 authtoken.render_to_string = rts
71
72 def _setup_mock_rendert_to_string(self):
73 rts = authtoken.render_to_string
74 mock_rts = MockRenderToString()
75 authtoken.render_to_string = mock_rts
76 return mock_rts, rts
77
78 def _get_new_email_validation_token(self):
79 account = Account.objects.get_by_email('test@canonical.com')
80 token = AuthTokenFactory().new_api_email_validation_token(
81 account, 'email@domain.com')
82 return token
83
84 def test_sendEmailValidationToken_default_template(self):
85 token = self._get_new_email_validation_token()
86 mock_rts, rts = self._setup_mock_rendert_to_string()
87
88 token.sendEmailValidationToken()
89
90 self.assertTrue(mock_rts.template.endswith(
91 'email-validation-token.txt'))
92
93 self._teardown_mock_render_to_string(rts)
94
49 def test_sendNewUserEmail(self):95 def test_sendNewUserEmail(self):
50 account = Account.objects.get_by_email('test@canonical.com')96 token = self._get_new_email_validation_token()
51 token = AuthTokenFactory().new_api_email_validation_token(
52 account, 'email@domain.com')
5397
54 self.assertFalse(hasattr(token, 'message'))98 self.assertFalse(hasattr(token, 'message'))
55 token.sendNewUserEmail()99 token.sendNewUserEmail()
56100
=== modified file 'identityprovider/webservice/models.py'
--- identityprovider/webservice/models.py 2010-08-16 11:55:02 +0000
+++ identityprovider/webservice/models.py 2010-08-23 12:47:40 +0000
@@ -223,32 +223,31 @@
223 for (k, v) in form.errors.items())223 for (k, v) in form.errors.items())
224 result = {'status': 'error', 'errors': errors}224 result = {'status': 'error', 'errors': errors}
225 return result225 return result
226
226 cleaned_data = form.cleaned_data227 cleaned_data = form.cleaned_data
227 requested_email = cleaned_data['email']228 requested_email = cleaned_data['email']
228 emails = EmailAddress.objects.filter(email__iexact=requested_email)229 emails = EmailAddress.objects.filter(email__iexact=requested_email)
229 if len(emails) > 0:230 if len(emails) > 0:
230 return {'status': 'error', 'errors':231 return {'status': 'error', 'errors':
231 {'email': 'Email already registered'}}232 {'email': 'Email already registered'}}
233
232 account = Account.objects.create(234 account = Account.objects.create(
233 creation_rationale=AccountCreationRationale.OWNER_CREATED_LAUNCHPAD,235 creation_rationale=AccountCreationRationale.OWNER_CREATED_LAUNCHPAD,
234 status=AccountStatus.ACTIVE,236 status=AccountStatus.ACTIVE,
235 displayname=''237 displayname='')
236 )238
237 account.emailaddress_set.create(239 account.emailaddress_set.create(
238 email=cleaned_data['email'],240 email=cleaned_data['email'],
239 status=EmailStatus.NEW241 status=EmailStatus.NEW)
240 )242
241 password = encrypt_launchpad_password(243 AccountPassword.objects.create(
242 cleaned_data['password'])244 password=encrypt_launchpad_password(cleaned_data['password']),
243 password_obj = AccountPassword.objects.create(245 account=account)
244 password=password,
245 account=account,
246 )
247 account.accountpassword
248246
249 token = AuthTokenFactory().new_api_email_validation_token(247 token = AuthTokenFactory().new_api_email_validation_token(
250 account, cleaned_data['email'])248 account, cleaned_data['email'])
251 token.sendEmailValidationToken()249
250 token.sendEmailValidationToken('api-email-validation-token.txt')
252251
253 account_created.send(sender=self,252 account_created.send(sender=self,
254 openid_identifier=account.openid_identifier)253 openid_identifier=account.openid_identifier)