diff --git a/conftest.py b/conftest.py index a3bd77c..423ab8d 100644 --- a/conftest.py +++ b/conftest.py @@ -3,8 +3,30 @@ import pytest from django_webtest import DjangoTestApp, WebTestMixin +DEFAULT_MARK = object() + + +class SigiTestApp(DjangoTestApp): + + def __init__(self, extra_environ=None, relative_to=None, default_user=None): + super(SigiTestApp, 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(SigiTestApp, self).get(url, params, headers, extra_environ, + status, expect_errors, user, auto_follow, + content_type, **kwargs) + + @pytest.fixture(scope='function') -def app(request): +def app(request, admin_user): """WebTest's TestApp. Patch and unpatch settings before and after each test. @@ -17,4 +39,4 @@ def app(request): wtm = WebTestMixin() wtm._patch_settings() request.addfinalizer(wtm._unpatch_settings) - return DjangoTestApp() + return SigiTestApp(default_user=admin_user.username) diff --git a/sigi/apps/parlamentares/test_parlamentares.py b/sigi/apps/parlamentares/test_parlamentares.py index 1f989f4..e9fc0bc 100644 --- a/sigi/apps/parlamentares/test_parlamentares.py +++ b/sigi/apps/parlamentares/test_parlamentares.py @@ -17,16 +17,16 @@ def some_parlamentarians(): return a, b, c -def test_list_all(some_parlamentarians, admin_user, app): - response = app.get('/parlamentares/parlamentar/', auto_follow=True, user=admin_user.username) +def test_list_all(some_parlamentarians, app): + response = app.get('/parlamentares/parlamentar/') assert response.status_code == 200 for x in some_parlamentarians: assert x.nome_completo in response.content -def test_list_filtered_by_capital_letter(some_parlamentarians, admin_client): - response = admin_client.get('/parlamentares/parlamentar/?nome_completo=B', follow=True) +def test_list_filtered_by_capital_letter(some_parlamentarians, app): + response = app.get('/parlamentares/parlamentar/?nome_completo=B') assert response.status_code == 200 decoded_content = response.content.decode('utf-8') @@ -36,8 +36,8 @@ def test_list_filtered_by_capital_letter(some_parlamentarians, admin_client): assert c.nome_completo not in decoded_content -def test_add_to_cart(some_parlamentarians, admin_user, app): - res = app.get('/parlamentares/parlamentar/', auto_follow=True, user=admin_user.username) +def test_add_to_cart(some_parlamentarians, app): + res = app.get('/parlamentares/parlamentar/') assert res.status_code == 200 a, b, c = some_parlamentarians @@ -47,7 +47,7 @@ def test_add_to_cart(some_parlamentarians, admin_user, app): form['action'] = 'adiciona_parlamentar' res = form.submit() "2 Parlamentares adicionados" in res.content - res = app.get('/parlamentares/parlamentar/carrinho/', auto_follow=True, user=admin_user.username) + res = app.get('/parlamentares/parlamentar/carrinho/') def right_people_present(content): assert a.nome_completo in content