From 6ae4c2508c2faae5ab29c9c459d3173739dd910a Mon Sep 17 00:00:00 2001 From: Oussama Jarrousse Date: Tue, 26 Dec 2023 17:33:21 +0100 Subject: [PATCH] 2 passed in 0.82s, 32% coverage --- pytest.ini | 16 ++++++------ tests/conftest.py | 19 +++++++++++++++ tests/settings.py | 51 +++++++++++++++++++++++++++++++++++++++ tests/templates/base.html | 11 +++++++++ tests/test_views.py | 23 ++++++++++++++++++ 5 files changed, 113 insertions(+), 7 deletions(-) create mode 100644 tests/settings.py create mode 100644 tests/templates/base.html diff --git a/pytest.ini b/pytest.ini index eea7252..e3e533a 100644 --- a/pytest.ini +++ b/pytest.ini @@ -8,16 +8,16 @@ env_files = .env # do not search for tests in these folders -norecursedirs = venv +norecursedirs = .vscode .tox docs example img mfa venv .coverage django_mfa2.egg-info # Add folder to PYTHONPATH # requires pytest >= 7.0.0 -pythonpath = mfa +pythonpath = . # https://pytest-django.readthedocs.io/en/latest/usage.html -; DJANGO_SETTINGS_MODULE = - ; +DJANGO_SETTINGS_MODULE = tests.settings + # do not override the debug mode (True/False) set in the django settings module # https://pytest-django.readthedocs.io/en/latest/usage.html#additional-pytest-ini-settings @@ -42,7 +42,7 @@ addopts = # generates coverage report # note that enabling pytest coverage will cause debugging pytest to fail on pycharm # add the --no-cov to the pytest configuration on pycharm to allow for debugging pytest - --cov=./src + --cov=./mfa # surpress generating converage if one or more tests failed ; --no-cov-on-fail # do not run migrations => faster test initialization @@ -53,6 +53,8 @@ addopts = # black # --black --hypothesis-show-statistics + # Add --reuse-db if you want to speed up tests by reusing the database between test runs. + #--reuse-db # Define additional pytest markers so that using them in test will not trigger warnings @@ -91,11 +93,11 @@ markers = PRIVILEGED_USER: tests for privileged users NON_PRIVILEGED_USER: tests for non-privileged users - PERMISSIONS: tests related to inspectre permissions + PERMISSIONS: tests related to permissions ENDPOINTS: tests for endpoints (API nodes) SERIALIZERS: tests for serializers - VIEWSETS: tests for DRF viewsets + VIEWS: tests for DRF viewsets FILTERS: tests for DRF filters MODELS: tests for models VALIDATORS: tests for validators diff --git a/tests/conftest.py b/tests/conftest.py index e69de29..583e86d 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -0,0 +1,19 @@ +import pytest + +# @pytest.fixture +# def api_request(rf): +# request = rf.get('/url') +# # Modify the request object as needed (e.g., set user, add data) +# return request + +# @pytest.fixture +# def create_test_model(db): +# def make_model(**kwargs): +# return MyModel.objects.create(**kwargs) +# return make_model + +@pytest.fixture +def authenticated_user(client, django_user_model): + user = django_user_model.objects.create_user(username='test', password='123') + client.login(username='test', password='123') + return user \ No newline at end of file diff --git a/tests/settings.py b/tests/settings.py new file mode 100644 index 0000000..c2851b1 --- /dev/null +++ b/tests/settings.py @@ -0,0 +1,51 @@ +import os + + +SECRET_KEY = 'fake-key-for-testing' +INSTALLED_APPS = [ + 'django.contrib.auth', + 'django.contrib.contenttypes', + 'django.contrib.sessions', + 'mfa' +] +ROOT_URLCONF="mfa.urls" + +DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.sqlite3', + 'NAME': ':memory:', + } +} + +MIDDLEWARE = [ + 'django.middleware.security.SecurityMiddleware', + 'django.contrib.sessions.middleware.SessionMiddleware', + 'django.middleware.common.CommonMiddleware', + 'django.middleware.csrf.CsrfViewMiddleware', + 'django.contrib.auth.middleware.AuthenticationMiddleware', + 'django.contrib.messages.middleware.MessageMiddleware', + 'django.middleware.clickjacking.XFrameOptionsMiddleware', +] + +BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) + +TEMPLATES = [ + { + 'BACKEND': 'django.template.backends.django.DjangoTemplates', + 'DIRS': [ + os.path.join(BASE_DIR ,'mfa','templates' ), + os.path.join(BASE_DIR ,'tests','templates' ) + ], + 'APP_DIRS': True, + 'OPTIONS': { + 'context_processors': [ + 'django.template.context_processors.debug', + 'django.template.context_processors.request', + 'django.contrib.auth.context_processors.auth', + 'django.contrib.messages.context_processors.messages', + ], + }, + }, +] + +MFA_UNALLOWED_METHODS = [] \ No newline at end of file diff --git a/tests/templates/base.html b/tests/templates/base.html new file mode 100644 index 0000000..0929c87 --- /dev/null +++ b/tests/templates/base.html @@ -0,0 +1,11 @@ + + + + {% block head %} + {% endblock %} + + + {% block content %} + {% endblock %} + + \ No newline at end of file diff --git a/tests/test_views.py b/tests/test_views.py index a1ace31..c39366a 100644 --- a/tests/test_views.py +++ b/tests/test_views.py @@ -1,2 +1,25 @@ import pytest +from django.urls import reverse + +@pytest.mark.django_db +def test_index_unauthenticated(client): + url = reverse("mfa_home") + response = client.get(url) + assert response is not None + assert response.status_code == 302 + assert response.url=="/accounts/login/?next=/" + + +@pytest.mark.django_db +def test_index_authenticated(client, authenticated_user): + url = reverse("mfa_home") + response = client.get(url) + assert response is not None + assert response.status_code == 200 + assert isinstance(response.templates, list) + assert len(response.templates) == 4 + for template in response.templates: + assert template.name in ["modal.html", "base.html", "mfa_base.html", "MFA.html"] + +