From c5a72ac3ee4d4e4fafd0b06b1acd805219f7b326 Mon Sep 17 00:00:00 2001 From: Michael van Tellingen Date: Mon, 19 Aug 2024 20:45:43 +0200 Subject: [PATCH] chore: just simplify --- .github/workflows/python-tests.yml | 42 +++++------------------------- pyproject.toml | 6 +++++ requirements-dev.lock | 41 +++++++++++++++++++++++++++++ src/django_iam_dbauth/utils.py | 1 + tests/conftest.py | 12 +++++++++ tests/test_aws_mysql.py | 25 ++++++------------ tests/test_aws_postgresql.py | 34 +++++------------------- tox.ini | 25 ------------------ 8 files changed, 82 insertions(+), 104 deletions(-) delete mode 100644 tox.ini diff --git a/.github/workflows/python-tests.yml b/.github/workflows/python-tests.yml index 2d04b75..fa29476 100644 --- a/.github/workflows/python-tests.yml +++ b/.github/workflows/python-tests.yml @@ -3,45 +3,14 @@ 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 @@ -49,5 +18,8 @@ jobs: - 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 diff --git a/pyproject.toml b/pyproject.toml index 9adf6b2..43679ed 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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] diff --git a/requirements-dev.lock b/requirements-dev.lock index 4a4bb6a..9b7fe37 100644 --- a/requirements-dev.lock +++ b/requirements-dev.lock @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/src/django_iam_dbauth/utils.py b/src/django_iam_dbauth/utils.py index b04035d..fafaa3d 100644 --- a/src/django_iam_dbauth/utils.py +++ b/src/django_iam_dbauth/utils.py @@ -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() base_domain = dns.name.from_text("rds.amazonaws.com") answer = dns.name.from_text(hostname) while not answer.is_subdomain(base_domain): diff --git a/tests/conftest.py b/tests/conftest.py index 489d705..ed2c729 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,4 +1,7 @@ from django.conf import settings +from moto import mock_aws +import boto3 +import pytest def pytest_configure(): @@ -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 diff --git a/tests/test_aws_mysql.py b/tests/test_aws_mysql.py index 49aac48..4b0666f 100644 --- a/tests/test_aws_mysql.py +++ b/tests/test_aws_mysql.py @@ -1,19 +1,11 @@ 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", @@ -21,15 +13,14 @@ def generate_db_auth_token(**kwargs): "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") diff --git a/tests/test_aws_postgresql.py b/tests/test_aws_postgresql.py index e74b1b2..f3f23e4 100644 --- a/tests/test_aws_postgresql.py +++ b/tests/test_aws_postgresql.py @@ -1,19 +1,7 @@ -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", @@ -21,22 +9,14 @@ def generate_db_auth_token(**kwargs): "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") diff --git a/tox.ini b/tox.ini deleted file mode 100644 index 093b9a4..0000000 --- a/tox.ini +++ /dev/null @@ -1,25 +0,0 @@ -[tox] -envlist = py{38,39,310,311,312}-django{42,50,51} - -[allow_externals] - - -[gh-actions] -python = - 3.8: py38 - 3.9: py39 - 3.10: py310 - 3.11: py311 - 3.12: py312 - -[testenv] -whitelist_externals = pytest -commands = pytest {posargs} -deps = - django42: Django>=4.2,<4.3 - django50: Django>=5.0,<5.1 - django51: Django>=5.1,<5.2 - psycopg2 - mysqlclient - boto3 -extras = test