Skip to content

Commit

Permalink
chore: just simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
mvantellingen committed Aug 19, 2024
1 parent 023595a commit c5a72ac
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 104 deletions.
42 changes: 7 additions & 35 deletions .github/workflows/python-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,51 +3,23 @@ name: Python Tests
on: [push]

jobs:
format:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Install the latest version of rye
uses: eifinger/setup-rye@v4

- name: Validate formatting
run: rye format --check

test:
runs-on: ubuntu-latest
needs: format
strategy:
max-parallel: 4
matrix:
tox_env:
- py38-django42
- py312-django42
- py312-django50
- py312-django51
include:
- python-version: 3.8
tox_env: py38-django42
- python-version: 3.12
tox_env: py312-django42
- python-version: 3.12
tox_env: py312-django50
- python-version: 3.12
tox_env: py312-django51

steps:
- uses: actions/checkout@v4
- name: Set up Python ${{ matrix.python-version }}

- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: "${{ matrix.python-version }}"
python-version: "3.12"

- name: Install the latest version of rye
uses: eifinger/setup-rye@v4

- name: Install dependencies
run: rye sync

- name: Test with tox
run: rye run tox -e ${{ matrix.tox_env }}
- name: Validate formatting
run: rye format --check

- name: Test
run: rye test
6 changes: 6 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ dev-dependencies = [
"coverage>=7.6.1",
"tox>=4.18.0",
"tox-gh-actions>=3.2.0",
"pretend>=1.0.9",
"psycopg2>=2.9.9",
"mysqlclient>=2.2.4",
"pytest-mock>=3.14.0",
"pytest-django>=4.8.0",
"moto>=5.0.13",
]

[build-system]
Expand Down
41 changes: 41 additions & 0 deletions requirements-dev.lock
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,26 @@ asgiref==3.8.1
# via django
boto3==1.35.0
# via django-iam-dbauth
# via moto
botocore==1.35.0
# via boto3
# via moto
# via s3transfer
cachetools==5.5.0
# via tox
certifi==2024.7.4
# via requests
cffi==1.17.0
# via cryptography
chardet==5.2.0
# via tox
charset-normalizer==3.3.2
# via requests
colorama==0.4.6
# via tox
coverage==7.6.1
cryptography==43.0.0
# via moto
distlib==0.3.8
# via virtualenv
django==5.1
Expand All @@ -33,11 +43,20 @@ dnspython==2.6.1
filelock==3.15.4
# via tox
# via virtualenv
idna==3.7
# via requests
iniconfig==2.0.0
# via pytest
jinja2==3.1.4
# via moto
jmespath==1.0.1
# via boto3
# via botocore
markupsafe==2.1.5
# via jinja2
# via werkzeug
moto==5.0.13
mysqlclient==2.2.4
packaging==24.1
# via pyproject-api
# via pytest
Expand All @@ -48,11 +67,27 @@ platformdirs==4.2.2
pluggy==1.5.0
# via pytest
# via tox
pretend==1.0.9
psycopg2==2.9.9
pycparser==2.22
# via cffi
pyproject-api==1.7.1
# via tox
pytest==8.3.2
# via pytest-django
# via pytest-mock
pytest-django==4.8.0
pytest-mock==3.14.0
python-dateutil==2.9.0.post0
# via botocore
# via moto
pyyaml==6.0.2
# via responses
requests==2.32.3
# via moto
# via responses
responses==0.25.3
# via moto
s3transfer==0.10.2
# via boto3
six==1.16.0
Expand All @@ -64,5 +99,11 @@ tox==4.18.0
tox-gh-actions==3.2.0
urllib3==2.2.2
# via botocore
# via requests
# via responses
virtualenv==20.26.3
# via tox
werkzeug==3.0.3
# via moto
xmltodict==0.13.0
# via moto
1 change: 1 addition & 0 deletions src/django_iam_dbauth/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ def resolve_cname(hostname):
Looking for the endpoint which is of the form cluster-name.accountandregionhash.regionid.rds.amazonaws.com
To do so, recursively resolve the host name until it's a subdomain of rds.amazonaws.com.
"""
breakpoint()

This comment has been minimized.

Copy link
@ticosax

ticosax Sep 6, 2024

oops

base_domain = dns.name.from_text("rds.amazonaws.com")
answer = dns.name.from_text(hostname)
while not answer.is_subdomain(base_domain):
Expand Down
12 changes: 12 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
from django.conf import settings
from moto import mock_aws
import boto3
import pytest


def pytest_configure():
Expand Down Expand Up @@ -36,3 +39,12 @@ def pytest_configure():
USE_TZ=True,
ROOT_URLCONF="test_urls",
)


@pytest.fixture
def rds_client():
# Use moto to mock the RDS client
with mock_aws():
session = boto3.session.Session()
client = session.client(service_name="rds", region_name="us-west-2")
yield client
25 changes: 8 additions & 17 deletions tests/test_aws_mysql.py
Original file line number Diff line number Diff line change
@@ -1,35 +1,26 @@
import boto3
import urllib
import pretend

from django_iam_dbauth.aws.mysql.base import DatabaseWrapper


def test_get_connection_params(mocker):
token_kwargs = {}

def generate_db_auth_token(**kwargs):
token_kwargs.update(kwargs)
return "generated-token"

client = pretend.stub(generate_db_auth_token=generate_db_auth_token)
mocker.patch.object(boto3, "client", return_value=client)

def test_get_connection_params(rds_client):
settings = {
"NAME": "example",
"USER": "mysql",
"PASSWORD": "secret",
"PORT": 3306,
"HOST": "example-cname.labd.nl",
"ENGINE": "django_iam_dbauth.aws.mysql",
"OPTIONS": {"use_iam_auth": 1, "region_name": "test"},
"OPTIONS": {
"use_iam_auth": 1,
"region_name": "test",
"resolve_cname_enabled": False,
},
}

db = DatabaseWrapper(settings)
params = db.get_connection_params()

assert params["password"] == "generated-token"
assert token_kwargs == {
"DBHostname": "cluster-name.accountandregionhash.eu-west-1.rds.amazonaws.com",
"DBUsername": "mysql",
"Port": 3306,
}
assert params["password"].startswith("example-cname.labd.nl")
34 changes: 7 additions & 27 deletions tests/test_aws_postgresql.py
Original file line number Diff line number Diff line change
@@ -1,42 +1,22 @@
import boto3
import pretend

from django_iam_dbauth.aws.postgresql.base import DatabaseWrapper


def test_get_connection_params(mocker):
token_kwargs = {}

def generate_db_auth_token(**kwargs):
token_kwargs.update(kwargs)
return "generated-token"

client = pretend.stub(generate_db_auth_token=generate_db_auth_token)
mocker.patch.object(boto3, "client", return_value=client)

def test_get_connection_params(rds_client):
settings = {
"NAME": "example",
"USER": "postgresql",
"PASSWORD": "secret",
"PORT": 5432,
"HOST": "example-cname.labd.nl",
"ENGINE": "django_iam_dbauth.aws.postgresql",
"OPTIONS": {"use_iam_auth": 1, "region_name": "test"},
"OPTIONS": {
"use_iam_auth": 1,
"region_name": "test",
"resolve_cname_enabled": False,
},
}

db = DatabaseWrapper(settings)
params = db.get_connection_params()

expected = {
"database": "example",
"user": "postgresql",
"password": "generated-token",
"port": 5432,
"host": "example-cname.labd.nl",
}
assert params == expected
assert token_kwargs == {
"DBHostname": "cluster-name.accountandregionhash.eu-west-1.rds.amazonaws.com",
"DBUsername": "postgresql",
"Port": 5432,
}
assert params["password"].startswith("example-cname.labd.nl")
25 changes: 0 additions & 25 deletions tox.ini

This file was deleted.

0 comments on commit c5a72ac

Please sign in to comment.