1818import pytest
1919from django .core .exceptions import ValidationError
2020
21- from paasng .utils .validators import DnsSafeNameValidator , ReservedWordValidator
21+ from paasng .utils .validators import DnsSafeNameValidator , ReservedWordValidator , validate_image_repo , validate_repo_url
2222
2323
2424class TestReservedWordValidator :
@@ -47,3 +47,64 @@ def test_negative_sample(self, input_str):
4747 with pytest .raises (ValidationError ) as exec_info :
4848 validator (input_str )
4949 assert exec_info .value .message == validator .message
50+
51+
52+ class Test__validate_repo_url :
53+ @pytest .mark .parametrize (
54+ "repo_url" ,
55+ ["mysql://127.0.0.1" , "postgres://127.0.0.1" ],
56+ )
57+ def test_invalid_protocol (self , repo_url ):
58+ with pytest .raises (ValueError , match = "Invalid url: only support http/https/git/svn scheme" ):
59+ validate_repo_url (repo_url )
60+
61+ @pytest .mark .parametrize (
62+ "repo_url" ,
63+ ["http://127.0.0.1:22/bkapps.git" , "https://127.0.0.1:23/bkapps.git" ],
64+ )
65+ def test_invalid_port (self , repo_url , settings ):
66+ settings .FORBIDDEN_REPO_PORTS = [22 , 23 ]
67+ with pytest .raises (ValueError , match = r"Invalid url: the port number \d+ is forbidden" ):
68+ validate_repo_url (repo_url )
69+
70+ @pytest .mark .parametrize (
71+ "repo_url" ,
72+ ["/www.example.com" , "//127.0.0.1" ],
73+ )
74+ def test_invalid_url (self , repo_url ):
75+ with pytest .raises (ValueError , match = "Invalid url" ):
76+ validate_repo_url (repo_url )
77+
78+ @pytest .mark .parametrize (
79+ "repo_url" ,
80+ [
81+ "http://127.0.0.1:80/bkapps.git" ,
82+ "https://127.0.0.1/bkapps.git" ,
83+ "git://127.0.0.1" ,
84+ "svn://127.0.0.1" ,
85+ ],
86+ )
87+ def test_valid_url (self , repo_url ):
88+ validate_repo_url (repo_url )
89+
90+
91+ class Test__validate_image_repo :
92+ @pytest .mark .parametrize (
93+ "image_repo" ,
94+ ["mirror.tencent.com:22/bkpaas" , "mirror.tencent.com:23/bkapps" ],
95+ )
96+ def test_invalid_port (self , image_repo , settings ):
97+ settings .FORBIDDEN_REPO_PORTS = [22 , 23 ]
98+ with pytest .raises (ValueError , match = r"Invalid image repo: the port number \d+ is forbidden" ):
99+ validate_image_repo (image_repo )
100+
101+ @pytest .mark .parametrize (
102+ "image_repo" ,
103+ [
104+ "mirror.tencent.com/bkapps" ,
105+ "mirror.tencent.com:443/bkapps" ,
106+ "nginx" ,
107+ ],
108+ )
109+ def test_valid_repo (self , image_repo ):
110+ validate_image_repo (image_repo )
0 commit comments