diff --git a/.test_env b/.test_env new file mode 100644 index 0000000..034e075 --- /dev/null +++ b/.test_env @@ -0,0 +1,4 @@ +HOST=127.0.0.1 +DEBUG=1 +USERNAME=test_username +PASSWORD=test_password diff --git a/groove/auth.py b/groove/auth.py new file mode 100644 index 0000000..2d134ea --- /dev/null +++ b/groove/auth.py @@ -0,0 +1,7 @@ +import logging +import os + + +def is_authenticated(username, password): + logging.debug(f"Authentication attempt for {username}, {password}") + return (username == os.environ.get('USERNAME') and password == os.environ.get('PASSWORD')) diff --git a/groove/conftest.py b/groove/conftest.py new file mode 100644 index 0000000..c671c5c --- /dev/null +++ b/groove/conftest.py @@ -0,0 +1,6 @@ +import pytest + + +@pytest.fixture(scope='session') +def valid_credentials(): + return (os.environ.get('USERNAME'), os.environ.get('PASSWORD')) diff --git a/groove/ondemand.py b/groove/ondemand.py index ce2d1f1..19ff729 100644 --- a/groove/ondemand.py +++ b/groove/ondemand.py @@ -1,4 +1,5 @@ -from bottle import Bottle +from bottle import Bottle, auth_basic +from groove.auth import is_authenticated server = Bottle() @@ -6,3 +7,9 @@ server = Bottle() @server.route('/') def index(): return "Groovy." + + +@server.route('/admin') +@auth_basic(is_authenticated) +def admin(): + return "Authenticated. Groovy." diff --git a/poetry.lock b/poetry.lock index 8d2bee6..22e7c01 100644 --- a/poetry.lock +++ b/poetry.lock @@ -2,7 +2,7 @@ name = "attrs" version = "22.1.0" description = "Classes Without Boilerplate" -category = "dev" +category = "main" optional = false python-versions = ">=3.5" @@ -54,7 +54,7 @@ python-versions = "!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*,!=3.4.*,!=3.5.*,!=3.6.*,>=2.7 name = "exceptiongroup" version = "1.0.4" description = "Backport of PEP 654 (exception groups)" -category = "dev" +category = "main" optional = false python-versions = ">=3.7" @@ -65,7 +65,7 @@ test = ["pytest (>=6)"] name = "iniconfig" version = "1.1.1" description = "iniconfig: brain-dead simple config-ini parsing" -category = "dev" +category = "main" optional = false python-versions = "*" @@ -73,7 +73,7 @@ python-versions = "*" name = "packaging" version = "21.3" description = "Core utilities for Python packages" -category = "dev" +category = "main" optional = false python-versions = ">=3.6" @@ -99,7 +99,7 @@ openid = ["python-openid"] name = "pluggy" version = "1.0.0" description = "plugin and hook calling mechanisms for python" -category = "dev" +category = "main" optional = false python-versions = ">=3.6" @@ -111,7 +111,7 @@ dev = ["tox", "pre-commit"] name = "pyparsing" version = "3.0.9" description = "pyparsing module - Classes and methods to define and execute parsing grammars" -category = "dev" +category = "main" optional = false python-versions = ">=3.6.8" @@ -122,7 +122,7 @@ diagrams = ["railroad-diagrams", "jinja2"] name = "pytest" version = "7.2.0" description = "pytest: simple powerful testing with Python" -category = "dev" +category = "main" optional = false python-versions = ">=3.7" @@ -152,6 +152,18 @@ pytest = ">=6.1.0" [package.extras] testing = ["coverage (>=6.2)", "hypothesis (>=5.7.1)", "flaky (>=3.5.0)", "mypy (>=0.931)", "pytest-trio (>=0.7.0)"] +[[package]] +name = "pytest-dotenv" +version = "0.5.2" +description = "A py.test plugin that parses environment files before running tests" +category = "main" +optional = false +python-versions = "*" + +[package.dependencies] +pytest = ">=5.0.0" +python-dotenv = ">=0.9.1" + [[package]] name = "python-dotenv" version = "0.21.0" @@ -175,7 +187,7 @@ python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*" name = "tomli" version = "2.0.1" description = "A lil' TOML parser" -category = "dev" +category = "main" optional = false python-versions = ">=3.7" @@ -199,7 +211,7 @@ test = ["shellingham (>=1.3.0,<2.0.0)", "pytest (>=4.4.0,<8.0.0)", "pytest-cov ( [metadata] lock-version = "1.1" python-versions = "^3.10" -content-hash = "378d1cbceb4b319261334ce6711477ffb7cfefbcfd6efbc4648276a78a63c2db" +content-hash = "88c9ad77d7dc2477ea033e38852a188c7b6cfdf2b75a28f6e2553ae6652c9e9e" [metadata.files] attrs = [] @@ -215,6 +227,7 @@ pluggy = [] pyparsing = [] pytest = [] pytest-asyncio = [] +pytest-dotenv = [] python-dotenv = [] six = [] tomli = [] diff --git a/pyproject.toml b/pyproject.toml index 896a6aa..a6ca91d 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -18,6 +18,7 @@ Paste = "^3.5.2" [tool.poetry.dev-dependencies] pytest = "^7.2.0" pytest-asyncio = "^0.20.2" +pytest-dotenv = "^0.5.2" boddle = "^0.2.9" [build-system] diff --git a/pytest.ini b/pytest.ini new file mode 100644 index 0000000..4daee8e --- /dev/null +++ b/pytest.ini @@ -0,0 +1,6 @@ +[pytest] +env_override_existing_values = 1 +env_files = .test_env +log_cli_level = DEBUG + + diff --git a/test/test_ondemand.py b/test/test_ondemand.py index 9b3945e..a5d8afd 100644 --- a/test/test_ondemand.py +++ b/test/test_ondemand.py @@ -1,7 +1,13 @@ from boddle import boddle from groove import ondemand +import os def test_ondemand_server(): with boddle(): assert ondemand.index() == 'Groovy.' + + +def test_ondemand_auth(): + with boddle(auth=(os.environ.get('USERNAME'), os.environ.get('PASSWORD'))): + assert ondemand.admin() == 'Authenticated. Groovy.'