test_link_team.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. from urllib.parse import urlparse
  2. from sentry.integrations.slack.views.link_team import build_team_linking_url
  3. from sentry.models import (
  4. ExternalActor,
  5. ExternalProviders,
  6. Identity,
  7. IdentityProvider,
  8. IdentityStatus,
  9. Integration,
  10. )
  11. from sentry.testutils import AcceptanceTestCase
  12. from sentry.testutils.silo import region_silo_test
  13. @region_silo_test
  14. class SlackLinkTeamTest(AcceptanceTestCase):
  15. def setUp(self):
  16. super().setUp()
  17. self.user = self.create_user("foo@example.com")
  18. self.org = self.create_organization(name="Rowdy Tiger", owner=self.user)
  19. self.team = self.create_team(organization=self.org, name="Mariachi Band")
  20. self.member = self.create_member(
  21. user=None,
  22. email="bar@example.com",
  23. organization=self.org,
  24. role="owner",
  25. teams=[self.team],
  26. )
  27. self.create_team_membership(user=self.user, team=self.team)
  28. self.integration = Integration.objects.create(
  29. provider="slack",
  30. name="Team A",
  31. external_id="TXXXXXXX1",
  32. metadata={
  33. "access_token": "xoxp-xxxxxxxxx-xxxxxxxxxx-xxxxxxxxxxxx",
  34. "installation_type": "born_as_bot",
  35. },
  36. )
  37. self.integration.add_organization(self.org, self.user)
  38. self.idp = IdentityProvider.objects.create(type="slack", external_id="TXXXXXXX1", config={})
  39. self.identity = Identity.objects.create(
  40. external_id="UXXXXXXX1",
  41. idp=self.idp,
  42. user=self.user,
  43. status=IdentityStatus.VALID,
  44. scopes=[],
  45. )
  46. linking_url = urlparse(
  47. build_team_linking_url(
  48. self.integration,
  49. "UXXXXXXX1",
  50. "CXXXXXXX9",
  51. "general",
  52. "http://example.slack.com/response_url",
  53. )
  54. )
  55. self.path = linking_url.path
  56. def test_link_team(self):
  57. self.login_as(self.user)
  58. self.browser.get(self.path)
  59. self.browser.wait_until_not(".loading")
  60. self.browser.click('[name="team"]')
  61. self.browser.click(f'[value="{self.team.id}"]')
  62. self.browser.snapshot(name="slack link team select team")
  63. self.browser.click('[type="submit"]')
  64. self.browser.wait_until_not(".loading")
  65. self.browser.snapshot(name="slack post linked team")
  66. assert ExternalActor.objects.filter(
  67. actor_id=self.team.actor_id,
  68. organization=self.org,
  69. integration_id=self.integration.id,
  70. provider=ExternalProviders.SLACK.value,
  71. external_name="general",
  72. external_id="CXXXXXXX9",
  73. ).exists()