11import os
2+ from unittest import mock
3+ from unittest .mock import call
24
35import pytest
4- from django .test import override_settings
6+ from django .test import TestCase , override_settings
7+ from shared .django_apps .codecov_auth .models import Service
58from shared .django_apps .codecov_auth .tests .factories import (
69 OrganizationLevelTokenFactory ,
10+ OwnerFactory ,
711)
812
913
@@ -19,9 +23,111 @@ def test_shelter_org_token_sync(mocker):
1923 publish = mocker .patch ("google.cloud.pubsub_v1.PublisherClient.publish" )
2024
2125 # this triggers the publish via Django signals
22- OrganizationLevelTokenFactory (id = 91728376 )
26+ OrganizationLevelTokenFactory (id = 91728376 , owner = OwnerFactory ( ownerid = 111 ) )
2327
24- publish .assert_called_once_with (
25- "projects/test-project-id/topics/test-topic-id" ,
26- b'{"type": "org_token", "sync": "one", "id": 91728376}' ,
28+ publish .assert_has_calls (
29+ [
30+ call (
31+ "projects/test-project-id/topics/test-topic-id" ,
32+ b'{"type": "owner", "sync": "one", "id": 111}' ,
33+ ),
34+ call (
35+ "projects/test-project-id/topics/test-topic-id" ,
36+ b'{"type": "org_token", "sync": "one", "id": 91728376}' ,
37+ ),
38+ ]
2739 )
40+
41+
42+ @override_settings (
43+ SHELTER_PUBSUB_PROJECT_ID = "test-project-id" ,
44+ SHELTER_PUBSUB_SYNC_REPO_TOPIC_ID = "test-topic-id" ,
45+ )
46+ @mock .patch ("google.cloud.pubsub_v1.PublisherClient.publish" )
47+ class TestCodecovAuthSignals (TestCase ):
48+ def setUp (self ):
49+ os .environ ["PUBSUB_EMULATOR_HOST" ] = "localhost"
50+
51+ def test_sync_on_create (self , mock_publish ):
52+ OwnerFactory (ownerid = 12345 )
53+ mock_publish .assert_called_once_with (
54+ "projects/test-project-id/topics/test-topic-id" ,
55+ b'{"type": "owner", "sync": "one", "id": 12345}' ,
56+ )
57+
58+ def test_sync_on_update_upload_token_required_for_public_repos (self , mock_publish ):
59+ owner = OwnerFactory (ownerid = 12345 , upload_token_required_for_public_repos = True )
60+ owner .upload_token_required_for_public_repos = False
61+ owner .save ()
62+ mock_publish .assert_has_calls (
63+ [
64+ call (
65+ "projects/test-project-id/topics/test-topic-id" ,
66+ b'{"type": "owner", "sync": "one", "id": 12345}' ,
67+ ),
68+ call (
69+ "projects/test-project-id/topics/test-topic-id" ,
70+ b'{"type": "owner", "sync": "one", "id": 12345}' ,
71+ ),
72+ ]
73+ )
74+
75+ def test_sync_on_update_username (self , mock_publish ):
76+ owner = OwnerFactory (ownerid = 12345 , username = "hello" )
77+ owner .username = "world"
78+ owner .save ()
79+ mock_publish .assert_has_calls (
80+ [
81+ call (
82+ "projects/test-project-id/topics/test-topic-id" ,
83+ b'{"type": "owner", "sync": "one", "id": 12345}' ,
84+ ),
85+ call (
86+ "projects/test-project-id/topics/test-topic-id" ,
87+ b'{"type": "owner", "sync": "one", "id": 12345}' ,
88+ ),
89+ ]
90+ )
91+
92+ def test_sync_on_update_service (self , mock_publish ):
93+ owner = OwnerFactory (ownerid = 12345 , service = Service .GITHUB .value )
94+ owner .service = Service .BITBUCKET .value
95+ owner .save ()
96+ mock_publish .assert_has_calls (
97+ [
98+ call (
99+ "projects/test-project-id/topics/test-topic-id" ,
100+ b'{"type": "owner", "sync": "one", "id": 12345}' ,
101+ ),
102+ call (
103+ "projects/test-project-id/topics/test-topic-id" ,
104+ b'{"type": "owner", "sync": "one", "id": 12345}' ,
105+ ),
106+ ]
107+ )
108+
109+ def test_no_sync_on_update_other_fields (self , mock_publish ):
110+ owner = OwnerFactory (ownerid = 12345 , name = "hello" )
111+ owner .name = "world"
112+ owner .save ()
113+ mock_publish .assert_called_once_with (
114+ "projects/test-project-id/topics/test-topic-id" ,
115+ b'{"type": "owner", "sync": "one", "id": 12345}' ,
116+ )
117+
118+ @mock .patch ("logging.Logger.warning" )
119+ def test_sync_error (self , mock_log , mock_publish ):
120+ mock_publish .side_effect = Exception ("publish error" )
121+
122+ OwnerFactory (ownerid = 12345 )
123+
124+ # publish is still called, raises an Exception
125+ mock_publish .assert_called_once_with (
126+ "projects/test-project-id/topics/test-topic-id" ,
127+ b'{"type": "owner", "sync": "one", "id": 12345}' ,
128+ )
129+
130+ mock_log .assert_called_once_with (
131+ "Failed to publish message for owner" ,
132+ extra = dict (owner_id = 12345 , error = mock_publish .side_effect ),
133+ )
0 commit comments