def test_validate_anvil_registration(self, mock_logger): url = TEST_TERRA_API_ROOT_URL + 'register' responses.add(responses.GET, url, status=404) r = validate_anvil_registration(GoogleOAuth2(), { 'access_token': '', 'email': '*****@*****.**' }) mock_logger.warning.assert_called_with( 'User [email protected] is trying to login without registration on AnVIL. None called Terra API: GET /register got status 404 with reason: Not Found' ) self.assertEqual(r.url, '/login?anvilLoginFailed=true') self.assertEqual(len(mock_logger.method_calls), 1) backend = GoogleOAuth2() backend.strategy.session_set('next', '/foo/bar') r = validate_anvil_registration(backend, { 'access_token': '', 'email': '*****@*****.**' }) self.assertEqual(r.url, '/login?anvilLoginFailed=true&next=%2Ffoo%2Fbar') mock_logger.reset_mock() responses.replace(responses.GET, url, status=200, body=REGISTER_RESPONSE) r = validate_anvil_registration(GoogleOAuth2(), { 'access_token': '', 'email': '*****@*****.**' }) mock_logger.warning.assert_not_called() self.assertIsNone(r)
def test_log_signed_in(self, mock_logger): log_signed_in(GoogleOAuth2(), {'email': '*****@*****.**'}, user='******') mock_logger.info.assert_called_with('Logged in [email protected] (google-oauth2)', extra={'user_email': '*****@*****.**'}) self.assertEqual(len(mock_logger.method_calls), 1) mock_logger.reset_mock() log_signed_in(GoogleOAuth2(), {'email': '*****@*****.**'}, is_new=True, user='******') mock_logger.info.assert_has_calls([ mock.call('Logged in [email protected] (google-oauth2)', extra={'user_email': '*****@*****.**'}), mock.call('Created user [email protected] (google-oauth2)', extra={'user_email': '*****@*****.**'}), ]) self.assertEqual(len(mock_logger.method_calls), 2)
def test_validate_user_exist(self, mock_logger): validate_user_exist(GoogleOAuth2(), {'email': '*****@*****.**'}, user='******') self.assertEqual(len(mock_logger.method_calls), 0) r = validate_user_exist(GoogleOAuth2(), {'email': '*****@*****.**'}) mock_logger.warning.assert_called_with( 'Google user [email protected] is trying to login without an existing seqr account (google-oauth2).' ) self.assertEqual(r.url, '/login?googleLoginFailed=true') self.assertEqual(len(mock_logger.method_calls), 1)
def test_validate_user_exist(self, mock_logger): validate_user_exist(GoogleOAuth2(), {'email': '*****@*****.**'}, user='******') self.assertEqual(len(mock_logger.method_calls), 0) r = validate_user_exist(GoogleOAuth2(), {'email': '*****@*****.**'}) mock_logger.warning.assert_called_with( 'Google user [email protected] is trying to login without an existing seqr account (google-oauth2).', extra={'user_email': '*****@*****.**'}) self.assertEqual(r.url, '/login/error/no_account') self.assertEqual(len(mock_logger.method_calls), 1) backend = GoogleOAuth2() backend.strategy.session_set('next', '/foo/bar') r = validate_user_exist(backend, {'email': '*****@*****.**'}) self.assertEqual(r.url, '/login/error/no_account?next=%2Ffoo%2Fbar')
def test_update_profile_details_but_no_data_provided(user_factory): user = user_factory(first_name="") pipelines.update_profile_details( backend=GoogleOAuth2(), user=user, response={}, ) assert user.profile.name == "Anonymous" assert user.profile.picture_url is None
def test_update_profile_details(user): pipelines.update_profile_details( backend=GoogleOAuth2(), user=user, response={ "given_name": "SudokuKid", "picture": "https://path.to/picture.png" }, ) assert user.profile.name == "SudokuKid" assert user.profile.picture_url == "https://path.to/picture.png"
def check_google_oauth2_credentials(app_configs, **kwargs): if getattr(settings, 'SOCIAL_AUTH_GOOGLE_OAUTH2_KEY', None) is not None: auth = GoogleOAuth2() response = requests.post( auth.access_token_url(), { 'grant_type': 'authorization_code', 'code': 'invalid', 'client_id': auth.setting('KEY'), 'client_secret': auth.setting('SECRET') }) if response.status_code == 401: return [W002] return []
def register_by_access_token(request, backend): # This view expects an access_token GET parameter, if it's needed, # request.backend and request.strategy will be loaded with the current # backend and strategy. backend = GoogleOAuth2() # third_party_token = 'ya29.GltrB2iXmDGi05UfQR5_qSn7RtbGLgYZygrv8GCfn1sD2fwW4rVAhqsGfLsME9xzaB4BmGktx8IchkhEbIxeDDWLJbISRM1DoKO9qtEwZQ8_w6gSeVv6gVBH37hg' user = backend.do_auth( 'ya29.GltrB_g6zAllGY9xmsO9gHxK5kTyRHyKtjlMJCRttOlUlOUh3F9BObtrmCXpQLIXXgt5uT85HNj_tZSm0Npt62g6L2JCqaFGDEtaFAhLydW7ZqxDxLnUqr2qeQPn' ) refresh = RefreshToken.for_user(user) print(str(refresh.access_token), '##################') print(user.__dict__, '**************') return HttpResponse('user found')
def post(self, request, backend): token = request.data['access_token'] email = request.data['email'] auth_time = request.data['auth_time'] try: user = User.objects.get(email=email) UserSocialAuth.objects.create( provider='google-oauth2', uid=email, extra_data={ "access_token": "token", "expires": None, "auth_time": auth_time, "token_type": 'Bearer' }, user_id=user.id) except: backend = GoogleOAuth2() try: backend.do_auth(token) except: return Response( { 'status': status.HTTP_400_BAD_REQUEST, 'message': 'Login failed, login with login page or provide correct credentials' }, status=status.HTTP_400_BAD_REQUEST ) user = backend.do_auth(token) if user.name: pass else: user.name = user.username user.username = email user.save() if not user: return Response( { 'status': status.HTTP_400_BAD_REQUEST, 'message': 'login failed! ,login with login page or provide correct credentials' }, status=status.HTTP_400_BAD_REQUEST ) try: UserProfile.objects.create(user=user) except: pass refresh = RefreshToken.for_user(user) if refresh: return Response( { 'status': status.HTTP_200_OK, 'message': 'user has been authenticated from google please find the jwt token', 'token': str(refresh.access_token) }, status=status.HTTP_200_OK ) else: return Response( { 'status': status.HTTP_400_BAD_REQUEST, 'message': 'login failed login with login page or provide correct credentials' }, status=status.HTTP_400_BAD_REQUEST )