diff --git a/base/forms.py b/base/forms.py index 6b05ee59b..4d13f6adb 100644 --- a/base/forms.py +++ b/base/forms.py @@ -1,6 +1,7 @@ from crispy_forms.helper import FormHelper from crispy_forms.layout import HTML, Fieldset, Layout from django import forms +from django.contrib.auth.forms import AuthenticationForm from django.core.exceptions import ValidationError from django.forms import ModelForm from django.utils.translation import ugettext_lazy as _ @@ -109,3 +110,14 @@ class CasaLegislativaTabelaAuxForm(ModelForm): ) ) super(CasaLegislativaTabelaAuxForm, self).__init__(*args, **kwargs) + + +class LoginForm(AuthenticationForm): + username = forms.CharField(label="Username", max_length=30, + widget=forms.TextInput( + attrs={ + 'class': 'form-control', 'name': 'username'})) + password = forms.CharField(label="Password", max_length=30, + widget=forms.PasswordInput( + attrs={ + 'class': 'form-control', 'name': 'password'})) diff --git a/base/tests/test_login.py b/base/tests/test_login.py new file mode 100755 index 000000000..3fe9301dd --- /dev/null +++ b/base/tests/test_login.py @@ -0,0 +1,69 @@ +# -*- coding: utf-8 -*- +import pytest +from django.contrib.auth.models import User + +pytestmark = pytest.mark.django_db + + +@pytest.fixture +def user(): + return User.objects.create_user('jfirmino', password='123') + + +def test_login_aparece_na_barra_para_usuario_nao_logado(client): + response = client.get('/') + assert 'Login' in str(response.content) + + +def test_username_do_usuario_logado_aparece_na_barra(client, user): + assert client.login(username='jfirmino', password='123') + response = client.get('/') + assert 'Login' not in str(response.content) + assert 'jfirmino' in str(response.content) + assert 'Sair' in str(response.content) + + +def test_nome_completo_do_usuario_logado_aparece_na_barra(client, user): + # nome completo para o usuario + user.first_name = 'Joao' + user.last_name = 'Firmino' + user.save() + assert client.login(username='jfirmino', password='123') + response = client.get('/') + assert 'Login' not in str(response.content) + assert 'Joao Firmino' in str(response.content) + assert 'Sair' in str(response.content) + + +@pytest.mark.urls('base.tests.teststub_urls') +@pytest.mark.parametrize("link_login,destino", [ + # login redireciona para home + ('/login/', '/'), +]) +def test_login(app, user, link_login, destino): + pagina_login = app.get(link_login) + form = pagina_login.forms['login-form'] + form['username'] = 'jfirmino' + form['password'] = '123' + res = form.submit() # login + + assert str(user.pk) == app.session['_auth_user_id'] + assert res.url == destino + + +@pytest.mark.parametrize("link_logout,destino", [ + # logout redireciona para a pagina de login + ('/logout/', '/login/'), +]) +def test_logout(client, user, link_logout, destino): + # com um usuário logado ... + assert client.login(username='jfirmino', password='123') + assert str(user.pk) == client.session['_auth_user_id'] + + # ... acionamos o link de logout + res = client.get(link_logout, follow=True) + + destino_real = res.redirect_chain[-1][0] + + assert '_auth_user_id' not in client.session + assert destino_real == destino diff --git a/base/tests/teststub_urls.py b/base/tests/teststub_urls.py new file mode 100644 index 000000000..3a3410f98 --- /dev/null +++ b/base/tests/teststub_urls.py @@ -0,0 +1,10 @@ +from django.conf.urls import patterns, url +from django.views.generic.base import TemplateView + +from sapl.urls import urlpatterns as original_patterns + +urlpatterns = original_patterns + patterns('', url(r'^zzzz$', + TemplateView.as_view( + template_name='index.html'), + name='zzzz') + ) diff --git a/base/urls.py b/base/urls.py index 4ddb4532a..0a4caad73 100644 --- a/base/urls.py +++ b/base/urls.py @@ -1,12 +1,15 @@ from django.conf.urls import url +from django.contrib.auth import views from django.contrib.staticfiles.urls import staticfiles_urlpatterns from django.views.generic.base import TemplateView from .apps import AppConfig +from .forms import LoginForm from .views import CasaLegislativaTableAuxView, HelpView app_name = AppConfig.name + urlpatterns = [ url(r'^sistema/', TemplateView.as_view(template_name='sistema.html')), url(r'^ajuda/(?P\w+)$', HelpView.as_view(), name='help_topic'), @@ -14,6 +17,11 @@ urlpatterns = [ name='help_base'), url(r'^casa-legislativa$', CasaLegislativaTableAuxView.as_view(), name='casa_legislativa'), + + url(r'^login/$', views.login, { + 'template_name': 'base/login.html', 'authentication_form': LoginForm}, + name='login'), + url(r'^logout/$', views.logout, {'next_page': '/login'}, name='logout') ] # Fix a static asset finding error on Django 1.9 + gunicorn: diff --git a/base/views.py b/base/views.py index 1950ce99a..2e801a3c5 100644 --- a/base/views.py +++ b/base/views.py @@ -42,7 +42,7 @@ class CasaLegislativaTableAuxView(FormView): casa = CasaLegislativa.objects.first() if casa: if ("remover" in request.POST or - (form.cleaned_data['logotipo'] and casa.logotipo)): + (form.cleaned_data['logotipo'] and casa.logotipo)): try: os.unlink(casa.logotipo.path) except OSError: diff --git a/protocoloadm/views.py b/protocoloadm/views.py index 19e67e9ba..5f874ef78 100644 --- a/protocoloadm/views.py +++ b/protocoloadm/views.py @@ -4,7 +4,7 @@ from datetime import date, datetime from braces.views import FormValidMessageMixin from django.core.exceptions import ObjectDoesNotExist from django.core.urlresolvers import reverse -from django.db.models import Q, Max +from django.db.models import Max, Q from django.http import HttpResponse, HttpResponseRedirect from django.shortcuts import redirect from django.utils.translation import ugettext_lazy as _ diff --git a/sapl/settings.py b/sapl/settings.py index 975b809e4..f0b6dc0e7 100644 --- a/sapl/settings.py +++ b/sapl/settings.py @@ -30,6 +30,8 @@ DEBUG = config('DEBUG', default=False, cast=bool) ALLOWED_HOSTS = ['*'] +LOGIN_REDIRECT_URL = '/' +LOGIN_URL = '/login/?next=' # SAPL business apps in dependency order SAPL_APPS = ( diff --git a/templates/base.html b/templates/base.html index 4822ebc2d..4fe933103 100644 --- a/templates/base.html +++ b/templates/base.html @@ -103,6 +103,26 @@ + + diff --git a/templates/base/login.html b/templates/base/login.html new file mode 100644 index 000000000..fb47ff084 --- /dev/null +++ b/templates/base/login.html @@ -0,0 +1,68 @@ +{% extends "crud/detail.html" %} +{% load i18n %} +{% block base_content %} + + + + +
+
+
+ +
+
+
+ +{% endblock base_content %} + + {% block javascript %} + + + + + {% endblock %}