Projet

Général

Profil

0001-settings_loaders-adapt-service-slug-for-secondary-se.patch

Benjamin Dauvergne, 03 octobre 2021 10:01

Télécharger (5,51 ko)

Voir les différences:

Subject: [PATCH] settings_loaders: adapt service slug for secondary services
 (#57482)

 hobo/multitenant/settings_loaders.py | 20 +++++++++++++++-
 tests/test_settings_loaders.py       | 36 ++++++++++++++++++++++++----
 2 files changed, 50 insertions(+), 6 deletions(-)
hobo/multitenant/settings_loaders.py
1 1
import hashlib
2 2
import json
3 3
import os
4
import urllib.parse
4 5

  
5 6
from django.conf import settings
6 7
from django.utils.encoding import force_bytes
......
131 132
    def get_hobo_json_variables(cls, hobo_json):
132 133
        variables = hobo_json.get('variables', {})
133 134
        variables['is_portal_agent'] = False
135

  
136
        authentic_service = None
137

  
134 138
        for service in hobo_json.get('services'):
135 139
            if not service.get('slug'):
136 140
                continue
......
146 150
                    variables['portal_user_slug'] = service.get('slug')
147 151

  
148 152
            if service.get('service-id') == 'authentic':
153
                authentic_service = service
149 154
                variables['idp_url'] = service.get('base_url')
150 155
                variables['idp_api_url'] = service.get('base_url') + 'api/'
151 156
                variables['idp_account_url'] = service.get('base_url') + 'accounts/'
......
165 170
            if 'portal_user_url' in variables:
166 171
                params['next'] = variables['portal_user_url']
167 172
            if 'portal_user_slug' in variables:
168
                params['service'] = variables['portal_user_slug']
173
                # if we are in a secondary hobo, adapt the ou slug and
174
                # portal_user_slug to match the service slug in the Authentic
175
                # of the primary hobo
176
                if authentic_service.get('secondary'):
177
                    ou_slug = variables['ou-slug']
178
                    service_slug = '_%s_%s' % (ou_slug, variables['portal_user_slug'])
179
                    params['service'] = '%s %s' % (ou_slug, service_slug)
180
                else:
181
                    # we should provider the default slug to have a full
182
                    # service reference, but it could change so for now we
183
                    # expect authentic to search first in the default ou
184
                    params['service'] = variables['portal_user_slug']
169 185
            if params:
170 186
                variables['idp_registration_url'] += '?%s' % urlencode(params)
187
                variables['idp_account_url'] += '?%s' % urlencode(params)
188
                variables['idp_url'] += '?%s' % urlencode(params)
171 189

  
172 190
        if getattr(settings, 'HOBO_MANAGER_HOMEPAGE_TITLE_VAR', None):
173 191
            variables['manager_homepage_title'] = variables.get(settings.HOBO_MANAGER_HOMEPAGE_TITLE_VAR)
tests/test_settings_loaders.py
1 1
import json
2 2
import os
3
import urllib.parse
3 4

  
4 5
import pytest
5 6
from django.conf import UserSettingsHolder
6 7

  
7 8
from hobo.deploy.utils import get_hobo_json
8
from hobo.environment.models import Authentic, Combo
9
from hobo.environment.models import Authentic, Combo, Variable
9 10
from hobo.multitenant.settings_loaders import Authentic as AuthenticLoader
10 11
from hobo.multitenant.settings_loaders import BackofficeLoginHint, TemplateVars
11 12
from hobo.profile.models import AttributeDefinition
......
175 176

  
176 177

  
177 178
def test_get_hobo_json_variables(tmpdir):
179
    from django.http.request import QueryDict
180

  
178 181
    a = Authentic(title='bar', slug='bar', base_url='http://bar.example.net')
179 182
    a.save()
180 183
    c = Combo(title='combo', slug='portal', base_url='http://portal.example.net', template_name='portal-user')
......
185 188

  
186 189
    variables = loader.get_hobo_json_variables(env)
187 190

  
188
    url = variables['idp_registration_url']
189
    assert url.startswith('http://bar.example.net/accounts/register/?')
190
    assert 'next=http%3A%2F%2Fportal.example.net%2F' in url
191
    assert 'service=portal' in url
191
    url, query = variables['idp_registration_url'].split('?')
192
    assert url == 'http://bar.example.net/accounts/register/'
193
    assert QueryDict(query).dict() == {'next': 'http://portal.example.net/', 'service': 'portal'}
194

  
195

  
196
def test_get_hobo_json_variables_secondary(tmpdir):
197
    from django.http.request import QueryDict
198

  
199
    a = Authentic(title='bar', slug='bar', base_url='http://bar.example.net', secondary=True)
200
    a.save()
201
    c = Combo(title='combo', slug='portal', base_url='http://portal.example.net', template_name='portal-user')
202
    c.save()
203
    # simulate hobo.json from a secondary hobo
204
    v = Variable(name='ou-slug', auto=True, service_pk=None, value='hobo-othercol')
205
    v.save()
206

  
207
    loader = TemplateVars()
208
    env = get_hobo_json()
209

  
210
    variables = loader.get_hobo_json_variables(env)
211

  
212
    url, query = variables['idp_registration_url'].split('?')
213
    assert url == 'http://bar.example.net/accounts/register/'
214
    assert QueryDict(query).dict() == {
215
        'next': 'http://portal.example.net/',
216
        'service': '_hobo-othercol _hobo-othercol_portal',
217
    }
192
-