Skip to content
This repository was archived by the owner on Jun 13, 2025. It is now read-only.

Commit 005f2f8

Browse files
committed
turn shelter off in tests, and do mocking a different way
1 parent 7f75f3b commit 005f2f8

3 files changed

Lines changed: 45 additions & 82 deletions

File tree

codecov/settings_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
ALLOWED_HOSTS = ["localhost"]
66
CORS_ALLOWED_ORIGINS = ["http://localhost:9000", "http://localhost"]
7-
SHELTER_ENABLED = True
7+
SHELTER_ENABLED = False
88
SHELTER_PUBSUB_PROJECT_ID = "test-project-id"
99
SHELTER_PUBSUB_SYNC_REPO_TOPIC_ID = "test-topic-id"
1010

codecov_auth/tests/test_signals.py

Lines changed: 37 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -2,42 +2,37 @@
22
from unittest.mock import call
33

44
import pytest
5-
from django.test import TestCase
5+
from django.test import TestCase, override_settings
66
from shared.django_apps.codecov_auth.models import Service
77
from 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
1416
def 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")
3531
class 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+
)

core/tests/test_signals.py

Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
@pytest.mark.django_db
99
def test_shelter_repo_sync(mocker):
10-
publish = mocker.patch("google.cloud.pubsub_v1.PublisherClient.publish")
10+
publish = mocker.patch("utils.shelter.ShelterPubsub.publish")
1111

1212
# this triggers the publish via Django signals
1313
repo = RepositoryFactory(
@@ -17,14 +17,8 @@ def test_shelter_repo_sync(mocker):
1717
# triggers publish on create
1818
publish.assert_has_calls(
1919
[
20-
call(
21-
"projects/test-project-id/topics/test-topic-id",
22-
b'{"type": "owner", "sync": "one", "id": 555}',
23-
),
24-
call(
25-
"projects/test-project-id/topics/test-topic-id",
26-
b'{"type": "repo", "sync": "one", "id": 91728376}',
27-
),
20+
call({"type": "owner", "sync": "one", "id": 555}),
21+
call({"type": "repo", "sync": "one", "id": 91728376}),
2822
]
2923
)
3024

@@ -35,10 +29,7 @@ def test_shelter_repo_sync(mocker):
3529
assert len(publish_calls) == 3
3630

3731
# triggers publish on update
38-
assert publish_calls[2] == call(
39-
"projects/test-project-id/topics/test-topic-id",
40-
b'{"type": "repo", "sync": "one", "id": 91728376}',
41-
)
32+
assert publish_calls[2] == call({"type": "repo", "sync": "one", "id": 91728376})
4233

4334
# Does not trigger another publish with untracked field
4435
repo.message = "foo"
@@ -54,14 +45,7 @@ def test_shelter_repo_sync(mocker):
5445
publish_calls = publish.call_args_list
5546
# 1 is for the new owner created
5647
assert len(publish_calls) == 5
57-
publish.assert_has_calls(
58-
[
59-
call(
60-
"projects/test-project-id/topics/test-topic-id",
61-
b'{"type": "owner", "sync": "one", "id": 888}',
62-
),
63-
]
64-
)
48+
publish.assert_has_calls([call({"type": "owner", "sync": "one", "id": 888})])
6549

6650
# Triggers call when private is changed
6751
repo.private = True
@@ -73,7 +57,7 @@ def test_shelter_repo_sync(mocker):
7357

7458
@pytest.mark.django_db
7559
def test_shelter_commit_sync(mocker):
76-
publish = mocker.patch("google.cloud.pubsub_v1.PublisherClient.publish")
60+
publish = mocker.patch("utils.shelter.ShelterPubsub.publish")
7761

7862
# this triggers the publish via Django signals - has to have this format
7963
owner = OwnerFactory(ownerid=555)
@@ -90,10 +74,7 @@ def test_shelter_commit_sync(mocker):
9074
assert len(publish_calls) == 3
9175

9276
# triggers publish on update
93-
assert publish_calls[2] == call(
94-
"projects/test-project-id/topics/test-topic-id",
95-
b'{"type": "commit", "sync": "one", "id": 167829367}',
96-
)
77+
assert publish_calls[2] == call({"type": "commit", "sync": "one", "id": 167829367})
9778

9879
commit.branch = "normal-incompatible-branch"
9980
commit.save()

0 commit comments

Comments
 (0)