tests: coding style (#29240)

This commit is contained in:
Benjamin Dauvergne 2019-03-08 02:51:39 +01:00
parent 9635e4c8f5
commit 8ced89e097
1 changed files with 35 additions and 19 deletions

View File

@ -1,26 +1,36 @@
import base64
import os
import StringIO
# hobo - portal to configure and deploy applications
# Copyright (C) 2015-2019 Entr'ouvert
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from django.conf import settings
from django.contrib.auth.models import User
import pytest
from webtest import TestApp
from hobo.profile import models
from hobo.environment.models import Authentic
from hobo.wsgi import application
pytestmark = pytest.mark.django_db
@pytest.fixture
def admin_user():
def admin_user(db):
try:
user = User.objects.get(username='admin')
except User.DoesNotExist:
user = User.objects.create_superuser('admin', email=None, password='admin')
return user
def login(app, username='admin', password='admin'):
login_page = app.get('/login/')
login_form = login_page.forms[0]
@ -30,31 +40,37 @@ def login(app, username='admin', password='admin'):
assert resp.status_int == 302
return app
def test_unlogged_access():
@pytest.fixture
def logged_app(app, admin_user):
return login(app)
def test_unlogged_access(app):
# connect while not being logged in
app = TestApp(application)
assert app.get('/', status=302).location.endswith('/login/?next=/')
def test_access(admin_user):
app = login(TestApp(application))
resp = app.get('/', status=200)
def test_access(logged_app):
resp = logged_app.get('/', status=200)
assert 'User Profile' not in resp.body
assert 'Services' in resp.body
assert 'Variables' in resp.body
Authentic(title='bar', slug='bar', base_url='http://bar.example.net').save()
resp = app.get('/', status=200)
resp = logged_app.get('/', status=200)
assert 'User Profile' in resp.body
def test_logout(admin_user):
app = login(TestApp(application))
def test_logout(logged_app):
app = logged_app
app.get('/logout/')
assert app.get('/', status=302).location.endswith('/login/?next=/')
@pytest.mark.parametrize('kind', ['boolean', 'string'])
def test_add_attribute(admin_user, kind):
app = login(TestApp(application))
def test_add_attribute(logged_app, admin_user, kind):
app = logged_app
assert models.AttributeDefinition.objects.filter(kind=kind).filter(name='test').count() == 0
page = app.get('/profile/add-attribute', status=200)
page.form['label'] = 'test'
@ -70,5 +86,5 @@ def test_add_attribute(admin_user, kind):
assert models.AttributeDefinition.objects.filter(kind=kind).filter(name='test').count() == 1
def test_attribute_kind_not_restricted_at_model_level():
def test_attribute_kind_not_restricted_at_model_level(db):
assert models.AttributeDefinition.objects.create(label='test', kind='somestring')