0001-settings_loaders-adapt-service-slug-for-secondary-se.patch
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 |
me = ([service for service in hobo_json.get('services') if service.get('this')] or [None])[0] |
|
136 | ||
137 |
authentic_service = None |
|
138 |
portal_user_service = None |
|
139 | ||
134 | 140 |
for service in hobo_json.get('services'): |
135 | 141 |
if not service.get('slug'): |
136 | 142 |
continue |
... | ... | |
144 | 150 |
variables['portal_user_url'] = service.get('base_url') |
145 | 151 |
variables['portal_user_title'] = service.get('title') |
146 | 152 |
variables['portal_user_slug'] = service.get('slug') |
153 |
portal_user_service = service |
|
147 | 154 | |
148 | 155 |
if service.get('service-id') == 'authentic': |
156 |
authentic_service = service |
|
149 | 157 |
variables['idp_url'] = service.get('base_url') |
150 | 158 |
variables['idp_api_url'] = service.get('base_url') + 'api/' |
151 | 159 |
variables['idp_account_url'] = service.get('base_url') + 'accounts/' |
... | ... | |
165 | 173 |
if 'portal_user_url' in variables: |
166 | 174 |
params['next'] = variables['portal_user_url'] |
167 | 175 |
if 'portal_user_slug' in variables: |
168 |
params['service'] = variables['portal_user_slug'] |
|
176 |
if authentic_service.get('secondary') and not portal_user_service.get('secondary'): |
|
177 |
params[ |
|
178 |
'service' |
|
179 |
] = f"_{variables['ou-slug']} _{variables['ou-slug']}_{variables['portal_user_slug']}" |
|
180 |
else: |
|
181 |
params['service'] = variables['portal_user_slug'] |
|
169 | 182 |
if params: |
170 | 183 |
variables['idp_registration_url'] += '?%s' % urlencode(params) |
184 |
variables['idp_account_url'] += '?%s' % urlencode(params) |
|
185 |
variables['idp_url'] += '?%s' % urlencode(params) |
|
171 | 186 | |
172 | 187 |
if getattr(settings, 'HOBO_MANAGER_HOMEPAGE_TITLE_VAR', None): |
173 | 188 |
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 |
- |