Projet

Général

Profil

0010-add-authentic2-agent-tests-8425.patch

Benjamin Dauvergne, 06 octobre 2015 11:21

Télécharger (7,29 ko)

Voir les différences:

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
jenkins.sh
9 9
pip install --upgrade pytest pytest-django pytest-cov
10 10
pip install --upgrade django-tenant-schemas
11 11
pip install --upgrade -r requirements.txt
12
pip install http://git.entrouvert.org/authentic.git/snapshot/authentic-master.tar.gz
12 13

  
13 14
DJANGO_SETTINGS_MODULE=hobo.settings \
14 15
HOBO_SETTINGS_FILE=tests/settings.py \
......
17 18

  
18 19
(cd tests_multitenant ; PYTHONPATH=. DJANGO_SETTINGS_MODULE=settings py.test --junitxml=../multitenant_test_results.xml --cov-report xml --cov=../hobo/ --cov-config .coveragerc .)
19 20
mv tests_multitenant/coverage.xml multitenant_coverage.xml
20
./merge-junit-results.py hobo_server_test_results.xml multitenant_test_results.xml >test_results.xml
21
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/
22
mv coverage.xml authentic2_agent_coverage.xml
23

  
24
./merge-junit-results.py hobo_server_test_results.xml multitenant_test_results.xml authentic2_agent_test_results.xml >test_results.xml
21 25
./merge-coverage.py -o coverage.xml *_coverage.xml
22 26

  
27

  
23 28
test -f pylint.out && cp pylint.out pylint.out.prev
24 29
(pylint -f parseable --rcfile /var/lib/jenkins/pylint.django.rc hobo | tee pylint.out) || /bin/true
25 30
test -f pylint.out.prev && (diff pylint.out.prev pylint.out | grep '^[><]' | grep .py) || /bin/true
tests_authentic/conftest.py
1
import os
2
import tempfile
3
import shutil
4
import json
5

  
6
import pytest
7

  
8
@pytest.fixture(scope='function')
9
def tenant(db, request, settings):
10
    from hobo.multitenant.models import Tenant
11
    base = tempfile.mkdtemp('combo-tenant-base')
12
    settings.TENANT_BASE = base
13
    @pytest.mark.django_db
14
    def make_tenant(name):
15
        tenant_dir = os.path.join(base, name)
16
        os.mkdir(tenant_dir)
17
        with open(os.path.join(tenant_dir, 'unsecure'), 'w') as fd:
18
            fd.write('1')
19
        with open(os.path.join(tenant_dir, 'settings.json'), 'w') as fd:
20
            json.dump({'HOBO_TEST_VARIABLE': name}, fd)
21
        with open(os.path.join(tenant_dir, 'hobo.json'), 'w') as fd:
22
            json.dump({
23
                'variables': {
24
                    'hobo_test_variable': True,
25
                    'other_variable': 'foo',
26
                },
27
                'services': [
28
                    {'slug': 'test',
29
                     'title': 'Test',
30
                     'this': True,
31
                     'secret_key': '12345',
32
                     'base_url': 'http://%s' % name,
33
                     'variables': {
34
                         'other_variable': 'bar',
35
                     }
36
                    },
37
                    {'slug': 'other',
38
                     'title': 'Other',
39
                     'base_url': 'http://other.example.net'},
40
                    ]}, fd)
41
        t = Tenant(domain_url=name,
42
                      schema_name=name.replace('-', '_').replace('.', '_'))
43
        t.create_schema()
44
        return t
45
    tenants = [make_tenant('authentic.example.net')]
46
    def fin():
47
        from django.db import connection
48
        connection.set_schema_to_public()
49
        for t in tenants:
50
            t.delete(True)
51
        shutil.rmtree(base)
52
    request.addfinalizer(fin)
53
    return tenants[0]
tests_authentic/settings.py
1
import os.path
2
import __builtin__ as builtin
3
from mock import mock_open, patch
4
import os
5

  
6
# Debian defaults
7
DEBUG = False
8

  
9
PROJECT_NAME = 'authentic2-multitenant'
10

  
11

  
12
with patch.object(builtin, 'file', mock_open(read_data='xxx')):
13
    execfile(os.environ['DEBIAN_CONFIG_COMMON'])
14

  
15
# Add the XForwardedForMiddleware
16
MIDDLEWARE_CLASSES = ('authentic2.middleware.XForwardedForMiddleware',) + MIDDLEWARE_CLASSES
17

  
18
# Add authentic settings loader
19
TENANT_SETTINGS_LOADERS = ('hobo.multitenant.settings_loaders.Authentic',) + TENANT_SETTINGS_LOADERS
20

  
21
# Add authentic2 hobo agent
22
INSTALLED_APPS = ('hobo.agent.authentic2',) + INSTALLED_APPS
23

  
24
CACHES = {
25
    'default': {
26
            'BACKEND': 'hobo.multitenant.cache.TenantCache',
27
            'REAL_BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
28
    }
29
}
30

  
31
HOBO_ROLE_EXPORT = True
tests_authentic/test_provisionning.py
1
# -*- coding: utf-8 -*-
2
import pytest
3

  
4
from mock import patch, MagicMock, call, ANY
5

  
6
from django.contrib.auth import get_user_model
7

  
8
from tenant_schemas.utils import tenant_context
9

  
10
from authentic2.a2_rbac.models import Role
11
from authentic2.a2_rbac.utils import get_default_ou
12

  
13
pytestmark = pytest.mark.django_db
14

  
15
def test_provision_role(tenant):
16
    with patch('hobo.agent.authentic2.apps.notify_agents') as notify_agents:
17
        with tenant_context(tenant):
18
            role = Role.objects.create(name='coin')
19
            assert notify_agents.call_count == 1
20
            arg = notify_agents.call_args
21
            assert arg == call(ANY)
22
            arg = arg[0][0]
23
            assert isinstance(arg, dict)
24
            assert set(arg.keys()) == set([
25
                'audience', '@type', 'objects', 'full'])
26
            assert arg['audience'] == []
27
            assert arg['@type'] == 'provision'
28
            assert arg['full'] == True
29
            objects = arg['objects']
30
            assert isinstance(objects, list)
31
            assert len(objects) == 2
32
            like_role = 0
33
            for o in objects:
34
                assert set(o.keys()) == set(['@type', 'emails_to_members',
35
                                             'description', 'uuid', 'name',
36
                                             'slug', 'emails'])
37
                assert o['@type'] == 'role'
38
                assert o['emails_to_members'] == False
39
                assert o['emails'] == []
40
                if o['uuid'] == role.uuid and o['name'] == role.name \
41
                   and o['description'] == role.description \
42
                   and o['slug'] == role.slug:
43
                    like_role += 1
44
            assert like_role == 1
0
-