Projet

Général

Profil

0001-environment-use-current-domain-name-for-services-cre.patch

Christophe Siraut, 12 juillet 2019 11:16

Télécharger (5,37 ko)

Voir les différences:

Subject: [PATCH] environment: use current domain name for services creation
 form (#34212)

 debian/control            |  3 ++-
 hobo/environment/utils.py | 15 +++++++++++++++
 hobo/environment/views.py |  4 +---
 hobo/settings.py          |  2 --
 tests/test_environment.py | 26 ++++++++++++++++++++++++++
 tox.ini                   |  1 +
 6 files changed, 45 insertions(+), 6 deletions(-)
debian/control
20 20
    python-prometheus-client,
21 21
    python-djangorestframework,
22 22
    python-dnspython,
23
    python-systemd
23
    python-systemd,
24
    python-six
24 25
Recommends:
25 26
    python-gadjo,
26 27
    python-django-mellon (>= 1.2.22.26),
hobo/environment/utils.py
1
from six.moves.urllib.parse import urlparse
2

  
1 3
from django.conf import settings
2 4
from django.core.urlresolvers import reverse
3 5
from django.db import connection
......
58 60
    variable, created = Variable.objects.get_or_create(name=name,
59 61
            defaults={'auto': True, 'value': settings.VARIABLE_SETTINGS_DEFAULTS.get(name) or ''})
60 62
    return variable
63

  
64
def create_base_url(hostname, service):
65
    """
66
    Distinguish mutualised domains (matching a "-" in the first part of the netloc)
67
    from the normal scenario with a dedicated parent domain.
68
    """
69
    ph = urlparse(hostname)
70
    parts = ph.netloc.split('.')
71
    if '-' in parts[0]:
72
        netloc = '%s-%s.%s' % (service, parts[0].split('-')[1], '.'.join(parts[1:]))
73
    else:
74
        netloc = '%s.%s' % (service, '.'.join(parts[1:]))
75
    return '%s://%s' % (ph.scheme, netloc)
hobo/environment/views.py
24 24

  
25 25
    def get_context_data(self, **kwargs):
26 26
        context = super(HomeView, self).get_context_data(**kwargs)
27
        context['url_template'] = settings.SERVICE_URL_TEMPLATE
28 27
        context['available_services'] = [
29 28
                AvailableService(x) for x in AVAILABLE_SERVICES if x.is_enabled()]
30 29
        context['installed_services'] = [x for x in utils.get_installed_services() if not x.secondary]
......
114 113

  
115 114
    def get_initial(self):
116 115
        initial = super(ServiceCreateView, self).get_initial()
117
        initial['base_url'] = string.Template(settings.SERVICE_URL_TEMPLATE
118
                ).substitute({'app': self.model.Extra.service_id})
116
        initial['base_url'] = utils.create_base_url(self.request.build_absolute_uri(), self.model.Extra.service_default_slug)
119 117
        initial['slug'] = self.model.Extra.service_default_slug
120 118
        return initial
121 119

  
hobo/settings.py
137 137
    'django.contrib.auth.backends.ModelBackend',
138 138
)
139 139

  
140
SERVICE_URL_TEMPLATE = 'https://${app}.example.net'
141

  
142 140
# SERVICE_TEMPLATES: possible "flavours" for the various service types.
143 141
# This variable expects a dictionary mapping service identifiers
144 142
# to a list of (template name, template title) tuples.
tests/test_environment.py
1 1
# -*- coding: utf-8 -*-
2 2
import pytest
3 3

  
4
from django.contrib.auth.models import User
4 5
from django.core.exceptions import ValidationError
5 6
from django.utils import timezone
7

  
6 8
from hobo.environment.models import AVAILABLE_SERVICES, Combo, Passerelle
7 9

  
8 10
pytestmark = pytest.mark.django_db
9 11

  
12

  
13
def login(app, username='admin', password='password'):
14
    login_page = app.get('/login/')
15
    login_form = login_page.forms[0]
16
    login_form['username'] = username
17
    login_form['password'] = password
18
    resp = login_form.submit()
19
    assert resp.status_int == 302
20
    return app
21

  
22

  
10 23
def test_service_id():
11 24
    for service in AVAILABLE_SERVICES:
12 25
        assert service.Extra.service_id
......
58 71
            combo = Combo(base_url=url, slug='wesh_'+str(cpt), **kwargs)
59 72
            combo.full_clean()
60 73
            combo.save()
74

  
75

  
76
def test_service_creation_filling(app, admin_user, monkeypatch):
77
    from django.http.request import HttpRequest
78
    monkeypatch.setattr(HttpRequest, 'get_host', lambda x: 'test.example.net')
79
    app = login(app)
80
    response = app.get('/sites/new-combo')
81
    assert 'value="http://portal.example.net"' in response.text
82

  
83
    monkeypatch.setattr(HttpRequest, 'get_host', lambda x: 'hobo-test.example.net')
84
    app = login(app)
85
    response = app.get('/sites/new-combo')
86
    assert 'value="http://portal-test.example.net"' in response.text
tox.ini
52 52
	httmock
53 53
	requests
54 54
	pytest-freezegun
55
	six
55 56
commands =
56 57
	./getlasso.sh
57 58
	hobo: py.test {env:COVERAGE:} {env:NOMIGRATIONS:} {posargs:tests/}
58
-