test_case.py 4.6 KB

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