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.identity import Identity, IdentityProvider, IdentityStatus
  4. from sentry.models.integrations.external_actor import ExternalActor
  5. from sentry.models.integrations.integration import Integration
  6. from sentry.testutils.cases import AcceptanceTestCase
  7. from sentry.testutils.silo import no_silo_test
  8. from sentry.types.integrations import ExternalProviders
  9. @no_silo_test(stable=True)
  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="Mariachi Band")
  16. self.member = 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.integration = Integration.objects.create(
  25. provider="slack",
  26. name="Team A",
  27. external_id="TXXXXXXX1",
  28. metadata={
  29. "access_token": "xoxp-xxxxxxxxx-xxxxxxxxxx-xxxxxxxxxxxx",
  30. "installation_type": "born_as_bot",
  31. },
  32. )
  33. self.integration.add_organization(self.org, self.user)
  34. self.idp = IdentityProvider.objects.create(type="slack", external_id="TXXXXXXX1", config={})
  35. self.identity = Identity.objects.create(
  36. external_id="UXXXXXXX1",
  37. idp=self.idp,
  38. user=self.user,
  39. status=IdentityStatus.VALID,
  40. scopes=[],
  41. )
  42. linking_url = urlparse(
  43. build_team_linking_url(
  44. self.integration,
  45. "UXXXXXXX1",
  46. "CXXXXXXX9",
  47. "general",
  48. "http://example.slack.com/response_url",
  49. )
  50. )
  51. self.path = linking_url.path
  52. def test_link_team(self):
  53. self.login_as(self.user)
  54. self.browser.get(self.path)
  55. self.browser.wait_until_not(".loading")
  56. self.browser.click('[name="team"]')
  57. self.browser.click(f'[value="{self.team.id}"]')
  58. self.browser.click('[type="submit"]')
  59. self.browser.wait_until_not(".loading")
  60. assert ExternalActor.objects.filter(
  61. team_id=self.team.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()