test_case.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. # pylint: disable=attribute-defined-outside-init,invalid-name
  2. from typing import Optional
  3. from django.test import TestCase
  4. from model_bakery import baker
  5. from apps.organizations_ext.constants import OrganizationUserRole
  6. from apps.organizations_ext.models import Organization
  7. class GlitchTestCase(TestCase):
  8. """
  9. Usage:
  10. class FooTestCase(GlitchTestCase):
  11. @classmethod
  12. def setUpTestData(cls):
  13. cls.create_user()
  14. cls.url = "something that doesn't change between tests"
  15. def setUp(self):
  16. self.client.force_login(self.user) # Must run every test
  17. """
  18. organization: Optional[Organization] = None
  19. @classmethod
  20. def create_project(cls):
  21. """Create project, dsn, and organization"""
  22. cls.project = baker.make(
  23. "projects.Project", organization__scrub_ip_addresses=False
  24. )
  25. cls.projectkey = cls.project.projectkey_set.first()
  26. cls.organization = cls.project.organization
  27. @classmethod
  28. def create_user(cls):
  29. """
  30. Create user and joins them to organization with a team
  31. If organization does not exist, create it
  32. """
  33. if not cls.organization:
  34. cls.create_project()
  35. cls.user = baker.make("users.user")
  36. cls.org_user = cls.organization.add_user(cls.user, OrganizationUserRole.ADMIN)
  37. cls.team = baker.make("teams.Team", organization=cls.organization)
  38. cls.team.members.add(cls.org_user)
  39. cls.project.teams.add(cls.team)
  40. class GlitchTipTestCaseMixin:
  41. organization: Optional[Organization] = None
  42. def create_project(self):
  43. """Create project, dsn, and organization"""
  44. self.project = baker.make(
  45. "projects.Project", organization__scrub_ip_addresses=False
  46. )
  47. self.projectkey = self.project.projectkey_set.first()
  48. self.organization = self.project.organization
  49. def create_logged_in_user(self):
  50. """
  51. Create user and joins them to organization with a team
  52. If organization does not exist, create it
  53. """
  54. if not self.organization:
  55. self.create_project()
  56. self.user = baker.make("users.user")
  57. self.client.force_login(self.user)
  58. self.org_user = self.organization.add_user(
  59. self.user, OrganizationUserRole.ADMIN
  60. )
  61. self.team = baker.make("teams.Team", organization=self.organization)
  62. self.team.members.add(self.org_user)
  63. self.project = baker.make("projects.Project", organization=self.organization)
  64. self.project.teams.add(self.team)
  65. class GlitchTipTestCase(GlitchTipTestCaseMixin, TestCase):
  66. """Use GlitchTestCase instead."""
  67. def create_user_and_project(self):
  68. self.create_logged_in_user()
  69. class APIPermissionTestCase(TestCase):
  70. """Base class for testing views with permissions"""
  71. token: Optional[str] = None
  72. def create_user_org(self):
  73. self.user = baker.make("users.user")
  74. self.organization = baker.make("organizations_ext.Organization")
  75. self.org_user = self.organization.add_user(self.user)
  76. self.auth_token = baker.make("api_tokens.APIToken", user=self.user)
  77. def create_org_team_project(self):
  78. self.create_user_org()
  79. self.team = baker.make("teams.Team", organization=self.organization)
  80. self.team.members.add(self.org_user)
  81. self.project = baker.make("projects.Project", organization=self.organization)
  82. self.project.teams.add(self.team)
  83. def get_headers(self):
  84. return {"HTTP_AUTHORIZATION": f"Bearer {self.token}"}
  85. def set_client_credentials(self, token: str):
  86. self.token = token
  87. def set_user_role(self, role: OrganizationUserRole):
  88. self.org_user.role = role
  89. self.org_user.save(update_fields=["role"])
  90. def assertGetReqStatusCode(self, url: str, status_code: int, msg=None):
  91. """Make GET request to url and check status code"""
  92. res = self.client.get(url, **self.get_headers())
  93. self.assertEqual(res.status_code, status_code, msg)
  94. def assertPostReqStatusCode(self, url: str, data, status_code: int, msg=None):
  95. """Make POST request to url and check status code"""
  96. res = self.client.post(
  97. url, data, content_type="application/json", **self.get_headers()
  98. )
  99. self.assertEqual(res.status_code, status_code, msg)
  100. def assertPutReqStatusCode(self, url: str, data, status_code: int, msg=None):
  101. """Make PUT request to url and check status code"""
  102. res = self.client.put(
  103. url, data, content_type="application/json", **self.get_headers()
  104. )
  105. self.assertEqual(res.status_code, status_code, msg)
  106. def assertDeleteReqStatusCode(self, url: str, status_code: int, msg=None):
  107. """Make DELETE request to url and check status code"""
  108. res = self.client.delete(url, **self.get_headers())
  109. self.assertEqual(res.status_code, status_code, msg)