# Path: shared/gcp_jupyterlab_shared/version.py
# VERSION = '1.1.3'


import asyncio
import base64
import json
import unittest
import tornado.testing
from unittest.mock import patch, MagicMock
from google.auth.exceptions import RefreshError
from tornado.web import Application
from tornado.httpclient import HTTPClientError
from gcp_jupyterlab_shared import handlers, test_data
from .version import VERSION
# Function that returns a future to be used with mocks that need to be awaited
def async_return(result, as_exception=False):
  f = asyncio.Future()
  if as_exception:
    f.set_exception(result)
  else:
    f.set_result(result)
  return f


@patch('google.auth')
class AuthProviderTest(unittest.TestCase):

  def setUp(self):
    handlers.AuthProvider._instance = None

  def test_is_singleton(self, mock_google_auth):
    mock_auth = MagicMock()
    mock_auth.valid = False
    mock_google_auth.default = MagicMock(return_value=(mock_auth, TEST_PROJECT))
    auth_provider = handlers.AuthProvider.get()
    self.assertEqual(auth_provider, handlers.AuthProvider.get())
    self.assertEqual(mock_auth.refresh.call_count, 2)

  def test_refresh_reraises_on_error(self, mock_google_auth):
    mock_auth = MagicMock()
    mock_auth.valid = False
    mock_auth.refresh.side_effect = RefreshError('Unable to refresh token')
    mock_google_auth.default = MagicMock(return_value=(mock_auth, TEST_PROJECT))
    with self.assertRaises(RefreshError):

# Path: shared/gcp_jupyterlab_shared/version.py
# VERSION = '1.1.3'


# Path: shared/gcp_jupyterlab_shared/handlers_test.py
import asyncio
import base64
import json
import unittest
import tornado.testing
from unittest.mock import patch, MagicMock
from google.auth.exceptions import RefreshError
from tornado.web import Application
from tornado.httpclient import HTTPClientError
from gcp_jupyterlab_shared import handlers, test_data
from .version import VERSION

# Function that returns a future to be used with mocks that need to be awaited
def async_return(result, as_exception=False):
  f = asyncio.Future()
  if as_exception:
    f.set_exception(result)
  else:
    f.set_result(result)
  return f


@patch('google.auth')
class AuthProviderTest(unittest.TestCase):

  def setUp(self):
    handlers.AuthProvider._instance = None

  def test_is_singleton(self, mock_google_auth):
    mock_auth = MagicMock()
    mock_auth.valid = False
    mock_google_auth.default = MagicMock(return_value=(mock_auth, TEST_PROJECT))
    auth_provider = handlers.AuthProvider.get()
    self.assertEqual(auth_provider, handlers.AuthProvider.get())
    self.assertEqual(mock_auth.refresh.call_count, 2)

  def test_refresh_reraises_on_error(self, mock_google_auth):
    mock_auth = MagicMock()
    mock_auth.valid = False
    mock_auth.refresh.side_effect = RefreshError('Unable to refresh token')
    mock_google_auth.default = MagicMock(return_value=(mock_auth, TEST_PROJECT))
    with self.assertRaises(RefreshError):

      handlers.AuthProvider.get()