|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import pytest |
| 4 | +from gidgethub.abc import sansio |
| 5 | +from model_bakery import baker |
| 6 | + |
| 7 | +from django_github_app.events.installation import create_installation |
| 8 | +from django_github_app.events.installation import delete_installation |
| 9 | +from django_github_app.events.installation import sync_installation_data |
| 10 | +from django_github_app.events.installation import sync_installation_repositories |
| 11 | +from django_github_app.events.installation import toggle_installation_status |
| 12 | +from django_github_app.models import Installation |
| 13 | +from django_github_app.models import InstallationStatus |
| 14 | +from django_github_app.models import Repository |
| 15 | +from tests.utils import seq |
| 16 | + |
| 17 | +pytestmark = [pytest.mark.django_db] |
| 18 | + |
| 19 | + |
| 20 | +def test_create_installation(installation_id, repository_id, override_app_settings): |
| 21 | + data = { |
| 22 | + "installation": { |
| 23 | + "id": installation_id, |
| 24 | + "app_id": seq.next(), |
| 25 | + }, |
| 26 | + "repositories": [ |
| 27 | + {"id": repository_id, "node_id": "node1234", "full_name": "owner/repo"} |
| 28 | + ], |
| 29 | + } |
| 30 | + event = sansio.Event(data, event="installation", delivery_id="1234") |
| 31 | + |
| 32 | + with override_app_settings(APP_ID=str(data["installation"]["app_id"])): |
| 33 | + create_installation(event, None) |
| 34 | + |
| 35 | + installation = Installation.objects.get(installation_id=data["installation"]["id"]) |
| 36 | + |
| 37 | + assert installation.data == data["installation"] |
| 38 | + |
| 39 | + |
| 40 | +def test_delete_installation(installation): |
| 41 | + data = { |
| 42 | + "installation": { |
| 43 | + "id": installation.installation_id, |
| 44 | + } |
| 45 | + } |
| 46 | + event = sansio.Event(data, event="installation", delivery_id="1234") |
| 47 | + |
| 48 | + delete_installation(event, None) |
| 49 | + |
| 50 | + assert not Installation.objects.filter( |
| 51 | + installation_id=data["installation"]["id"] |
| 52 | + ).exists() |
| 53 | + |
| 54 | + |
| 55 | +@pytest.mark.parametrize( |
| 56 | + "status,action,expected", |
| 57 | + [ |
| 58 | + (InstallationStatus.ACTIVE, "suspend", InstallationStatus.INACTIVE), |
| 59 | + (InstallationStatus.INACTIVE, "unsuspend", InstallationStatus.ACTIVE), |
| 60 | + ], |
| 61 | +) |
| 62 | +def test_toggle_installation_status_suspend(status, action, expected, installation): |
| 63 | + installation.status = status |
| 64 | + installation.save() |
| 65 | + |
| 66 | + data = { |
| 67 | + "action": action, |
| 68 | + "installation": { |
| 69 | + "id": installation.installation_id, |
| 70 | + }, |
| 71 | + } |
| 72 | + event = sansio.Event(data, event="installation", delivery_id="1234") |
| 73 | + |
| 74 | + assert installation.status != expected |
| 75 | + |
| 76 | + toggle_installation_status(event, None) |
| 77 | + |
| 78 | + installation.refresh_from_db() |
| 79 | + assert installation.status == expected |
| 80 | + |
| 81 | + |
| 82 | +def test_sync_installation_data(installation): |
| 83 | + data = { |
| 84 | + "installation": { |
| 85 | + "id": installation.installation_id, |
| 86 | + }, |
| 87 | + } |
| 88 | + event = sansio.Event(data, event="installation", delivery_id="1234") |
| 89 | + |
| 90 | + assert installation.data != data |
| 91 | + |
| 92 | + sync_installation_data(event, None) |
| 93 | + |
| 94 | + installation.refresh_from_db() |
| 95 | + assert installation.data == data["installation"] |
| 96 | + |
| 97 | + |
| 98 | +def test_sync_installation_repositories(installation): |
| 99 | + existing_repo = baker.make( |
| 100 | + "django_github_app.Repository", |
| 101 | + installation=installation, |
| 102 | + repository_id=seq.next(), |
| 103 | + ) |
| 104 | + |
| 105 | + data = { |
| 106 | + "installation": { |
| 107 | + "id": installation.installation_id, |
| 108 | + }, |
| 109 | + "repositories_removed": [ |
| 110 | + { |
| 111 | + "id": existing_repo.repository_id, |
| 112 | + }, |
| 113 | + ], |
| 114 | + "repositories_added": [ |
| 115 | + { |
| 116 | + "id": seq.next(), |
| 117 | + "node_id": "repo1234", |
| 118 | + "full_name": "owner/repo", |
| 119 | + } |
| 120 | + ], |
| 121 | + } |
| 122 | + event = sansio.Event(data, event="installation", delivery_id="1234") |
| 123 | + |
| 124 | + assert Repository.objects.filter( |
| 125 | + repository_id=data["repositories_removed"][0]["id"] |
| 126 | + ).exists() |
| 127 | + assert not Repository.objects.filter( |
| 128 | + repository_id=data["repositories_added"][0]["id"] |
| 129 | + ).exists() |
| 130 | + |
| 131 | + sync_installation_repositories(event, None) |
| 132 | + |
| 133 | + assert not Repository.objects.filter( |
| 134 | + repository_id=data["repositories_removed"][0]["id"] |
| 135 | + ).exists() |
| 136 | + assert Repository.objects.filter( |
| 137 | + repository_id=data["repositories_added"][0]["id"] |
| 138 | + ).exists() |
0 commit comments