22from unittest .mock import call
33
44import pytest
5- from django .test import TestCase
5+ from django .test import TestCase , override_settings
66from shared .django_apps .codecov_auth .models import Service
77from shared .django_apps .codecov_auth .tests .factories import (
88 OrganizationLevelTokenFactory ,
99 OwnerFactory ,
1010)
1111
12+ from utils .shelter import ShelterPubsub
13+
1214
1315@pytest .mark .django_db
1416def test_shelter_org_token_sync (mocker ):
15- publish = mocker .patch ("google.cloud.pubsub_v1.PublisherClient .publish" )
17+ publish = mocker .patch ("utils.shelter.ShelterPubsub .publish" )
1618
1719 # this triggers the publish via Django signals
1820 OrganizationLevelTokenFactory (id = 91728376 , owner = OwnerFactory (ownerid = 111 ))
1921
2022 publish .assert_has_calls (
2123 [
22- call (
23- "projects/test-project-id/topics/test-topic-id" ,
24- b'{"type": "owner", "sync": "one", "id": 111}' ,
25- ),
26- call (
27- "projects/test-project-id/topics/test-topic-id" ,
28- b'{"type": "org_token", "sync": "one", "id": 91728376}' ,
29- ),
24+ call ({"type" : "owner" , "sync" : "one" , "id" : 111 }),
25+ call ({"type" : "org_token" , "sync" : "one" , "id" : 91728376 }),
3026 ]
3127 )
3228
3329
34- @mock .patch ("google.cloud.pubsub_v1.PublisherClient .publish" )
30+ @mock .patch ("utils.shelter.ShelterPubsub .publish" )
3531class TestCodecovAuthSignals (TestCase ):
3632 def test_sync_on_create (self , mock_publish ):
3733 OwnerFactory (ownerid = 12345 )
3834 mock_publish .assert_called_once_with (
39- "projects/test-project-id/topics/test-topic-id" ,
40- b'{"type": "owner", "sync": "one", "id": 12345}' ,
35+ {"type" : "owner" , "sync" : "one" , "id" : 12345 }
4136 )
4237
4338 def test_sync_on_update_upload_token_required_for_public_repos (self , mock_publish ):
@@ -46,14 +41,8 @@ def test_sync_on_update_upload_token_required_for_public_repos(self, mock_publis
4641 owner .save ()
4742 mock_publish .assert_has_calls (
4843 [
49- call (
50- "projects/test-project-id/topics/test-topic-id" ,
51- b'{"type": "owner", "sync": "one", "id": 12345}' ,
52- ),
53- call (
54- "projects/test-project-id/topics/test-topic-id" ,
55- b'{"type": "owner", "sync": "one", "id": 12345}' ,
56- ),
44+ call ({"type" : "owner" , "sync" : "one" , "id" : 12345 }),
45+ call ({"type" : "owner" , "sync" : "one" , "id" : 12345 }),
5746 ]
5847 )
5948
@@ -63,14 +52,8 @@ def test_sync_on_update_username(self, mock_publish):
6352 owner .save ()
6453 mock_publish .assert_has_calls (
6554 [
66- call (
67- "projects/test-project-id/topics/test-topic-id" ,
68- b'{"type": "owner", "sync": "one", "id": 12345}' ,
69- ),
70- call (
71- "projects/test-project-id/topics/test-topic-id" ,
72- b'{"type": "owner", "sync": "one", "id": 12345}' ,
73- ),
55+ call ({"type" : "owner" , "sync" : "one" , "id" : 12345 }),
56+ call ({"type" : "owner" , "sync" : "one" , "id" : 12345 }),
7457 ]
7558 )
7659
@@ -80,14 +63,8 @@ def test_sync_on_update_service(self, mock_publish):
8063 owner .save ()
8164 mock_publish .assert_has_calls (
8265 [
83- call (
84- "projects/test-project-id/topics/test-topic-id" ,
85- b'{"type": "owner", "sync": "one", "id": 12345}' ,
86- ),
87- call (
88- "projects/test-project-id/topics/test-topic-id" ,
89- b'{"type": "owner", "sync": "one", "id": 12345}' ,
90- ),
66+ call ({"type" : "owner" , "sync" : "one" , "id" : 12345 }),
67+ call ({"type" : "owner" , "sync" : "one" , "id" : 12345 }),
9168 ]
9269 )
9370
@@ -96,26 +73,31 @@ def test_no_sync_on_update_other_fields(self, mock_publish):
9673 owner .name = "world"
9774 owner .save ()
9875 mock_publish .assert_called_once_with (
99- "projects/test-project-id/topics/test-topic-id" ,
100- b'{"type": "owner", "sync": "one", "id": 12345}' ,
76+ {"type" : "owner" , "sync" : "one" , "id" : 12345 }
10177 )
10278
103- @mock .patch ("logging.Logger.warning" )
104- def test_sync_error (self , mock_log , mock_publish ):
105- mock_publish .side_effect = Exception ("publish error" )
10679
107- OwnerFactory (ownerid = 12345 )
80+ @mock .patch ("logging.Logger.warning" )
81+ @mock .patch ("google.cloud.pubsub_v1.PublisherClient.publish" )
82+ @mock .patch ("utils.shelter.ShelterPubsub.get_instance" )
83+ @override_settings (SHELTER_ENABLED = True )
84+ @pytest .mark .django_db
85+ def test_sync_error (mock_instance , mock_publish , mock_log ):
86+ mock_instance .return_value = ShelterPubsub ()
87+ mock_publish .side_effect = Exception ("publish error" )
10888
109- # publish is still called, raises an Exception
110- mock_publish .assert_called_once_with (
111- "projects/test-project-id/topics/test-topic-id" ,
112- b'{"type": "owner", "sync": "one", "id": 12345}' ,
113- )
89+ OwnerFactory (ownerid = 12345 )
11490
115- mock_log .assert_called_once_with (
116- "Failed to publish a message" ,
117- extra = dict (
118- data_to_publish = {"type" : "owner" , "sync" : "one" , "id" : 12345 },
119- error = mock_publish .side_effect ,
120- ),
121- )
91+ # publish is still called, raises an Exception
92+ mock_publish .assert_called_once_with (
93+ "projects/test-project-id/topics/test-topic-id" ,
94+ b'{"type": "owner", "sync": "one", "id": 12345}' ,
95+ )
96+
97+ mock_log .assert_called_once_with (
98+ "Failed to publish a message" ,
99+ extra = dict (
100+ data_to_publish = {"type" : "owner" , "sync" : "one" , "id" : 12345 },
101+ error = mock_publish .side_effect ,
102+ ),
103+ )
0 commit comments