test_case.py 2.4 KB

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