Skip to content

Commit

Permalink
Merge pull request #245 from consideRatio/pr/refactors
Browse files Browse the repository at this point in the history
Add test as optional dependency, refactor fixtures to conftest.py, fix code coverage setup
  • Loading branch information
consideRatio authored May 21, 2023
2 parents 5d4724b + 274ec02 commit 1ec8ffd
Show file tree
Hide file tree
Showing 10 changed files with 67 additions and 82 deletions.
35 changes: 16 additions & 19 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,12 @@ on:
paths-ignore:
- "docs/**"
- "**.md"
- "**.rst"
- ".github/workflows/*"
- "!.github/workflows/test.yaml"
push:
paths-ignore:
- "docs/**"
- "**.md"
- "**.rst"
- ".github/workflows/*"
- "!.github/workflows/test.yaml"
branches-ignore:
Expand All @@ -25,25 +23,29 @@ on:

jobs:
pytest:
name: Run pytests
# FIXME: ubuntu-20.04 comes with py36, but ubuntu-22.04 doesn't, so only
# bump this when we stop testing py36
runs-on: ubuntu-20.04

strategy:
fail-fast: false
matrix:
include:
- python-version: "3.6"
jupyterhub-version: "1.3.*"
pip-install-spec: "jupyterhub==1.3.* sqlalchemy==1.*"
- python-version: "3.7"
jupyterhub-version: "1.4.*"
pip-install-spec: "jupyterhub==1.4.* sqlalchemy==1.*"
- python-version: "3.8"
jupyterhub-version: "1.*"
pip-install-spec: "jupyterhub==1.* sqlalchemy==1.*"
- python-version: "3.9"
jupyterhub-version: "2.*"
pip-install-spec: "jupyterhub==2.* sqlalchemy==1.*"
- python-version: "3.10"
jupyterhub-version: "3.*"
pip-install-spec: "jupyterhub==3.*"
- python-version: "3.11"
jupyterhub-version: "4.*"
pip-install-spec: "jupyterhub==4.*"
- python-version: "3.x"
pip-install-spec: "--pre jupyterhub"
accept-failure: true

steps:
- uses: actions/checkout@v3
Expand All @@ -53,21 +55,16 @@ jobs:

- name: Install Python dependencies
run: |
pip install --no-cache-dir -r dev-requirements.txt
pip install --pre "jupyterhub==${{ matrix.jupyterhub-version }}"
if [ $(echo "${{ matrix.jupyterhub-version }}" | grep '[12]\.') ]; then
# pin sqlalchemy 1 on jupyterhub < 3
pip install 'sqlalchemy==1.*'
fi
pip install .
pip install ${{ matrix.pip-install-spec }}
pip install ".[test]"
- name: List packages
run: pip freeze

- name: Run tests
continue-on-error: ${{ matrix.accept-failure == true }}
run: |
pytest --verbose --maxfail=2 --color=yes
pytest --maxfail=2 --cov=nativeauthenticator
- name: Upload code coverage report
run: codecov
# GitHub action reference: https://github.com/codecov/codecov-action
- uses: codecov/codecov-action@v3
15 changes: 4 additions & 11 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,10 @@ for a friendly and welcoming collaborative environment.

## Setting up a development environment

### Install requirements

First install [JupyterHub](https://github.com/jupyterhub/jupyterhub). Then you can install de development requirements for Native Authenticator:

```shell
pip install -r dev-requirements.txt
```

And then installing Native Authenticator from master branch:
### Install

```shell
pip install -e .
pip install -e ".[test]"
```

### Configure pre-commit
Expand All @@ -37,7 +29,8 @@ following command from the root of this repository next to the
`.pre-commit-config.yaml` file.

```shell
pre-commit install
pip install pre-commit
pre-commit install --install-hooks
```

### Running your local project
Expand Down
1 change: 0 additions & 1 deletion MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
include LICENSE
include dev-requirements.txt

graft docs
prune docs/_build
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ The latest documentation is always on readTheDocs, available [here](https://nati
To run the tests locally, you can install the development dependencies like so:

```shell
pip install -r dev-requirements.txt
pip install -e ".[test]"
```

Then run tests with pytest:

```
```shell
pytest
```
8 changes: 0 additions & 8 deletions dev-requirements.txt

This file was deleted.

14 changes: 14 additions & 0 deletions nativeauthenticator/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import pytest
from jupyterhub.tests.mocking import MockHub


@pytest.fixture
def tmpcwd(tmpdir):
tmpdir.chdir()


@pytest.fixture
def app():
hub = MockHub()
hub.init_db()
return hub
23 changes: 2 additions & 21 deletions nativeauthenticator/tests/test_authenticator.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,13 @@
from datetime import timezone as tz

import pytest
from jupyterhub.tests.mocking import MockHub

from nativeauthenticator import NativeAuthenticator

from ..handlers import EmailAuthorizationHandler
from ..orm import UserInfo


@pytest.fixture
def tmpcwd(tmpdir):
tmpdir.chdir()


@pytest.fixture
def app():
hub = MockHub()
hub.init_db()
return hub


# use pytest-asyncio
pytestmark = pytest.mark.asyncio
# run each test in a temporary working directory
pytestmark = pytestmark(pytest.mark.usefixtures("tmpcwd"))


@pytest.mark.parametrize(
"is_admin,open_signup,expected_authorization",
[
Expand Down Expand Up @@ -346,7 +327,7 @@ async def test_import_from_firstuse_invalid_password(user, pwd, tmpcwd, app):
auth.add_data_from_firstuse()


async def test_secret_key(app):
async def test_secret_key(tmpcwd, app):
auth = NativeAuthenticator(db=app.db)
auth.ask_email_on_signup = False
auth.allow_self_approval_for = ".*@example.com$"
Expand All @@ -361,7 +342,7 @@ async def test_secret_key(app):
assert auth.ask_email_on_signup is True


async def test_approval_url(app):
async def test_approval_url(tmpcwd, app):
auth = NativeAuthenticator(db=app.db)
auth.allow_self_approval_for = ".*@example.com$"
auth.secret_key = "very long and kind-of random asdgaisgfjbafksdgasg"
Expand Down
25 changes: 6 additions & 19 deletions nativeauthenticator/tests/test_orm.py
Original file line number Diff line number Diff line change
@@ -1,37 +1,24 @@
import pytest
from jupyterhub.tests.mocking import MockHub
from sqlalchemy.exc import StatementError

from ..orm import UserInfo


@pytest.fixture
def tmpcwd(tmpdir):
tmpdir.chdir()


@pytest.fixture
def app():
hub = MockHub()
hub.init_db()
return hub


@pytest.mark.parametrize("email", ["john", "john@john"])
def test_validate_method_wrong_email(email, tmpdir, app):
def test_validate_method_wrong_email(email, tmpcwd, app):
with pytest.raises(AssertionError):
UserInfo(username="john", password=b"pwd", email=email)


def test_validate_method_correct_email(tmpdir, app):
def test_validate_method_correct_email(tmpcwd, app):
user = UserInfo(username="john", password=b"pwd", email="john@john.com")
app.db.add(user)
app.db.commit()
assert UserInfo.find(app.db, "john")


def test_all_users(tmpdir, app):
assert len(UserInfo.all_users(app.db)) == 1
def test_all_users(tmpcwd, app):
assert len(UserInfo.all_users(app.db)) == 0
user = UserInfo(
username="daenerystargaryen",
password=b"yesispeakvalyrian",
Expand All @@ -40,10 +27,10 @@ def test_all_users(tmpdir, app):
app.db.add(user)
app.db.commit()

assert len(UserInfo.all_users(app.db)) == 2
assert len(UserInfo.all_users(app.db)) == 1


def test_wrong_pwd_type(tmpdir, app):
def test_wrong_pwd_type(tmpcwd, app):
with pytest.raises(StatementError):
user = UserInfo(username="john", password="pwd", email="john@john.com")
app.db.add(user)
Expand Down
10 changes: 10 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,16 @@ target_version = [
profile = "black"


# pytest is used for running Python based tests
#
# ref: https://docs.pytest.org/en/stable/
#
[tool.pytest.ini_options]
addopts = "--verbose --color=yes --durations=10"
asyncio_mode = "auto"
testpaths = ["nativeauthenticator/tests"]


# tbump is used to simplify and standardize the release process when updating
# the version, making a git commit and tag, and pushing changes.
#
Expand Down
14 changes: 13 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,18 @@
author_email="leportella@protonmail.com",
license="3 Clause BSD",
packages=find_packages(),
install_requires=["jupyterhub>=1.3", "bcrypt", "onetimepass"],
install_requires=[
"jupyterhub>=1.3",
"bcrypt",
"onetimepass",
],
extras_require={
"test": [
"notebook>=6.4.1",
"pytest",
"pytest-asyncio",
"pytest-cov",
],
},
include_package_data=True,
)

0 comments on commit 1ec8ffd

Please sign in to comment.