#23 - add test case to cover solcial auth for existing user

This commit is contained in:
Mateusz Sikora 2015-02-27 13:56:29 +01:00
parent 4e24b2a842
commit 8182a33c66
3 changed files with 75 additions and 21 deletions

View File

@ -18,5 +18,6 @@ urlpatterns += patterns('',
name='account_email_verification_sent'), name='account_email_verification_sent'),
url(r'^account-confirm-email/(?P<key>\w+)/$', TemplateView.as_view(), url(r'^account-confirm-email/(?P<key>\w+)/$', TemplateView.as_view(),
name='account_confirm_email'), 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'))
) )

View File

@ -108,6 +108,25 @@ class BaseAPITestCase(object):
settings.DEBUG = True settings.DEBUG = True
self.client = APIClient() 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 # T E S T H E R E
@ -149,23 +168,6 @@ class APITestCase1(TestCase, BaseAPITestCase):
def setUp(self): def setUp(self):
self.init() 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): def _generate_uid_and_token(self, user):
result = {} result = {}
@ -282,7 +284,6 @@ class APITestCase1(TestCase, BaseAPITestCase):
login_payload['password'] = new_password_payload['new_password1'] login_payload['password'] = new_password_payload['new_password1']
self.post(self.login_url, data=login_payload, status_code=200) self.post(self.login_url, data=login_payload, status_code=200)
def test_password_reset(self): def test_password_reset(self):
user = User.objects.create_user(self.USERNAME, self.EMAIL, self.PASS) user = User.objects.create_user(self.USERNAME, self.EMAIL, self.PASS)
@ -410,7 +411,18 @@ class TestSocialAuth(TestCase, BaseAPITestCase):
urls = 'rest_auth.test_urls' 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): def setUp(self):
self.init()
social_app = SocialApp.objects.create( social_app = SocialApp.objects.create(
provider='facebook', provider='facebook',
@ -420,7 +432,6 @@ class TestSocialAuth(TestCase, BaseAPITestCase):
) )
site = Site.objects.get_current() site = Site.objects.get_current()
social_app.sites.add(site) social_app.sites.add(site)
self.fb_login_url = reverse('fb_login')
self.graph_api_url = GRAPH_API_URL + '/me' self.graph_api_url = GRAPH_API_URL + '/me'
@responses.activate @responses.activate
@ -441,6 +452,49 @@ class TestSocialAuth(TestCase, BaseAPITestCase):
responses.add(responses.GET, self.graph_api_url, body=resp_body, responses.add(responses.GET, self.graph_api_url, body=resp_body,
status=200, content_type='application/json') 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 = { payload = {
'access_token': 'abc123' 'access_token': 'abc123'
} }

View File

@ -31,7 +31,6 @@ class Login(GenericAPIView):
response_serializer = TokenSerializer response_serializer = TokenSerializer
def login(self): def login(self):
# import ipdb; ipdb.set_trace()
self.user = self.serializer.validated_data['user'] self.user = self.serializer.validated_data['user']
self.token, created = self.token_model.objects.get_or_create( self.token, created = self.token_model.objects.get_or_create(
user=self.user) user=self.user)