From 1977f8bd19de3db19e08fef508e7a791803da4b8 Mon Sep 17 00:00:00 2001 From: avdata99 Date: Mon, 28 Oct 2024 10:13:45 -0300 Subject: [PATCH] Add test for _get_user_by_email --- .../saml2auth/tests/test_get_user_by_email.py | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 ckanext/saml2auth/tests/test_get_user_by_email.py diff --git a/ckanext/saml2auth/tests/test_get_user_by_email.py b/ckanext/saml2auth/tests/test_get_user_by_email.py new file mode 100644 index 0000000..0bf2398 --- /dev/null +++ b/ckanext/saml2auth/tests/test_get_user_by_email.py @@ -0,0 +1,43 @@ +import pytest +from types import SimpleNamespace +import ckan.model as model +from ckan.tests import factories +from ckan.plugins import toolkit +from ckanext.saml2auth.views.saml2auth import _get_user_by_email + + +@pytest.fixture +def tdv_data(): + """TestDatasetViews setup data""" + obj = SimpleNamespace() + obj.user1 = factories.User( + email='user1@example.com', + plugin_extras={'saml2auth': {'saml_id': 'saml_id1'}} + ) + obj.user2 = factories.User( + email='user2@example.com', + plugin_extras={'saml2auth': {'saml_id': 'saml_id2'}} + ) + + +@pytest.mark.usefixtures(u'clean_db', u'clean_index') +@pytest.mark.ckan_config(u'ckan.plugins', u'saml2auth') +class TestDatasetViews(object): + def test_get_user_by_email_empty(self, tdv_data): + """ The the function _get_user_by_email for empty response """ + ret = _get_user_by_email('user3@example.com') + assert ret is None + + def test_get_user_by_email_ok(self, tdv_data): + """ The the function _get_user_by_email for empty response """ + ret = _get_user_by_email(tdv_data.user1.email) + assert ret is not None + assert ret.email == tdv_data.user1.email + + def test_get_user_by_email_multiple(self, tdv_data): + """ The the function _get_user_by_email for duplicated emails """ + # Generate a duplciate email + tdv_data.user2.email = tdv_data.user1.email + model.Session.commit() + with pytest.raises(toolkit.ValidationError): + _get_user_by_email(tdv_data.user1.email)