Adding pytest config / env and basic auth

This commit is contained in:
evilchili 2022-11-19 16:06:23 -08:00
parent e28a2ffb95
commit 27b97f2bc4
8 changed files with 60 additions and 10 deletions

4
.test_env Normal file
View File

@ -0,0 +1,4 @@
HOST=127.0.0.1
DEBUG=1
USERNAME=test_username
PASSWORD=test_password

7
groove/auth.py Normal file
View File

@ -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'))

6
groove/conftest.py Normal file
View File

@ -0,0 +1,6 @@
import pytest
@pytest.fixture(scope='session')
def valid_credentials():
return (os.environ.get('USERNAME'), os.environ.get('PASSWORD'))

View File

@ -1,4 +1,5 @@
from bottle import Bottle from bottle import Bottle, auth_basic
from groove.auth import is_authenticated
server = Bottle() server = Bottle()
@ -6,3 +7,9 @@ server = Bottle()
@server.route('/') @server.route('/')
def index(): def index():
return "Groovy." return "Groovy."
@server.route('/admin')
@auth_basic(is_authenticated)
def admin():
return "Authenticated. Groovy."

31
poetry.lock generated
View File

@ -2,7 +2,7 @@
name = "attrs" name = "attrs"
version = "22.1.0" version = "22.1.0"
description = "Classes Without Boilerplate" description = "Classes Without Boilerplate"
category = "dev" category = "main"
optional = false optional = false
python-versions = ">=3.5" 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" name = "exceptiongroup"
version = "1.0.4" version = "1.0.4"
description = "Backport of PEP 654 (exception groups)" description = "Backport of PEP 654 (exception groups)"
category = "dev" category = "main"
optional = false optional = false
python-versions = ">=3.7" python-versions = ">=3.7"
@ -65,7 +65,7 @@ test = ["pytest (>=6)"]
name = "iniconfig" name = "iniconfig"
version = "1.1.1" version = "1.1.1"
description = "iniconfig: brain-dead simple config-ini parsing" description = "iniconfig: brain-dead simple config-ini parsing"
category = "dev" category = "main"
optional = false optional = false
python-versions = "*" python-versions = "*"
@ -73,7 +73,7 @@ python-versions = "*"
name = "packaging" name = "packaging"
version = "21.3" version = "21.3"
description = "Core utilities for Python packages" description = "Core utilities for Python packages"
category = "dev" category = "main"
optional = false optional = false
python-versions = ">=3.6" python-versions = ">=3.6"
@ -99,7 +99,7 @@ openid = ["python-openid"]
name = "pluggy" name = "pluggy"
version = "1.0.0" version = "1.0.0"
description = "plugin and hook calling mechanisms for python" description = "plugin and hook calling mechanisms for python"
category = "dev" category = "main"
optional = false optional = false
python-versions = ">=3.6" python-versions = ">=3.6"
@ -111,7 +111,7 @@ dev = ["tox", "pre-commit"]
name = "pyparsing" name = "pyparsing"
version = "3.0.9" version = "3.0.9"
description = "pyparsing module - Classes and methods to define and execute parsing grammars" description = "pyparsing module - Classes and methods to define and execute parsing grammars"
category = "dev" category = "main"
optional = false optional = false
python-versions = ">=3.6.8" python-versions = ">=3.6.8"
@ -122,7 +122,7 @@ diagrams = ["railroad-diagrams", "jinja2"]
name = "pytest" name = "pytest"
version = "7.2.0" version = "7.2.0"
description = "pytest: simple powerful testing with Python" description = "pytest: simple powerful testing with Python"
category = "dev" category = "main"
optional = false optional = false
python-versions = ">=3.7" python-versions = ">=3.7"
@ -152,6 +152,18 @@ pytest = ">=6.1.0"
[package.extras] [package.extras]
testing = ["coverage (>=6.2)", "hypothesis (>=5.7.1)", "flaky (>=3.5.0)", "mypy (>=0.931)", "pytest-trio (>=0.7.0)"] 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]] [[package]]
name = "python-dotenv" name = "python-dotenv"
version = "0.21.0" version = "0.21.0"
@ -175,7 +187,7 @@ python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*"
name = "tomli" name = "tomli"
version = "2.0.1" version = "2.0.1"
description = "A lil' TOML parser" description = "A lil' TOML parser"
category = "dev" category = "main"
optional = false optional = false
python-versions = ">=3.7" 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] [metadata]
lock-version = "1.1" lock-version = "1.1"
python-versions = "^3.10" python-versions = "^3.10"
content-hash = "378d1cbceb4b319261334ce6711477ffb7cfefbcfd6efbc4648276a78a63c2db" content-hash = "88c9ad77d7dc2477ea033e38852a188c7b6cfdf2b75a28f6e2553ae6652c9e9e"
[metadata.files] [metadata.files]
attrs = [] attrs = []
@ -215,6 +227,7 @@ pluggy = []
pyparsing = [] pyparsing = []
pytest = [] pytest = []
pytest-asyncio = [] pytest-asyncio = []
pytest-dotenv = []
python-dotenv = [] python-dotenv = []
six = [] six = []
tomli = [] tomli = []

View File

@ -18,6 +18,7 @@ Paste = "^3.5.2"
[tool.poetry.dev-dependencies] [tool.poetry.dev-dependencies]
pytest = "^7.2.0" pytest = "^7.2.0"
pytest-asyncio = "^0.20.2" pytest-asyncio = "^0.20.2"
pytest-dotenv = "^0.5.2"
boddle = "^0.2.9" boddle = "^0.2.9"
[build-system] [build-system]

6
pytest.ini Normal file
View File

@ -0,0 +1,6 @@
[pytest]
env_override_existing_values = 1
env_files = .test_env
log_cli_level = DEBUG

View File

@ -1,7 +1,13 @@
from boddle import boddle from boddle import boddle
from groove import ondemand from groove import ondemand
import os
def test_ondemand_server(): def test_ondemand_server():
with boddle(): with boddle():
assert ondemand.index() == 'Groovy.' assert ondemand.index() == 'Groovy.'
def test_ondemand_auth():
with boddle(auth=(os.environ.get('USERNAME'), os.environ.get('PASSWORD'))):
assert ondemand.admin() == 'Authenticated. Groovy.'