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
8 changes: 4 additions & 4 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from unittest.mock import MagicMock

import pytest
import pytest_asyncio
from asgiref.sync import sync_to_async
from django.conf import settings
from django.test import override_settings
Expand Down Expand Up @@ -163,7 +164,7 @@ def installation(get_mock_github_api, baker):
return installation


@pytest.fixture
@pytest_asyncio.fixture
async def ainstallation(get_mock_github_api, baker):
installation = await sync_to_async(baker.make)(
"django_github_app.Installation", installation_id=seq.next()
Expand Down Expand Up @@ -206,14 +207,13 @@ def repository(installation, get_mock_github_api, baker):
return repository


@pytest.fixture
@pytest_asyncio.fixture
async def arepository(ainstallation, get_mock_github_api, baker):
installation = await ainstallation
repository = await sync_to_async(baker.make)(
"django_github_app.Repository",
repository_id=seq.next(),
full_name="owner/repo",
installation=installation,
installation=ainstallation,
)
mock_github_api = get_mock_github_api(
[
Expand Down
31 changes: 13 additions & 18 deletions tests/events/test_ainstallation.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,9 @@ async def test_acreate_installation(


async def test_adelete_installation(ainstallation):
installation = await ainstallation
data = {
"installation": {
"id": installation.installation_id,
"id": ainstallation.installation_id,
}
}
event = sansio.Event(data, event="installation", delivery_id="1234")
Expand All @@ -73,55 +72,51 @@ async def test_adelete_installation(ainstallation):
async def test_atoggle_installation_status_suspend(
status, action, expected, ainstallation
):
installation = await ainstallation
installation.status = status
await installation.asave()
ainstallation.status = status
await ainstallation.asave()

data = {
"action": action,
"installation": {
"id": installation.installation_id,
"id": ainstallation.installation_id,
},
}
event = sansio.Event(data, event="installation", delivery_id="1234")

assert installation.status != expected
assert ainstallation.status != expected

await atoggle_installation_status(event, None)

await installation.arefresh_from_db()
assert installation.status == expected
await ainstallation.arefresh_from_db()
assert ainstallation.status == expected


async def test_async_installation_data(ainstallation):
installation = await ainstallation

data = {
"installation": {
"id": installation.installation_id,
"id": ainstallation.installation_id,
},
}
event = sansio.Event(data, event="installation", delivery_id="1234")

assert installation.data != data
assert ainstallation.data != data

await async_installation_data(event, None)

await installation.arefresh_from_db()
assert installation.data == data["installation"]
await ainstallation.arefresh_from_db()
assert ainstallation.data == data["installation"]


async def test_async_installation_repositories(ainstallation):
installation = await ainstallation
existing_repo = await sync_to_async(baker.make)(
"django_github_app.Repository",
installation=installation,
installation=ainstallation,
repository_id=seq.next(),
)

data = {
"installation": {
"id": installation.installation_id,
"id": ainstallation.installation_id,
},
"repositories_removed": [
{
Expand Down
3 changes: 1 addition & 2 deletions tests/events/test_arepository.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@


async def test_arename_repository(ainstallation, repository_id):
installation = await ainstallation
repository = await sync_to_async(baker.make)(
"django_github_app.Repository",
installation=installation,
installation=ainstallation,
repository_id=repository_id,
full_name=f"owner/old_name_{seq.next()}",
)
Expand Down
14 changes: 4 additions & 10 deletions tests/test_github.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,11 @@
@pytest.mark.django_db
class TestAsyncGitHubAPI:
async def test_init_with_two_installation_kwargs(self, ainstallation):
installation = await ainstallation

with pytest.raises(ValueError):
AsyncGitHubAPI(
"test",
installation=installation,
installation_id=installation.installation_id,
installation=ainstallation,
installation_id=ainstallation.installation_id,
)

async def test_request(self, httpx_mock):
Expand All @@ -36,9 +34,7 @@ async def mock_aget_access_token(*args, **kwargs):

monkeypatch.setattr(Installation, "aget_access_token", mock_aget_access_token)

installation = await ainstallation

async with AsyncGitHubAPI("test", installation=installation) as gh:
async with AsyncGitHubAPI("test", installation=ainstallation) as gh:
assert gh.oauth_token == "ABC123"

async def test_oauth_token_installation_id(self, ainstallation, monkeypatch):
Expand All @@ -47,10 +43,8 @@ async def mock_aget_access_token(*args, **kwargs):

monkeypatch.setattr(Installation, "aget_access_token", mock_aget_access_token)

installation = await ainstallation

async with AsyncGitHubAPI(
"test", installation_id=installation.installation_id
"test", installation_id=ainstallation.installation_id
) as gh:
assert gh.oauth_token == "ABC123"

Expand Down
43 changes: 17 additions & 26 deletions tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,14 +220,13 @@ def test_create_from_gh_data(self):

@pytest.mark.asyncio
async def test_aget_from_event(self, ainstallation, create_event):
installation = await ainstallation
event = create_event(
{"installation": {"id": installation.installation_id}}, "installation"
{"installation": {"id": ainstallation.installation_id}}, "installation"
)

result = await Installation.objects.aget_from_event(event)

assert result == installation
assert result == ainstallation

@pytest.mark.asyncio
async def test_aget_from_event_doesnotexist(self, installation_id, create_event):
Expand Down Expand Up @@ -287,15 +286,13 @@ async def test_arefresh_from_gh(
get_mock_github_api,
override_app_settings,
):
installation = await ainstallation

mock_github_api = get_mock_github_api({"foo": "bar"})
installation.get_gh_client = MagicMock(return_value=mock_github_api)
ainstallation.get_gh_client = MagicMock(return_value=mock_github_api)

with override_app_settings(PRIVATE_KEY=private_key):
await installation.arefresh_from_gh(account_type, "test")
await ainstallation.arefresh_from_gh(account_type, "test")

assert installation.data == {"foo": "bar"}
assert ainstallation.data == {"foo": "bar"}

@pytest.mark.parametrize("account_type", ["org", "user"])
def test_refresh_from_gh(
Expand All @@ -320,9 +317,7 @@ def test_refresh_from_gh_invalid_account_type(self, installation):

@pytest.mark.asyncio
async def test_aget_repos(self, ainstallation):
installation = await ainstallation

repos = await installation.aget_repos()
repos = await ainstallation.aget_repos()

assert len(repos) == 2
assert repos[0]["node_id"] == "node1"
Expand Down Expand Up @@ -353,20 +348,21 @@ def test_app_slug(self):
class TestRepositoryManager:
@pytest.mark.asyncio
async def test_acreate_from_gh_data_list(self, ainstallation):
installation = await ainstallation
data = [
{"id": seq.next(), "node_id": "node1", "full_name": "owner/repo1"},
{"id": seq.next(), "node_id": "node2", "full_name": "owner/repo2"},
]

repositories = await Repository.objects.acreate_from_gh_data(data, installation)
repositories = await Repository.objects.acreate_from_gh_data(
data, ainstallation
)

assert len(repositories) == len(data)
for i, repo in enumerate(repositories):
assert repo.repository_id == data[i]["id"]
assert repo.repository_node_id == data[i]["node_id"]
assert repo.full_name == data[i]["full_name"]
assert repo.installation_id == installation.id
assert repo.installation_id == ainstallation.id

def test_create_from_gh_data_list(self, installation):
data = [
Expand All @@ -385,15 +381,14 @@ def test_create_from_gh_data_list(self, installation):

@pytest.mark.asyncio
async def test_acreate_from_gh_data_single(self, ainstallation):
installation = await ainstallation
data = {"id": seq.next(), "node_id": "node1", "full_name": "owner/repo1"}

repository = await Repository.objects.acreate_from_gh_data(data, installation)
repository = await Repository.objects.acreate_from_gh_data(data, ainstallation)

assert repository.repository_id == data["id"]
assert repository.repository_node_id == data["node_id"]
assert repository.full_name == data["full_name"]
assert repository.installation_id == installation.id
assert repository.installation_id == ainstallation.id

def test_create_from_gh_data_single(self, installation):
data = {"id": seq.next(), "node_id": "node1", "full_name": "owner/repo1"}
Expand All @@ -407,13 +402,11 @@ def test_create_from_gh_data_single(self, installation):

@pytest.mark.asyncio
async def test_aget_from_event(self, arepository, create_event):
repository = await arepository

data = {
"repository": {
"id": repository.repository_id,
"node_id": repository.repository_node_id,
"full_name": repository.full_name,
"id": arepository.repository_id,
"node_id": arepository.repository_node_id,
"full_name": arepository.full_name,
}
}

Expand All @@ -424,7 +417,7 @@ async def test_aget_from_event(self, arepository, create_event):
assert repo.repository_id == data["repository"]["id"]
assert repo.repository_node_id == data["repository"]["node_id"]
assert repo.full_name == data["repository"]["full_name"]
assert repo.installation_id == repository.installation.id
assert repo.installation_id == arepository.installation.id

@pytest.mark.asyncio
async def test_aget_from_event_doesnotexist(self, repository_id, create_event):
Expand Down Expand Up @@ -466,9 +459,7 @@ def test_get_gh_client(self, repository):

@pytest.mark.asyncio
async def test_aget_issues(self, arepository):
repository = await arepository

issues = await repository.aget_issues()
issues = await arepository.aget_issues()

assert len(issues) == 2
assert issues[0]["number"] == 1
Expand Down