tests.py 3.2 KB

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