Skip to content
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
13 changes: 13 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
from __future__ import annotations

import contextlib
import logging
from unittest.mock import AsyncMock
from unittest.mock import MagicMock

import pytest
from asgiref.sync import sync_to_async
from django.conf import settings
from django.test import override_settings
from model_bakery import baker

from django_github_app.conf import GITHUB_APP_SETTINGS_NAME
from django_github_app.github import AsyncGitHubAPI

from .settings import DEFAULT_SETTINGS
Expand Down Expand Up @@ -55,6 +58,16 @@ def pytest_configure(config):
}


@pytest.fixture
def override_app_settings():
@contextlib.contextmanager
def _override_app_settings(**kwargs):
with override_settings(**{GITHUB_APP_SETTINGS_NAME: {**kwargs}}):
yield

return _override_app_settings


@pytest.fixture
def installation_id():
return seq.next()
Expand Down
9 changes: 4 additions & 5 deletions tests/events/test_installation.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import pytest
from asgiref.sync import sync_to_async
from django.test import override_settings
from gidgethub.abc import sansio
from model_bakery import baker

Expand All @@ -19,7 +18,9 @@
pytestmark = [pytest.mark.asyncio, pytest.mark.django_db]


async def test_create_installation(installation_id, repository_id):
async def test_create_installation(
installation_id, repository_id, override_app_settings
):
data = {
"installation": {
"id": installation_id,
Expand All @@ -31,9 +32,7 @@ async def test_create_installation(installation_id, repository_id):
}
event = sansio.Event(data, event="installation", delivery_id="1234")

with override_settings(
DJANGO_GITHUB_APP={"APP_ID": str(data["installation"]["app_id"])}
):
with override_app_settings(APP_ID=str(data["installation"]["app_id"])):
await create_installation(event, None)

installation = await Installation.objects.aget(
Expand Down
12 changes: 4 additions & 8 deletions tests/test_conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import pytest
from django.conf import settings
from django.test import override_settings

from django_github_app.conf import GITHUB_APP_SETTINGS_NAME
from django_github_app.conf import app_settings


Expand All @@ -20,7 +20,7 @@
],
)
def test_default_settings(setting, default_setting):
user_settings = getattr(settings, "DJANGO_GITHUB_APP", {})
user_settings = getattr(settings, GITHUB_APP_SETTINGS_NAME, {})

assert user_settings == {}
assert getattr(app_settings, setting) == default_setting
Expand All @@ -46,10 +46,6 @@ def test_default_settings(setting, default_setting):
("v1.0.0-beta", "v100-beta"),
],
)
def test_slug(name, expected):
with override_settings(
DJANGO_GITHUB_APP={
"NAME": name,
},
):
def test_slug(name, expected, override_app_settings):
with override_app_settings(NAME=name):
assert app_settings.SLUG == expected
13 changes: 4 additions & 9 deletions tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import pytest
from asgiref.sync import sync_to_async
from django.test import override_settings
from django.utils import timezone
from gidgethub import sansio
from model_bakery import baker
Expand Down Expand Up @@ -109,7 +108,7 @@ def test_action_property(self, payload, expected):

class TestInstallationManager:
@pytest.mark.asyncio
async def test_acreate_from_event(self, create_event):
async def test_acreate_from_event(self, create_event, override_app_settings):
repositories = [
{"id": seq.next(), "node_id": "node1", "full_name": "owner/repo1"},
{"id": seq.next(), "node_id": "node2", "full_name": "owner/repo2"},
Expand All @@ -126,9 +125,7 @@ async def test_acreate_from_event(self, create_event):
"installation",
)

with override_settings(
DJANGO_GITHUB_APP={"APP_ID": str(installation_data["app_id"])}
):
with override_app_settings(APP_ID=str(installation_data["app_id"])):
installation = await Installation.objects.acreate_from_event(event)

assert installation.installation_id == installation_data["id"]
Expand All @@ -137,7 +134,7 @@ async def test_acreate_from_event(self, create_event):
installation=installation
).acount() == len(repositories)

def test_create_from_event(self, create_event):
def test_create_from_event(self, create_event, override_app_settings):
repositories = [
{"id": seq.next(), "node_id": "node1", "full_name": "owner/repo1"},
{"id": seq.next(), "node_id": "node2", "full_name": "owner/repo2"},
Expand All @@ -154,9 +151,7 @@ def test_create_from_event(self, create_event):
"installation",
)

with override_settings(
DJANGO_GITHUB_APP={"APP_ID": str(installation_data["app_id"])}
):
with override_app_settings(APP_ID=str(installation_data["app_id"])):
installation = Installation.objects.create_from_event(event)

assert installation.installation_id == installation_data["id"]
Expand Down
5 changes: 2 additions & 3 deletions tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
from asgiref.sync import sync_to_async
from django.core.exceptions import BadRequest
from django.http import JsonResponse
from django.test import override_settings
from django.utils import timezone
from gidgethub import sansio
from model_bakery import baker
Expand All @@ -28,8 +27,8 @@


@pytest.fixture(autouse=True)
def webhook_secret():
with override_settings(DJANGO_GITHUB_APP={"WEBHOOK_SECRET": WEBHOOK_SECRET}):
def webhook_secret(override_app_settings):
with override_app_settings(WEBHOOK_SECRET=WEBHOOK_SECRET):
yield


Expand Down