test_link_team.py 2.7 KB

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