test_sentry_flagpole_context.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. import pytest
  2. from django.contrib.auth.models import AnonymousUser
  3. from flagpole.sentry_flagpole_context import (
  4. InvalidContextDataException,
  5. SentryContextData,
  6. get_sentry_flagpole_context_builder,
  7. organization_context_transformer,
  8. project_context_transformer,
  9. user_context_transformer,
  10. )
  11. from sentry.models.useremail import UserEmail
  12. from sentry.testutils.cases import TestCase
  13. from sentry.testutils.silo import control_silo_test
  14. class TestSentryFlagpoleContext(TestCase):
  15. def test_sentry_flagpole_context_builder(self):
  16. org = self.create_organization()
  17. project = self.create_project(organization=org)
  18. sentry_flagpole_builder = get_sentry_flagpole_context_builder()
  19. sentry_context = sentry_flagpole_builder.build(
  20. SentryContextData(organization=org, project=project)
  21. )
  22. assert sentry_context.get("organization_slug") == org.slug
  23. assert sentry_context.get("organization_slug") == org.slug
  24. assert sentry_context.get("project_slug") == project.slug
  25. assert sentry_context.get("project_id") == project.id
  26. class TestSentryOrganizationContextTransformer(TestCase):
  27. def test_without_organization_passed(self):
  28. context_data = organization_context_transformer(SentryContextData())
  29. assert context_data == {}
  30. def test_with_invalid_organization(self):
  31. with pytest.raises(InvalidContextDataException):
  32. organization_context_transformer(SentryContextData(organization=1234)) # type: ignore[arg-type]
  33. with pytest.raises(InvalidContextDataException):
  34. organization_context_transformer(SentryContextData(organization=self.create_project()))
  35. def test_with_valid_organization(self):
  36. org = self.create_organization(slug="foobar", name="Foo Bar")
  37. org.flags.early_adopter = True
  38. org.save()
  39. assert bool(org.flags.early_adopter) is True
  40. context_data = organization_context_transformer(SentryContextData(organization=org))
  41. assert context_data == {
  42. "organization_slug": "foobar",
  43. "organization_id": org.id,
  44. "organization_name": "Foo Bar",
  45. "organization_is-early-adopter": True,
  46. }
  47. class TestProjectContextTransformer(TestCase):
  48. def test_without_project_passed(self):
  49. context_data = project_context_transformer(SentryContextData())
  50. assert context_data == {}
  51. def test_with_invalid_project_passed(self):
  52. with pytest.raises(InvalidContextDataException):
  53. project_context_transformer(SentryContextData(project=123)) # type: ignore[arg-type]
  54. with pytest.raises(InvalidContextDataException):
  55. project_context_transformer(SentryContextData(project=self.create_organization()))
  56. def test_with_valid_project(self):
  57. project = self.create_project(slug="foobar", name="Foo Bar")
  58. context_data = project_context_transformer(SentryContextData(project=project))
  59. assert context_data == {
  60. "project_slug": "foobar",
  61. "project_name": "Foo Bar",
  62. "project_id": project.id,
  63. }
  64. @control_silo_test
  65. class TestUserContextTransformer(TestCase):
  66. def test_without_user_passed(self):
  67. context_data = project_context_transformer(SentryContextData())
  68. assert context_data == {}
  69. def test_with_invalid_user_passed(self):
  70. with pytest.raises(InvalidContextDataException):
  71. user_context_transformer(SentryContextData(actor=123)) # type: ignore[arg-type]
  72. with pytest.raises(InvalidContextDataException):
  73. user_context_transformer(SentryContextData(actor=self.create_organization()))
  74. def test_with_valid_user(self):
  75. user = self.create_user(email="foobar@example.com")
  76. # Create a new, unverified email to ensure we don't list it
  77. self.create_useremail(user=user, email="unverified_email@example.com")
  78. context_data = user_context_transformer(SentryContextData(actor=user))
  79. assert context_data == {
  80. "user_email": "foobar@example.com",
  81. "user_domain": "example.com",
  82. "user_id": user.id,
  83. "user_is-superuser": False,
  84. "user_is-staff": False,
  85. }
  86. def test_with_only_unverified_emails(self):
  87. user = self.create_user(email="foobar@example.com")
  88. user_email = UserEmail.objects.filter(user_id=user.id).get()
  89. user_email.is_verified = False
  90. user_email.save()
  91. context_data = user_context_transformer(SentryContextData(actor=user))
  92. assert context_data == {
  93. "user_id": user.id,
  94. "user_is-superuser": False,
  95. "user_is-staff": False,
  96. }
  97. def test_with_super_user_and_staff(self):
  98. user = self.create_user(email="super_user_admin_person@sentry.io", is_superuser=True)
  99. context_data = user_context_transformer(SentryContextData(actor=user))
  100. assert context_data == {
  101. "user_email": "super_user_admin_person@sentry.io",
  102. "user_domain": "sentry.io",
  103. "user_id": user.id,
  104. "user_is-superuser": True,
  105. "user_is-staff": False,
  106. }
  107. user.is_staff = True
  108. user.is_superuser = False
  109. user.save()
  110. context_data = user_context_transformer(SentryContextData(actor=user))
  111. assert context_data == {
  112. "user_email": "super_user_admin_person@sentry.io",
  113. "user_domain": "sentry.io",
  114. "user_id": user.id,
  115. "user_is-superuser": False,
  116. "user_is-staff": True,
  117. }
  118. def test_with_anonymous_user(self):
  119. user = AnonymousUser()
  120. context_data = user_context_transformer(SentryContextData(actor=user))
  121. assert context_data == {}