views.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. from django.conf import settings
  2. from rest_framework.views import APIView
  3. from rest_framework.response import Response
  4. from rest_framework.permissions import AllowAny
  5. from rest_framework.exceptions import NotFound
  6. from allauth.account.models import EmailAddress
  7. from users.models import User
  8. from organizations_ext.models import Organization
  9. from teams.models import Team
  10. from projects.models import Project
  11. from glitchtip.uptime.models import Monitor
  12. class SeedDataAPIView(APIView):
  13. """
  14. Delete existing data and seed data used in end to end testing
  15. Very destructive. Never enable on production.
  16. """
  17. permission_classes = [AllowAny]
  18. def post(self, request):
  19. if settings.ENABLE_TEST_API is not True:
  20. raise NotFound("Enable Test API is not enabled")
  21. user_email = "cypresstest@example.com"
  22. other_user_email = "cypresstest-other@example.com"
  23. user_password = "hunter22" # nosec
  24. organization_name = "Business Company, Inc."
  25. team_slug = "cypresstestteam"
  26. project_name = "NicheScrip"
  27. User.objects.filter(email=user_email).delete()
  28. user = User.objects.create_user(email=user_email, password=user_password)
  29. User.objects.filter(email=other_user_email).delete()
  30. other_user = User.objects.create_user(
  31. email=other_user_email, password=user_password
  32. )
  33. EmailAddress.objects.create(
  34. user=user, email=user_email, primary=True, verified=False
  35. )
  36. EmailAddress.objects.create(
  37. user=other_user, email=other_user_email, primary=True, verified=False
  38. )
  39. Organization.objects.filter(name=organization_name).delete()
  40. organization = Organization.objects.create(name=organization_name)
  41. orgUser = organization.add_user(user=user)
  42. Team.objects.filter(slug=team_slug).delete()
  43. team = Team.objects.create(slug=team_slug, organization=organization)
  44. Project.objects.filter(name=project_name).delete()
  45. project = Project.objects.create(name=project_name, organization=organization)
  46. Monitor.objects.filter(name="cytestmonitor").delete()
  47. Monitor.objects.create(
  48. name = "cytestmonitor",
  49. organization=organization,
  50. project=project,
  51. url="https://www.google.com",
  52. monitor_type="Ping",
  53. interval="00:01:00"
  54. )
  55. if (request.query_params.get("extras")):
  56. project_name = "SwitchGrip"
  57. project2 = Project.objects.create(name=project_name, organization=organization)
  58. project_name = "PitchFlip"
  59. project3 = Project.objects.create(name=project_name, organization=organization, platform="JavaScript")
  60. team.projects.add(project)
  61. team.projects.add(project2)
  62. team.projects.add(project3)
  63. team.members.add(orgUser)
  64. return Response()