tests.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. from aioresponses import aioresponses
  2. from django.core.management import call_command
  3. from django.test import TestCase
  4. from django.urls import reverse
  5. from model_bakery import baker
  6. from apps.projects.models import Project
  7. from apps.teams.models import Team
  8. from glitchtip.test_utils import generators # noqa: F401
  9. from glitchtip.test_utils.test_case import GlitchTipTestCaseMixin
  10. from .importer import GlitchTipImporter
  11. test_project = {"id": "1", "slug": "project", "name": "project"}
  12. test_key = {
  13. "id": "a" * 32,
  14. "public": "a" * 32,
  15. "projectId": 1,
  16. "label": "Default",
  17. }
  18. class ImporterTestCase(GlitchTipTestCaseMixin, TestCase):
  19. def setUp(self):
  20. self.url = "https://example.com"
  21. self.org_name = "org"
  22. self.auth_token = "token"
  23. self.importer = GlitchTipImporter(
  24. self.url.lstrip("htps:/"), self.auth_token, self.org_name
  25. )
  26. def set_mocks(self, m):
  27. m.get(self.url + "/api/0/", payload={"user": {"username": "foo"}})
  28. m.get(self.url + self.importer.organization_url, payload={"id": 1})
  29. m.get(self.url + self.importer.organization_users_url, payload=[])
  30. m.get(self.url + self.importer.projects_url, payload=[test_project])
  31. m.get(self.url + "/api/0/projects/org/project/keys/", payload=[test_key])
  32. m.get(
  33. self.url + self.importer.teams_url,
  34. payload=[
  35. {
  36. "id": "1",
  37. "slug": "team",
  38. "projects": [test_project],
  39. }
  40. ],
  41. )
  42. m.get(self.url + "/api/0/teams/org/team/members/", payload=[])
  43. @aioresponses()
  44. def test_import_command(self, m):
  45. self.set_mocks(m)
  46. call_command("import", self.url, self.auth_token, self.org_name)
  47. self.assertTrue(Team.objects.filter(slug="team").exists())
  48. self.assertTrue(
  49. Project.objects.filter(
  50. slug=test_project["slug"],
  51. teams__slug="team",
  52. projectkey__public_key=test_key["public"],
  53. ).exists()
  54. )
  55. @aioresponses()
  56. def test_view(self, m):
  57. self.create_logged_in_user()
  58. self.organization.slug = self.org_name
  59. self.organization.save()
  60. self.set_mocks(m)
  61. url = reverse("api:importer")
  62. data = {
  63. "url": self.url,
  64. "authToken": self.auth_token,
  65. "organizationSlug": self.org_name,
  66. }
  67. res = self.client.post(url, data, content_type="application/json")
  68. self.assertEqual(res.status_code, 200)
  69. self.assertTrue(Team.objects.filter(slug="team").exists())
  70. @aioresponses()
  71. def test_invalid_org(self, m):
  72. self.create_logged_in_user()
  73. url = reverse("api:importer")
  74. data = {
  75. "url": self.url,
  76. "authToken": self.auth_token,
  77. "organizationSlug": "foo",
  78. }
  79. res = self.client.post(url, data)
  80. self.assertEqual(res.status_code, 400)
  81. other_user = baker.make("users.User")
  82. other_org = baker.make("Organization", name="foo")
  83. other_org.add_user(other_user)
  84. res = self.client.post(url, data)
  85. self.assertEqual(res.status_code, 400)
  86. other_org.add_user(self.user)
  87. m.get(self.url + "api/0/", payload={"user": {"username": "foo"}})
  88. res = self.client.post(url, data)
  89. self.assertEqual(res.status_code, 400)