Mitja Nikolaus | 7ccc2bc | 2018-09-21 10:55:40 +0200 | [diff] [blame] | 1 | """Tests for the allauth adapters module.""" |
| 2 | |
| 3 | from allauth.account.models import EmailAddress |
| 4 | from allauth.socialaccount.models import SocialLogin, SocialAccount |
| 5 | from django.contrib.auth.models import Group, User |
| 6 | from django.core.exceptions import PermissionDenied |
| 7 | from django.http import HttpRequest |
| 8 | from django.test import TestCase |
| 9 | |
| 10 | from hiccup.allauth_adapters import ( |
| 11 | FairphoneAccountAdapter, |
| 12 | FormAccountAdapter, |
| 13 | FP_STAFF_GROUP_NAME, |
| 14 | ) |
| 15 | from hiccup import settings |
| 16 | |
| 17 | |
| 18 | class Dummy: |
| 19 | """Create dummies class instances for testing.""" |
| 20 | |
Mitja Nikolaus | d6907dd | 2018-10-17 14:13:06 +0200 | [diff] [blame] | 21 | # pylint: disable=too-few-public-methods |
| 22 | |
Mitja Nikolaus | 7ccc2bc | 2018-09-21 10:55:40 +0200 | [diff] [blame] | 23 | EMAIL_FAIRPHONE = "test@fairphone.com" |
| 24 | EMAIL_OTHER = "test@test.com" |
| 25 | |
| 26 | @staticmethod |
| 27 | def create_sociallogin(user_email): |
| 28 | """Create a dummy sociallogin instance. |
| 29 | |
| 30 | Args: |
| 31 | user_email: The email used to login. |
| 32 | |
| 33 | Returns: The sociallogin instance. |
| 34 | |
| 35 | """ |
| 36 | sociallogin = SocialLogin( |
| 37 | user=User(email=user_email), |
| 38 | account=SocialAccount( |
| 39 | provider=next(iter(settings.SOCIALACCOUNT_PROVIDERS)) |
| 40 | ), |
| 41 | email_addresses=[EmailAddress(email=user_email, verified=True)], |
| 42 | ) |
| 43 | return sociallogin |
| 44 | |
Mitja Nikolaus | 7ccc2bc | 2018-09-21 10:55:40 +0200 | [diff] [blame] | 45 | |
| 46 | class FairphoneAccountAdapterTests(TestCase): |
| 47 | """Tests for the Fairphone account adapter.""" |
| 48 | |
| 49 | def setUp(self): |
| 50 | """Create a Fairphone account adapter instance.""" |
| 51 | self.fp_account_adapter = FairphoneAccountAdapter() |
| 52 | |
| 53 | def test_is_open_for_signup(self): |
| 54 | """Validate that the adapter is open for signup.""" |
| 55 | request = HttpRequest() |
| 56 | sociallogin = Dummy.create_sociallogin(Dummy.EMAIL_FAIRPHONE) |
| 57 | |
| 58 | self.assertTrue( |
| 59 | self.fp_account_adapter.is_open_for_signup(request, sociallogin) |
| 60 | ) |
| 61 | |
| 62 | def _save_user(self, user_email): |
| 63 | request = HttpRequest() |
| 64 | request.session = self.client.session |
| 65 | |
| 66 | sociallogin = Dummy.create_sociallogin(user_email) |
| 67 | |
| 68 | return self.fp_account_adapter.save_user(request, sociallogin) |
| 69 | |
| 70 | def test_save_user_with_fp_email(self): |
| 71 | """Test saving a user with a Fairphone E-Mail address.""" |
Mitja Nikolaus | d6907dd | 2018-10-17 14:13:06 +0200 | [diff] [blame] | 72 | # Get the Fairphone staff group |
| 73 | fp_staff_group = Group.objects.get(name=FP_STAFF_GROUP_NAME) |
Mitja Nikolaus | 7ccc2bc | 2018-09-21 10:55:40 +0200 | [diff] [blame] | 74 | |
| 75 | # Save a user with a Fairphone E-Mail address |
| 76 | user = self._save_user(Dummy.EMAIL_FAIRPHONE) |
| 77 | |
| 78 | # Assert that the user has been added to the Fairphone staff group |
| 79 | self.assertIn(fp_staff_group, user.groups.all()) |
| 80 | |
| 81 | def test_save_user_without_fp_email(self): |
| 82 | """Test saving a user without a Fairphone E-Mail address.""" |
Mitja Nikolaus | d6907dd | 2018-10-17 14:13:06 +0200 | [diff] [blame] | 83 | # Get the Fairphone staff group |
| 84 | fp_staff_group = Group.objects.get(name=FP_STAFF_GROUP_NAME) |
Mitja Nikolaus | 7ccc2bc | 2018-09-21 10:55:40 +0200 | [diff] [blame] | 85 | |
| 86 | # Save a user without a Fairphone E-Mail address |
| 87 | user = self._save_user(Dummy.EMAIL_OTHER) |
| 88 | |
| 89 | # Assert that the user has not been added to the Fairphone staff group |
| 90 | self.assertNotIn(fp_staff_group, user.groups.all()) |
| 91 | |
| 92 | def _populate_user(self, user_email): |
| 93 | request = HttpRequest() |
| 94 | sociallogin = Dummy.create_sociallogin(user_email) |
| 95 | data = {"email": user_email} |
| 96 | |
| 97 | return self.fp_account_adapter.populate_user(request, sociallogin, data) |
| 98 | |
| 99 | def test_populate_user_with_fp_email(self): |
| 100 | """Test populating a user with a Fairphone E-Mail address.""" |
| 101 | user = self._populate_user(Dummy.EMAIL_FAIRPHONE) |
| 102 | |
| 103 | # Assert that the user has been populated successfully and its email |
| 104 | # address is correct |
| 105 | self.assertEqual(user.email, Dummy.EMAIL_FAIRPHONE) |
| 106 | |
| 107 | def test_populate_user_without_fp_email(self): |
| 108 | """Test populating a user without a Fairphone E-Mail address.""" |
| 109 | user_email = Dummy.EMAIL_OTHER |
| 110 | |
| 111 | # Assert that a permission denied error is raised when attempting to |
| 112 | # populate the user |
| 113 | with self.assertRaises(PermissionDenied): |
| 114 | self._populate_user(user_email) |
| 115 | |
| 116 | |
| 117 | class FormAccountAdapterTests(TestCase): |
| 118 | """Tests for the form account adapter.""" |
| 119 | |
| 120 | def setUp(self): |
| 121 | """Create a form account adapter instance.""" |
| 122 | self.form_account_adapter = FormAccountAdapter() |
| 123 | |
| 124 | def test_is_not_open_for_signup(self): |
| 125 | """Validate that the adapter is not open for signup.""" |
| 126 | request = HttpRequest() |
| 127 | self.assertFalse(self.form_account_adapter.is_open_for_signup(request)) |