Skip to content

Commit 85f46e7

Browse files
add tests for refresh and adjust fail floor for coverage
1 parent 6e122f6 commit 85f46e7

File tree

2 files changed

+56
-2
lines changed

2 files changed

+56
-2
lines changed

noxfile.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,8 @@ def coverage(session):
115115
for arg in session.posargs:
116116
args.extend(arg.split(" "))
117117
command.extend(args)
118+
if "--integration" not in command:
119+
command.append("--cov-fail-under=99")
118120
session.run(*command)
119121
finally:
120122
# 0 -> OK

tests/test_models.py

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
from __future__ import annotations
22

33
import datetime
4+
from unittest.mock import MagicMock
45

56
import pytest
67
from asgiref.sync import sync_to_async
8+
from cryptography.hazmat.backends import default_backend
9+
from cryptography.hazmat.primitives import serialization
10+
from cryptography.hazmat.primitives.asymmetric import rsa
711
from django.utils import timezone
812
from gidgethub import sansio
913
from model_bakery import baker
@@ -27,6 +31,21 @@ def _create_event(data, event):
2731
return _create_event
2832

2933

34+
@pytest.fixture
35+
def private_key():
36+
private_key = rsa.generate_private_key(
37+
public_exponent=65537, key_size=2048, backend=default_backend()
38+
)
39+
40+
pem = private_key.private_bytes(
41+
encoding=serialization.Encoding.PEM,
42+
format=serialization.PrivateFormat.PKCS8,
43+
encryption_algorithm=serialization.NoEncryption(),
44+
)
45+
46+
return pem.decode("utf-8")
47+
48+
3049
class TestEventLogManager:
3150
@pytest.mark.asyncio
3251
async def test_acreate_from_event(self, create_event):
@@ -244,9 +263,42 @@ def test_get_gh_client(self, installation):
244263
assert isinstance(client, AsyncGitHubAPI)
245264
assert client.installation_id == installation.installation_id
246265

266+
@pytest.mark.parametrize("account_type", ["org", "user"])
247267
@pytest.mark.asyncio
248-
async def test_arefresh_from_gh(self): ...
249-
def test_refresh_from_gh(self): ...
268+
async def test_arefresh_from_gh(
269+
self,
270+
account_type,
271+
private_key,
272+
ainstallation,
273+
get_mock_github_api,
274+
override_app_settings,
275+
):
276+
installation = await ainstallation
277+
278+
mock_github_api = get_mock_github_api({"foo": "bar"})
279+
installation.get_gh_client = MagicMock(return_value=mock_github_api)
280+
281+
with override_app_settings(PRIVATE_KEY=private_key):
282+
await installation.arefresh_from_gh(account_type, "test")
283+
284+
assert installation.data == {"foo": "bar"}
285+
286+
@pytest.mark.parametrize("account_type", ["org", "user"])
287+
def test_refresh_from_gh(
288+
self,
289+
account_type,
290+
private_key,
291+
installation,
292+
get_mock_github_api,
293+
override_app_settings,
294+
):
295+
mock_github_api = get_mock_github_api({"foo": "bar"})
296+
installation.get_gh_client = MagicMock(return_value=mock_github_api)
297+
298+
with override_app_settings(PRIVATE_KEY=private_key):
299+
installation.refresh_from_gh(account_type, "test")
300+
301+
assert installation.data == {"foo": "bar"}
250302

251303
def test_refresh_from_gh_invalid_account_type(self, installation):
252304
with pytest.raises(ValueError):

0 commit comments

Comments
 (0)