def test_login(self): resp = self.client.post(reverse(views.login), dict(openid='http://me.yahoo.com')) assert 'login.yahooapis' in resp['location'] with patch('allauth.socialaccount.providers' '.openid.views._openid_consumer') as consumer_mock: client = Mock() complete = Mock() consumer_mock.return_value = client client.complete = complete complete_response = Mock() complete.return_value = complete_response complete_response.status = consumer.SUCCESS complete_response.identity_url = 'http://dummy/john/' with patch('allauth.socialaccount.providers' '.openid.utils.SRegResponse') as sr_mock: with patch('allauth.socialaccount.providers' '.openid.utils.FetchResponse') as fr_mock: sreg_mock = Mock() ax_mock = Mock() sr_mock.fromSuccessResponse = sreg_mock fr_mock.fromSuccessResponse = ax_mock sreg_mock.return_value = {} ax_mock.return_value = {AXAttribute.PERSON_FIRST_NAME: ['raymond']} resp = self.client.post(reverse('openid_callback')) self.assertRedirects( resp, "/accounts/profile/", fetch_redirect_response=False ) get_user_model().objects.get(first_name='raymond')
def test_login(self): # Location: https://s.yimg.com/wm/mbr/html/openid-eol-0.0.1.html resp = self.client.post(reverse(views.login), dict(openid='http://me.yahoo.com')) assert 'login.yahooapis' in resp['location'] with patch('allauth.socialaccount.providers' '.openid.views._openid_consumer') as consumer_mock: client = Mock() complete = Mock() consumer_mock.return_value = client client.complete = complete complete_response = Mock() complete.return_value = complete_response complete_response.status = consumer.SUCCESS complete_response.identity_url = 'http://dummy/john/' with patch('allauth.socialaccount.providers' '.openid.utils.SRegResponse') as sr_mock: with patch('allauth.socialaccount.providers' '.openid.utils.FetchResponse') as fr_mock: sreg_mock = Mock() ax_mock = Mock() sr_mock.fromSuccessResponse = sreg_mock fr_mock.fromSuccessResponse = ax_mock sreg_mock.return_value = {} ax_mock.return_value = { AXAttribute.PERSON_FIRST_NAME: ['raymond'] } resp = self.client.post(reverse('openid_callback')) self.assertRedirects(resp, "/accounts/profile/", fetch_redirect_response=False) get_user_model().objects.get(first_name='raymond')
def test_login_by_token(self): resp = self.client.get(reverse("account_login")) with patch("allauth.socialaccount.providers.facebook.views" ".requests") as requests_mock: mocks = [self.get_mocked_response().json()] requests_mock.get.return_value.json = lambda: mocks.pop() resp = self.client.post(reverse("facebook_login_by_token"), data={"access_token": "dummy"}) self.assertRedirects(resp, "http://testserver/accounts/profile/", fetch_redirect_response=False)
def test_ynab_compelete_login_401(self): from allauth.socialaccount.providers.ynab.views import ( YNABOAuth2Adapter, ) class LessMockedResponse(MockedResponse): def raise_for_status(self): if self.status_code != 200: raise HTTPError(None) request = RequestFactory().get(reverse(self.provider.id + "_login"), dict(process="login")) adapter = YNABOAuth2Adapter(request) app = adapter.get_provider().get_app(request) token = SocialToken(token="some_token") response_with_401 = LessMockedResponse( 401, """ {"error": { "errors": [{ "domain": "global", "reason": "authError", "message": "Invalid Credentials", "locationType": "header", "location": "Authorization" } ], "code": 401, "message": "Invalid Credentials" } }""", ) with patch("allauth.socialaccount.providers.ynab.views.requests" ) as patched_requests: patched_requests.get.return_value = response_with_401 with self.assertRaises(HTTPError): adapter.complete_login(request, app, token)
def test_login_on_confirm_uuid_user(self, mocked_gum, mock_perform_login): user = UUIDUser( is_active=True, email='*****@*****.**', username='******') # fake post-signup account_user stash session = self.client.session session['account_user'] = user_pk_to_url_str(user) session.save() # fake email and email confirmation to avoid swappable model hell email = Mock(verified=False, user=user) key = 'mockkey' confirmation = Mock(autospec=EmailConfirmationHMAC, key=key) confirmation.email_address = email confirmation.from_key.return_value = confirmation mock_perform_login.return_value = HttpResponseRedirect(redirect_to='/') with patch('allauth.account.views.EmailConfirmationHMAC', confirmation): self.client.post( reverse('account_confirm_email', args=[key])) assert mock_perform_login.called
def test_google_compelete_login_401(self): class LessMockedResponse(MockedResponse): def raise_for_status(self): if self.status_code != 200: raise HTTPError(None) request = RequestFactory().get(reverse(self.provider.id + '_login'), dict(process='login')) app = self.provider.get_app(request) token = SocialToken(token='some_token') response_with_401 = LessMockedResponse( 401, """ {"error": { "errors": [{ "domain": "global", "reason": "authError", "message": "Invalid Credentials", "locationType": "header", "location": "Authorization" } ], "code": 401, "message": "Invalid Credentials" } }""") with patch('allauth.socialaccount.providers.common.google.provider' '.requests') as patched_requests: patched_requests.get.return_value = response_with_401 with self.assertRaises(HTTPError): self.provider.complete_login(request, app, token)
def test_google_compelete_login_401(self): from allauth.socialaccount.providers.google.views import \ GoogleOAuth2Adapter class LessMockedResponse(MockedResponse): def raise_for_status(self): if self.status_code != 200: raise HTTPError(None) request = RequestFactory().get( reverse(self.provider.id + '_login'), dict(process='login')) adapter = GoogleOAuth2Adapter() app = adapter.get_provider().get_app(request) token = SocialToken(token='some_token') response_with_401 = LessMockedResponse( 401, """ {"error": { "errors": [{ "domain": "global", "reason": "authError", "message": "Invalid Credentials", "locationType": "header", "location": "Authorization" } ], "code": 401, "message": "Invalid Credentials" } }""") with patch( 'allauth.socialaccount.providers.google.views' '.requests') as patched_requests: patched_requests.get.return_value = response_with_401 with self.assertRaises(HTTPError): adapter.complete_login(request, app, token)
def test_login_with_extra_attributes(self): with patch("allauth.socialaccount.providers.openid.views.QUERY_EMAIL", True): resp = self.client.post(reverse(views.login), dict(openid="http://me.yahoo.com")) assert "login.yahooapis" in resp["location"] with patch( "allauth.socialaccount.providers.openid.views._openid_consumer" ) as consumer_mock: client = Mock() complete = Mock() endpoint = Mock() consumer_mock.return_value = client client.complete = complete complete_response = Mock() complete.return_value = complete_response complete_response.endpoint = endpoint complete_response.endpoint.server_url = "http://me.yahoo.com" complete_response.status = consumer.SUCCESS complete_response.identity_url = "http://dummy/john/" with patch( "allauth.socialaccount.providers.openid.utils.SRegResponse" ) as sr_mock: with patch( "allauth.socialaccount.providers.openid.utils.FetchResponse" ) as fr_mock: sreg_mock = Mock() ax_mock = Mock() sr_mock.fromSuccessResponse = sreg_mock fr_mock.fromSuccessResponse = ax_mock sreg_mock.return_value = {} ax_mock.return_value = { AXAttribute.CONTACT_EMAIL: ["*****@*****.**"], AXAttribute.PERSON_FIRST_NAME: ["raymond"], "http://axschema.org/contact/phone/default": ["123456789"], } resp = self.client.post(reverse("openid_callback")) self.assertRedirects( resp, "/accounts/profile/", fetch_redirect_response=False, ) socialaccount = SocialAccount.objects.get( user__first_name="raymond") self.assertEqual(socialaccount.extra_data.get("phone"), "123456789")
def test_login_by_token_reauthenticate(self): resp = self.client.get(reverse("account_login")) nonce = json.loads(resp.context["fb_data"])["loginOptions"]["auth_nonce"] with patch("allauth.socialaccount.providers.facebook.views" ".requests") as requests_mock: mocks = [self.get_mocked_response().json(), {"auth_nonce": nonce}] requests_mock.get.return_value.json = lambda: mocks.pop() resp = self.client.post(reverse("facebook_login_by_token"), data={"access_token": "dummy"}) self.assertRedirects(resp, "http://testserver/accounts/profile/", fetch_redirect_response=False)
def test_login_with_extra_attributes(self): with patch( 'allauth.socialaccount.providers.core.openid.views.QUERY_EMAIL', True): resp = self.client.post(reverse(self.factory.login_view), dict(openid='http://me.yahoo.com')) assert 'login.yahooapis' in resp['location'] with patch('allauth.socialaccount.providers.core' '.openid.views._openid_consumer') as consumer_mock: client = Mock() complete = Mock() endpoint = Mock() consumer_mock.return_value = client client.complete = complete complete_response = Mock() complete.return_value = complete_response complete_response.endpoint = endpoint complete_response.endpoint.server_url = 'http://me.yahoo.com' complete_response.status = consumer.SUCCESS complete_response.identity_url = 'http://dummy/john/' with patch('allauth.socialaccount.providers.core' '.openid.utils.SRegResponse') as sr_mock: with patch('allauth.socialaccount.providers.core' '.openid.utils.FetchResponse') as fr_mock: sreg_mock = Mock() ax_mock = Mock() sr_mock.fromSuccessResponse = sreg_mock fr_mock.fromSuccessResponse = ax_mock sreg_mock.return_value = {} ax_mock.return_value = { AXAttribute.CONTACT_EMAIL: ['*****@*****.**'], AXAttribute.PERSON_FIRST_NAME: ['raymond'], 'http://axschema.org/contact/phone/default': ['123456789'] } resp = self.client.post(reverse('openid_callback')) self.assertRedirects(resp, "/accounts/profile/", fetch_redirect_response=False) socialaccount = SocialAccount.objects.get( user__first_name='raymond') self.assertEqual(socialaccount.extra_data.get('phone'), '123456789')
def test_login_with_extra_attributes(self): with patch('allauth.socialaccount.providers.openid.views.QUERY_EMAIL', True): resp = self.client.post(reverse(views.login), dict(openid='http://me.yahoo.com')) assert 'login.yahooapis' in resp['location'] with patch('allauth.socialaccount.providers' '.openid.views._openid_consumer') as consumer_mock: client = Mock() complete = Mock() endpoint = Mock() consumer_mock.return_value = client client.complete = complete complete_response = Mock() complete.return_value = complete_response complete_response.endpoint = endpoint complete_response.endpoint.server_url = 'http://me.yahoo.com' complete_response.status = consumer.SUCCESS complete_response.identity_url = 'http://dummy/john/' with patch('allauth.socialaccount.providers' '.openid.utils.SRegResponse') as sr_mock: with patch('allauth.socialaccount.providers' '.openid.utils.FetchResponse') as fr_mock: sreg_mock = Mock() ax_mock = Mock() sr_mock.fromSuccessResponse = sreg_mock fr_mock.fromSuccessResponse = ax_mock sreg_mock.return_value = {} ax_mock.return_value = { AXAttribute.CONTACT_EMAIL: ['*****@*****.**'], AXAttribute.PERSON_FIRST_NAME: ['raymond'], 'http://axschema.org/contact/phone/default': ['123456789']} resp = self.client.post(reverse('openid_callback')) self.assertRedirects( resp, "/accounts/profile/", fetch_redirect_response=False ) socialaccount = \ SocialAccount.objects.get(user__first_name='raymond') self.assertEqual( socialaccount.extra_data.get('phone'), '123456789')
def test_login_by_token(self): resp = self.client.get(reverse('account_login')) with patch('allauth.socialaccount.providers.facebook.views' '.requests') as requests_mock: mocks = [self.get_mocked_response().json()] requests_mock.get.return_value.json \ = lambda: mocks.pop() resp = self.client.post(reverse('facebook_login_by_token'), data={'access_token': 'dummy'}) self.assertRedirects(resp, 'http://testserver/accounts/profile/', fetch_redirect_response=False)
def setUp(self): super(BitbucketOAuth2Tests, self).setUp() self.mocks = { 'requests': patch('allauth.socialaccount.providers.dev_sites' '.bitbucket_oauth2.provider.requests') } self.patches = dict((name, mocked.start()) for (name, mocked) in self.mocks.items()) self.patches['requests'].get.side_effect = [ MockedResponse(200, self.response_data), MockedResponse(200, self.email_response_data), ]
def setUp(self): super(BitbucketOAuth2Tests, self).setUp() self.mocks = { 'requests': patch('allauth.socialaccount.providers' '.bitbucket_oauth2.views.requests') } self.patches = dict((name, mocked.start()) for (name, mocked) in self.mocks.items()) self.patches['requests'].get.side_effect = [ MockedResponse(200, self.response_data), MockedResponse(200, self.email_response_data), ]
def test_login(self): with patch('allauth.socialaccount.providers.persona.views' '.requests') as requests_mock: requests_mock.post.return_value.json.return_value = { 'status': 'okay', 'email': '*****@*****.**' } resp = self.client.post(reverse('persona_login'), dict(assertion='dummy')) self.assertRedirects(resp, '/accounts/profile/', fetch_redirect_response=False) get_user_model().objects.get(email='*****@*****.**')
def test_callback(self): with patch('allauth.socialaccount.providers.draugiem.views.draugiem_complete_login') as draugiem_complete_login: self.mock_socialaccount_state() response_json = self.get_draugiem_login_response() token = SocialToken(app=self.app, token=response_json['apikey']) login = self.get_socialaccount(response_json, token) draugiem_complete_login.return_value = login response = self.client.get(reverse(views.callback), {'dr_auth_status': 'ok', 'dr_auth_code': '42'}) self.assertRedirects(response, '/accounts/profile/', fetch_redirect_response=False)
def test_login_by_token(self): resp = self.client.get(reverse("account_login")) with patch("allauth.socialaccount.providers.facebook.views.requests" ) as requests_mock: mocks = [self.get_mocked_response().json()] requests_mock.get.return_value.json = lambda: mocks.pop() resp = self.client.post( reverse("facebook_login_by_token"), data={"access_token": "dummy"}, ) self.assertRedirects(resp, "/accounts/profile/", fetch_redirect_response=False)
def test_login_by_token_reauthenticate(self): resp = self.client.get(reverse('account_login')) nonce = json.loads(resp.context['fb_data'])['loginOptions']['auth_nonce'] with patch('allauth.socialaccount.providers.facebook.views' '.requests') as requests_mock: mocks = [self.get_mocked_response().json(), {'auth_nonce': nonce}] requests_mock.get.return_value.json \ = lambda: mocks.pop() resp = self.client.post(reverse('facebook_login_by_token'), data={'access_token': 'dummy'}) self.assertRedirects(resp, 'http://testserver/accounts/profile/', fetch_redirect_response=False)
def setUp(self): super(BitbucketOAuth2Tests, self).setUp() self.mocks = { "requests": patch("allauth.socialaccount.providers" ".bitbucket_oauth2.views.requests") } self.patches = dict( (name, mocked.start()) for (name, mocked) in self.mocks.items()) self.patches["requests"].get.side_effect = [ MockedResponse(200, self.response_data), MockedResponse(200, self.email_response_data), ]
def test_login_by_token_reauthenticate(self): resp = self.client.get(reverse('account_login')) nonce = json.loads( resp.context['fb_data'])['loginOptions']['auth_nonce'] with patch('allauth.socialaccount.providers.facebook.views' '.requests') as requests_mock: mocks = [self.get_mocked_response().json(), {'auth_nonce': nonce}] requests_mock.get.return_value.json \ = lambda: mocks.pop() resp = self.client.post(reverse('facebook_login_by_token'), data={'access_token': 'dummy'}) self.assertRedirects(resp, 'http://testserver/accounts/profile/', fetch_redirect_response=False)
def test_login(self): with patch("allauth.socialaccount.providers.persona.views.requests" ) as requests_mock: requests_mock.post.return_value.json.return_value = { "status": "okay", "email": "*****@*****.**", } resp = self.client.post(reverse("persona_login"), dict(assertion="dummy")) self.assertRedirects(resp, "/accounts/profile/", fetch_redirect_response=False) get_user_model().objects.get(email="*****@*****.**")
def test_login_by_token_reauthenticate(self): resp = self.client.get(reverse("account_login")) nonce = json.loads( resp.context["fb_data"])["loginOptions"]["auth_nonce"] with patch("allauth.socialaccount.providers.facebook.views.requests" ) as requests_mock: mocks = [self.get_mocked_response().json(), {"auth_nonce": nonce}] requests_mock.get.return_value.json = lambda: mocks.pop() resp = self.client.post( reverse("facebook_login_by_token"), data={"access_token": "dummy"}, ) self.assertRedirects(resp, "/accounts/profile/", fetch_redirect_response=False)
def test_callback(self): with patch('allauth.socialaccount.providers.draugiem.views' '.draugiem_complete_login') as draugiem_complete_login: self.mock_socialaccount_state() response_json = self.get_draugiem_login_response() token = SocialToken(app=self.app, token=response_json['apikey']) login = self.get_socialaccount(response_json, token) draugiem_complete_login.return_value = login response = self.client.get(reverse(views.callback), { 'dr_auth_status': 'ok', 'dr_auth_code': '42' }) self.assertRedirects(response, '/accounts/profile/', fetch_redirect_response=False)
def test_callback(self): with patch("allauth.socialaccount.providers.draugiem.views" ".draugiem_complete_login") as draugiem_complete_login: self.mock_socialaccount_state() response_json = self.get_draugiem_login_response() token = SocialToken(app=self.app, token=response_json["apikey"]) login = self.get_socialaccount(response_json, token) draugiem_complete_login.return_value = login response = self.client.get( reverse(views.callback), { "dr_auth_status": "ok", "dr_auth_code": "42" }, ) self.assertRedirects(response, "/accounts/profile/", fetch_redirect_response=False)
def test_url_str_to_pk_identifies_UUID_as_stringlike(self): with patch('allauth.account.utils.get_user_model') as mocked_gum: mocked_gum.return_value = self.UUIDUser self.assertEqual(url_str_to_user_pk(self.user_id), self.user_id)