From 8182a33c6660c01dce5f32eeedf71f4d23328651 Mon Sep 17 00:00:00 2001 From: Mateusz Sikora Date: Fri, 27 Feb 2015 13:56:29 +0100 Subject: [PATCH] #23 - add test case to cover solcial auth for existing user --- rest_auth/test_urls.py | 3 +- rest_auth/tests.py | 92 +++++++++++++++++++++++++++++++++--------- rest_auth/views.py | 1 - 3 files changed, 75 insertions(+), 21 deletions(-) diff --git a/rest_auth/test_urls.py b/rest_auth/test_urls.py index 2a321cd..02cbab5 100644 --- a/rest_auth/test_urls.py +++ b/rest_auth/test_urls.py @@ -18,5 +18,6 @@ urlpatterns += patterns('', name='account_email_verification_sent'), url(r'^account-confirm-email/(?P\w+)/$', TemplateView.as_view(), name='account_confirm_email'), - url(r'^social-login/facebook/$', FacebookLogin.as_view(), name='fb_login') + url(r'^social-login/facebook/$', FacebookLogin.as_view(), name='fb_login'), + url(r'^accounts/', include('allauth.socialaccount.urls')) ) diff --git a/rest_auth/tests.py b/rest_auth/tests.py index b21e0b7..96be32a 100644 --- a/rest_auth/tests.py +++ b/rest_auth/tests.py @@ -108,6 +108,25 @@ class BaseAPITestCase(object): settings.DEBUG = True self.client = APIClient() + self.login_url = reverse('rest_login') + self.logout_url = reverse('rest_logout') + self.password_change_url = reverse('rest_password_change') + self.register_url = reverse('rest_register') + self.password_reset_url = reverse('rest_password_reset') + self.user_url = reverse('rest_user_details') + self.veirfy_email_url = reverse('rest_verify_email') + self.fb_login_url = reverse('fb_login') + + def _login(self): + payload = { + "username": self.USERNAME, + "password": self.PASS + } + self.post(self.login_url, data=payload, status_code=status.HTTP_200_OK) + + def _logout(self): + self.post(self.logout_url, status=status.HTTP_200_OK) + # ----------------------- # T E S T H E R E @@ -149,23 +168,6 @@ class APITestCase1(TestCase, BaseAPITestCase): def setUp(self): self.init() - self.login_url = reverse('rest_login') - self.logout_url = reverse('rest_logout') - self.password_change_url = reverse('rest_password_change') - self.register_url = reverse('rest_register') - self.password_reset_url = reverse('rest_password_reset') - self.user_url = reverse('rest_user_details') - self.veirfy_email_url = reverse('rest_verify_email') - - def _login(self): - payload = { - "username": self.USERNAME, - "password": self.PASS - } - self.post(self.login_url, data=payload, status_code=status.HTTP_200_OK) - - def _logout(self): - self.post(self.logout_url, status=status.HTTP_200_OK) def _generate_uid_and_token(self, user): result = {} @@ -282,7 +284,6 @@ class APITestCase1(TestCase, BaseAPITestCase): login_payload['password'] = new_password_payload['new_password1'] self.post(self.login_url, data=login_payload, status_code=200) - def test_password_reset(self): user = User.objects.create_user(self.USERNAME, self.EMAIL, self.PASS) @@ -410,7 +411,18 @@ class TestSocialAuth(TestCase, BaseAPITestCase): urls = 'rest_auth.test_urls' + USERNAME = 'person' + PASS = 'person' + EMAIL = "person1@world.com" + REGISTRATION_DATA = { + "username": USERNAME, + "password1": PASS, + "password2": PASS, + "email": EMAIL + } + def setUp(self): + self.init() social_app = SocialApp.objects.create( provider='facebook', @@ -420,7 +432,6 @@ class TestSocialAuth(TestCase, BaseAPITestCase): ) site = Site.objects.get_current() social_app.sites.add(site) - self.fb_login_url = reverse('fb_login') self.graph_api_url = GRAPH_API_URL + '/me' @responses.activate @@ -441,6 +452,49 @@ class TestSocialAuth(TestCase, BaseAPITestCase): responses.add(responses.GET, self.graph_api_url, body=resp_body, status=200, content_type='application/json') + users_count = User.objects.all().count() + payload = { + 'access_token': 'abc123' + } + + self.post(self.fb_login_url, data=payload, status_code=200) + self.assertIn('key', self.response.json.keys()) + self.assertEqual(User.objects.all().count(), users_count + 1) + + # make sure that second request will not create a new user + self.post(self.fb_login_url, data=payload, status_code=200) + self.assertIn('key', self.response.json.keys()) + self.assertEqual(User.objects.all().count(), users_count + 1) + + @responses.activate + @override_settings( + ACCOUNT_EMAIL_VERIFICATION='mandatory', + ACCOUNT_EMAIL_REQUIRED=True, + REST_SESSION_LOGIN=False + ) + def teste_edge_case(self): + resp_body = '{"id":"123123123123","first_name":"John","gender":"male","last_name":"Smith","link":"https:\\/\\/www.facebook.com\\/john.smith","locale":"en_US","name":"John Smith","timezone":2,"updated_time":"2014-08-13T10:14:38+0000","username":"john.smith","verified":true,"email":"%s"}' + responses.add(responses.GET, self.graph_api_url, + body=resp_body % self.EMAIL, + status=200, content_type='application/json') + + # test empty payload + self.post(self.register_url, data={}, status_code=400) + + self.post(self.register_url, data=self.REGISTRATION_DATA, + status_code=201) + new_user = get_user_model().objects.latest('id') + self.assertEqual(new_user.username, self.REGISTRATION_DATA['username']) + + # veirfy email + email_confirmation = new_user.emailaddress_set.get(email=self.EMAIL)\ + .emailconfirmation_set.order_by('-created')[0] + self.post(self.veirfy_email_url, data={"key": email_confirmation.key}, + status_code=status.HTTP_200_OK) + + self._login() + self._logout() + payload = { 'access_token': 'abc123' } diff --git a/rest_auth/views.py b/rest_auth/views.py index 38430a8..05217cf 100644 --- a/rest_auth/views.py +++ b/rest_auth/views.py @@ -31,7 +31,6 @@ class Login(GenericAPIView): response_serializer = TokenSerializer def login(self): - # import ipdb; ipdb.set_trace() self.user = self.serializer.validated_data['user'] self.token, created = self.token_model.objects.get_or_create( user=self.user)