test_client.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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.testutils import TestCase
  11. from sentry.testutils.silo import control_silo_test
  12. from sentry_plugins.client import ApiClient, AuthApiClient
  13. class ApiClientTest(TestCase):
  14. @responses.activate
  15. def test_get(self):
  16. responses.add(responses.GET, "http://example.com", json={})
  17. resp = ApiClient().get("http://example.com")
  18. assert resp.status_code == 200
  19. @responses.activate
  20. def test_post(self):
  21. responses.add(responses.POST, "http://example.com", json={})
  22. resp = ApiClient().post("http://example.com")
  23. assert resp.status_code == 200
  24. @responses.activate
  25. def test_delete(self):
  26. responses.add(responses.DELETE, "http://example.com", json={})
  27. resp = ApiClient().delete("http://example.com")
  28. assert resp.status_code == 200
  29. @responses.activate
  30. def test_put(self):
  31. responses.add(responses.PUT, "http://example.com", json={})
  32. resp = ApiClient().put("http://example.com")
  33. assert resp.status_code == 200
  34. @responses.activate
  35. def test_patch(self):
  36. responses.add(responses.PATCH, "http://example.com", json={})
  37. resp = ApiClient().patch("http://example.com")
  38. assert resp.status_code == 200
  39. @control_silo_test
  40. class AuthApiClientTest(TestCase):
  41. @responses.activate
  42. def test_without_authorization(self):
  43. responses.add(responses.GET, "http://example.com", json={})
  44. resp = AuthApiClient().get("http://example.com")
  45. assert resp.status_code == 200
  46. request = responses.calls[-1].request
  47. assert not request.headers.get("Authorization")
  48. @responses.activate
  49. def test_with_authorization(self):
  50. responses.add(responses.GET, "http://example.com", json={})
  51. auth = Mock()
  52. auth.tokens = {"access_token": "access-token"}
  53. resp = AuthApiClient(auth=auth).get("http://example.com")
  54. assert resp.status_code == 200
  55. request = responses.calls[-1].request
  56. assert request.headers.get("Authorization") == "Bearer access-token"
  57. @responses.activate
  58. def test_with_authorization_and_no_auth(self):
  59. responses.add(responses.GET, "http://example.com", json={})
  60. auth = Mock()
  61. auth.tokens = {"access_token": "access-token"}
  62. resp = AuthApiClient(auth=auth).get("http://example.com", auth=None)
  63. assert resp.status_code == 200
  64. request = responses.calls[-1].request
  65. assert not request.headers.get("Authorization")
  66. @responses.activate
  67. def test_invalid_host(self):
  68. with pytest.raises(ApiHostError):
  69. AuthApiClient().get("http://example.com")
  70. @responses.activate
  71. def test_unauthorized(self):
  72. responses.add(responses.GET, "http://example.com", status=404)
  73. with pytest.raises(ApiError):
  74. AuthApiClient().get("http://example.com")
  75. @responses.activate
  76. def test_forbidden(self):
  77. responses.add(responses.GET, "http://example.com", status=401)
  78. with pytest.raises(ApiUnauthorized):
  79. AuthApiClient().get("http://example.com")
  80. @responses.activate
  81. def test_invalid_plaintext(self):
  82. responses.add(responses.GET, "http://example.com", body="")
  83. with pytest.raises(UnsupportedResponseType):
  84. AuthApiClient().get("http://example.com")