test_client.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. from unittest.mock import Mock
  2. import pytest
  3. import responses
  4. from sentry.shared_integrations.exceptions import (
  5. ApiError,
  6. ApiHostError,
  7. ApiUnauthorized,
  8. UnsupportedResponseType,
  9. )
  10. from sentry.shared_integrations.response.base import BaseApiResponse
  11. from sentry.testutils import TestCase
  12. from sentry.testutils.silo import region_silo_test
  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. @region_silo_test(stable=True)
  46. class AuthApiClientTest(TestCase):
  47. @responses.activate
  48. def test_without_authorization(self):
  49. responses.add(responses.GET, "http://example.com", json={})
  50. resp = AuthApiClient().get("http://example.com")
  51. assert isinstance(resp, BaseApiResponse)
  52. assert resp.status_code == 200
  53. request = responses.calls[-1].request
  54. assert not request.headers.get("Authorization")
  55. @responses.activate
  56. def test_with_authorization(self):
  57. responses.add(responses.GET, "http://example.com", json={})
  58. auth = Mock()
  59. auth.tokens = {"access_token": "access-token"}
  60. resp = AuthApiClient(auth=auth).get("http://example.com")
  61. assert isinstance(resp, BaseApiResponse)
  62. assert resp.status_code == 200
  63. request = responses.calls[-1].request
  64. assert request.headers.get("Authorization") == "Bearer access-token"
  65. @responses.activate
  66. def test_with_authorization_and_no_auth(self):
  67. responses.add(responses.GET, "http://example.com", json={})
  68. auth = Mock()
  69. auth.tokens = {"access_token": "access-token"}
  70. resp = AuthApiClient(auth=auth).get("http://example.com", auth=None)
  71. assert isinstance(resp, BaseApiResponse)
  72. assert resp.status_code == 200
  73. request = responses.calls[-1].request
  74. assert not request.headers.get("Authorization")
  75. @responses.activate
  76. def test_invalid_host(self):
  77. with pytest.raises(ApiHostError):
  78. AuthApiClient().get("http://example.com")
  79. @responses.activate
  80. def test_unauthorized(self):
  81. responses.add(responses.GET, "http://example.com", status=404)
  82. with pytest.raises(ApiError):
  83. AuthApiClient().get("http://example.com")
  84. @responses.activate
  85. def test_forbidden(self):
  86. responses.add(responses.GET, "http://example.com", status=401)
  87. with pytest.raises(ApiUnauthorized):
  88. AuthApiClient().get("http://example.com")
  89. @responses.activate
  90. def test_invalid_plaintext(self):
  91. responses.add(responses.GET, "http://example.com", body="")
  92. with pytest.raises(UnsupportedResponseType):
  93. AuthApiClient().get("http://example.com")