@@ -616,3 +616,79 @@ def test_tsigkeys_allowed_globally():
616616 environment = deepcopy (dummy_proxy_environment )
617617 environment .global_tsigkeys = True
618618 assert check_pdns_tsigkeys_allowed (environment ) is True
619+
620+
621+ def test_global_read_only_without_zones ():
622+ """Test that global_read_only=True allows empty zones list"""
623+ env = ProxyConfigEnvironment (
624+ name = "Test Global Read Only" ,
625+ token_sha512 = dummy_proxy_environment_token_sha512 ,
626+ global_read_only = True ,
627+ )
628+ assert env .global_read_only is True
629+ assert env .zones == []
630+
631+
632+ def test_environment_with_neither_zones_nor_global_read_only_fails ():
633+ """Test that providing neither zones nor global_read_only fails validation"""
634+ with pytest .raises (ValueError ) as err :
635+ ProxyConfigEnvironment (
636+ name = "test" , token_sha512 = dummy_proxy_environment_token_sha512
637+ )
638+ assert "Either 'zones' must be non-empty or 'global_read_only' must be True" in str (
639+ err .value
640+ )
641+
642+
643+ def test_environment_with_empty_zones_and_no_global_read_only_fails ():
644+ """Test that explicitly providing empty zones without global_read_only fails"""
645+ with pytest .raises (ValueError ) as err :
646+ ProxyConfigEnvironment (
647+ name = "test" , token_sha512 = dummy_proxy_environment_token_sha512 , zones = []
648+ )
649+ assert "Either 'zones' must be non-empty or 'global_read_only' must be True" in str (
650+ err .value
651+ )
652+
653+
654+ def test_proxy_config_with_global_read_only_environment ():
655+ """Test that ProxyConfig works with global_read_only environment without zones"""
656+ config = ProxyConfig (
657+ pdns_api_url = "https://powerdns-api.example.com" ,
658+ pdns_api_token = "blablub" ,
659+ environments = [
660+ ProxyConfigEnvironment (
661+ name = "foo" ,
662+ token_sha512 = dummy_proxy_environment_token_sha512 ,
663+ global_read_only = True ,
664+ )
665+ ],
666+ )
667+ assert config .environments [0 ].global_read_only is True
668+ assert config .environments [0 ].zones == []
669+
670+
671+ def test_global_read_only_with_explicit_zones_keeps_zone_permissions ():
672+ """Test that global_read_only=True doesn't force explicit zones to be read_only"""
673+ # Create a zone that should remain writable
674+ writable_zone = ProxyConfigZone (name = "example.com" , read_only = False )
675+ readonly_zone = ProxyConfigZone (name = "readonly.com" , read_only = True )
676+
677+ env = ProxyConfigEnvironment (
678+ name = "Test Global Read Only with Zones" ,
679+ token_sha512 = dummy_proxy_environment_token_sha512 ,
680+ zones = [writable_zone , readonly_zone ],
681+ global_read_only = True ,
682+ )
683+
684+ # global_read_only should be True
685+ assert env .global_read_only is True
686+
687+ # But explicit zones should keep their original read_only settings
688+ assert env .zones [0 ].read_only is False # writable_zone should remain writable
689+ assert env .zones [1 ].read_only is True # readonly_zone should remain read_only
690+
691+ # Should have access to zones via lookup
692+ assert len (env ._zones_lookup ) == 2
693+ assert "example.com" in env ._zones_lookup
694+ assert "readonly.com" in env ._zones_lookup
0 commit comments