From 300423d72c6b89603cf409df5f3169c4da2ac048 Mon Sep 17 00:00:00 2001 From: Josh Humphries Date: Thu, 21 Sep 2023 21:13:12 +0100 Subject: [PATCH 1/2] test: add test for login_failed helper --- tests/routes/__init__.py | 0 tests/routes/test_helpers.py | 21 +++++++++++++++++++++ 2 files changed, 21 insertions(+) create mode 100644 tests/routes/__init__.py create mode 100644 tests/routes/test_helpers.py diff --git a/tests/routes/__init__.py b/tests/routes/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/routes/test_helpers.py b/tests/routes/test_helpers.py new file mode 100644 index 0000000..c9ad6fe --- /dev/null +++ b/tests/routes/test_helpers.py @@ -0,0 +1,21 @@ +from unittest.mock import patch, MagicMock + +import pytest + +from ckan.lib.helpers import url_for +from ckanext.ldap.routes._helpers import login_failed + + +@pytest.mark.filterwarnings("ignore::sqlalchemy.exc.SADeprecationWarning") +@pytest.mark.usefixtures("with_request_context") +@patch("ckan.plugins.toolkit.h.flash_error") +@patch("ckan.plugins.toolkit.h.flash_notice") +def test_login_failed(flash_notice_mock: MagicMock, flash_error_mock: MagicMock): + notice = "A notice!" + error = "An error!" + response = login_failed(notice=notice, error=error) + + flash_notice_mock.assert_called_once_with(notice) + flash_error_mock.assert_called_once_with(error) + assert response.status_code == 302 + assert response.location.endswith(url_for("user.login")) From b68e13d7a209112182026171eec396de82544021 Mon Sep 17 00:00:00 2001 From: Josh Humphries Date: Thu, 21 Sep 2023 21:13:57 +0100 Subject: [PATCH 2/2] test: add a test for login_success This is an important test as it is something that works on CKAN 2.9 but not on CKAN 2.10. --- tests/routes/test_helpers.py | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/tests/routes/test_helpers.py b/tests/routes/test_helpers.py index c9ad6fe..c9a3aea 100644 --- a/tests/routes/test_helpers.py +++ b/tests/routes/test_helpers.py @@ -3,7 +3,7 @@ import pytest from ckan.lib.helpers import url_for -from ckanext.ldap.routes._helpers import login_failed +from ckanext.ldap.routes._helpers import login_failed, login_success @pytest.mark.filterwarnings("ignore::sqlalchemy.exc.SADeprecationWarning") @@ -19,3 +19,20 @@ def test_login_failed(flash_notice_mock: MagicMock, flash_error_mock: MagicMock) flash_error_mock.assert_called_once_with(error) assert response.status_code == 302 assert response.location.endswith(url_for("user.login")) + + +@pytest.mark.filterwarnings("ignore::sqlalchemy.exc.SADeprecationWarning") +@pytest.mark.usefixtures("with_request_context") +def test_login_success(): + username = "a_user" + came_from = "somewhere_else" + + with patch("ckanext.ldap.routes._helpers.session") as mock_session: + response = login_success(username, came_from) + + mock_session.__setitem__.assert_called_once_with("ckanext-ldap-user", username) + assert mock_session.save.called + assert response.status_code == 302 + assert response.location.endswith( + f'{url_for("user.logged_in")}?came_from={came_from}' + )