Commit 8a875ccaaa70191274dee9083c78aebb8d7b0159
Committed by
Matheus de Sousa Faria
1 parent
d2f9859b
Exists in
master
and in
4 other branches
Adding test to generation of context data
Signed-off-by: Matheus Faria <matheus.sousa.faria@gmail.com> Signed-off-by: TomazMartins <tomaz.r.martins@gmail.com> Signed-off-by: Luiz Oliveira <ziuloliveira@gmail.com>
Showing
1 changed file
with
46 additions
and
5 deletions
Show diff stats
colab/accounts/tests/test_user_subscription.py
| ... | ... | @@ -3,8 +3,10 @@ Test User Mailing list Subscriptions class. |
| 3 | 3 | Objective: Test parameters, and behavior. |
| 4 | 4 | """ |
| 5 | 5 | |
| 6 | +from mock import patch | |
| 6 | 7 | from colab.accounts.models import User |
| 7 | 8 | from django.test import TestCase, Client |
| 9 | +from colab.accounts.utils import mailman | |
| 8 | 10 | |
| 9 | 11 | |
| 10 | 12 | class UserSubscriptionTest(TestCase): |
| ... | ... | @@ -32,11 +34,13 @@ class UserSubscriptionTest(TestCase): |
| 32 | 34 | |
| 33 | 35 | return user |
| 34 | 36 | |
| 35 | - def authenticate_user(self): | |
| 36 | - self.user.needs_update = False | |
| 37 | - self.user.save() | |
| 38 | - self.client.login(username=self.user.username, | |
| 39 | - password='123colab4') | |
| 37 | + def authenticate_user(self, user=None, password='123colab4'): | |
| 38 | + if not user: | |
| 39 | + user = self.user | |
| 40 | + user.needs_update = False | |
| 41 | + user.save() | |
| 42 | + self.client.login(username=user.username, | |
| 43 | + password=password) | |
| 40 | 44 | |
| 41 | 45 | def test_manage_subscription_logged_in(self): |
| 42 | 46 | self.authenticate_user() |
| ... | ... | @@ -48,3 +52,40 @@ class UserSubscriptionTest(TestCase): |
| 48 | 52 | response = self.client.get("/account/" + self.user.username + |
| 49 | 53 | "/subscriptions") |
| 50 | 54 | self.assertEqual(response.status_code, self.FORBIDDEN_ACCESS) |
| 55 | + | |
| 56 | + @patch.object(mailman, 'all_lists') | |
| 57 | + @patch.object(mailman, 'mailing_lists') | |
| 58 | + def test_context_data_generation(self, mailing_lists, all_lists): | |
| 59 | + data_user = { | |
| 60 | + 'username': 'username1', | |
| 61 | + 'first_name': 'first name1', | |
| 62 | + 'last_name': 'last name1', | |
| 63 | + 'email': 'mail1@mail.com', | |
| 64 | + 'password1': 'safepassword', | |
| 65 | + 'password2': 'safepassword', | |
| 66 | + } | |
| 67 | + self.client.post('/account/register', data=data_user) | |
| 68 | + user1 = User.objects.last() | |
| 69 | + user1.is_active = True | |
| 70 | + user1.save() | |
| 71 | + self.authenticate_user(user1, 'safepassword') | |
| 72 | + | |
| 73 | + mail_lists = [ | |
| 74 | + {"listname": "name_mock_1", "description": "descript_1"}, | |
| 75 | + {"listname": "name_mock_2", "description": "descript_2"}, | |
| 76 | + {"listname": "name_mock_3", "description": "descript_3"}, | |
| 77 | + ] | |
| 78 | + all_lists.return_value = mail_lists | |
| 79 | + | |
| 80 | + my_mail_lists = [ | |
| 81 | + "name_mock_1", | |
| 82 | + "name_mock_3", | |
| 83 | + ] | |
| 84 | + mailing_lists.return_value = my_mail_lists | |
| 85 | + response = self.client.get("/account/" + user1.username + | |
| 86 | + "/subscriptions") | |
| 87 | + self.assertEqual(response.status_code, self.OK) | |
| 88 | + mailresponse = response.context_data['membership'][user1.email] | |
| 89 | + mailresponse = map(lambda x: x[-1], mailresponse) | |
| 90 | + expected_value = [True, False, True] | |
| 91 | + self.assertEqual(mailresponse, expected_value) | ... | ... |