test_link_team.py 4.0 KB

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