diff --git a/.coveragerc b/.coveragerc new file mode 100644 index 0000000..7527ca1 --- /dev/null +++ b/.coveragerc @@ -0,0 +1,8 @@ +[run] +source=impostor + +[report] +omit = */migrations/*,*/tests/*,impostor/admin.py,impostor/apps.py +exclude_lines = + pragma: no cover + noqa diff --git a/.github/workflows/python-package.yml b/.github/workflows/python-package.yml index 1d0da6f..6905a05 100644 --- a/.github/workflows/python-package.yml +++ b/.github/workflows/python-package.yml @@ -7,30 +7,30 @@ on: branches: [ master, _work_in_progress ] jobs: - build: + test: runs-on: ubuntu-latest - strategy: - matrix: -# python: [3.5, 3.6, 3.7, 3.8] - python: [3.6, 3.7, 3.8] - django: [dj111, dj20, dj21, dj22, dj30] - exclude: - - python: 3.5 - django: dj30 - +# strategy: +# matrix: +# python: [3.6, 3.7, 3.8] +# django: [dj111, dj20, dj21, dj22, dj30] +# exclude: +# - python: 3.5 +# django: dj30 +# steps: - uses: actions/checkout@v2 - name: Setup Python uses: actions/setup-python@v2 - with: - python-version: ${{ matrix.python }} +# with: +# python-version: ${{ matrix.python }} - name: Install Tox and any other packages run: | pip install tox flake8 pytest - name: Run Tox # Run tox using the version of Python in `PATH` - run: tox -e py -- -v +# run: tox -e py -- -v + run: tox -- -v lint: runs-on: ubuntu-latest @@ -47,3 +47,20 @@ jobs: flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics # exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics + + coverage: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - name: Setup Python + uses: actions/setup-python@v2 + - name: Install Coverage + run: | + pip install django pytest pytest-django pytest-lazy-fixture pytest-cov + pip install -e . + - name: Execute coverage + run: | + # execute tests with coverage + pytest --cov-report=xml --cov=impostor tests/ + export CODECOV_TOKEN="${{ secrets.CODECOV_TOKEN }}" + bash <(curl -s https://codecov.io/bash) \ No newline at end of file diff --git a/.gitignore b/.gitignore index 5b63f85..07ebc91 100644 --- a/.gitignore +++ b/.gitignore @@ -42,6 +42,7 @@ htmlcov/ .cache nosetests.xml coverage.xml +htmlcov *,cover .hypothesis/ diff --git a/CHANGELOG.md b/CHANGELOG.md index 89edceb..df6f088 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Change Log +## 2.0.1 (2020-06-14) + +* Added codecov coverage +* Improved code coverage +* Fixed test execution with django versions + ## 2.0.0 (2020-06-13) * Added informative badges on README.md diff --git a/README.md b/README.md index da60f5c..2b9871d 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,7 @@ # Impostor [![pypi](https://img.shields.io/pypi/v/impostor.svg)](https://pypi.python.org/pypi/impostor/) +[![codecov](https://codecov.io/gh/avallbona/Impostor/branch/master/graph/badge.svg)](https://codecov.io/gh/avallbona/Impostor) [![Downloads](https://pepy.tech/badge/impostor)](https://pepy.tech/project/impostor) [![Hit counter](http://hits.dwyl.com/avallbona/impostor.svg)](http://hits.dwyl.com/avallbona/impostor) [![Python versions](https://img.shields.io/pypi/pyversions/impostor.svg)](https://pypi.org/project/Impostor/) diff --git a/impostor/__init__.py b/impostor/__init__.py index ec7ca62..7071849 100644 --- a/impostor/__init__.py +++ b/impostor/__init__.py @@ -1 +1 @@ -__VERSION__ = 2.0 +__VERSION__ = "2.0.1" diff --git a/setup.py b/setup.py index 86391cb..fa88ab6 100644 --- a/setup.py +++ b/setup.py @@ -85,7 +85,7 @@ def read(fname): setup( name="Impostor", - version="2.0.0", + version="2.0.1", url='https://github.com/avallbona/Impostor/', author='Marko Samastur', author_email='markos@gaivo.net', diff --git a/tests/test_impostor.py b/tests/test_impostor.py index e31e0ce..21d7ff3 100644 --- a/tests/test_impostor.py +++ b/tests/test_impostor.py @@ -3,7 +3,7 @@ import pytest from django.contrib.auth import authenticate, get_user_model - +from impostor.backend import AuthBackend from impostor.forms import BigAuthenticationForm from impostor.models import ImpostorLog @@ -114,6 +114,17 @@ def test_login_admin_as_user_with_email(self): ]) def test_impersonation(self, first_user, password, impersonated_user, expected, custom_settings, rf): + """ + check diferent use cases of impersonation + + :param first_user: + :param password: + :param impersonated_user: + :param expected: + :param custom_settings: + :param rf: + :return: + """ assert ImpostorLog.objects.count() == 0 composed_username = '{} as {}'.format(first_user.username, impersonated_user.username) authenticated_user = authenticate(request=rf, username=composed_username, password=password) @@ -125,3 +136,37 @@ def test_impersonation(self, first_user, password, impersonated_user, expected, assert log.imposted_as == impersonated_user else: assert authenticated_user is None + + @pytest.mark.parametrize('user_passed, user_expected', [ + (pytest.lazy_fixture('real_user_with_supergroup'), pytest.lazy_fixture('real_user_with_supergroup')), + (None, None) + ]) + def test_get_user(self, user_passed, user_expected): + """ + check get_user method + :param user_passed: + :param user_expected: + :return: + """ + try: + user_id = user_passed.id + except AttributeError: + user_id = None + result = AuthBackend.get_user(user_id) + assert result == user_expected + + @pytest.mark.parametrize('existing_attr', [ + True, + False + ]) + def test_impostor_group(self, custom_settings, existing_attr): + """ + check impostor_group property + :param custom_settings: + :return: + """ + if existing_attr: + delattr(custom_settings, 'IMPOSTOR_GROUP') + assert AuthBackend().impostor_group is None + else: + assert AuthBackend().impostor_group is not None diff --git a/tox.ini b/tox.ini index 8e9ebd0..6f2f217 100644 --- a/tox.ini +++ b/tox.ini @@ -10,11 +10,11 @@ deps = pytest==5.4.3 pytest-django==3.9.0 pytest-lazy-fixture==0.6.3 - dj111: Django==1.11.29 - dj20: Django==2.0.13 - dj21: Django>=2.1.15 + dj111: Django>=1.11,<2.0 + dj20: Django==2.0.13,<2.1 + dj21: Django>=2.1.15,<2.2 dj22: Django>=2.2,<3.0 - dj30: Django>=3.0 + dj30: Django>=3.0,<4.0 setenv = PYTHONPATH = {toxinidir}:{toxinidir}