test_link_team.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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.click('[type="submit"]')
  57. self.browser.wait_until_not(".loading")
  58. assert ExternalActor.objects.filter(
  59. team_id=self.team.id,
  60. organization=self.org,
  61. integration_id=self.integration.id,
  62. provider=ExternalProviders.SLACK.value,
  63. external_name="general",
  64. external_id="CXXXXXXX9",
  65. ).exists()