test_sso.py 1.7 KB

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