test_client.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. from unittest.mock import patch
  2. import pytest
  3. import responses
  4. from sentry.services.hybrid_cloud.usersocialauth.serial import serialize_usersocialauth
  5. from sentry.shared_integrations.exceptions import (
  6. ApiError,
  7. ApiHostError,
  8. ApiUnauthorized,
  9. UnsupportedResponseType,
  10. )
  11. from sentry.shared_integrations.response.base import BaseApiResponse
  12. from sentry.testutils.cases import TestCase
  13. from sentry_plugins.client import ApiClient, AuthApiClient
  14. class ApiClientTest(TestCase):
  15. @responses.activate
  16. def test_get(self):
  17. responses.add(responses.GET, "http://example.com", json={})
  18. resp = ApiClient().get("http://example.com")
  19. assert isinstance(resp, BaseApiResponse)
  20. assert resp.status_code == 200
  21. @responses.activate
  22. def test_post(self):
  23. responses.add(responses.POST, "http://example.com", json={})
  24. resp = ApiClient().post("http://example.com")
  25. assert isinstance(resp, BaseApiResponse)
  26. assert resp.status_code == 200
  27. @responses.activate
  28. def test_delete(self):
  29. responses.add(responses.DELETE, "http://example.com", json={})
  30. resp = ApiClient().delete("http://example.com")
  31. assert isinstance(resp, BaseApiResponse)
  32. assert resp.status_code == 200
  33. @responses.activate
  34. def test_put(self):
  35. responses.add(responses.PUT, "http://example.com", json={})
  36. resp = ApiClient().put("http://example.com")
  37. assert isinstance(resp, BaseApiResponse)
  38. assert resp.status_code == 200
  39. @responses.activate
  40. def test_patch(self):
  41. responses.add(responses.PATCH, "http://example.com", json={})
  42. resp = ApiClient().patch("http://example.com")
  43. assert isinstance(resp, BaseApiResponse)
  44. assert resp.status_code == 200
  45. class AuthApiClientTest(TestCase):
  46. @responses.activate
  47. def test_without_authorization(self):
  48. responses.add(responses.GET, "http://example.com", json={})
  49. resp = AuthApiClient().get("http://example.com")
  50. assert isinstance(resp, BaseApiResponse)
  51. assert resp.status_code == 200
  52. request = responses.calls[-1].request
  53. assert not request.headers.get("Authorization")
  54. @responses.activate
  55. def test_with_authorization(self):
  56. responses.add(responses.GET, "http://example.com", json={})
  57. auth = self.create_usersocialauth(extra_data={"access_token": "access-token"})
  58. rpc_auth = serialize_usersocialauth(auth=auth)
  59. resp = AuthApiClient(auth=rpc_auth).get("http://example.com")
  60. assert isinstance(resp, BaseApiResponse)
  61. assert resp.status_code == 200
  62. request = responses.calls[-1].request
  63. assert request.headers.get("Authorization") == "Bearer access-token"
  64. @responses.activate
  65. def test_with_authorization_and_no_auth(self):
  66. responses.add(responses.GET, "http://example.com", json={})
  67. auth = self.create_usersocialauth(extra_data={"access_token": "access-token"})
  68. rpc_auth = serialize_usersocialauth(auth=auth)
  69. resp = AuthApiClient(auth=rpc_auth).get("http://example.com", auth=None)
  70. assert isinstance(resp, BaseApiResponse)
  71. assert resp.status_code == 200
  72. request = responses.calls[-1].request
  73. assert not request.headers.get("Authorization")
  74. @responses.activate
  75. def test_with_authorized_token_refresh(self):
  76. # First attempt
  77. responses.add(responses.GET, "http://example.com", json={}, status=401)
  78. # After refresh
  79. responses.add(responses.GET, "http://example.com", json={}, status=200)
  80. auth = self.create_usersocialauth(extra_data={"access_token": "access-token"})
  81. rpc_auth = serialize_usersocialauth(auth=auth)
  82. with patch("social_auth.models.UserSocialAuth.refresh_token") as mock_refresh_token:
  83. resp = AuthApiClient(auth=rpc_auth).get("http://example.com")
  84. assert mock_refresh_token.called
  85. assert isinstance(resp, BaseApiResponse)
  86. assert resp.status_code == 200
  87. request = responses.calls[-1].request
  88. assert request.headers.get("Authorization") == "Bearer access-token"
  89. @responses.activate
  90. def test_invalid_host(self):
  91. with pytest.raises(ApiHostError):
  92. AuthApiClient().get("http://example.com")
  93. @responses.activate
  94. def test_unauthorized(self):
  95. responses.add(responses.GET, "http://example.com", status=404)
  96. with pytest.raises(ApiError):
  97. AuthApiClient().get("http://example.com")
  98. @responses.activate
  99. def test_forbidden(self):
  100. responses.add(responses.GET, "http://example.com", status=401)
  101. with pytest.raises(ApiUnauthorized):
  102. AuthApiClient().get("http://example.com")
  103. @responses.activate
  104. def test_invalid_plaintext(self):
  105. responses.add(responses.GET, "http://example.com", body="")
  106. with pytest.raises(UnsupportedResponseType):
  107. AuthApiClient().get("http://example.com")