From 150d1e38e0c2c362603de49a7dab538e2b184993 Mon Sep 17 00:00:00 2001 From: Benjamin Dauvergne Date: Tue, 6 Oct 2015 11:20:11 +0200 Subject: [PATCH 10/10] add authentic2 agent tests (#8425) --- jenkins.sh | 7 ++++- tests_authentic/conftest.py | 53 +++++++++++++++++++++++++++++++++++ tests_authentic/settings.py | 31 ++++++++++++++++++++ tests_authentic/test_provisionning.py | 44 +++++++++++++++++++++++++++++ 4 files changed, 134 insertions(+), 1 deletion(-) create mode 100644 tests_authentic/conftest.py create mode 100644 tests_authentic/settings.py create mode 100644 tests_authentic/test_provisionning.py diff --git a/jenkins.sh b/jenkins.sh index aba2b9a..244f1dc 100755 --- a/jenkins.sh +++ b/jenkins.sh @@ -9,6 +9,7 @@ pip install --upgrade pylint==1.4.0 astroid==1.3.2 # 1.4.1 is buggy pip install --upgrade pytest pytest-django pytest-cov pip install --upgrade django-tenant-schemas pip install --upgrade -r requirements.txt +pip install http://git.entrouvert.org/authentic.git/snapshot/authentic-master.tar.gz DJANGO_SETTINGS_MODULE=hobo.settings \ HOBO_SETTINGS_FILE=tests/settings.py \ @@ -17,9 +18,13 @@ mv coverage.xml hobo_server_coverage.xml (cd tests_multitenant ; PYTHONPATH=. DJANGO_SETTINGS_MODULE=settings py.test --junitxml=../multitenant_test_results.xml --cov-report xml --cov=../hobo/ --cov-config .coveragerc .) mv tests_multitenant/coverage.xml multitenant_coverage.xml -./merge-junit-results.py hobo_server_test_results.xml multitenant_test_results.xml >test_results.xml +DEBIAN_CONFIG_COMMON=debian/debian_config_common.py DJANGO_SETTINGS_MODULE=authentic2.settings AUTHENTIC2_SETTINGS_FILE=tests_authentic/settings.py py.test --junitxml=authentic2_agent_test_results.xml --cov-report xml --cov=hobo/ --cov-config .coveragerc --nomigration tests_authentic/ +mv coverage.xml authentic2_agent_coverage.xml + +./merge-junit-results.py hobo_server_test_results.xml multitenant_test_results.xml authentic2_agent_test_results.xml >test_results.xml ./merge-coverage.py -o coverage.xml *_coverage.xml + test -f pylint.out && cp pylint.out pylint.out.prev (pylint -f parseable --rcfile /var/lib/jenkins/pylint.django.rc hobo | tee pylint.out) || /bin/true test -f pylint.out.prev && (diff pylint.out.prev pylint.out | grep '^[><]' | grep .py) || /bin/true diff --git a/tests_authentic/conftest.py b/tests_authentic/conftest.py new file mode 100644 index 0000000..9cbb1bf --- /dev/null +++ b/tests_authentic/conftest.py @@ -0,0 +1,53 @@ +import os +import tempfile +import shutil +import json + +import pytest + +@pytest.fixture(scope='function') +def tenant(db, request, settings): + from hobo.multitenant.models import Tenant + base = tempfile.mkdtemp('combo-tenant-base') + settings.TENANT_BASE = base + @pytest.mark.django_db + def make_tenant(name): + tenant_dir = os.path.join(base, name) + os.mkdir(tenant_dir) + with open(os.path.join(tenant_dir, 'unsecure'), 'w') as fd: + fd.write('1') + with open(os.path.join(tenant_dir, 'settings.json'), 'w') as fd: + json.dump({'HOBO_TEST_VARIABLE': name}, fd) + with open(os.path.join(tenant_dir, 'hobo.json'), 'w') as fd: + json.dump({ + 'variables': { + 'hobo_test_variable': True, + 'other_variable': 'foo', + }, + 'services': [ + {'slug': 'test', + 'title': 'Test', + 'this': True, + 'secret_key': '12345', + 'base_url': 'http://%s' % name, + 'variables': { + 'other_variable': 'bar', + } + }, + {'slug': 'other', + 'title': 'Other', + 'base_url': 'http://other.example.net'}, + ]}, fd) + t = Tenant(domain_url=name, + schema_name=name.replace('-', '_').replace('.', '_')) + t.create_schema() + return t + tenants = [make_tenant('authentic.example.net')] + def fin(): + from django.db import connection + connection.set_schema_to_public() + for t in tenants: + t.delete(True) + shutil.rmtree(base) + request.addfinalizer(fin) + return tenants[0] diff --git a/tests_authentic/settings.py b/tests_authentic/settings.py new file mode 100644 index 0000000..27d7d42 --- /dev/null +++ b/tests_authentic/settings.py @@ -0,0 +1,31 @@ +import os.path +import __builtin__ as builtin +from mock import mock_open, patch +import os + +# Debian defaults +DEBUG = False + +PROJECT_NAME = 'authentic2-multitenant' + + +with patch.object(builtin, 'file', mock_open(read_data='xxx')): + execfile(os.environ['DEBIAN_CONFIG_COMMON']) + +# Add the XForwardedForMiddleware +MIDDLEWARE_CLASSES = ('authentic2.middleware.XForwardedForMiddleware',) + MIDDLEWARE_CLASSES + +# Add authentic settings loader +TENANT_SETTINGS_LOADERS = ('hobo.multitenant.settings_loaders.Authentic',) + TENANT_SETTINGS_LOADERS + +# Add authentic2 hobo agent +INSTALLED_APPS = ('hobo.agent.authentic2',) + INSTALLED_APPS + +CACHES = { + 'default': { + 'BACKEND': 'hobo.multitenant.cache.TenantCache', + 'REAL_BACKEND': 'django.core.cache.backends.locmem.LocMemCache', + } +} + +HOBO_ROLE_EXPORT = True diff --git a/tests_authentic/test_provisionning.py b/tests_authentic/test_provisionning.py new file mode 100644 index 0000000..c347890 --- /dev/null +++ b/tests_authentic/test_provisionning.py @@ -0,0 +1,44 @@ +# -*- coding: utf-8 -*- +import pytest + +from mock import patch, MagicMock, call, ANY + +from django.contrib.auth import get_user_model + +from tenant_schemas.utils import tenant_context + +from authentic2.a2_rbac.models import Role +from authentic2.a2_rbac.utils import get_default_ou + +pytestmark = pytest.mark.django_db + +def test_provision_role(tenant): + with patch('hobo.agent.authentic2.apps.notify_agents') as notify_agents: + with tenant_context(tenant): + role = Role.objects.create(name='coin') + assert notify_agents.call_count == 1 + arg = notify_agents.call_args + assert arg == call(ANY) + arg = arg[0][0] + assert isinstance(arg, dict) + assert set(arg.keys()) == set([ + 'audience', '@type', 'objects', 'full']) + assert arg['audience'] == [] + assert arg['@type'] == 'provision' + assert arg['full'] == True + objects = arg['objects'] + assert isinstance(objects, list) + assert len(objects) == 2 + like_role = 0 + for o in objects: + assert set(o.keys()) == set(['@type', 'emails_to_members', + 'description', 'uuid', 'name', + 'slug', 'emails']) + assert o['@type'] == 'role' + assert o['emails_to_members'] == False + assert o['emails'] == [] + if o['uuid'] == role.uuid and o['name'] == role.name \ + and o['description'] == role.description \ + and o['slug'] == role.slug: + like_role += 1 + assert like_role == 1 -- 2.1.4