Projet

Général

Profil

0005-hobo_deploy-allow-several-templates-for-user-agent-c.patch

Nicolas Roche, 21 juin 2019 18:15

Télécharger (10,8 ko)

Voir les différences:

Subject: [PATCH 5/5] hobo_deploy: allow several templates for user/agent combo
 portals (#33876)

 .../combo/management/commands/import_template.py |  5 +++--
 hobo/matomo/utils.py                             |  2 +-
 hobo/multitenant/settings_loaders.py             | 13 +++++++------
 tests/test_import_template.py                    |  4 ++--
 tests/test_matomo_utils.py                       | 16 +++++++++++-----
 tests_multitenant/conftest.py                    | 15 ++++++++++++++-
 tests_multitenant/test_settings.py               |  2 ++
 7 files changed, 40 insertions(+), 17 deletions(-)
hobo/agent/combo/management/commands/import_template.py
22 22
        try:
23 23
            return super(Command, self).handle(*args, **kwargs)
24 24
        except import_template.UnknownTemplateError:
25
            # ignore errors if template name is portal-user or portal-agent as
25
            # ignore errors if template name contains portal-user or portal-agent as
26 26
            # those names do not actually require an existing file to work.
27
            if kwargs.get('template_name') not in ('portal-user', 'portal-agent'):
27
            if not any(name in kwargs.get('template_name')
28
                       for name in ('portal-user', 'portal-agent')):
28 29
                raise
hobo/matomo/utils.py
89 89
def get_tenant_name_and_public_urls():
90 90
    """get an alias for our matomo's id and urls to monitor"""
91 91
    tenant_name = None
92
    services = [x for x in Combo.objects.all() if x.template_name == 'portal-user']
92
    services = [x for x in Combo.objects.all() if 'portal-user' in x.template_name]
93 93
    if services != [] and services[0] != '':
94 94
        tenant_name = urlparse.urlparse(services[0].base_url).netloc
95 95
    services += [x for x in Wcs.objects.all()]
hobo/multitenant/settings_loaders.py
99 99
                        service.get('title'),
100 100
                        service.get('variables').get('ou-label'))
101 101

  
102
            if service.get('service-id') == 'combo' and service.get('template_name') == 'portal-agent':
102
            if (service.get('service-id') == 'combo' and
103
                    'portal-agent' in service.get('template_name', '')):
103 104
                service_data['is-portal-agent'] = True
104 105

  
105 106
            # compute a symmetric shared secret using XOR
......
132 133
            variables['%s_url' % service.get('slug').replace('-','_')] = service.get('base_url')
133 134

  
134 135
            if service.get('service-id') == 'combo' and not service.get('secondary'):
135
                if service.get('template_name') == 'portal-agent':
136
                if 'portal-agent' in service.get('template_name', ''):
136 137
                    variables['portal_agent_url'] = service.get('base_url')
137 138
                    variables['portal_agent_title'] = service.get('title')
138
                if service.get('template_name') == 'portal-user':
139
                if 'portal-user' in service.get('template_name', ''):
139 140
                    variables['portal_user_url'] = service.get('base_url')
140 141
                    variables['portal_user_title'] = service.get('title')
141 142

  
......
151 152
            variables.update(service.get('variables') or {})
152 153
            variables['site_title'] = service.get('title')
153 154

  
154
            if service.get('template_name') == 'portal-agent':
155
            if 'portal-agent' in service.get('template_name', ''):
155 156
                variables['is_portal_agent'] = True
156 157

  
157 158
        if getattr(settings, 'HOBO_MANAGER_HOMEPAGE_TITLE_VAR', None):
......
258 259
        for service in hobo_json.get('services', []):
259 260
            if service.get('service-id') != 'combo':
260 261
                continue
261
            if service.get('template_name') != 'portal-user':
262
            if 'portal-user' not in service.get('template_name', ''):
262 263
                continue
263 264
            if service.get('secondary'):
264 265
                continue
......
337 338
        for service in hobo_json.get('services', []):
338 339
            if service.get('service-id') != 'combo':
339 340
                continue
340
            if service.get('template_name') != 'portal-user':
341
            if 'portal-user' not in service.get('template_name', ''):
341 342
                continue
342 343
            tenant_settings.A2_HOMEPAGE_URL = service.get('base_url')
343 344

  
tests/test_import_template.py
2 2
import mock
3 3

  
4 4
from django.conf import settings
5
from django.core.management import load_command_class
5 6
from django.core.management.base import CommandError
6 7
from django.test import override_settings
7 8

  
8 9
from hobo.agent.common.management.commands.import_template import Command, UnknownTemplateError
9
from django.core.management import load_command_class
10 10

  
11 11

  
12 12
@mock.patch('hobo.agent.common.management.commands.import_template.os.path.exists')
......
90 90

  
91 91
    # do nothing on internal templates
92 92
    mocked_super.side_effect = UnknownTemplateError
93
    for template in 'portal-user', 'portal-agent':
93
    for template in 'portal-user', 'portal-agent', 'x-portal-user-x', 'x-portal-agent-x':
94 94
        command.handle(template_name=template)
95 95

  
96 96
    # template no found
tests/test_matomo_utils.py
7 7

  
8 8
from django.test import override_settings
9 9

  
10
from hobo.environment.models import Variable, Wcs, Combo, Fargo
10
from hobo.environment.models import Variable, Combo, Hobo, Fargo, Wcs
11 11
from hobo.matomo.utils import \
12 12
     get_variable, get_variable_value, get_tracking_js, put_tracking_js, \
13 13
     get_tenant_name_and_public_urls, MatomoError, MatomoException, MatomoWS, \
......
207 207

  
208 208
def test_get_tenant_name_and_public_urls():
209 209
    Combo.objects.create(base_url='https://combo.dev.publik.love',
210
                         template_name='portal-user')
210
                         template_name='...portal-user...')
211
    Combo.objects.create(base_url='https://agent-combo.dev.publik.love',
212
                         template_name='...portal-agent...')
213
    Combo.objects.create(base_url='https://no-template-combo.dev.publik.love')
211 214
    Wcs.objects.create(base_url='https://wcs.dev.publik.love')
212 215
    Fargo.objects.create(base_url='https://fargo.dev.publik.love')
216
    Hobo.objects.create(base_url='https://hobo.dev.publik.love')
213 217
    tenant_name, site_urls = get_tenant_name_and_public_urls()
214 218
    assert tenant_name == 'combo.dev.publik.love'
215
    assert site_urls[2] == 'https://fargo.dev.publik.love/'
219
    assert site_urls == ['https://combo.dev.publik.love/',
220
                         'https://wcs.dev.publik.love/',
221
                         'https://fargo.dev.publik.love/']
216 222

  
217 223
def test_matomo_constructor():
218 224
    """build the matomo webservice object"""
......
652 658
    logme_url_var = get_variable('matomo_logme_url', '')
653 659

  
654 660
    Combo.objects.create(base_url='https://combo.dev.publik.love',
655
                         template_name='portal-user')
661
                         template_name='...portal-user...')
656 662
    Wcs.objects.create(base_url='https://wcs.dev.publik.love')
657 663
    Fargo.objects.create(base_url='https://fargo.dev.publik.love')
658 664

  
......
685 691
    tracking_js_var = get_variable('visits_tracking_js', 'js_code')
686 692

  
687 693
    Combo.objects.create(base_url='https://combo.dev.publik.love',
688
                         template_name='portal-user')
694
                         template_name='...portal-user...')
689 695
    Wcs.objects.create(base_url='https://wcs.dev.publik.love')
690 696
    Fargo.objects.create(base_url='https://fargo.dev.publik.love')
691 697

  
tests_multitenant/conftest.py
63 63
                     'title': 'Other',
64 64
                     'secret_key': 'abcde',
65 65
                     'service-id': 'authentic',
66
                     'base_url': 'http://other.example.net'},
66
                     'base_url': 'http://other.example.net'
67
                    },
68
                    {'slug': 'another',
69
                     'title': 'Agent portal',
70
                     'service-id': 'combo',
71
                     'template_name': '...portal-agent...',
72
                     'base_url': 'http://portal-agent.example.net'
73
                    },
74
                    {'slug': 'another2',
75
                     'title': 'User portal',
76
                     'service-id': 'combo',
77
                     'template_name': '...portal-user...',
78
                     'base_url': 'http://portal-user.example.net'
79
                    }
67 80
                    ]}, fd)
68 81
        t = Tenant(domain_url=name,
69 82
                      schema_name=name.replace('-', '_').replace('.', '_'))
tests_multitenant/test_settings.py
87 87
                assert django.conf.settings.TEMPLATE_VARS['other_url'] == 'http://other.example.net'
88 88
                assert django.conf.settings.TEMPLATE_VARS['site_title'] == 'Test'
89 89
                assert django.conf.settings.TEMPLATE_VARS['other_variable'] == 'bar'
90
                assert django.conf.settings.TEMPLATE_VARS['portal_agent_url'] == 'http://portal-agent.example.net'
91
                assert django.conf.settings.TEMPLATE_VARS['portal_user_url'] == 'http://portal-user.example.net'
90 92

  
91 93
        # check it's no longer defined after going back to the public schema
92 94
        with pytest.raises(AttributeError):
93
-