11import typing
22from unittest .mock import patch
33
4- import httpretty
54import pytest
5+ import responses
66from rest_framework import exceptions
77from 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
8787def 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
101101def 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
121121def 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
137137def 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
166166def 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
203203def 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
243243def 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
271271def 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
299299def 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