test_sentry_flagpole_context.py 5.9 KB

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