|
@@ -1,5 +1,4 @@
|
|
|
import errno
|
|
|
-from time import time
|
|
|
from unittest import mock
|
|
|
|
|
|
import pytest
|
|
@@ -12,10 +11,8 @@ from sentry_sdk.tracing import Transaction
|
|
|
from urllib3.exceptions import InvalidChunkLength
|
|
|
from urllib3.response import HTTPResponse
|
|
|
|
|
|
-from sentry.identity import register
|
|
|
from sentry.identity.oauth2 import OAuth2Provider
|
|
|
-from sentry.integrations.client import ApiClient, OAuth2RefreshMixin
|
|
|
-from sentry.models import Identity, IdentityProvider
|
|
|
+from sentry.integrations.client import ApiClient
|
|
|
from sentry.shared_integrations.exceptions import (
|
|
|
ApiConnectionResetError,
|
|
|
ApiHostError,
|
|
@@ -24,7 +21,6 @@ from sentry.shared_integrations.exceptions import (
|
|
|
from sentry.shared_integrations.exceptions.base import ApiError
|
|
|
from sentry.shared_integrations.response.base import BaseApiResponse
|
|
|
from sentry.testutils import TestCase
|
|
|
-from sentry.testutils.silo import control_silo_test
|
|
|
|
|
|
|
|
|
class ApiClientTest(TestCase):
|
|
@@ -319,70 +315,3 @@ class OAuthProvider(OAuth2Provider):
|
|
|
|
|
|
def get_refresh_token_url(self):
|
|
|
return "https://example.com"
|
|
|
-
|
|
|
-
|
|
|
-class OAuth2ApiClient(ApiClient, OAuth2RefreshMixin):
|
|
|
- def __init__(self, identity, *args, **kwargs):
|
|
|
- super().__init__(*args, **kwargs)
|
|
|
- self.identity = identity
|
|
|
-
|
|
|
-
|
|
|
-@control_silo_test(stable=True)
|
|
|
-class OAuth2ApiClientTest(TestCase):
|
|
|
- def setUp(self):
|
|
|
- self.user = self.create_user()
|
|
|
- self.organization = self.create_organization()
|
|
|
- self.access_token = "1234567890"
|
|
|
- self.identity_provider_model = IdentityProvider.objects.create(type="oauth")
|
|
|
- register(OAuthProvider)
|
|
|
-
|
|
|
- @responses.activate
|
|
|
- def test_check_auth(self):
|
|
|
- new_auth = {
|
|
|
- "access_token": "1234567890",
|
|
|
- "refresh_token": "0987654321",
|
|
|
- "expires_in": 45678988239,
|
|
|
- }
|
|
|
- responses.add(responses.POST, "https://example.com", json=new_auth)
|
|
|
- identity = Identity.objects.create(
|
|
|
- idp=self.identity_provider_model,
|
|
|
- user=self.user,
|
|
|
- external_id="oauth_base",
|
|
|
- data={
|
|
|
- "access_token": "access_token",
|
|
|
- "refresh_token": "refresh_token",
|
|
|
- "expires": int(time()) - 3600,
|
|
|
- },
|
|
|
- )
|
|
|
-
|
|
|
- client = OAuth2ApiClient(identity)
|
|
|
- client.check_auth()
|
|
|
-
|
|
|
- assert client.identity.data["access_token"] == new_auth["access_token"]
|
|
|
- assert client.identity.data["refresh_token"] == new_auth["refresh_token"]
|
|
|
- assert client.identity.data["expires"] > int(time())
|
|
|
-
|
|
|
- @responses.activate
|
|
|
- def test_check_auth_no_refresh(self):
|
|
|
- new_auth = {
|
|
|
- "access_token": "1234567890",
|
|
|
- "refresh_token": "0987654321",
|
|
|
- "expires_in": 45678988239,
|
|
|
- }
|
|
|
- old_auth = {
|
|
|
- "access_token": "access_token",
|
|
|
- "refresh_token": "refresh_token",
|
|
|
- "expires": int(time()) + 3600,
|
|
|
- }
|
|
|
- responses.add(responses.POST, "https://example.com", json=new_auth)
|
|
|
- identity = Identity.objects.create(
|
|
|
- idp=self.identity_provider_model,
|
|
|
- user=self.user,
|
|
|
- external_id="oauth_base",
|
|
|
- data=old_auth,
|
|
|
- )
|
|
|
-
|
|
|
- client = OAuth2ApiClient(identity)
|
|
|
- client.check_auth()
|
|
|
-
|
|
|
- assert client.identity.data == old_auth
|