test_api.py 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. from django.urls import reverse
  2. from sentry.models import AuthIdentity, AuthProvider
  3. from sentry.testutils import AuthProviderTestCase
  4. from sentry.utils.auth import SSO_SESSION_KEY
  5. from sentry.utils.linksign import generate_signed_link
  6. class AuthenticationTest(AuthProviderTestCase):
  7. def test_sso_auth_required(self):
  8. user = self.create_user("foo@example.com", is_superuser=False)
  9. organization = self.create_organization(name="foo")
  10. team = self.create_team(name="bar", organization=organization)
  11. project = self.create_project(name="baz", organization=organization, teams=[team])
  12. member = self.create_member(user=user, organization=organization, teams=[team])
  13. setattr(member.flags, "sso:linked", True)
  14. member.save()
  15. event = self.store_event(data={}, project_id=project.id)
  16. group_id = event.group_id
  17. auth_provider = AuthProvider.objects.create(
  18. organization=organization, provider="dummy", flags=0
  19. )
  20. AuthIdentity.objects.create(auth_provider=auth_provider, user=user)
  21. self.login_as(user)
  22. paths = (
  23. f"/api/0/organizations/{organization.slug}/",
  24. f"/api/0/projects/{organization.slug}/{project.slug}/",
  25. f"/api/0/teams/{organization.slug}/{team.slug}/",
  26. f"/api/0/issues/{group_id}/",
  27. # this uses the internal API, which once upon a time was broken
  28. f"/api/0/issues/{group_id}/events/latest/",
  29. )
  30. for path in paths:
  31. # we should be redirecting the user to the authentication form as they
  32. # haven't verified this specific organization
  33. resp = self.client.get(path)
  34. assert resp.status_code == 401, (resp.status_code, resp.content)
  35. # superuser should still require SSO as they're a member of the org
  36. user.update(is_superuser=True)
  37. for path in paths:
  38. resp = self.client.get(path)
  39. assert resp.status_code == 401, (resp.status_code, resp.content)
  40. # XXX(dcramer): using internal API as exposing a request object is hard
  41. self.session[SSO_SESSION_KEY] = str(organization.id)
  42. self.save_session()
  43. # now that SSO is marked as complete, we should be able to access dash
  44. for path in paths:
  45. resp = self.client.get(path)
  46. assert resp.status_code == 200, (path, resp.status_code, resp.content)
  47. def test_sso_auth_required_signed_link(self):
  48. user = self.create_user("foo@example.com", is_superuser=False)
  49. organization = self.create_organization(name="foo")
  50. team = self.create_team(name="bar", organization=organization)
  51. project = self.create_project(name="baz", organization=organization, teams=[team])
  52. member = self.create_member(user=user, organization=organization, teams=[team])
  53. setattr(member.flags, "sso:linked", True)
  54. member.save()
  55. self.store_event(data={}, project_id=project.id)
  56. auth_provider = AuthProvider.objects.create(
  57. organization=organization, provider="dummy", flags=0
  58. )
  59. AuthIdentity.objects.create(auth_provider=auth_provider, user=user)
  60. self.login_as(user)
  61. unsigned_link = reverse(
  62. "sentry-api-0-project-fix-processing-issues",
  63. kwargs={"project_slug": project.slug, "organization_slug": organization.slug},
  64. )
  65. resp = self.client.get(unsigned_link)
  66. assert resp.status_code == 401, (resp.status_code, resp.content)
  67. signed_link = generate_signed_link(
  68. user,
  69. "sentry-api-0-project-fix-processing-issues",
  70. kwargs={"project_slug": project.slug, "organization_slug": organization.slug},
  71. )
  72. resp = self.client.get(signed_link)
  73. assert resp.status_code == 200