11from __future__ import annotations
22
33import datetime
4+ from unittest .mock import MagicMock
45
56import pytest
67from 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
711from django .utils import timezone
812from gidgethub import sansio
913from 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+
3049class 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