summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorFrédéric Péters <fpeters@entrouvert.com>2015-05-07 09:20:31 (GMT)
committerFrédéric Péters <fpeters@entrouvert.com>2015-05-07 09:32:53 (GMT)
commit0315d395bf33bd02cdb1888e1e0bcf43f2c21130 (patch)
tree4542e72c5230f98892c1640bb2780f28979fc541 /tests
parente5c947a05287527050591232f0c4ce1571f7b937 (diff)
downloaddjango-mellon-0315d395bf33bd02cdb1888e1e0bcf43f2c21130.zip
django-mellon-0315d395bf33bd02cdb1888e1e0bcf43f2c21130.tar.gz
django-mellon-0315d395bf33bd02cdb1888e1e0bcf43f2c21130.tar.bz2
tests: initial adapter tests
Diffstat (limited to 'tests')
-rw-r--r--tests/test_default_adapter.py79
1 files changed, 79 insertions, 0 deletions
diff --git a/tests/test_default_adapter.py b/tests/test_default_adapter.py
new file mode 100644
index 0000000..0f910da
--- /dev/null
+++ b/tests/test_default_adapter.py
@@ -0,0 +1,79 @@
+import pytest
+
+from django.conf import settings
+from django.contrib import auth
+
+from mellon.adapters import DefaultAdapter
+
+pytestmark = pytest.mark.django_db
+
+idp = {}
+saml_attributes = {
+ 'name_id_content': 'x'*32,
+ 'issuer': 'https://idp.example.net/saml/metadata',
+ 'username': ['foobar'],
+ 'email': ['test@example.net'],
+ 'first_name': ['Foo'],
+ 'last_name': ['Bar'],
+ 'is_superuser': ['true'],
+}
+
+def test_format_username(settings):
+ adapter = DefaultAdapter()
+ assert adapter.format_username(idp, {}) == None
+ assert adapter.format_username(idp, saml_attributes) == 'x'*32 + '@saml'
+ settings.MELLON_USERNAME_TEMPLATE = '{attributes[name_id_content]}'
+ assert adapter.format_username(idp, saml_attributes) == 'x'*32
+ settings.MELLON_USERNAME_TEMPLATE = '{attributes[username][0]}'
+ assert adapter.format_username(idp, saml_attributes) == 'foobar'
+
+def test_lookup_user(settings):
+ User = auth.get_user_model()
+ adapter = DefaultAdapter()
+ user = adapter.lookup_user(idp, saml_attributes)
+ assert user is not None
+
+ user2 = adapter.lookup_user(idp, saml_attributes)
+ assert user.id == user2.id
+
+ User.objects.all().delete()
+ assert User.objects.count() == 0
+
+ settings.MELLON_PROVISION = False
+ user = adapter.lookup_user(idp, saml_attributes)
+ assert user is None
+ assert User.objects.count() == 0
+
+def test_provision(settings):
+ User = auth.get_user_model()
+ adapter = DefaultAdapter()
+ settings.MELLON_ATTRIBUTE_MAPPING = {
+ 'email': '{attributes[email][0]}',
+ 'first_name': '{attributes[first_name][0]}',
+ 'last_name': '{attributes[last_name][0]}',
+ }
+ user = User(username='xx')
+ user.save()
+ adapter.provision(user, idp, saml_attributes)
+ assert user.first_name == 'Foo'
+ assert user.last_name == 'Bar'
+ assert user.email == 'test@example.net'
+ assert user.is_superuser == False
+ User.objects.all().delete()
+
+ settings.MELLON_SUPERUSER_MAPPING = {
+ 'is_superuser': 'true',
+ }
+ user = User(username='xx')
+ user.save()
+ adapter.provision(user, idp, saml_attributes)
+ assert user.is_superuser == True
+ User.objects.all().delete()
+
+ local_saml_attributes = saml_attributes.copy()
+ del local_saml_attributes['email']
+ user = User(username='xx')
+ user.save()
+ adapter.provision(user, idp, local_saml_attributes)
+ assert not user.email
+ User.objects.all().delete()