Commit 1910805860ede783e8cbadeab073e15f163362cd
1 parent
0df355e5
Exists in
master
and in
4 other branches
Testing email validation view
Signed-off-by: Lucas Moura <lucas.moura128@gmail.com> Signed-off-by: Matheus Faria <matheus.sousa.faria@gmail.com>
Showing
3 changed files
with
173 additions
and
2 deletions
Show diff stats
| ... | ... | @@ -0,0 +1,100 @@ |
| 1 | +[ | |
| 2 | + { | |
| 3 | + "fields": { | |
| 4 | + "last_name": "Administrator", | |
| 5 | + "webpage": "", | |
| 6 | + "twitter": "", | |
| 7 | + "is_staff": true, | |
| 8 | + "user_permissions": [], | |
| 9 | + "date_joined": "2015-01-28T12:34:58.770Z", | |
| 10 | + "google_talk": "", | |
| 11 | + "first_name": "Admin", | |
| 12 | + "is_superuser": true, | |
| 13 | + "last_login": "2015-01-28T12:35:39.621Z", | |
| 14 | + "verification_hash": null, | |
| 15 | + "role": "", | |
| 16 | + "email": "admin@mail.com", | |
| 17 | + "username": "admin", | |
| 18 | + "bio": "", | |
| 19 | + "needs_update": true, | |
| 20 | + "is_active": true, | |
| 21 | + "facebook": "", | |
| 22 | + "groups": [], | |
| 23 | + "password": "pbkdf2_sha256$12000$iiKCMnLZnFJw$UTx89LB8oYTiw9UqkcglzFLmIaZtbr+ZzF1cG3vfcyo=", | |
| 24 | + "institution": "", | |
| 25 | + "github": "", | |
| 26 | + "modified": "2015-01-28T12:45:27.375Z" | |
| 27 | + }, | |
| 28 | + "model": "accounts.user", | |
| 29 | + "pk": 1 | |
| 30 | + }, | |
| 31 | + { | |
| 32 | + "fields": { | |
| 33 | + "last_name": "Norris", | |
| 34 | + "webpage": "", | |
| 35 | + "twitter": "", | |
| 36 | + "is_staff": true, | |
| 37 | + "user_permissions": [], | |
| 38 | + "date_joined": "2015-01-28T12:34:58.770Z", | |
| 39 | + "google_talk": "", | |
| 40 | + "first_name": "Chuck", | |
| 41 | + "is_superuser": true, | |
| 42 | + "last_login": "2015-01-28T12:35:39.621Z", | |
| 43 | + "verification_hash": null, | |
| 44 | + "role": "", | |
| 45 | + "email": "chucknorris@mail.com", | |
| 46 | + "username": "chucknorris", | |
| 47 | + "bio": "", | |
| 48 | + "needs_update": true, | |
| 49 | + "is_active": true, | |
| 50 | + "facebook": "", | |
| 51 | + "groups": [], | |
| 52 | + "password": "123colab4", | |
| 53 | + "institution": "", | |
| 54 | + "github": "", | |
| 55 | + "modified": "2015-01-28T12:45:27.375Z" | |
| 56 | + }, | |
| 57 | + "model": "accounts.user", | |
| 58 | + "pk": 2 | |
| 59 | + }, | |
| 60 | + | |
| 61 | + { | |
| 62 | + "fields": { | |
| 63 | + "last_name": "Norris", | |
| 64 | + "webpage": "", | |
| 65 | + "twitter": "", | |
| 66 | + "is_staff": true, | |
| 67 | + "user_permissions": [], | |
| 68 | + "date_joined": "2015-01-28T12:34:58.770Z", | |
| 69 | + "google_talk": "", | |
| 70 | + "first_name": "Heisenberg", | |
| 71 | + "is_superuser": true, | |
| 72 | + "last_login": "2015-01-28T12:35:39.621Z", | |
| 73 | + "verification_hash": null, | |
| 74 | + "role": "", | |
| 75 | + "email": "heisenberg@mail.com", | |
| 76 | + "username": "heisenbergnorris", | |
| 77 | + "bio": "", | |
| 78 | + "needs_update": true, | |
| 79 | + "is_active": true, | |
| 80 | + "facebook": "", | |
| 81 | + "groups": [], | |
| 82 | + "password": "pbkdf2_sha256$12000$iiKCMnLZnFJw$UTx89LB8oYTiw9UqkcglzFLmIaZtbr+ZzF1cG3vfcyo=", | |
| 83 | + "institution": "", | |
| 84 | + "github": "", | |
| 85 | + "modified": "2015-01-28T12:45:27.375Z" | |
| 86 | + }, | |
| 87 | + "model": "accounts.user", | |
| 88 | + "pk": 3 | |
| 89 | + }, | |
| 90 | + { | |
| 91 | + "fields": { | |
| 92 | + "real_name": "Administrator", | |
| 93 | + "user": 1, | |
| 94 | + "md5": "edb0e96701c209ab4b50211c856c50c4", | |
| 95 | + "address": "admin@mail.com" | |
| 96 | + }, | |
| 97 | + "model": "super_archives.emailaddress", | |
| 98 | + "pk": 1 | |
| 99 | + } | |
| 100 | +] | ... | ... |
| ... | ... | @@ -0,0 +1,71 @@ |
| 1 | +# -*- coding:utf-8 -*- | |
| 2 | + | |
| 3 | +from mock import patch | |
| 4 | + | |
| 5 | +from colab.accounts.models import User | |
| 6 | +from django.test import TestCase, Client | |
| 7 | +from colab.super_archives.models import EmailAddressValidation, EmailAddress | |
| 8 | + | |
| 9 | + | |
| 10 | +class EmailValidationTest(TestCase): | |
| 11 | + | |
| 12 | + fixtures = ['test_user.json'] | |
| 13 | + | |
| 14 | + def setUp(self): | |
| 15 | + self.client = Client() | |
| 16 | + | |
| 17 | + def authenticate_user(self): | |
| 18 | + self.client.login(username='chucknorris', password='123colab4') | |
| 19 | + | |
| 20 | + @patch('colab.super_archives.views.send_verification_email', | |
| 21 | + return_value="") | |
| 22 | + def test_send_verification_email_successfuly(self, | |
| 23 | + mock_send_verification_email): | |
| 24 | + user = User.objects.get(username='chucknorris') | |
| 25 | + | |
| 26 | + EmailAddressValidation.create(user.email, user) | |
| 27 | + | |
| 28 | + email_addr, created = EmailAddress.objects.get_or_create( | |
| 29 | + address=user.email) | |
| 30 | + email_addr.user = user | |
| 31 | + email_addr.save() | |
| 32 | + | |
| 33 | + self.authenticate_user() | |
| 34 | + | |
| 35 | + response = self.client.post('/archives/manage/email/validate/', | |
| 36 | + {'user': user.id, 'email': user.email}) | |
| 37 | + | |
| 38 | + self.assertEqual(response.status_code, 204) | |
| 39 | + self.assertTrue(mock_send_verification_email.called) | |
| 40 | + | |
| 41 | + @patch('colab.super_archives.views.send_verification_email', | |
| 42 | + return_value="") | |
| 43 | + def test_send_verification_email_with_not_verified_email( | |
| 44 | + self, send_verification_email): | |
| 45 | + self.authenticate_user() | |
| 46 | + | |
| 47 | + user = User.objects.get(username='chucknorris') | |
| 48 | + | |
| 49 | + response = self.client.post('/archives/manage/email/validate/', | |
| 50 | + { | |
| 51 | + 'user': user.id, | |
| 52 | + 'email': "email@mail.com", | |
| 53 | + }) | |
| 54 | + | |
| 55 | + self.assertEqual(response.status_code, 404) | |
| 56 | + self.assertFalse(send_verification_email.called) | |
| 57 | + | |
| 58 | + @patch('colab.super_archives.views.send_verification_email', | |
| 59 | + return_value="") | |
| 60 | + def test_send_verification_email_with_not_valid_user_id( | |
| 61 | + self, send_verification_email): | |
| 62 | + self.authenticate_user() | |
| 63 | + | |
| 64 | + user = User.objects.get(username='chucknorris') | |
| 65 | + | |
| 66 | + response = self.client.post('/archives/manage/email/validate/', | |
| 67 | + {'user': len(User.objects.all()) + 1, | |
| 68 | + 'email': user.email}) | |
| 69 | + | |
| 70 | + self.assertEqual(response.status_code, 404) | |
| 71 | + self.assertFalse(send_verification_email.called) | ... | ... |
colab/super_archives/views.py
| ... | ... | @@ -277,14 +277,14 @@ class EmailValidationView(View): |
| 277 | 277 | try: |
| 278 | 278 | email = EmailAddressValidation.objects.get(address=email_addr, |
| 279 | 279 | user_id=user_id) |
| 280 | - except http.DoesNotExist: | |
| 280 | + except EmailAddressValidation.DoesNotExist: | |
| 281 | 281 | raise http.Http404 |
| 282 | 282 | |
| 283 | 283 | try: |
| 284 | 284 | location = reverse('archive_email_view', |
| 285 | 285 | kwargs={'key': email.validation_key}) |
| 286 | 286 | verification_url = request.build_absolute_uri(location) |
| 287 | - send_verification_email(request, email_addr, email.user, | |
| 287 | + send_verification_email(email_addr, email.user, | |
| 288 | 288 | email.validation_key, verification_url) |
| 289 | 289 | except smtplib.SMTPException: |
| 290 | 290 | logging.exception('Error sending validation email') | ... | ... |