test_repository_provider.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. from functools import cached_property
  2. import responses
  3. from sentry.models.repository import Repository
  4. from sentry.testutils.cases import TestCase
  5. from sentry.testutils.silo import region_silo_test
  6. from sentry_plugins.bitbucket.repository_provider import BitbucketRepositoryProvider
  7. from sentry_plugins.bitbucket.testutils import COMMIT_DIFF_PATCH, COMPARE_COMMITS_EXAMPLE
  8. @region_silo_test
  9. class BitbucketPluginTest(TestCase):
  10. @cached_property
  11. def provider(self):
  12. return BitbucketRepositoryProvider("bitbucket")
  13. @responses.activate
  14. def test_compare_commits(self):
  15. responses.add(
  16. responses.GET,
  17. "https://api.bitbucket.org/2.0/repositories/maxbittker/newsdiffs/commits/e18e4e72de0d824edfbe0d73efe34cbd0d01d301",
  18. body=COMPARE_COMMITS_EXAMPLE,
  19. )
  20. responses.add(
  21. responses.GET,
  22. "https://api.bitbucket.org/2.0/repositories/maxbittker/newsdiffs/diff/e18e4e72de0d824edfbe0d73efe34cbd0d01d301",
  23. body=COMMIT_DIFF_PATCH,
  24. )
  25. repo = Repository.objects.create(
  26. provider="bitbucket",
  27. name="maxbittker/newsdiffs",
  28. organization_id=1,
  29. config={"name": "maxbittker/newsdiffs"},
  30. )
  31. user = self.user
  32. self.create_usersocialauth(
  33. provider="bitbucket",
  34. user=user,
  35. uid="1",
  36. extra_data={
  37. "access_token": "oauth_token=oauth-token&oauth_token_secret=oauth-token-secret"
  38. },
  39. )
  40. res = self.provider.compare_commits(
  41. repo, None, "e18e4e72de0d824edfbe0d73efe34cbd0d01d301", actor=user
  42. )
  43. assert res == [
  44. {
  45. "author_email": "max@getsentry.com",
  46. "author_name": "Max Bittker",
  47. "message": "README.md edited online with Bitbucket",
  48. "id": "e18e4e72de0d824edfbe0d73efe34cbd0d01d301",
  49. "repository": "maxbittker/newsdiffs",
  50. "patch_set": [{"path": "README.md", "type": "M"}],
  51. }
  52. ]