Skip to content

Commit 3666186

Browse files
authored
chore: refactoring and upgrade telegram/urllib3 deps (#5611)
1 parent 03b20bb commit 3666186

37 files changed

+408
-259
lines changed

engine/apps/alerts/tests/test_related_incident.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from unittest.mock import patch
22

3-
import httpretty
43
import pytest
4+
import responses
55

66
from apps.alerts.models import AlertGroupLogRecord, EscalationPolicy, RelatedIncident
77
from apps.alerts.tasks.declare_incident import (
@@ -72,7 +72,7 @@ def test_declare_incident_default_route(setup_alert_group_and_escalation_step):
7272

7373

7474
@pytest.mark.django_db
75-
@httpretty.activate(verbose=True, allow_net_connect=False)
75+
@responses.activate
7676
def test_declare_incident_ok(setup_alert_group_and_escalation_step):
7777
alert_group, declare_incident_step, _ = setup_alert_group_and_escalation_step(already_declared_incident=False)
7878

@@ -105,7 +105,7 @@ def test_declare_incident_ok(setup_alert_group_and_escalation_step):
105105

106106

107107
@pytest.mark.django_db
108-
@httpretty.activate(verbose=True, allow_net_connect=False)
108+
@responses.activate
109109
def test_declare_incident_set_severity(setup_alert_group_and_escalation_step):
110110
alert_group, declare_incident_step, _ = setup_alert_group_and_escalation_step(already_declared_incident=False)
111111
severity = "critical"
@@ -120,7 +120,7 @@ def test_declare_incident_set_severity(setup_alert_group_and_escalation_step):
120120

121121

122122
@pytest.mark.django_db
123-
@httpretty.activate(verbose=True, allow_net_connect=False)
123+
@responses.activate
124124
def test_declare_incident_set_severity_from_label(setup_alert_group_and_escalation_step):
125125
alert_group, declare_incident_step, _ = setup_alert_group_and_escalation_step(already_declared_incident=False)
126126
expected_severity = "minor"
@@ -143,7 +143,7 @@ def test_declare_incident_set_severity_from_label(setup_alert_group_and_escalati
143143

144144

145145
@pytest.mark.django_db
146-
@httpretty.activate(verbose=True, allow_net_connect=False)
146+
@responses.activate
147147
def test_declare_incident_invalid_severity_fallback(setup_alert_group_and_escalation_step):
148148
alert_group, declare_incident_step, _ = setup_alert_group_and_escalation_step(already_declared_incident=False)
149149
severity = "INVALID"
@@ -166,7 +166,7 @@ def test_declare_incident_invalid_severity_fallback(setup_alert_group_and_escala
166166

167167

168168
@pytest.mark.django_db
169-
@httpretty.activate(verbose=True, allow_net_connect=False)
169+
@responses.activate
170170
def test_declare_incident_attach_alert_group(setup_alert_group_and_escalation_step):
171171
alert_group, declare_incident_step, existing_open_incident = setup_alert_group_and_escalation_step(
172172
already_declared_incident=True
@@ -191,7 +191,7 @@ def test_declare_incident_attach_alert_group(setup_alert_group_and_escalation_st
191191

192192

193193
@pytest.mark.django_db
194-
@httpretty.activate(verbose=True, allow_net_connect=False)
194+
@responses.activate
195195
def test_declare_incident_resolved_update(setup_alert_group_and_escalation_step):
196196
alert_group, declare_incident_step, existing_open_incident = setup_alert_group_and_escalation_step(
197197
already_declared_incident=True
@@ -226,7 +226,7 @@ def test_declare_incident_resolved_update(setup_alert_group_and_escalation_step)
226226

227227

228228
@pytest.mark.django_db
229-
@httpretty.activate(verbose=True, allow_net_connect=False)
229+
@responses.activate
230230
def test_declare_incident_attach_alert_group_skip_incident_update(
231231
setup_alert_group_and_escalation_step, make_alert_group
232232
):
@@ -261,7 +261,7 @@ def test_declare_incident_attach_alert_group_skip_incident_update(
261261

262262

263263
@pytest.mark.django_db
264-
@httpretty.activate(verbose=True, allow_net_connect=False)
264+
@responses.activate
265265
def test_get_existing_incident_error(setup_alert_group_and_escalation_step):
266266
alert_group, declare_incident_step, existing_open_incident = setup_alert_group_and_escalation_step(
267267
already_declared_incident=True
@@ -295,7 +295,7 @@ def test_get_existing_incident_error(setup_alert_group_and_escalation_step):
295295

296296

297297
@pytest.mark.django_db
298-
@httpretty.activate(verbose=True, allow_net_connect=False)
298+
@responses.activate
299299
def test_attach_alert_group_error(setup_alert_group_and_escalation_step):
300300
alert_group, declare_incident_step, existing_open_incident = setup_alert_group_and_escalation_step(
301301
already_declared_incident=True
@@ -322,7 +322,7 @@ def test_attach_alert_group_error(setup_alert_group_and_escalation_step):
322322

323323

324324
@pytest.mark.django_db
325-
@httpretty.activate(verbose=True, allow_net_connect=False)
325+
@responses.activate
326326
def test_create_incident_error(setup_alert_group_and_escalation_step):
327327
alert_group, declare_incident_step, _ = setup_alert_group_and_escalation_step(already_declared_incident=False)
328328

engine/apps/api/tests/test_organization.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,8 @@ def test_organization_get_telegram_verification_code_permissions(
199199
client = APIClient()
200200

201201
url = reverse("api-internal:api-get-telegram-verification-code")
202-
response = client.get(url, format="json", **make_user_auth_headers(tester, token))
202+
with patch("apps.telegram.client.TelegramClient.get_bot_username", return_value="test_bot_username"):
203+
response = client.get(url, format="json", **make_user_auth_headers(tester, token))
203204

204205
assert response.status_code == expected_status
205206

engine/apps/api/tests/test_user.py

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -808,7 +808,8 @@ def test_user_get_own_telegram_verification_code_with_telegram_connected(
808808
client = APIClient()
809809
make_telegram_user_connector(tester)
810810
url = reverse("api-internal:user-get-telegram-verification-code", kwargs={"pk": tester.public_primary_key})
811-
response = client.get(url, format="json", **make_user_auth_headers(tester, token))
811+
with patch("apps.telegram.client.TelegramClient.get_bot_username", return_value="test_bot_username"):
812+
response = client.get(url, format="json", **make_user_auth_headers(tester, token))
812813
assert response.status_code == expected_status
813814

814815

@@ -867,7 +868,8 @@ def test_user_get_own_telegram_verification_code(
867868
client = APIClient()
868869
url = reverse("api-internal:user-get-telegram-verification-code", kwargs={"pk": tester.public_primary_key})
869870

870-
response = client.get(url, format="json", **make_user_auth_headers(tester, token))
871+
with patch("apps.telegram.client.TelegramClient.get_bot_username", return_value="test_bot_username"):
872+
response = client.get(url, format="json", **make_user_auth_headers(tester, token))
871873
assert response.status_code == expected_status
872874

873875

@@ -894,7 +896,8 @@ def test_user_get_another_telegram_verification_code(
894896
client = APIClient()
895897
url = reverse("api-internal:user-get-telegram-verification-code", kwargs={"pk": other_user.public_primary_key})
896898

897-
response = client.get(url, format="json", **make_user_auth_headers(tester, token))
899+
with patch("apps.telegram.client.TelegramClient.get_bot_username", return_value="test_bot_username"):
900+
response = client.get(url, format="json", **make_user_auth_headers(tester, token))
898901
assert response.status_code == expected_status
899902

900903

@@ -1042,7 +1045,8 @@ def test_admin_can_get_own_telegram_verification_code(
10421045
client = APIClient()
10431046
url = reverse("api-internal:user-get-telegram-verification-code", kwargs={"pk": user.public_primary_key})
10441047

1045-
response = client.get(url, format="json", **make_user_auth_headers(user, token))
1048+
with patch("apps.telegram.client.TelegramClient.get_bot_username", return_value="test_bot_username"):
1049+
response = client.get(url, format="json", **make_user_auth_headers(user, token))
10461050
assert response.status_code == status.HTTP_200_OK
10471051

10481052

@@ -1058,7 +1062,8 @@ def test_admin_can_get_another_user_telegram_verification_code(
10581062
client = APIClient()
10591063
url = reverse("api-internal:user-get-telegram-verification-code", kwargs={"pk": first_user.public_primary_key})
10601064

1061-
response = client.get(url, format="json", **make_user_auth_headers(second_user, token))
1065+
with patch("apps.telegram.client.TelegramClient.get_bot_username", return_value="test_bot_username"):
1066+
response = client.get(url, format="json", **make_user_auth_headers(second_user, token))
10621067
assert response.status_code == status.HTTP_200_OK
10631068

10641069

@@ -1280,7 +1285,8 @@ def test_user_can_get_own_telegram_verification_code(
12801285
client = APIClient()
12811286
url = reverse("api-internal:user-get-telegram-verification-code", kwargs={"pk": user.public_primary_key})
12821287

1283-
response = client.get(f"{url}", format="json", **make_user_auth_headers(user, token))
1288+
with patch("apps.telegram.client.TelegramClient.get_bot_username", return_value="test_bot_username"):
1289+
response = client.get(f"{url}", format="json", **make_user_auth_headers(user, token))
12841290
assert response.status_code == status.HTTP_200_OK
12851291

12861292

@@ -1298,7 +1304,8 @@ def test_user_cant_get_another_user_telegram_verification_code(
12981304
client = APIClient()
12991305
url = reverse("api-internal:user-get-telegram-verification-code", kwargs={"pk": first_user.public_primary_key})
13001306

1301-
response = client.get(f"{url}", format="json", **make_user_auth_headers(second_user, token))
1307+
with patch("apps.telegram.client.TelegramClient.get_bot_username", return_value="test_bot_username"):
1308+
response = client.get(f"{url}", format="json", **make_user_auth_headers(second_user, token))
13021309
assert response.status_code == status.HTTP_403_FORBIDDEN
13031310

13041311

@@ -1584,7 +1591,8 @@ def test_viewer_cant_get_own_telegram_verification_code(
15841591
client = APIClient()
15851592
url = reverse("api-internal:user-get-telegram-verification-code", kwargs={"pk": user.public_primary_key})
15861593

1587-
response = client.get(f"{url}", format="json", **make_user_auth_headers(user, token))
1594+
with patch("apps.telegram.client.TelegramClient.get_bot_username", return_value="test_bot_username"):
1595+
response = client.get(f"{url}", format="json", **make_user_auth_headers(user, token))
15881596
assert response.status_code == status.HTTP_403_FORBIDDEN
15891597

15901598

@@ -1600,7 +1608,8 @@ def test_viewer_cant_get_another_user_telegram_verification_code(
16001608
client = APIClient()
16011609
url = reverse("api-internal:user-get-telegram-verification-code", kwargs={"pk": first_user.public_primary_key})
16021610

1603-
response = client.get(url, format="json", **make_user_auth_headers(second_user, token))
1611+
with patch("apps.telegram.client.TelegramClient.get_bot_username", return_value="test_bot_username"):
1612+
response = client.get(url, format="json", **make_user_auth_headers(second_user, token))
16041613
assert response.status_code == status.HTTP_403_FORBIDDEN
16051614

16061615

engine/apps/api/views/organization.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ def get(self, request):
8282
existing_verification_code.delete()
8383
new_code = TelegramChannelVerificationCode.objects.create(organization=organization, author=user)
8484
telegram_client = TelegramClient()
85-
bot_username = telegram_client.api_client.username
85+
bot_username = telegram_client.get_bot_username()
8686
bot_link = f"https://t.me/{bot_username}"
8787
return Response(
8888
{"telegram_code": str(new_code.uuid_with_org_uuid), "bot_link": bot_link}, status=status.HTTP_200_OK

engine/apps/api/views/user.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -667,7 +667,7 @@ def get_telegram_verification_code(self, request, pk) -> Response:
667667
new_code.save()
668668

669669
telegram_client = TelegramClient()
670-
bot_username = telegram_client.api_client.username
670+
bot_username = telegram_client.get_bot_username()
671671
bot_link = f"https://t.me/{bot_username}"
672672

673673
return Response(
Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,12 @@
1-
import json
2-
3-
import httpretty
1+
import responses
42

53

64
def setup_service_account_api_mocks(grafana_url, perms=None, user_data=None, perms_status=200, user_status=200):
7-
# requires enabling httpretty
5+
# requires enabling responses
86
if perms is None:
97
perms = {}
10-
mock_response = httpretty.Response(status=perms_status, body=json.dumps(perms))
11-
perms_url = f"{grafana_url}/api/access-control/user/permissions"
12-
httpretty.register_uri(httpretty.GET, perms_url, responses=[mock_response])
8+
responses.add(responses.GET, f"{grafana_url}/api/access-control/user/permissions", json=perms, status=perms_status)
139

1410
if user_data is None:
1511
user_data = {"login": "some-login", "uid": "service-account:42"}
16-
mock_response = httpretty.Response(status=user_status, body=json.dumps(user_data))
17-
user_url = f"{grafana_url}/api/user"
18-
httpretty.register_uri(httpretty.GET, user_url, responses=[mock_response])
12+
responses.add(responses.GET, f"{grafana_url}/api/user", json=user_data, status=user_status)

engine/apps/auth_token/tests/test_grafana_auth.py

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import typing
22
from unittest.mock import patch
33

4-
import httpretty
54
import pytest
5+
import responses
66
from rest_framework import exceptions
77
from rest_framework.test import APIRequestFactory
88

@@ -83,7 +83,7 @@ def check_common_inputs() -> tuple[dict[str, typing.Any], str]:
8383

8484

8585
@pytest.mark.django_db
86-
@httpretty.activate(verbose=True, allow_net_connect=False)
86+
@responses.activate
8787
def test_grafana_authentication_missing_org():
8888
token = f"{ServiceAccountToken.GRAFANA_SA_PREFIX}xyz"
8989
headers = {
@@ -97,7 +97,7 @@ def test_grafana_authentication_missing_org():
9797

9898

9999
@pytest.mark.django_db
100-
@httpretty.activate(verbose=True, allow_net_connect=False)
100+
@responses.activate
101101
def test_grafana_authentication_no_org_grafana_url():
102102
grafana_url = "http://grafana.test"
103103
token = f"{ServiceAccountToken.GRAFANA_SA_PREFIX}xyz"
@@ -108,7 +108,7 @@ def test_grafana_authentication_no_org_grafana_url():
108108
request = APIRequestFactory().get("/", **headers)
109109

110110
request_sync_url = f"{grafana_url}/api/plugins/{PluginID.ONCALL}/resources/plugin/sync?wait=true&force=true"
111-
httpretty.register_uri(httpretty.POST, request_sync_url, status=404)
111+
responses.add(responses.POST, request_sync_url, status=404)
112112

113113
with pytest.raises(exceptions.Throttled) as exc:
114114
GrafanaServiceAccountAuthentication().authenticate(request)
@@ -117,7 +117,7 @@ def test_grafana_authentication_no_org_grafana_url():
117117

118118
@pytest.mark.parametrize("grafana_url", ["null;", "foo", ""])
119119
@pytest.mark.django_db
120-
@httpretty.activate(verbose=True, allow_net_connect=False)
120+
@responses.activate
121121
def test_grafana_authentication_invalid_grafana_url(grafana_url):
122122
token = f"{ServiceAccountToken.GRAFANA_SA_PREFIX}xyz"
123123
headers = {
@@ -133,7 +133,7 @@ def test_grafana_authentication_invalid_grafana_url(grafana_url):
133133

134134

135135
@pytest.mark.django_db
136-
@httpretty.activate(verbose=True, allow_net_connect=False)
136+
@responses.activate
137137
def test_grafana_authentication_permissions_call_fails(make_organization):
138138
organization = make_organization(grafana_url="http://grafana.test")
139139

@@ -152,7 +152,7 @@ def test_grafana_authentication_permissions_call_fails(make_organization):
152152
GrafanaServiceAccountAuthentication().authenticate(request)
153153
assert exc.value.detail == "Invalid token."
154154

155-
last_request = httpretty.last_request()
155+
last_request = responses.calls[-1].request
156156
assert last_request.method == "GET"
157157
expected_url = f"{organization.grafana_url}/api/access-control/user/permissions"
158158
assert last_request.url == expected_url
@@ -162,7 +162,7 @@ def test_grafana_authentication_permissions_call_fails(make_organization):
162162

163163
@pytest.mark.django_db
164164
@pytest.mark.parametrize("grafana_url", ["http://grafana.test", "http://grafana.test/"])
165-
@httpretty.activate(verbose=True, allow_net_connect=False)
165+
@responses.activate
166166
def test_grafana_authentication_existing_token(
167167
make_organization, make_service_account_for_organization, make_token_for_service_account, grafana_url
168168
):
@@ -190,7 +190,7 @@ def test_grafana_authentication_existing_token(
190190
assert user.role == LegacyAccessControlRole.NONE
191191
assert auth_token == token
192192

193-
last_request = httpretty.last_request()
193+
last_request = responses.calls[-1].request
194194
assert last_request.method == "GET"
195195
expected_url = f"{organization.grafana_url}/api/access-control/user/permissions"
196196
assert last_request.url == expected_url
@@ -199,7 +199,7 @@ def test_grafana_authentication_existing_token(
199199

200200

201201
@pytest.mark.django_db
202-
@httpretty.activate(verbose=True, allow_net_connect=False)
202+
@responses.activate
203203
def test_grafana_authentication_token_created(make_organization):
204204
organization = make_organization(grafana_url="http://grafana.test")
205205
token_string = "glsa_the-token"
@@ -228,18 +228,18 @@ def test_grafana_authentication_token_created(make_organization):
228228
assert user.permissions == [{"action": p} for p in permissions]
229229
assert auth_token.service_account == user.service_account
230230

231-
perms_request, user_request = httpretty.latest_requests()
231+
perms_request, user_request = responses.calls
232232
for req in (perms_request, user_request):
233-
assert req.method == "GET"
234-
assert req.headers["Authorization"] == f"Bearer {token_string}"
233+
assert req.request.method == "GET"
234+
assert req.request.headers["Authorization"] == f"Bearer {token_string}"
235235
perms_url = f"{organization.grafana_url}/api/access-control/user/permissions"
236-
assert perms_request.url == perms_url
236+
assert perms_request.request.url == perms_url
237237
user_url = f"{organization.grafana_url}/api/user"
238-
assert user_request.url == user_url
238+
assert user_request.request.url == user_url
239239

240240

241241
@pytest.mark.django_db
242-
@httpretty.activate(verbose=True, allow_net_connect=False)
242+
@responses.activate
243243
def test_grafana_authentication_token_created_older_grafana(make_organization):
244244
organization = make_organization(grafana_url="http://grafana.test")
245245
token_string = "glsa_the-token"
@@ -267,7 +267,7 @@ def test_grafana_authentication_token_created_older_grafana(make_organization):
267267

268268

269269
@pytest.mark.django_db
270-
@httpretty.activate(verbose=True, allow_net_connect=False)
270+
@responses.activate
271271
def test_grafana_authentication_token_reuse_service_account(make_organization, make_service_account_for_organization):
272272
organization = make_organization(grafana_url="http://grafana.test")
273273
service_account = make_service_account_for_organization(organization)
@@ -295,7 +295,7 @@ def test_grafana_authentication_token_reuse_service_account(make_organization, m
295295

296296

297297
@pytest.mark.django_db
298-
@httpretty.activate(verbose=True, allow_net_connect=False)
298+
@responses.activate
299299
def test_grafana_authentication_token_setup_org_if_missing(make_organization):
300300
grafana_url = "http://grafana.test"
301301
token_string = "glsa_the-token"
@@ -311,7 +311,7 @@ def test_grafana_authentication_token_setup_org_if_missing(make_organization):
311311
setup_service_account_api_mocks(grafana_url, permissions)
312312

313313
request_sync_url = f"{grafana_url}/api/plugins/{PluginID.ONCALL}/resources/plugin/sync?wait=true&force=true"
314-
httpretty.register_uri(httpretty.POST, request_sync_url)
314+
responses.add(responses.POST, request_sync_url)
315315

316316
assert Organization.objects.filter(grafana_url=grafana_url).count() == 0
317317

0 commit comments

Comments
 (0)