test_organization_actions.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. import re
  2. import pytest
  3. from sentry.hybridcloud.models.outbox import RegionOutbox, outbox_context
  4. from sentry.hybridcloud.outbox.category import OutboxCategory, OutboxScope
  5. from sentry.models.organization import Organization, OrganizationStatus
  6. from sentry.organizations.services.organization_actions.impl import (
  7. generate_deterministic_organization_slug,
  8. mark_organization_as_pending_deletion_with_outbox_message,
  9. unmark_organization_as_pending_deletion_with_outbox_message,
  10. update_organization_with_outbox_message,
  11. upsert_organization_by_org_id_with_outbox_message,
  12. )
  13. from sentry.testutils.cases import TestCase
  14. def assert_outbox_update_message_exists(org: Organization, expected_count: int):
  15. outbox_messages = RegionOutbox.objects.filter()
  16. # TODO(HC): Remove this once we can ensure an expected count of 1 for every message
  17. # It's not essential since these messages will coallesce, but there's no reason we
  18. # should be queueing 2 outbox messages per create/update
  19. assert outbox_messages.count() == expected_count
  20. for org_update_outbox in outbox_messages:
  21. assert org_update_outbox.shard_identifier == org.id
  22. assert org_update_outbox.shard_scope == OutboxScope.ORGANIZATION_SCOPE
  23. assert org_update_outbox.category == OutboxCategory.ORGANIZATION_UPDATE
  24. class OrganizationUpdateWithOutboxTest(TestCase):
  25. def setUp(self):
  26. self.org: Organization = self.create_organization(slug="sluggy", name="barfoo")
  27. def test_update_organization_with_outbox_message(self):
  28. with outbox_context(flush=False):
  29. update_organization_with_outbox_message(
  30. org_id=self.org.id, update_data={"name": "foobar"}
  31. )
  32. self.org.refresh_from_db()
  33. assert self.org.name == "foobar"
  34. assert self.org.slug == "sluggy"
  35. assert_outbox_update_message_exists(org=self.org, expected_count=1)
  36. def test_update_with_missing_org_id(self):
  37. with pytest.raises(Organization.DoesNotExist):
  38. update_organization_with_outbox_message(org_id=1234, update_data={"name": "foobar"})
  39. class OrganizationUpsertWithOutboxTest(TestCase):
  40. def setUp(self):
  41. self.org: Organization = self.create_organization(slug="sluggy", name="barfoo")
  42. def test_upsert_queues_outbox_message_and_updates_org(self):
  43. # The test fixture creates at least 1 org so comparing count before
  44. # and after the upsert is the safest way to assert we haven't created
  45. # a new entry.
  46. previous_org_count = Organization.objects.count()
  47. org_before_modification = Organization.objects.get(id=self.org.id)
  48. with outbox_context(flush=False):
  49. updated_org: Organization = upsert_organization_by_org_id_with_outbox_message(
  50. org_id=self.org.id,
  51. upsert_data={
  52. "slug": "foobar",
  53. "status": OrganizationStatus.DELETION_IN_PROGRESS,
  54. },
  55. )
  56. assert Organization.objects.count() == previous_org_count
  57. self.org.refresh_from_db()
  58. assert updated_org.slug == self.org.slug == "foobar"
  59. assert updated_org.name == self.org.name == "barfoo"
  60. assert updated_org.status == self.org.status == OrganizationStatus.DELETION_IN_PROGRESS
  61. assert (
  62. updated_org.default_role
  63. == self.org.default_role
  64. == org_before_modification.default_role
  65. )
  66. assert_outbox_update_message_exists(org=self.org, expected_count=1)
  67. def test_upsert_creates_organization_with_desired_id(self):
  68. previous_org_count = Organization.objects.count()
  69. org_before_modification = Organization.objects.get(id=self.org.id)
  70. desired_org_id = 1234
  71. with outbox_context(flush=False):
  72. created_org: Organization = upsert_organization_by_org_id_with_outbox_message(
  73. org_id=desired_org_id,
  74. upsert_data={
  75. "slug": "random",
  76. "name": "rando",
  77. "status": OrganizationStatus.ACTIVE,
  78. },
  79. )
  80. assert Organization.objects.count() == previous_org_count + 1
  81. db_created_org = Organization.objects.get(id=desired_org_id)
  82. assert db_created_org.slug == created_org.slug == "random"
  83. assert db_created_org.status == created_org.status == OrganizationStatus.ACTIVE
  84. assert db_created_org.name == created_org.name == "rando"
  85. # Probably overly cautious, but assert that previous org has not been modified
  86. self.org.refresh_from_db()
  87. assert org_before_modification.slug == self.org.slug
  88. assert org_before_modification.name == self.org.name
  89. assert org_before_modification.status == self.org.status
  90. assert_outbox_update_message_exists(org=db_created_org, expected_count=1)
  91. class OrganizationMarkOrganizationAsPendingDeletionWithOutboxMessageTest(TestCase):
  92. def setUp(self):
  93. self.org: Organization = self.create_organization(
  94. slug="sluggy", name="barfoo", status=OrganizationStatus.ACTIVE
  95. )
  96. def test_mark_for_deletion_and_outbox_generation(self):
  97. org_before_update = Organization.objects.get(id=self.org.id)
  98. with outbox_context(flush=False):
  99. updated_org = mark_organization_as_pending_deletion_with_outbox_message(
  100. org_id=self.org.id
  101. )
  102. assert updated_org
  103. self.org.refresh_from_db()
  104. assert updated_org.status == self.org.status == OrganizationStatus.PENDING_DELETION
  105. assert updated_org.name == self.org.name == org_before_update.name
  106. assert updated_org.slug == self.org.slug == org_before_update.slug
  107. assert_outbox_update_message_exists(self.org, 1)
  108. def test_mark_for_deletion_on_already_deleted_org(self):
  109. self.org.status = OrganizationStatus.PENDING_DELETION
  110. self.org.save()
  111. org_before_update = Organization.objects.get(id=self.org.id)
  112. with outbox_context(flush=False):
  113. updated_org = mark_organization_as_pending_deletion_with_outbox_message(
  114. org_id=self.org.id
  115. )
  116. assert updated_org is None
  117. self.org.refresh_from_db()
  118. assert self.org.status == org_before_update.status
  119. assert self.org.name == org_before_update.name
  120. assert self.org.slug == org_before_update.slug
  121. assert_outbox_update_message_exists(self.org, 0)
  122. class UnmarkOrganizationForDeletionWithOutboxMessageTest(TestCase):
  123. def setUp(self):
  124. self.org: Organization = self.create_organization(
  125. slug="sluggy", name="barfoo", status=OrganizationStatus.PENDING_DELETION
  126. )
  127. def test_unmark_for_pending_deletion_and_outbox_generation(self):
  128. with outbox_context(flush=False):
  129. updated_org = unmark_organization_as_pending_deletion_with_outbox_message(
  130. org_id=self.org.id
  131. )
  132. assert updated_org
  133. self.org.refresh_from_db()
  134. assert updated_org.status == self.org.status == OrganizationStatus.ACTIVE
  135. assert updated_org.name == self.org.name
  136. assert updated_org.slug == self.org.slug
  137. assert_outbox_update_message_exists(self.org, 1)
  138. def test_unmark_for_deletion_in_progress_and_outbox_generation(self):
  139. update_organization_with_outbox_message(
  140. org_id=self.org.id, update_data={"status": OrganizationStatus.DELETION_IN_PROGRESS}
  141. )
  142. with outbox_context(flush=False):
  143. updated_org = unmark_organization_as_pending_deletion_with_outbox_message(
  144. org_id=self.org.id
  145. )
  146. assert updated_org
  147. self.org.refresh_from_db()
  148. assert updated_org.status == self.org.status == OrganizationStatus.ACTIVE
  149. assert updated_org.name == self.org.name
  150. assert updated_org.slug == self.org.slug
  151. assert_outbox_update_message_exists(self.org, 1)
  152. def test_unmark_org_when_already_active(self):
  153. update_organization_with_outbox_message(
  154. org_id=self.org.id, update_data={"status": OrganizationStatus.ACTIVE}
  155. )
  156. org_before_update = Organization.objects.get(id=self.org.id)
  157. with outbox_context(flush=False):
  158. updated_org = unmark_organization_as_pending_deletion_with_outbox_message(
  159. org_id=self.org.id
  160. )
  161. assert not updated_org
  162. self.org.refresh_from_db()
  163. assert self.org.status == org_before_update.status
  164. assert self.org.name == org_before_update.name
  165. assert self.org.slug == org_before_update.slug
  166. assert_outbox_update_message_exists(self.org, 0)
  167. class TestGenerateDeterministicOrganizationSlug(TestCase):
  168. def test_slug_under_size_limit(self):
  169. slug = generate_deterministic_organization_slug(
  170. desired_slug_base="santry", desired_org_name="santry", owning_user_id=42
  171. )
  172. assert slug == "santry-095a9012d"
  173. def test_slug_above_size_limit(self):
  174. slug = generate_deterministic_organization_slug(
  175. desired_slug_base="areallylongsentryorgnamethatiswaytoolong",
  176. desired_org_name="santry",
  177. owning_user_id=42,
  178. )
  179. assert len(slug) == 30
  180. assert slug == "areallylongsentryorg-945bda148"
  181. def test_slug_with_mixed_casing(self):
  182. slug = generate_deterministic_organization_slug(
  183. desired_slug_base="A mixed CASING str",
  184. desired_org_name="santry",
  185. owning_user_id=42,
  186. )
  187. assert slug == "a-mixed-casing-str-9e9173167"
  188. def test_slug_with_unicode_chars(self):
  189. unicoded_str = "Sí Señtry 😅"
  190. slug = generate_deterministic_organization_slug(
  191. desired_slug_base=unicoded_str, desired_org_name=unicoded_str, owning_user_id=42
  192. )
  193. assert slug == "si-sentry-3471b1b85"
  194. def test_slug_with_0_length(self):
  195. unicoded_str = "😅"
  196. slug = generate_deterministic_organization_slug(
  197. desired_slug_base=unicoded_str, desired_org_name=unicoded_str, owning_user_id=42
  198. )
  199. random_slug_regex = re.compile(r"^[a-f0-9]{10}-[a-f0-9]{9}")
  200. assert random_slug_regex.match(slug)
  201. slug = generate_deterministic_organization_slug(
  202. desired_slug_base="", desired_org_name=unicoded_str, owning_user_id=42
  203. )
  204. assert random_slug_regex.match(slug)