1+ from unittest .mock import call , patch
2+
13from django .urls import reverse
24
35from sentry .flags .models import (
1113from sentry .utils import json
1214
1315
16+ @patch ("sentry.utils.metrics.incr" )
1417class OrganizationFlagsHooksEndpointTestCase (APITestCase ):
1518 endpoint = "sentry-api-0-organization-flag-hooks"
1619
@@ -22,7 +25,7 @@ def setUp(self):
2225 def features (self ):
2326 return {"organizations:feature-flag-audit-log" : True }
2427
25- def test_generic_post_create (self ):
28+ def test_generic_post_create (self , mock_incr ):
2629 request_data = {
2730 "data" : [
2831 {
@@ -47,9 +50,12 @@ def test_generic_post_create(self):
4750 headers = {"X-Sentry-Signature" : signature },
4851 )
4952 assert response .status_code == 200 , response .content
53+ mock_incr .assert_any_call (
54+ "feature_flags.audit_log_event_posted" , tags = {"provider" : "generic" }
55+ )
5056 assert FlagAuditLogModel .objects .count () == 1
5157
52- def test_unleash_post_create (self ):
58+ def test_unleash_post_create (self , mock_incr ):
5359 request_data = {
5460 "id" : 28 ,
5561 "tags" : [{"type" : "simple" , "value" : "testvalue" }],
@@ -75,9 +81,12 @@ def test_unleash_post_create(self):
7581 headers = {"Authorization" : signature },
7682 )
7783 assert response .status_code == 200 , response .content
84+ mock_incr .assert_any_call (
85+ "feature_flags.audit_log_event_posted" , tags = {"provider" : "unleash" }
86+ )
7887 assert FlagAuditLogModel .objects .count () == 1
7988
80- def test_launchdarkly_post_create (self ):
89+ def test_launchdarkly_post_create (self , mock_incr ):
8190 request_data = LD_REQUEST
8291 signature = hmac_sha256_hex_digest (key = "456" , message = json .dumps (request_data ).encode ())
8392
@@ -95,6 +104,9 @@ def test_launchdarkly_post_create(self):
95104 )
96105
97106 assert response .status_code == 200
107+ mock_incr .assert_any_call (
108+ "feature_flags.audit_log_event_posted" , tags = {"provider" : "launchdarkly" }
109+ )
98110 assert FlagAuditLogModel .objects .count () == 1
99111 flag = FlagAuditLogModel .objects .first ()
100112 assert flag is not None
@@ -106,13 +118,14 @@ def test_launchdarkly_post_create(self):
106118 assert flag .tags is not None
107119 assert flag .tags ["description" ] == "flag was created"
108120
109- def test_launchdarkly_post_create_invalid_signature (self ):
121+ def test_launchdarkly_post_create_invalid_signature (self , mock_incr ):
110122 with self .feature (self .features ):
111123 sig = hmac_sha256_hex_digest (key = "123" , message = b"456" )
112124 response = self .client .post (self .url , LD_REQUEST , headers = {"X-LD-Signature" : sig })
113125 assert response .status_code == 401
126+ assert call ("feature_flags.audit_log_event_posted" ) not in mock_incr .call_args_list
114127
115- def test_post_launchdarkly_deserialization_failed (self ):
128+ def test_post_launchdarkly_deserialization_failed (self , mock_incr ):
116129 signature = hmac_sha256_hex_digest (key = "123" , message = json .dumps ({}).encode ())
117130 FlagWebHookSigningSecretModel .objects .create (
118131 organization = self .organization , provider = "launchdarkly" , secret = "123"
@@ -122,22 +135,26 @@ def test_post_launchdarkly_deserialization_failed(self):
122135 response = self .client .post (self .url , {}, headers = {"X-LD-Signature" : signature })
123136 assert response .status_code == 200
124137 assert FlagAuditLogModel .objects .count () == 0
138+ assert call ("feature_flags.audit_log_event_posted" ) not in mock_incr .call_args_list
125139
126- def test_post_invalid_provider (self ):
140+ def test_post_invalid_provider (self , mock_incr ):
127141 url = reverse (self .endpoint , args = (self .organization .slug , "test" ))
128142 with self .feature (self .features ):
129143 response = self .client .post (url , {})
130144 assert response .status_code == 404
145+ assert call ("feature_flags.audit_log_event_posted" ) not in mock_incr .call_args_list
131146
132- def test_post_disabled (self ):
147+ def test_post_disabled (self , mock_incr ):
133148 response = self .client .post (self .url , data = {})
134149 assert response .status_code == 404
135150 assert response .content == b'"Not enabled."'
151+ assert call ("feature_flags.audit_log_event_posted" ) not in mock_incr .call_args_list
136152
137- def test_post_missing_signature (self ):
153+ def test_post_missing_signature (self , mock_incr ):
138154 with self .feature (self .features ):
139155 response = self .client .post (self .url , {})
140156 assert response .status_code == 401 , response .content
157+ assert call ("feature_flags.audit_log_event_posted" ) not in mock_incr .call_args_list
141158
142159
143160LD_REQUEST = {
0 commit comments