|
13 | 13 | from django.urls import NoReverseMatch
|
14 | 14 | from saml2 import BINDING_HTTP_POST
|
15 | 15 |
|
| 16 | +from django_saml2_auth.errors import INACTIVE_USER |
16 | 17 | from django_saml2_auth.exceptions import SAMLAuthError
|
17 | 18 | from django_saml2_auth.saml import (
|
18 | 19 | decode_saml_response,
|
@@ -771,3 +772,81 @@ def test_get_metadata_success_with_custom_trigger(settings: SettingsWrapper):
|
771 | 772 | get_metadata(domain="not-mapped-example.com")
|
772 | 773 |
|
773 | 774 | assert str(exc_info.value) == "Domain not-mapped-example.com not mapped!"
|
| 775 | + |
| 776 | + |
| 777 | +@pytest.mark.django_db |
| 778 | +@responses.activate |
| 779 | +def test_acs_view_when_use_jwt_set_redirects_user( |
| 780 | + settings: SettingsWrapper, |
| 781 | + monkeypatch: "MonkeyPatch", # type: ignore # noqa: F821 |
| 782 | +): |
| 783 | + """Test Acs view when USE_JWT is set that the user is correctly redirected""" |
| 784 | + responses.add(responses.GET, METADATA_URL1, body=METADATA1) |
| 785 | + settings.SAML2_AUTH = { |
| 786 | + "DEFAULT_NEXT_URL": "default_next_url", |
| 787 | + "USE_JWT": True, |
| 788 | + "JWT_SECRET": "JWT_SECRET", |
| 789 | + "JWT_ALGORITHM": "HS256", |
| 790 | + "FRONTEND_URL": "https://app.example.com/account/login/saml", |
| 791 | + "TRIGGER": { |
| 792 | + "BEFORE_LOGIN": None, |
| 793 | + "AFTER_LOGIN": None, |
| 794 | + "GET_METADATA_AUTO_CONF_URLS": GET_METADATA_AUTO_CONF_URLS, |
| 795 | + }, |
| 796 | + } |
| 797 | + post_request = RequestFactory().post(METADATA_URL1, {"SAMLResponse": "SAML RESPONSE"}) |
| 798 | + monkeypatch.setattr( |
| 799 | + Saml2Client, "parse_authn_request_response", mock_parse_authn_request_response |
| 800 | + ) |
| 801 | + created, mock_user = user.get_or_create_user( |
| 802 | + { "username": "[email protected]", "first_name": "John", "last_name": "Doe"} |
| 803 | + ) |
| 804 | + monkeypatch.setattr(user, "get_or_create_user", (created, mock_user)) |
| 805 | + |
| 806 | + middleware = SessionMiddleware(MagicMock()) |
| 807 | + middleware.process_request(post_request) |
| 808 | + post_request.session.save() |
| 809 | + |
| 810 | + result = acs(post_request) |
| 811 | + assert result.status_code == 302 |
| 812 | + assert "https://app.example.com/account/login/saml?token=eyJ" in result.url |
| 813 | + |
| 814 | + |
| 815 | +@pytest.mark.django_db |
| 816 | +@responses.activate |
| 817 | +def test_acs_view_use_jwt_set_inactive_user( |
| 818 | + settings: SettingsWrapper, |
| 819 | + monkeypatch: "MonkeyPatch", # type: ignore # noqa: F821 |
| 820 | +): |
| 821 | + """Test Acs view when USE_JWT is set that inactive users can not log in""" |
| 822 | + responses.add(responses.GET, METADATA_URL1, body=METADATA1) |
| 823 | + settings.SAML2_AUTH = { |
| 824 | + "DEFAULT_NEXT_URL": "default_next_url", |
| 825 | + "USE_JWT": True, |
| 826 | + "JWT_SECRET": "JWT_SECRET", |
| 827 | + "JWT_ALGORITHM": "HS256", |
| 828 | + "FRONTEND_URL": "https://app.example.com/account/login/saml", |
| 829 | + "TRIGGER": { |
| 830 | + "BEFORE_LOGIN": None, |
| 831 | + "AFTER_LOGIN": None, |
| 832 | + "GET_METADATA_AUTO_CONF_URLS": GET_METADATA_AUTO_CONF_URLS, |
| 833 | + }, |
| 834 | + } |
| 835 | + post_request = RequestFactory().post(METADATA_URL1, {"SAMLResponse": "SAML RESPONSE"}) |
| 836 | + monkeypatch.setattr( |
| 837 | + Saml2Client, "parse_authn_request_response", mock_parse_authn_request_response |
| 838 | + ) |
| 839 | + created, mock_user = user.get_or_create_user( |
| 840 | + { "username": "[email protected]", "first_name": "John", "last_name": "Doe"} |
| 841 | + ) |
| 842 | + mock_user.is_active = False |
| 843 | + mock_user.save() |
| 844 | + monkeypatch.setattr(user, "get_or_create_user", (created, mock_user)) |
| 845 | + |
| 846 | + middleware = SessionMiddleware(MagicMock()) |
| 847 | + middleware.process_request(post_request) |
| 848 | + post_request.session.save() |
| 849 | + |
| 850 | + result = acs(post_request) |
| 851 | + assert result.status_code == 500 |
| 852 | + assert f"Error code: {INACTIVE_USER}" in result.content.decode() |
0 commit comments