11# -*- coding: utf-8 -*-
22"""Unit tests for content security service."""
33
4+ import sys
5+ import threading
46import pytest
57import mcpgateway .services .content_security as cs_mod
68from unittest .mock import patch , MagicMock
79
10+ from mcpgateway import config
811from mcpgateway .services .content_security import (
912 ContentSecurityService ,
1013 ContentSizeError ,
@@ -213,8 +216,6 @@ def test_get_service_returns_singleton(self):
213216
214217 def test_get_service_thread_safe (self ):
215218 """Test that singleton is thread-safe."""
216- import threading
217-
218219 results = []
219220
220221 def get_service ():
@@ -327,7 +328,6 @@ def test_validate_empty_mime_type(self):
327328
328329 def test_validate_allowed_mime_type (self , monkeypatch ):
329330 """Test validation passes for allowed MIME types."""
330- from mcpgateway import config
331331 # Ensure strict mode is off so this test is independent of .env settings
332332 monkeypatch .setattr (config .settings , "content_strict_mime_validation" , False )
333333 service = ContentSecurityService ()
@@ -339,7 +339,6 @@ def test_validate_allowed_mime_type(self, monkeypatch):
339339
340340 def test_validate_vendor_mime_type_log_only_mode (self , monkeypatch ):
341341 """Test that vendor types (x- prefix) are allowed in log-only mode."""
342- from mcpgateway import config
343342 monkeypatch .setattr (config .settings , "content_strict_mime_validation" , False )
344343
345344 service = ContentSecurityService ()
@@ -349,7 +348,6 @@ def test_validate_vendor_mime_type_log_only_mode(self, monkeypatch):
349348
350349 def test_validate_vendor_mime_type_strict_mode (self , monkeypatch ):
351350 """Test that vendor types (x- prefix) are rejected in strict mode unless in allowlist."""
352- from mcpgateway import config
353351 monkeypatch .setattr (config .settings , "content_strict_mime_validation" , True )
354352 monkeypatch .setattr (config .settings , "content_allowed_resource_mimetypes" , ["text/plain" ])
355353
@@ -365,7 +363,6 @@ def test_validate_vendor_mime_type_strict_mode(self, monkeypatch):
365363
366364 def test_validate_suffix_mime_type_log_only_mode (self , monkeypatch ):
367365 """Test that suffix types (with +) are allowed in log-only mode."""
368- from mcpgateway import config
369366 monkeypatch .setattr (config .settings , "content_strict_mime_validation" , False )
370367
371368 service = ContentSecurityService ()
@@ -375,7 +372,6 @@ def test_validate_suffix_mime_type_log_only_mode(self, monkeypatch):
375372
376373 def test_validate_suffix_mime_type_strict_mode (self , monkeypatch ):
377374 """Test that suffix types (with +) are rejected in strict mode unless in allowlist."""
378- from mcpgateway import config
379375 monkeypatch .setattr (config .settings , "content_strict_mime_validation" , True )
380376 monkeypatch .setattr (config .settings , "content_allowed_resource_mimetypes" , ["text/plain" ])
381377
@@ -392,7 +388,6 @@ def test_validate_suffix_mime_type_strict_mode(self, monkeypatch):
392388 def test_validate_disallowed_mime_type_strict_mode (self , monkeypatch ):
393389 """Test validation fails for disallowed MIME types in strict mode."""
394390 # Enable strict validation
395- from mcpgateway import config
396391 monkeypatch .setattr (config .settings , "content_strict_mime_validation" , True )
397392
398393 service = ContentSecurityService ()
@@ -406,7 +401,6 @@ def test_validate_disallowed_mime_type_strict_mode(self, monkeypatch):
406401 def test_validate_disallowed_mime_type_log_only_mode (self , monkeypatch ):
407402 """Test validation logs but doesn't raise in log-only mode."""
408403 # Disable strict validation (log-only mode)
409- from mcpgateway import config
410404 monkeypatch .setattr (config .settings , "content_strict_mime_validation" , False )
411405
412406 service = ContentSecurityService ()
@@ -415,7 +409,6 @@ def test_validate_disallowed_mime_type_log_only_mode(self, monkeypatch):
415409
416410 def test_validate_with_logging_context (self , monkeypatch ):
417411 """Test validation with full logging context."""
418- from mcpgateway import config
419412 monkeypatch .setattr (config .settings , "content_strict_mime_validation" , True )
420413
421414 service = ContentSecurityService ()
@@ -429,7 +422,6 @@ def test_validate_with_logging_context(self, monkeypatch):
429422
430423 def test_validate_case_sensitive (self , monkeypatch ):
431424 """Test that MIME type validation is case-sensitive."""
432- from mcpgateway import config
433425 monkeypatch .setattr (config .settings , "content_strict_mime_validation" , True )
434426
435427 service = ContentSecurityService ()
@@ -447,7 +439,6 @@ class TestMimeTypeIntegration:
447439
448440 def test_size_and_mime_validation_order (self , monkeypatch ):
449441 """Test that size validation happens before MIME validation."""
450- from mcpgateway import config
451442 monkeypatch .setattr (config .settings , "content_strict_mime_validation" , True )
452443
453444 service = ContentSecurityService ()
@@ -473,7 +464,6 @@ class TestVendorSuffixMimeTypeInStrictMode:
473464
474465 def test_vendor_type_rejected_in_strict_mode_without_allowlist (self , monkeypatch ):
475466 """Test that application/x- vendor types are rejected in strict mode if not in allowlist."""
476- from mcpgateway import config
477467 monkeypatch .setattr (config .settings , "content_strict_mime_validation" , True )
478468 # Use a custom allowlist that does NOT include application/x-custom
479469 monkeypatch .setattr (config .settings , "content_allowed_resource_mimetypes" , ["text/plain" ])
@@ -486,7 +476,6 @@ def test_vendor_type_rejected_in_strict_mode_without_allowlist(self, monkeypatch
486476
487477 def test_vendor_type_allowed_when_in_allowlist (self , monkeypatch ):
488478 """Test that vendor types pass when explicitly added to allowlist."""
489- from mcpgateway import config
490479 monkeypatch .setattr (config .settings , "content_strict_mime_validation" , True )
491480 # Add vendor type to allowlist
492481 monkeypatch .setattr (config .settings , "content_allowed_resource_mimetypes" , ["text/plain" , "application/x-custom" ])
@@ -497,7 +486,6 @@ def test_vendor_type_allowed_when_in_allowlist(self, monkeypatch):
497486
498487 def test_text_vendor_type_rejected_in_strict_mode_without_allowlist (self , monkeypatch ):
499488 """Test that text/x- vendor types are rejected in strict mode if not in allowlist."""
500- from mcpgateway import config
501489 monkeypatch .setattr (config .settings , "content_strict_mime_validation" , True )
502490 monkeypatch .setattr (config .settings , "content_allowed_resource_mimetypes" , ["application/json" ])
503491
@@ -509,7 +497,6 @@ def test_text_vendor_type_rejected_in_strict_mode_without_allowlist(self, monkey
509497
510498 def test_suffix_type_rejected_in_strict_mode_without_allowlist (self , monkeypatch ):
511499 """Test that suffix types (+json, +xml) are rejected in strict mode if not in allowlist."""
512- from mcpgateway import config
513500 monkeypatch .setattr (config .settings , "content_strict_mime_validation" , True )
514501 monkeypatch .setattr (config .settings , "content_allowed_resource_mimetypes" , ["text/plain" ])
515502
@@ -521,7 +508,6 @@ def test_suffix_type_rejected_in_strict_mode_without_allowlist(self, monkeypatch
521508
522509 def test_suffix_type_allowed_when_in_allowlist (self , monkeypatch ):
523510 """Test that suffix types pass when explicitly added to allowlist."""
524- from mcpgateway import config
525511 monkeypatch .setattr (config .settings , "content_strict_mime_validation" , True )
526512 # Add suffix type to allowlist
527513 monkeypatch .setattr (config .settings , "content_allowed_resource_mimetypes" , ["text/plain" , "application/vnd.api+json" ])
@@ -554,8 +540,6 @@ def inc(self, amount=1):
554540
555541 def test_noop_counter_import_fallback (self ):
556542 """Test that content_security module handles missing metrics gracefully (line 26)."""
557- import sys
558-
559543 # Temporarily hide the metrics module to trigger the ImportError fallback
560544 original_metrics = sys .modules .get ("mcpgateway.services.metrics" )
561545 original_cs = sys .modules .get ("mcpgateway.services.content_security" )
0 commit comments