87 lines
2.9 KiB
Python
87 lines
2.9 KiB
Python
# -*- coding: utf-8 -*-
|
|
import pytest
|
|
|
|
from django.contrib.auth.models import User
|
|
from django.core.exceptions import ValidationError
|
|
from django.utils import timezone
|
|
|
|
from hobo.environment.models import AVAILABLE_SERVICES, Combo, Passerelle
|
|
|
|
pytestmark = pytest.mark.django_db
|
|
|
|
|
|
def login(app, username='admin', password='password'):
|
|
login_page = app.get('/login/')
|
|
login_form = login_page.forms[0]
|
|
login_form['username'] = username
|
|
login_form['password'] = password
|
|
resp = login_form.submit()
|
|
assert resp.status_int == 302
|
|
return app
|
|
|
|
|
|
def test_service_id():
|
|
for service in AVAILABLE_SERVICES:
|
|
assert service.Extra.service_id
|
|
|
|
|
|
def test_unique_slug():
|
|
combo = Combo(title='Combo test',
|
|
slug='wesh',
|
|
last_operational_success_timestamp=timezone.now(),
|
|
last_operational_check_timestamp=timezone.now(),
|
|
secret_key='1nesüper5Cr!eteKAaY~',
|
|
base_url='http://example.com')
|
|
combo.save()
|
|
|
|
passerelle = Passerelle(title='Passerelle test',
|
|
slug='wesh',
|
|
last_operational_success_timestamp=timezone.now(),
|
|
last_operational_check_timestamp=timezone.now(),
|
|
secret_key='1nesüper5Cr!eteKAaY~',
|
|
base_url='http://example.com')
|
|
|
|
with pytest.raises(ValidationError) as e:
|
|
passerelle.clean()
|
|
|
|
assert e.value.messages[0] == u'This slug is already used. It must be unique.'
|
|
|
|
|
|
def test_base_url_field_validator():
|
|
kwargs = {'title': 'Combo test',
|
|
'last_operational_success_timestamp': timezone.now(),
|
|
'last_operational_check_timestamp': timezone.now(),
|
|
'secret_key': '1nesüper5Cr!eteKAaY~'}
|
|
cpt = 0 # slugs must be unique
|
|
|
|
# accept well formated url
|
|
for url in ('https://example.com', 'http://example.com'):
|
|
cpt += 1
|
|
slug = 'wesh' + str(cpt)
|
|
combo = Combo(base_url=url, slug=slug ,**kwargs)
|
|
combo.full_clean()
|
|
combo.save()
|
|
assert True # no exception raised
|
|
|
|
# reject baddly formated url
|
|
for url in ('example.com', 'http:/example.com', 'file:///home/me'):
|
|
cpt += 1
|
|
slug = 'wesh' + str(cpt)
|
|
with pytest.raises(ValidationError):
|
|
combo = Combo(base_url=url, slug='wesh_'+str(cpt), **kwargs)
|
|
combo.full_clean()
|
|
combo.save()
|
|
|
|
|
|
def test_service_creation_filling(app, admin_user, monkeypatch):
|
|
from django.http.request import HttpRequest
|
|
monkeypatch.setattr(HttpRequest, 'get_host', lambda x: 'test.example.net')
|
|
app = login(app)
|
|
response = app.get('/sites/new-combo')
|
|
assert 'value="http://portal.example.net"' in response.text
|
|
|
|
monkeypatch.setattr(HttpRequest, 'get_host', lambda x: 'hobo-test.example.net')
|
|
app = login(app)
|
|
response = app.get('/sites/new-combo')
|
|
assert 'value="http://portal-test.example.net"' in response.text
|