def login(self, username='******', verified_email='*****@*****.**', process='login', with_refresh_token=True, public_email='*****@*****.**'): resp = self.client.get(reverse('github_login', locale=settings.WIKI_DEFAULT_LANGUAGE), {'process': process}) path = urlparse(resp['location']) query = parse_qs(path.query) complete_url = reverse('github_callback', unprefixed=True) self.assertGreater(query['redirect_uri'][0] .find(complete_url), 0) response_json = self.get_login_response_json( with_refresh_token=with_refresh_token) with mocked_response( MockedResponse(200, response_json, {'content-type': 'application/json'}), MockedResponse(200, self.mocked_user_response % {'username': username, 'public_email': json.dumps(public_email)}), MockedResponse(200, self.mocked_email_response % {'verified_email': verified_email})): resp = self.client.get(complete_url, {'code': 'test', 'state': query['state'][0]}, follow=True) return resp
def login(self, resp_mock): resp = self.client.get(reverse(self.provider.id + '_login')) p = urlparse.urlparse(resp['location']) q = urlparse.parse_qs(p.query) complete_url = reverse(self.provider.id+'_callback') self.assertGreater(q['redirect_uri'][0] .find(complete_url), 0) with mocked_response(MockedResponse(200, '{"access_token":"testac"}', {'content-type': 'application/json'}), resp_mock): resp = self.client.get(complete_url, { 'code': 'test' }) return resp
def _complete_shopify_login(self, q, resp, resp_mock, with_refresh_token): complete_url = reverse(self.provider.id + '_callback') self.assertGreater(q['redirect_uri'][0] .find(complete_url), 0) response_json = self \ .get_login_response_json(with_refresh_token=with_refresh_token) with mocked_response( MockedResponse( 200, response_json, {'content-type': 'application/json'}), resp_mock): resp = self.client.get(complete_url, {'code': 'test', 'state': q['state'][0], 'shop': 'test', }) return resp
def test_login(self): resp = self.client.get(reverse(self.provider.id + '_login')) p = urlparse.urlparse(resp['location']) q = urlparse.parse_qs(p.query) complete_url = reverse(self.provider.id+'_callback') self.assertGreater(q['redirect_uri'][0] .find(complete_url), 0) resp_mock = mocked_oauth_responses.get(self.provider.id) if not resp_mock: warnings.warn("Cannot test provider %s, no oauth mock" % self.provider.id) return with mocked_response(MockedResponse(200, '{"access_token":"testac"}', {'content-type': 'application/json'}), resp_mock): resp = self.client.get(complete_url, { 'code': 'test' }) self.assertRedirects(resp, reverse('socialaccount_signup'))
def login(self, resp_mock, process='login', with_refresh_token=True): resp = self.client.get(reverse(self.provider.id + '_login'), {'process': process, 'shop': 'test'}) p = urlparse(resp['location']) q = parse_qs(p.query) complete_url = reverse(self.provider.id+'_callback') self.assertGreater(q['redirect_uri'][0] .find(complete_url), 0) response_json = self \ .get_login_response_json(with_refresh_token=with_refresh_token) with mocked_response( MockedResponse( 200, response_json, {'content-type': 'application/json'}), resp_mock): resp = self.client.get(complete_url, {'code': 'test', 'state': q['state'][0], 'shop': 'test', }) return resp
def login( self, username="******", verified_email="*****@*****.**", process="login", with_refresh_token=True, public_email="*****@*****.**", ): resp = self.client.get(reverse("github_login", locale=settings.WIKI_DEFAULT_LANGUAGE), {"process": process}) path = urlparse(resp["location"]) query = parse_qs(path.query) complete_url = reverse("github_callback", unprefixed=True) self.assertGreater(query["redirect_uri"][0].find(complete_url), 0) response_json = self.get_login_response_json(with_refresh_token=with_refresh_token) with mocked_response( MockedResponse(200, response_json, {"content-type": "application/json"}), MockedResponse( 200, self.mocked_user_response % {"username": username, "public_email": json.dumps(public_email)} ), MockedResponse(200, self.mocked_email_response % {"verified_email": verified_email}), ): resp = self.client.get(complete_url, {"code": "test", "state": query["state"][0]}, follow=True) return resp
def login(self, resp_mock, process="login", with_refresh_token=True): resp = self.client.get(reverse(self.provider.id + "_login"), dict(process=process)) p = urlparse(resp["location"]) q = parse_qs(p.query) complete_url = reverse(self.provider.id + "_callback") self.assertGreater(q["redirect_uri"][0].find(complete_url), 0) response_json = self.get_login_response_json( with_refresh_token=with_refresh_token) with mocked_response( MockedResponse(200, response_json, {"content-type": "application/json"}), resp_mock, ): resp = self.client.post( complete_url, data=self.get_complete_parameters(q), ) assert reverse("apple_finish_callback") in resp.url # Follow the redirect resp = self.client.get(resp.url) return resp
def _slack_login(self, team_name, team_id, user_name, user_id, user_email=None, scope=None, process='login', next_url=None, **kwargs): if scope is None: scope = settings.SLACK_LOGIN_SCOPE if next_url is None: next_url = settings.LOGIN_REDIRECT_URL if user_email is None: user_email = '{}_{}@example.com'.format('_'.join(user_name.split()), user_id) scope_list = scope.split(',') try: app = SocialApp.objects.get_current(SlackProvider.id) account = SlackAccount.objects.get(team_id=team_id, slack_user_id=user_id) current_scope_list = SlackUserToken.objects.get(app=app, slack_account=account).scope.split(',') except (SlackAccount.DoesNotExist, SlackUserToken.DoesNotExist): current_scope_list = [] current_scope_set = set(current_scope_list) full_scope_list = [] for sc in scope_list: if sc not in current_scope_set: full_scope_list.append(sc) full_scope_list.extend(current_scope_list) full_scope = ','.join(full_scope_list) access_token = { 'access_token': generate_access_token(team_id, user_id), 'scope': full_scope, } if 'bot' in full_scope_list: access_token['bot'] = { 'bot_user_id': kwargs['bot_user_id'], 'bot_access_token': generate_access_token(team_id, user_id, bot=True) } access_token_resp = MockedResponse( 200, json.dumps(access_token), { 'content-type': 'text/json', } ) user_info_resp = MockedResponse( 200, json.dumps( { "ok": True, "user": { "name": user_name, "id": user_id, "email": user_email, }, "team": { "name": team_name, "domain": '-'.join(team_name.split()), "id": team_id, } } ), { 'content-type': 'text/json', } ) resp = self.get(reverse('slack_login'), { 'next': next_url, 'process': process, 'scope': scope, }) if resp.status_code != 302: return False parsed_url = urlparse(resp.url) redirect_url = '{}://{}{}'.format(parsed_url.scheme, parsed_url.netloc, parsed_url.path) if redirect_url != SlackOAuth2Adapter.authorize_url: return False query = parse_qs(parsed_url.query) if 'state' not in query or len(query['state']) != 1: return False state = query['state'][0] with mocked_response(access_token_resp, user_info_resp): resp = self.get(reverse('slack_callback'), {'code': '123', 'state': state}) if resp.status_code != 302 or resp.url != next_url: return False return True