Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Start adding unit testing #2

Merged
merged 8 commits into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
FROM python:3.10-alpine
FROM python:3.12-alpine

RUN pip install redis aiohttp starlette uvicorn
COPY ./requirements.txt /

COPY ./GeoWhitelist.py /app/
RUN pip install -r /requirements.txt

COPY ./src/geowhitelist.py /app/

COPY ./config /app/config/

Expand All @@ -19,4 +21,4 @@ WORKDIR /app

ENTRYPOINT ["python3"]

CMD ["/usr/local/bin/uvicorn","--port","9500","--host","0.0.0.0","GeoWhitelist:app"]
CMD ["/usr/local/bin/uvicorn","--port","9500","--host","0.0.0.0","geowhitelist:app"]
1 change: 1 addition & 0 deletions config/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ redis:
#password: ""

logging:
version: 1
formatters:
form01:
format: "%(asctime)s - %(levelname)s - %(message)s"
Expand Down
12 changes: 12 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"

[project]
name = "geowhitelist"
version = "0.2.2"

[tool.pytest.ini_options]
addopts = [
"--import-mode=importlib",
]
2 changes: 2 additions & 0 deletions pytest.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[pytest]
pythonpath = src
4 changes: 2 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
redis
starlette
aiohttp
gunicorn
starlette
uvicorn
pyyaml
Empty file added src/__init__.py
Empty file.
20 changes: 16 additions & 4 deletions GeoWhitelist.py → src/geowhitelist.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from starlette.requests import Request
from starlette.responses import Response

import os
import redis
import ipaddress
import logging
Expand All @@ -11,7 +12,17 @@
import datetime
import yaml

with open('./config/config.yaml', 'r') as config_file:
# Set config paths (should probably turn this into a bootstrap function)
absolutepath = os.path.abspath(__file__)
fileDirectory = os.path.dirname(absolutepath)

if fileDirectory == "/app":
configDirectory = "/app/config/"
else:
configDirectory = os.path.dirname(fileDirectory) + "/config"


with open(configDirectory + "/config.yaml", 'r') as config_file:
config = yaml.safe_load(config_file)

# Default 3h window to keep in Redis
Expand All @@ -20,7 +31,7 @@
serviceURL = config.get('service_url')

# Set Logging config
logging.config.fileConfig(config.get('logging'))
logging.config.dictConfig(config.get('logging'))

# Setup cache
cache = False
Expand All @@ -47,7 +58,7 @@
logging.info("Internal Cache set")

# Create whitelist (change to an async function with watchgod)
with open('./config/whitelist.yaml', 'r') as f:
with open(configDirectory + '/whitelist.yaml', 'r') as f:
wl_config = yaml.safe_load(f)

wl_ip = set()
Expand Down Expand Up @@ -261,10 +272,11 @@ async def getGeo(address):
"""
# Encode IP (especially v6) into URL
url = serviceURL + urllib.parse.quote(address)

logging.debug(f"URL: {url}")
async with ClientSession() as session:
async with session.get(url) as response:
html = await response.json()
logging.debug(f"Response from geojs.io:\n{html}")
return html


Expand Down
3 changes: 3 additions & 0 deletions tests/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pytest
pytest-asyncio
pytest-mock
Empty file added tests/unit/__init__.py
Empty file.
26 changes: 26 additions & 0 deletions tests/unit/test_check_ip.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
"""
Unit tests for checkIP function of GeoWhitelist
"""
import pytest
from geowhitelist import checkIP


@pytest.mark.parametrize("maybe_ip, expected_result", [
("", False),
("not an IP", False),
("10.0.0.1", True),
("6.7.8.9", True),
(['1.2.3.4', '4.6.7.8'], False),
(None, False),
])
@pytest.mark.asyncio
async def test_bad_ip(
maybe_ip,
expected_result,
monkeypatch,
mocker
):
mock_access_control = mocker.patch('geowhitelist.accessControl',
return_value=True)
res = await checkIP(maybe_ip)
assert res == expected_result