test_sso.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. from sentry.models import AuthIdentity, AuthProvider
  2. from sentry.testutils.cases import AuthProviderTestCase
  3. from sentry.utils.auth import SsoSession
  4. # @control_silo_test(stable=True)
  5. class OrganizationAuthLoginTest(AuthProviderTestCase):
  6. def test_sso_auth_required(self):
  7. user = self.create_user("foo@example.com", is_superuser=False)
  8. organization = self.create_organization(name="foo")
  9. member = self.create_member(user=user, organization=organization)
  10. setattr(member.flags, "sso:linked", True)
  11. member.save()
  12. auth_provider = AuthProvider.objects.create(
  13. organization_id=organization.id, provider="dummy", flags=0
  14. )
  15. AuthIdentity.objects.create(auth_provider=auth_provider, user=user)
  16. self.login_as(user)
  17. path = f"/{organization.slug}/"
  18. redirect_uri = f"/auth/login/{organization.slug}/?next=%2Ffoo%2F"
  19. # we should be redirecting the user to the authentication form as they
  20. # haven't verified this specific organization
  21. resp = self.client.get(path)
  22. self.assertRedirects(resp, redirect_uri)
  23. # superuser should still require SSO as they're a member of the org
  24. user.update(is_superuser=True)
  25. resp = self.client.get(path)
  26. self.assertRedirects(resp, redirect_uri)
  27. # XXX(dcramer): using internal API as exposing a request object is hard
  28. sso_session = SsoSession.create(organization.id)
  29. self.session[sso_session.session_key] = sso_session.to_dict()
  30. self.save_session()
  31. # now that SSO is marked as complete, we should be able to access dash
  32. resp = self.client.get(path)
  33. assert resp.status_code == 200