From 3aad8c3b4da601bb5891397d8d7737fd75a4a18e Mon Sep 17 00:00:00 2001 From: Johanna England Date: Fri, 15 Sep 2023 13:40:55 +0200 Subject: [PATCH] Factor out admin account as general fixture --- tests/integration/api_test.py | 5 ++--- tests/integration/conftest.py | 7 +++++++ tests/integration/models/alertsubscription_test.py | 13 ++++--------- tests/integration/seeddb_test.py | 4 ++-- tests/integration/web/info_test.py | 4 ++-- tests/integration/web/webfront_test.py | 9 +-------- 6 files changed, 18 insertions(+), 24 deletions(-) diff --git a/tests/integration/api_test.py b/tests/integration/api_test.py index 216c7795b9..44faf5b483 100644 --- a/tests/integration/api_test.py +++ b/tests/integration/api_test.py @@ -380,7 +380,7 @@ def test_api_urls_should_resolve(urlname, arg): @pytest.fixture() -def serializer_models(localhost): +def serializer_models(localhost, admin_account): """Fixture for testing API serializers - unrecognized_neighbor @@ -433,7 +433,6 @@ def serializer_models(localhost): alert_type_id=boxdown_id, end_time=INFINITY, ).save() - admin = profiles.Account.objects.get(login='admin') - auditlog.LogEntry.add_log_entry(admin, verb='verb', template='asd') + auditlog.LogEntry.add_log_entry(admin_account, verb='verb', template='asd') manage.Usage(id='ans', description='Ansatte').save() manage.Usage(id='student', description='Studenter').save() diff --git a/tests/integration/conftest.py b/tests/integration/conftest.py index 5f74f1d6a0..aadc768365 100644 --- a/tests/integration/conftest.py +++ b/tests/integration/conftest.py @@ -366,3 +366,10 @@ def _lookfor(string, filename): """Very simple grep-like function""" data = io.open(filename, 'r', encoding='utf-8').read() return string in data + + +@pytest.fixture +def admin_account(db): + from nav.models.profiles import Account + + yield Account.objects.get(id=Account.ADMIN_ACCOUNT) diff --git a/tests/integration/models/alertsubscription_test.py b/tests/integration/models/alertsubscription_test.py index 160cc12358..04a2d0998c 100644 --- a/tests/integration/models/alertsubscription_test.py +++ b/tests/integration/models/alertsubscription_test.py @@ -22,14 +22,9 @@ def test_delete_alert_subscription(db, alert, alertsub): @pytest.fixture -def account(): - return Account.objects.get(pk=Account.ADMIN_ACCOUNT) - - -@pytest.fixture -def alert_address(account): +def alert_address(admin_account): addr = AlertAddress( - account=account, + account=admin_account, type=AlertSender.objects.get(name=AlertSender.SMS), ) addr.save() @@ -39,8 +34,8 @@ def alert_address(account): @pytest.fixture -def alert_profile(account): - profile = AlertProfile(account=account) +def alert_profile(admin_account): + profile = AlertProfile(account=admin_account) profile.save() yield profile if profile.pk: diff --git a/tests/integration/seeddb_test.py b/tests/integration/seeddb_test.py index 7b0f195e87..efa7edfe60 100644 --- a/tests/integration/seeddb_test.py +++ b/tests/integration/seeddb_test.py @@ -17,12 +17,12 @@ def test_usage_edit_url_should_allow_slashes(): assert reverse('seeddb-usage-edit', args=('TEST/SLASH',)) -def test_editing_deleted_netboxes_should_raise_404(): +def test_editing_deleted_netboxes_should_raise_404(admin_account): netboxid = 666 # Assuming no such netbox exists in test data set! factory = RequestFactory() url = reverse('seeddb-netbox-edit', args=(netboxid,)) request = factory.get(url) - request.account = Account.objects.get(pk=Account.ADMIN_ACCOUNT) + request.account = admin_account request.session = MagicMock() with pytest.raises(Http404): diff --git a/tests/integration/web/info_test.py b/tests/integration/web/info_test.py index 365311370f..b2700c0897 100644 --- a/tests/integration/web/info_test.py +++ b/tests/integration/web/info_test.py @@ -35,12 +35,12 @@ def test_failures_should_be_mentioned_in_search_page(client, failing_searchprovi assert failing_searchprovider in response.content.decode('utf-8') -def test_room_csv_download_should_not_produce_bytestring_representations(): +def test_room_csv_download_should_not_produce_bytestring_representations(admin_account): factory = RequestFactory() request = factory.post( reverse("room-csv"), data={"roomid": "myroom", "rows": "one;two;three\n"} ) - request.account = Account.objects.get(pk=Account.ADMIN_ACCOUNT) + request.account = admin_account request.session = MagicMock() response = create_csv(request) # type: django.http.response.HttpResponse diff --git a/tests/integration/web/webfront_test.py b/tests/integration/web/webfront_test.py index 8dc234b974..0d38caac4e 100644 --- a/tests/integration/web/webfront_test.py +++ b/tests/integration/web/webfront_test.py @@ -2,11 +2,9 @@ from django.urls import reverse from nav.compatibility import smart_str -from nav.models.profiles import Account, AccountDashboard +from nav.models.profiles import AccountDashboard from nav.web.webfront.utils import tool_list -import pytest - def test_tools_should_be_readable(): admin = Mock() @@ -60,8 +58,3 @@ def test_set_default_dashboard_with_multiple_previous_defaults_should_succeed( AccountDashboard.objects.filter(account=admin_account, is_default=True).count() == 1 ) - - -@pytest.fixture -def admin_account(db): - yield Account.objects.get(id=Account.ADMIN_ACCOUNT)