test_client.py 3.4 KB

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