test_case.py 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. # pylint: disable=attribute-defined-outside-init,invalid-name
  2. from model_bakery import baker
  3. from rest_framework.test import APITestCase
  4. from glitchtip.test_utils import generators # pylint: disable=unused-import
  5. from organizations_ext.models import OrganizationUserRole
  6. class GlitchTipTestCase(APITestCase):
  7. def create_user_and_project(self):
  8. self.user = baker.make("users.user")
  9. self.organization = baker.make(
  10. "organizations_ext.Organization", scrub_ip_addresses=False
  11. )
  12. self.org_user = self.organization.add_user(
  13. self.user, OrganizationUserRole.ADMIN
  14. )
  15. self.team = baker.make("teams.Team", organization=self.organization)
  16. self.team.members.add(self.org_user)
  17. self.project = baker.make("projects.Project", organization=self.organization)
  18. self.project.team_set.add(self.team)
  19. self.client.force_login(self.user)
  20. class APIPermissionTestCase(APITestCase):
  21. """Base class for testing viewsets with permissions"""
  22. def create_user_org(self):
  23. self.user = baker.make("users.user")
  24. self.organization = baker.make("organizations_ext.Organization")
  25. self.org_user = self.organization.add_user(self.user)
  26. self.auth_token = baker.make("api_tokens.APIToken", user=self.user)
  27. def set_client_credentials(self, token: str):
  28. self.client.credentials(HTTP_AUTHORIZATION="Bearer " + token)
  29. def set_user_role(self, role: OrganizationUserRole):
  30. self.org_user.role = role
  31. self.org_user.save(update_fields=["role"])
  32. def assertGetReqStatusCode(self, url: str, status_code: int, msg=None):
  33. """Make GET request to url and check status code"""
  34. res = self.client.get(url)
  35. self.assertEqual(res.status_code, status_code, msg)
  36. def assertPostReqStatusCode(self, url: str, data, status_code: int, msg=None):
  37. """Make POST request to url and check status code"""
  38. res = self.client.post(url, data)
  39. self.assertEqual(res.status_code, status_code, msg)
  40. def assertPutReqStatusCode(self, url: str, data, status_code: int, msg=None):
  41. """Make PUT request to url and check status code"""
  42. res = self.client.put(url, data)
  43. self.assertEqual(res.status_code, status_code, msg)
  44. def assertDeleteReqStatusCode(self, url: str, status_code: int, msg=None):
  45. """Make DELETE request to url and check status code"""
  46. res = self.client.delete(url)
  47. self.assertEqual(res.status_code, status_code, msg)