diff --git a/conftest.py b/conftest.py new file mode 100644 index 000000000..78f021a9c --- /dev/null +++ b/conftest.py @@ -0,0 +1,43 @@ +import pytest +from django_webtest import DjangoTestApp, WebTestMixin + + +DEFAULT_MARK = object() + + +class SaplTestApp(DjangoTestApp): + + def __init__(self, extra_environ=None, relative_to=None, + default_user=None): + super(SaplTestApp, self).__init__(extra_environ, relative_to) + self.default_user = default_user + + def get(self, url, params=None, headers=None, extra_environ=None, + status=None, expect_errors=False, user=DEFAULT_MARK, + auto_follow=True, content_type=None, **kwargs): + # note we altered the default values for user and auto_follow + + if user is DEFAULT_MARK: # a trick to allow explicit user=None + user = self.default_user + + return super(SaplTestApp, self).get( + url, params, headers, extra_environ, status, expect_errors, user, + auto_follow, content_type, **kwargs) + + +@pytest.fixture(scope='function') +def app(request, admin_user): + """WebTest's TestApp. + + Patch and unpatch settings before and after each test. + + WebTestMixin, when used in a unittest.TestCase, automatically calls + _patch_settings() and _unpatchsettings. + + source: https://gist.github.com/magopian/6673250 + """ + wtm = WebTestMixin() + wtm._patch_settings() + request.addfinalizer(wtm._unpatch_settings) + # XXX change this admin user to "saploper" + return SaplTestApp(default_user=admin_user.username) diff --git a/sapl/crud.py b/sapl/crud.py index f89555d77..5e91db62f 100644 --- a/sapl/crud.py +++ b/sapl/crud.py @@ -33,6 +33,9 @@ class Crud(object): create_url = reverse_lazy(in_namespace('create')) help_url = '/comissoes/ajuda' # FIXME + create_title = _('Adicionar %(model_name)s') % { + 'model_name': self.model._meta.verbose_name} + def get_url_for_this_object(self, url_name): return reverse(in_namespace(url_name), args=(self.object.id,)) @@ -55,8 +58,7 @@ class Crud(object): class CrudCreateView(BaseMixin, FormMessagesMixin, CreateView): model = self.model form_class = model_form - title = _('Adicionar %(model_name)s') % { - 'model_name': self.model._meta.verbose_name} + title = BaseMixin.create_title form_valid_message = _('Registro criado com sucesso!') form_invalid_message = make_form_invalid_message( _('O registro não foi criado.')) diff --git a/sapl/test_crud.py b/sapl/test_crud.py index b8c0aa7eb..5b152d10a 100644 --- a/sapl/test_crud.py +++ b/sapl/test_crud.py @@ -1,4 +1,9 @@ from django.core.urlresolvers import reverse +from model_mommy import mommy +from comissoes.models import Comissao, TipoComissao + +# XXX These tests are based on comissoes app +# but could be done with a stub one def test_reverse(): @@ -7,3 +12,50 @@ def test_reverse(): assert '/comissoes/2' == reverse('comissao:detail', args=(2,)) assert '/comissoes/2/edit' == reverse('comissao:update', args=(2,)) assert '/comissoes/2/delete' == reverse('comissao:delete', args=(2,)) + + +def assert_h1(res, title): + assert res.html.find('h1').text == title + + +def test_flux_list_create_detail(app): + + # to have a couple an option for tipo field + stub_tipo = mommy.make(TipoComissao) + + res = app.get('/comissoes/') + print(res.url) + + # on list page + assert_h1(res, 'Comissões') + res = res.click('Adicionar Comissão') + print(res.url) + + # on create page + assert_h1(res, 'Adicionar Comissão') + form = res.form + assert not any( + form[k].value for k in form.fields if k != 'csrfmiddlewaretoken') + + # some fields are required => validation error + res = res.form.submit() + print(res.url) + 'Formulário inválido. O registro não foi criado.' in res + + # now fill out some fields + form = res.form + stub_name = '### Nome Especial ###' + form['nome'] = stub_name + form['sigla'] = 'SIGLA' + form['tipo'] = stub_tipo.id + form['data_criacao'] = '1/1/2001' + res = form.submit() + + # on redirect to detail page + created = Comissao.objects.get(nome=stub_name) + assert res.url.endswith('comissoes/%s' % created.id) + res = res.follow() + + # on detail page + assert 'Registro criado com sucesso!' in res + assert_h1(res, stub_name) diff --git a/templates/comissoes/comissao_list.html b/templates/comissoes/comissao_list.html index a6f421585..056686fbc 100644 --- a/templates/comissoes/comissao_list.html +++ b/templates/comissoes/comissao_list.html @@ -2,7 +2,11 @@ {% load i18n %} {% block base_content %} - Incluir Comissão + + {# FIXME is this the best markup to use? #} +