test_provider.py 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. from __future__ import absolute_import
  2. import responses
  3. from exam import fixture
  4. from social_auth.models import UserSocialAuth
  5. from sentry.testutils import PluginTestCase
  6. from sentry.models import Repository
  7. from sentry_plugins.vsts.repository_provider import VisualStudioRepositoryProvider
  8. from sentry_plugins.vsts.testutils import COMPARE_COMMITS_EXAMPLE, FILE_CHANGES_EXAMPLE
  9. class VisualStudioRepositoryProviderPluginTest(PluginTestCase):
  10. @fixture
  11. def provider(self):
  12. return VisualStudioRepositoryProvider("visualstudio")
  13. @responses.activate
  14. def test_compare_commits(self):
  15. responses.add(
  16. responses.POST,
  17. "https://visualstudio.com/DefaultCollection/_apis/git/repositories/None/commitsBatch",
  18. body=COMPARE_COMMITS_EXAMPLE,
  19. )
  20. responses.add(
  21. responses.GET,
  22. "https://visualstudio.com/DefaultCollection/_apis/git/repositories/None/commits/6c36052c58bde5e57040ebe6bdb9f6a52c906fff/changes",
  23. body=FILE_CHANGES_EXAMPLE,
  24. )
  25. repo = Repository.objects.create(
  26. provider="visualstudio",
  27. name="example",
  28. organization_id=1,
  29. config={"instance": "visualstudio.com", "project": "project-name", "name": "example"},
  30. )
  31. user = self.create_user()
  32. UserSocialAuth.objects.create(
  33. user=user, provider="visualstudio", extra_data={"access_token": "abcdefg"}
  34. )
  35. res = self.provider.compare_commits(repo, "a", "b", user)
  36. assert res == [
  37. {
  38. "patch_set": [{"path": u"/README.md", "type": "M"}],
  39. "author_email": "max@sentry.io",
  40. "author_name": "max bittker",
  41. "message": "Updated README.md",
  42. "id": "6c36052c58bde5e57040ebe6bdb9f6a52c906fff",
  43. "repository": "example",
  44. }
  45. ]
  46. @responses.activate
  47. def test_create_repository(self):
  48. user = self.create_user()
  49. organization = self.create_organization()
  50. UserSocialAuth.objects.create(
  51. user=user, provider="visualstudio", extra_data={"access_token": "abcdefg"}
  52. )
  53. data = {
  54. "name": "MyFirstProject",
  55. "external_id": "654321",
  56. "url": "https://mbittker.visualstudio.com/_git/MyFirstProject/",
  57. "instance": "https://visualstudio.com",
  58. "project": "MyFirstProject",
  59. }
  60. data = self.provider.create_repository(organization, data, user)
  61. assert data == {
  62. "name": "MyFirstProject",
  63. "external_id": "654321",
  64. "url": "https://mbittker.visualstudio.com/_git/MyFirstProject/",
  65. "config": {
  66. "project": "MyFirstProject",
  67. "name": "MyFirstProject",
  68. "instance": "https://visualstudio.com",
  69. },
  70. }