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()
@ -6,3 +7,9 @@ server = Bottle()
@server.route('/')
def index():
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"
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 = []

View File

@ -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]

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 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.'