test_case.py 3.6 KB

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