test_link_team.py 2.6 KB

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