test_link_team.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. from urllib.parse import urlparse
  2. from selenium.webdriver.common.by import By
  3. from sentry.integrations.models.external_actor import ExternalActor
  4. from sentry.integrations.slack.views.link_team import build_team_linking_url
  5. from sentry.integrations.types import ExternalProviders
  6. from sentry.testutils.cases import AcceptanceTestCase
  7. from sentry.testutils.silo import no_silo_test
  8. from sentry.users.models.identity import Identity, IdentityStatus
  9. @no_silo_test
  10. class SlackLinkTeamTest(AcceptanceTestCase):
  11. def setUp(self):
  12. super().setUp()
  13. self.user = self.create_user("foo@example.com")
  14. self.org = self.create_organization(name="Rowdy Tiger", owner=self.user)
  15. self.team = self.create_team(organization=self.org, name="Team One")
  16. self.create_member(
  17. user=None,
  18. email="bar@example.com",
  19. organization=self.org,
  20. role="owner",
  21. teams=[self.team],
  22. )
  23. self.create_team_membership(user=self.user, team=self.team)
  24. self.team_admin_user = self.create_user()
  25. self.create_member(
  26. user=self.team_admin_user,
  27. team_roles=[(self.team, "admin")],
  28. organization=self.org,
  29. role="member",
  30. )
  31. self.integration = self.create_provider_integration(
  32. provider="slack",
  33. name="Team A",
  34. external_id="TXXXXXXX1",
  35. metadata={
  36. "access_token": "xoxp-xxxxxxxxx-xxxxxxxxxx-xxxxxxxxxxxx",
  37. "installation_type": "born_as_bot",
  38. },
  39. )
  40. self.integration.add_organization(self.org, self.user)
  41. self.idp = self.create_identity_provider(type="slack", external_id="TXXXXXXX1")
  42. self.identity = Identity.objects.create(
  43. external_id="UXXXXXXX1",
  44. idp=self.idp,
  45. user=self.user,
  46. status=IdentityStatus.VALID,
  47. scopes=[],
  48. )
  49. linking_url = urlparse(
  50. build_team_linking_url(
  51. self.integration,
  52. "UXXXXXXX1",
  53. "CXXXXXXX9",
  54. "general",
  55. "http://example.slack.com/response_url",
  56. )
  57. )
  58. self.path = linking_url.path
  59. def test_link_team(self):
  60. self.login_as(self.user)
  61. self.browser.get(self.path)
  62. self.browser.wait_until_not(".loading")
  63. self.browser.click('[name="team"]')
  64. self.browser.click(f'[value="{self.team.id}"]')
  65. self.browser.click('[type="submit"]')
  66. self.browser.wait_until_not(".loading")
  67. assert ExternalActor.objects.filter(
  68. team_id=self.team.id,
  69. organization=self.org,
  70. integration_id=self.integration.id,
  71. provider=ExternalProviders.SLACK.value,
  72. external_name="general",
  73. external_id="CXXXXXXX9",
  74. ).exists()
  75. def test_link_team_as_team_admin(self):
  76. self.create_team(organization=self.org, name="Team Two")
  77. self.create_team(organization=self.org, name="Team Three")
  78. self.login_as(self.team_admin_user)
  79. self.browser.get(self.path)
  80. self.browser.wait_until_not(".loading")
  81. self.browser.click('[name="team"]')
  82. select_element = self.browser.find_element(by=By.ID, value="id_team")
  83. option_elements = select_element.find_elements(by=By.TAG_NAME, value="option")
  84. # Ensure only the team the user is team admin is on is shown
  85. assert len(option_elements) == 1
  86. self.browser.click(f'[value="{self.team.id}"]')
  87. self.browser.click('[type="submit"]')
  88. self.browser.wait_until_not(".loading")
  89. assert ExternalActor.objects.filter(
  90. team_id=self.team.id,
  91. organization=self.org,
  92. integration_id=self.integration.id,
  93. provider=ExternalProviders.SLACK.value,
  94. external_name="general",
  95. external_id="CXXXXXXX9",
  96. ).exists()