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
1=== modified file 'identityprovider/models/authtoken.py'
2--- identityprovider/models/authtoken.py 2010-07-19 17:34:01 +0000
3+++ identityprovider/models/authtoken.py 2010-08-23 12:47:40 +0000
4@@ -103,13 +103,13 @@
5 self._send_email(u"%s Email Validator" % current_brand.name, subject,
6 message)
7
8- def sendEmailValidationToken(self):
9+ def sendEmailValidationToken(self, template='email-validation-token.txt'):
10 replacements = {
11 'token': self.token,
12 'toaddress': self.email,
13 }
14- message = render_to_string('%s/email/email-validation-token.txt' %
15- current_brand.template_dir, replacements)
16+ template_path = '%s/email/%s' % (current_brand.template_dir, template)
17+ message = render_to_string(template_path, replacements)
18 subject = u"%s: %s" % (current_brand.name,
19 _("Validate your email address"))
20 self._send_email(u"%s Email Validator" % current_brand.name, subject,
21
22=== added file 'identityprovider/templates/ubuntu/email/api-email-validation-token.txt'
23--- identityprovider/templates/ubuntu/email/api-email-validation-token.txt 1970-01-01 00:00:00 +0000
24+++ identityprovider/templates/ubuntu/email/api-email-validation-token.txt 2010-08-23 12:47:40 +0000
25@@ -0,0 +1,15 @@
26+{% load i18n %}
27+{% blocktrans %}Hello
28+
29+As 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.
30+
31+Copy and paste the confirmation code below into the desktop application.
32+
33+ {{ token }}
34+
35+If 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.
36+
37+Thank you,
38+
39+The Ubuntu Single Sign On team
40+https://login.ubuntu.com/{% endblocktrans %}
41
42=== modified file 'identityprovider/tests/test_models_authtoken.py'
43--- identityprovider/tests/test_models_authtoken.py 2010-05-20 14:07:52 +0000
44+++ identityprovider/tests/test_models_authtoken.py 2010-08-23 12:47:40 +0000
45@@ -6,11 +6,25 @@
46
47 from identityprovider.tests.utils import BasicAccountTestCase
48 from identityprovider.models.account import Account
49+from identityprovider.models import authtoken
50 from identityprovider.models.authtoken import (AuthToken, AuthTokenFactory,
51 send_validation_email_request, create_unique_token_for_table, valid_email)
52 from identityprovider.models.const import EmailStatus, LoginTokenType
53
54
55+class MockRenderToString(object):
56+
57+ def __init__(self, return_value=None):
58+ self.template = None
59+ self.context = None
60+ self.return_value = None
61+
62+ def __call__(self, template, context):
63+ self.template = template
64+ self.context = context
65+ return self.return_value
66+
67+
68 class AuthTokenTestCase(BasicAccountTestCase):
69 def setUp(self):
70 def mock_send_email(self, from_name, subject, message):
71@@ -28,9 +42,7 @@
72 super(AuthTokenTestCase, self).tearDown()
73
74 def test_sendEmailValidationRequest(self):
75- account = Account.objects.get_by_email('test@canonical.com')
76- token = AuthTokenFactory().new_api_email_validation_token(
77- account, 'email@domain.com')
78+ token = self._get_new_email_validation_token()
79
80 self.assertFalse(hasattr(token, 'message'))
81 token.sendEmailValidationRequest()
82@@ -38,18 +50,50 @@
83 self.assertTrue(settings.SUPPORT_FORM_URL in token.message)
84
85 def test_sendEmailValidationToken(self):
86- account = Account.objects.get_by_email('test@canonical.com')
87- token = AuthTokenFactory().new_api_email_validation_token(
88- account, 'email@domain.com')
89+ token = self._get_new_email_validation_token()
90 self.assertFalse(hasattr(token, 'message'))
91 token.sendEmailValidationToken()
92 self.assertTrue(hasattr(token, 'message'))
93 self.assertTrue(token.token in token.message)
94
95+ def test_sendEmailValidationToken_uses_provided_template(self):
96+ token = self._get_new_email_validation_token()
97+ mock_rts, rts = self._setup_mock_rendert_to_string()
98+
99+ token.sendEmailValidationToken('new-template.txt')
100+
101+ self.assertTrue(mock_rts.template.endswith('new-template.txt'))
102+
103+ self._teardown_mock_render_to_string(rts)
104+
105+ def _teardown_mock_render_to_string(self, rts):
106+ authtoken.render_to_string = rts
107+
108+ def _setup_mock_rendert_to_string(self):
109+ rts = authtoken.render_to_string
110+ mock_rts = MockRenderToString()
111+ authtoken.render_to_string = mock_rts
112+ return mock_rts, rts
113+
114+ def _get_new_email_validation_token(self):
115+ account = Account.objects.get_by_email('test@canonical.com')
116+ token = AuthTokenFactory().new_api_email_validation_token(
117+ account, 'email@domain.com')
118+ return token
119+
120+ def test_sendEmailValidationToken_default_template(self):
121+ token = self._get_new_email_validation_token()
122+ mock_rts, rts = self._setup_mock_rendert_to_string()
123+
124+ token.sendEmailValidationToken()
125+
126+ self.assertTrue(mock_rts.template.endswith(
127+ 'email-validation-token.txt'))
128+
129+ self._teardown_mock_render_to_string(rts)
130+
131 def test_sendNewUserEmail(self):
132- account = Account.objects.get_by_email('test@canonical.com')
133- token = AuthTokenFactory().new_api_email_validation_token(
134- account, 'email@domain.com')
135+ token = self._get_new_email_validation_token()
136
137 self.assertFalse(hasattr(token, 'message'))
138 token.sendNewUserEmail()
139
140=== modified file 'identityprovider/webservice/models.py'
141--- identityprovider/webservice/models.py 2010-08-16 11:55:02 +0000
142+++ identityprovider/webservice/models.py 2010-08-23 12:47:40 +0000
143@@ -223,32 +223,31 @@
144 for (k, v) in form.errors.items())
145 result = {'status': 'error', 'errors': errors}
146 return result
147+
148 cleaned_data = form.cleaned_data
149 requested_email = cleaned_data['email']
150 emails = EmailAddress.objects.filter(email__iexact=requested_email)
151 if len(emails) > 0:
152 return {'status': 'error', 'errors':
153 {'email': 'Email already registered'}}
154+
155 account = Account.objects.create(
156 creation_rationale=AccountCreationRationale.OWNER_CREATED_LAUNCHPAD,
157 status=AccountStatus.ACTIVE,
158- displayname=''
159- )
160+ displayname='')
161+
162 account.emailaddress_set.create(
163 email=cleaned_data['email'],
164- status=EmailStatus.NEW
165- )
166- password = encrypt_launchpad_password(
167- cleaned_data['password'])
168- password_obj = AccountPassword.objects.create(
169- password=password,
170- account=account,
171- )
172- account.accountpassword
173+ status=EmailStatus.NEW)
174+
175+ AccountPassword.objects.create(
176+ password=encrypt_launchpad_password(cleaned_data['password']),
177+ account=account)
178
179 token = AuthTokenFactory().new_api_email_validation_token(
180 account, cleaned_data['email'])
181- token.sendEmailValidationToken()
182+
183+ token.sendEmailValidationToken('api-email-validation-token.txt')
184
185 account_created.send(sender=self,
186 openid_identifier=account.openid_identifier)