def test_get_user_info_non_200_status_code(mock_requests):
    # Given
    access_token = "access-token"
    mock_response = mock.MagicMock(status_code=400)
    mock_requests.get.return_value = mock_response

    # When
    with pytest.raises(GoogleError):
        get_user_info(access_token)
def test_get_user_info(mock_requests):
    # Given
    access_token = "access-token"
    mock_google_response_data = {
        "id": "test-id",
        "given_name": "testy",
        "family_name": "tester",
        "email": "*****@*****.**"
    }
    expected_headers = {"Authorization": f"Bearer {access_token}"}
    mock_response = mock.MagicMock(status_code=200)
    mock_requests.get.return_value = mock_response
    mock_response.json.return_value = mock_google_response_data

    # When
    response = get_user_info(access_token)

    # Then
    mock_requests.get.assert_called_with(USER_INFO_URL,
                                         headers=expected_headers)
    assert response == {
        "email": mock_google_response_data["email"],
        "first_name": mock_google_response_data["given_name"],
        "last_name": mock_google_response_data["family_name"],
        "google_user_id": mock_google_response_data["id"]
    }
    def create(self, validated_data):
        """
        get or create a user and token based on the access token and return a DRF token

        TODO: make this generic to allow for other oauth access methods
        """
        user_data = get_user_info(validated_data["access_token"])
        email = user_data.pop("email")
        user, _ = UserModel.objects.get_or_create(email=email, defaults=user_data)
        return Token.objects.get_or_create(user=user)[0]
 def get_user_info(self):
     return get_user_info(self.validated_data["access_token"])