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

Commit 88ca9ac

Browse files
committed
reorganization, cleanup and use settings_test
1 parent c00b865 commit 88ca9ac

File tree

7 files changed

+56
-93
lines changed

7 files changed

+56
-93
lines changed

codecov/settings_test.py

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,12 @@
1-
from .settings_base import *
1+
import os
2+
3+
from .settings_dev import *
24

3-
DEBUG = False
45
ALLOWED_HOSTS = ["localhost"]
5-
WEBHOOK_URL = "" # NGROK TUNNEL HERE
6-
STRIPE_API_KEY = ""
76
CORS_ALLOWED_ORIGINS = ["http://localhost:9000", "http://localhost"]
8-
CORS_ALLOW_CREDENTIALS = True
9-
CODECOV_URL = "localhost"
10-
CODECOV_API_URL = get_config("setup", "codecov_api_url", default=CODECOV_URL)
11-
DATABASE_HOST = "postgres"
7+
SHELTER_PUBSUB_PROJECT_ID = "test-project-id"
8+
SHELTER_PUBSUB_SYNC_REPO_TOPIC_ID = "test-topic-id"
129

13-
DATABASES = {
14-
"default": {
15-
"ENGINE": "django.db.backends.postgresql",
16-
"NAME": DATABASE_NAME,
17-
"USER": DATABASE_USER,
18-
"PASSWORD": DATABASE_PASSWORD,
19-
"HOST": DATABASE_HOST,
20-
"PORT": "5432",
21-
}
22-
}
10+
# Mock the Pub/Sub host for testing
11+
# this prevents the pubsub SDK from trying to load credentials
12+
os.environ["PUBSUB_EMULATOR_HOST"] = "localhost"

codecov_auth/signals.py

Lines changed: 1 addition & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
1-
import json
2-
import logging
31
from typing import Any, Dict, Optional, Type
42

5-
from django.conf import settings
63
from django.db.models.signals import post_save
74
from django.dispatch import receiver
8-
from google.cloud import pubsub_v1
95

106
from codecov_auth.models import OrganizationLevelToken, Owner, OwnerProfile
11-
12-
log = logging.getLogger(__name__)
7+
from utils.shelter import ShelterPubsub
138

149

1510
@receiver(post_save, sender=Owner)
@@ -20,42 +15,6 @@ def create_owner_profile_when_owner_is_created(
2015
return OwnerProfile.objects.create(owner_id=instance.ownerid)
2116

2217

23-
class ShelterPubsub:
24-
pubsub_publisher = None
25-
_instance = None
26-
27-
@classmethod
28-
def get_instance(cls) -> "ShelterPubsub":
29-
"""
30-
This class needs the Django settings to be fully loaded before it can be instantiated,
31-
therefore use this method to get an instance rather than instantiating directly.
32-
"""
33-
if cls._instance is None:
34-
cls._instance = cls()
35-
return cls._instance
36-
37-
def __init__(self) -> None:
38-
if not self.pubsub_publisher:
39-
self.pubsub_publisher = pubsub_v1.PublisherClient()
40-
pubsub_project_id: str = settings.SHELTER_PUBSUB_PROJECT_ID
41-
42-
# topic_id has REPO in the name but it is used for all types of objects
43-
topic_id: str = settings.SHELTER_PUBSUB_SYNC_REPO_TOPIC_ID
44-
self.topic_path = self.pubsub_publisher.topic_path(pubsub_project_id, topic_id)
45-
46-
def publish(self, data: Dict[str, Any]) -> None:
47-
try:
48-
self.pubsub_publisher.publish(
49-
self.topic_path,
50-
json.dumps(data).encode("utf-8"),
51-
)
52-
except Exception as e:
53-
log.warning(
54-
"Failed to publish a message",
55-
extra=dict(data_to_publish=data, error=e),
56-
)
57-
58-
5918
@receiver(
6019
post_save, sender=OrganizationLevelToken, dispatch_uid="shelter_sync_org_token"
6120
)

codecov_auth/tests/test_signals.py

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,17 @@
1-
import os
21
from unittest import mock
32
from unittest.mock import call
43

54
import pytest
6-
from django.test import TestCase, override_settings
5+
from django.test import TestCase
76
from shared.django_apps.codecov_auth.models import Service
87
from shared.django_apps.codecov_auth.tests.factories import (
98
OrganizationLevelTokenFactory,
109
OwnerFactory,
1110
)
1211

1312

14-
@override_settings(
15-
SHELTER_PUBSUB_PROJECT_ID="test-project-id",
16-
SHELTER_PUBSUB_SYNC_REPO_TOPIC_ID="test-topic-id",
17-
)
1813
@pytest.mark.django_db
1914
def test_shelter_org_token_sync(mocker):
20-
# this prevents the pubsub SDK from trying to load credentials
21-
os.environ["PUBSUB_EMULATOR_HOST"] = "localhost"
22-
2315
publish = mocker.patch("google.cloud.pubsub_v1.PublisherClient.publish")
2416

2517
# this triggers the publish via Django signals
@@ -39,15 +31,8 @@ def test_shelter_org_token_sync(mocker):
3931
)
4032

4133

42-
@override_settings(
43-
SHELTER_PUBSUB_PROJECT_ID="test-project-id",
44-
SHELTER_PUBSUB_SYNC_REPO_TOPIC_ID="test-topic-id",
45-
)
4634
@mock.patch("google.cloud.pubsub_v1.PublisherClient.publish")
4735
class TestCodecovAuthSignals(TestCase):
48-
def setUp(self):
49-
os.environ["PUBSUB_EMULATOR_HOST"] = "localhost"
50-
5136
def test_sync_on_create(self, mock_publish):
5237
OwnerFactory(ownerid=12345)
5338
mock_publish.assert_called_once_with(

core/signals.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
from django.dispatch import receiver
66
from shared.django_apps.core.models import Commit
77

8-
from codecov_auth.signals import ShelterPubsub
98
from core.models import Repository
9+
from utils.shelter import ShelterPubsub
1010

1111
log = logging.getLogger(__name__)
1212

core/tests/test_signals.py

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,12 @@
1-
import os
21
from unittest.mock import call
32

43
import pytest
5-
from django.test import override_settings
64
from shared.django_apps.codecov_auth.tests.factories import OwnerFactory
75
from shared.django_apps.core.tests.factories import CommitFactory, RepositoryFactory
86

97

10-
@override_settings(
11-
SHELTER_PUBSUB_PROJECT_ID="test-project-id",
12-
SHELTER_PUBSUB_SYNC_REPO_TOPIC_ID="test-topic-id",
13-
)
148
@pytest.mark.django_db
159
def test_shelter_repo_sync(mocker):
16-
# this prevents the pubsub SDK from trying to load credentials
17-
os.environ["PUBSUB_EMULATOR_HOST"] = "localhost"
18-
1910
publish = mocker.patch("google.cloud.pubsub_v1.PublisherClient.publish")
2011

2112
# this triggers the publish via Django signals
@@ -55,14 +46,8 @@ def test_shelter_repo_sync(mocker):
5546
assert len(publish_calls) == 3
5647

5748

58-
@override_settings(
59-
SHELTER_PUBSUB_PROJECT_ID="test-project-id",
60-
SHELTER_PUBSUB_SYNC_REPO_TOPIC_ID="test-topic-id",
61-
)
6249
@pytest.mark.django_db
6350
def test_shelter_commit_sync(mocker):
64-
# this prevents the pubsub SDK from trying to load credentials
65-
os.environ["PUBSUB_EMULATOR_HOST"] = "localhost"
6651
publish = mocker.patch("google.cloud.pubsub_v1.PublisherClient.publish")
6752

6853
# this triggers the publish via Django signals - has to have this format

pytest.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
[pytest]
2-
DJANGO_SETTINGS_MODULE = codecov.settings_dev
2+
DJANGO_SETTINGS_MODULE = codecov.settings_test
33
addopts = -p no:warnings --ignore=shared --ignore-glob=**/test_results*

utils/shelter.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import json
2+
import logging
3+
from typing import Any, Dict
4+
5+
from django.conf import settings
6+
from google.cloud import pubsub_v1
7+
8+
log = logging.getLogger(__name__)
9+
10+
11+
class ShelterPubsub:
12+
pubsub_publisher = None
13+
_instance = None
14+
15+
@classmethod
16+
def get_instance(cls) -> "ShelterPubsub":
17+
"""
18+
This class needs the Django settings to be fully loaded before it can be instantiated,
19+
therefore use this method to get an instance rather than instantiating directly.
20+
"""
21+
if cls._instance is None:
22+
cls._instance = cls()
23+
return cls._instance
24+
25+
def __init__(self) -> None:
26+
if not self.pubsub_publisher:
27+
self.pubsub_publisher = pubsub_v1.PublisherClient()
28+
pubsub_project_id: str = settings.SHELTER_PUBSUB_PROJECT_ID
29+
30+
# topic_id has REPO in the name but it is used for all types of objects
31+
topic_id: str = settings.SHELTER_PUBSUB_SYNC_REPO_TOPIC_ID
32+
self.topic_path = self.pubsub_publisher.topic_path(pubsub_project_id, topic_id)
33+
34+
def publish(self, data: Dict[str, Any]) -> None:
35+
try:
36+
self.pubsub_publisher.publish(
37+
self.topic_path,
38+
json.dumps(data).encode("utf-8"),
39+
)
40+
except Exception as e:
41+
log.warning(
42+
"Failed to publish a message",
43+
extra=dict(data_to_publish=data, error=e),
44+
)

0 commit comments

Comments
 (0)