@@ -43,17 +43,19 @@ def test_requires_api_key(self):
4343
4444 @parameterized .expand (
4545 [
46- ("valid_key" , " \n phc_validkey\t " , "phc_validkey" , False ),
47- ("whitespace_only" , " \n \t " , "" , True ),
46+ ("valid_key" , " \n phc_validkey\t " , "phc_validkey" , False , False ),
47+ ("whitespace_only" , " \n \t " , "" , True , True ),
48+ ("empty_string" , "" , "" , True , True ),
4849 ]
4950 )
5051 def test_trims_api_key_whitespace (
51- self , _ , raw_api_key , expected_api_key , expect_error_log
52+ self , _ , raw_api_key , expected_api_key , expected_disabled , expect_error_log
5253 ):
5354 with mock .patch .object (Client .log , "error" ) as mock_error :
5455 client = Client (raw_api_key , send = False )
5556
5657 self .assertEqual (client .api_key , expected_api_key )
58+ self .assertEqual (client .disabled , expected_disabled )
5759 if expect_error_log :
5860 mock_error .assert_called_once_with (
5961 "api_key is empty after trimming whitespace; check your project API key"
@@ -73,6 +75,37 @@ def test_trims_host_and_personal_api_key_whitespace(self):
7375 self .assertEqual (client .host , "https://eu.i.posthog.com" )
7476 self .assertIsNone (client .personal_api_key )
7577
78+ def test_client_with_empty_api_key_is_noop (self ):
79+ client = Client ("" , send = False )
80+
81+ self .assertIsNone (client .capture ("event" , distinct_id = "distinct_id" ))
82+
83+ @mock .patch ("posthog.client.get" )
84+ def test_disabled_client_does_not_load_feature_flags (self , patch_get ):
85+ client = Client ("" , personal_api_key = "test" , send = False )
86+
87+ client .load_feature_flags ()
88+
89+ patch_get .assert_not_called ()
90+ self .assertEqual (client .feature_flags , [])
91+ self .assertIsNone (client .poller )
92+
93+ @mock .patch ("posthog.client.flags" )
94+ def test_disabled_client_does_not_get_flags_decision (self , patch_flags ):
95+ client = Client ("" , send = False )
96+
97+ self .assertEqual (client .get_flags_decision ("distinct_id" )["flags" ], {})
98+ self .assertEqual (client .get_feature_variants ("distinct_id" ), {})
99+ self .assertEqual (client .get_feature_payloads ("distinct_id" ), {})
100+ self .assertEqual (
101+ client .get_feature_flags_and_payloads ("distinct_id" ),
102+ {"featureFlags" : {}, "featureFlagPayloads" : {}},
103+ )
104+ self .assertIsNone (
105+ client .capture ("event" , distinct_id = "distinct_id" , send_feature_flags = True )
106+ )
107+ patch_flags .assert_not_called ()
108+
76109 def test_empty_flush (self ):
77110 self .client .flush ()
78111
0 commit comments