test_case.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. # pylint: disable=attribute-defined-outside-init,invalid-name
  2. from typing import Optional
  3. from model_bakery import baker
  4. from rest_framework.test import APITestCase
  5. from organizations_ext.models import Organization, OrganizationUserRole
  6. class GlitchTipTestCaseMixin:
  7. organization: Optional[Organization] = None
  8. def create_project(self):
  9. """Create project, dsn, and organization"""
  10. self.project = baker.make(
  11. "projects.Project", organization__scrub_ip_addresses=False
  12. )
  13. self.projectkey = self.project.projectkey_set.first()
  14. self.organization = self.project.organization
  15. def create_logged_in_user(self):
  16. """
  17. Create user and joins them to organization with a team
  18. If organization does not exist, create it
  19. """
  20. if not self.organization:
  21. self.create_project()
  22. self.user = baker.make("users.user")
  23. self.client.force_login(self.user)
  24. self.org_user = self.organization.add_user(
  25. self.user, OrganizationUserRole.ADMIN
  26. )
  27. self.team = baker.make("teams.Team", organization=self.organization)
  28. self.team.members.add(self.org_user)
  29. self.project = baker.make("projects.Project", organization=self.organization)
  30. self.project.team_set.add(self.team)
  31. class GlitchTipTestCase(GlitchTipTestCaseMixin, APITestCase):
  32. def create_user_and_project(self):
  33. self.create_logged_in_user()
  34. class APIPermissionTestCase(APITestCase):
  35. """Base class for testing viewsets with permissions"""
  36. def create_user_org(self):
  37. self.user = baker.make("users.user")
  38. self.organization = baker.make("organizations_ext.Organization")
  39. self.org_user = self.organization.add_user(self.user)
  40. self.auth_token = baker.make("api_tokens.APIToken", user=self.user)
  41. def set_client_credentials(self, token: str):
  42. self.client.credentials(HTTP_AUTHORIZATION="Bearer " + token)
  43. def set_user_role(self, role: OrganizationUserRole):
  44. self.org_user.role = role
  45. self.org_user.save(update_fields=["role"])
  46. def assertGetReqStatusCode(self, url: str, status_code: int, msg=None):
  47. """Make GET request to url and check status code"""
  48. res = self.client.get(url)
  49. self.assertEqual(res.status_code, status_code, msg)
  50. def assertPostReqStatusCode(self, url: str, data, status_code: int, msg=None):
  51. """Make POST request to url and check status code"""
  52. res = self.client.post(url, data)
  53. self.assertEqual(res.status_code, status_code, msg)
  54. def assertPutReqStatusCode(self, url: str, data, status_code: int, msg=None):
  55. """Make PUT request to url and check status code"""
  56. res = self.client.put(url, data)
  57. self.assertEqual(res.status_code, status_code, msg)
  58. def assertDeleteReqStatusCode(self, url: str, status_code: int, msg=None):
  59. """Make DELETE request to url and check status code"""
  60. res = self.client.delete(url)
  61. self.assertEqual(res.status_code, status_code, msg)