@@ -19,22 +19,52 @@ func TestUpdateRestrictionsCommand(t *testing.T) {
1919 token := apitest .RandomAccessToken (t )
2020 t .Setenv ("SUPABASE_ACCESS_TOKEN" , string (token ))
2121
22- t .Run ("updates v4 and v6 CIDR" , func (t * testing.T ) {
22+ t .Run ("replaces v4 and v6 CIDR" , func (t * testing.T ) {
2323 // Setup mock api
2424 defer gock .OffAll ()
25+ expectedV4 := []string {"12.3.4.5/32" , "1.2.3.1/24" }
26+ expectedV6 := []string {"2001:db8:abcd:0012::0/64" }
2527 gock .New (utils .DefaultApiHost ).
2628 Post ("/v1/projects/" + projectRef + "/network-restrictions/apply" ).
2729 MatchType ("json" ).
2830 JSON (api.NetworkRestrictionsRequest {
29- DbAllowedCidrs : & [] string { "12.3.4.5/32" , "1.2.3.1/24" } ,
30- DbAllowedCidrsV6 : & [] string { "2001:db8:abcd:0012::0/64" } ,
31+ DbAllowedCidrs : & expectedV4 ,
32+ DbAllowedCidrsV6 : & expectedV6 ,
3133 }).
3234 Reply (http .StatusCreated ).
3335 JSON (api.NetworkRestrictionsResponse {
3436 Status : api .NetworkRestrictionsResponseStatus ("applied" ),
3537 })
3638 // Run test
37- err := Run (context .Background (), projectRef , []string {"12.3.4.5/32" , "2001:db8:abcd:0012::0/64" , "1.2.3.1/24" }, false )
39+ err := Run (context .Background (), projectRef , []string {"12.3.4.5/32" , "2001:db8:abcd:0012::0/64" , "1.2.3.1/24" }, false , false )
40+ // Check error
41+ assert .NoError (t , err )
42+ assert .Empty (t , apitest .ListUnmatchedRequests ())
43+ })
44+
45+ t .Run ("appends v4 and v6 CIDR" , func (t * testing.T ) {
46+ // Setup mock api
47+ defer gock .OffAll ()
48+ addV4 := []string {"12.3.4.5/32" , "1.2.3.1/24" }
49+ addV6 := []string {"2001:db8:abcd:0012::0/64" }
50+ gock .New (utils .DefaultApiHost ).
51+ Patch ("/v1/projects/" + projectRef + "/network-restrictions" ).
52+ MatchType ("json" ).
53+ JSON (api.NetworkRestrictionsPatchRequest {
54+ Add : & struct {
55+ DbAllowedCidrs * []string `json:"dbAllowedCidrs,omitempty"`
56+ DbAllowedCidrsV6 * []string `json:"dbAllowedCidrsV6,omitempty"`
57+ }{
58+ DbAllowedCidrs : & addV4 ,
59+ DbAllowedCidrsV6 : & addV6 ,
60+ },
61+ }).
62+ Reply (http .StatusOK ).
63+ JSON (api.NetworkRestrictionsV2Response {
64+ Status : api .NetworkRestrictionsV2ResponseStatus ("applied" ),
65+ })
66+ // Run test
67+ err := Run (context .Background (), projectRef , []string {"12.3.4.5/32" , "1.2.3.1/24" , "2001:db8:abcd:0012::0/64" }, false , true )
3868 // Check error
3969 assert .NoError (t , err )
4070 assert .Empty (t , apitest .ListUnmatchedRequests ())
@@ -53,7 +83,7 @@ func TestUpdateRestrictionsCommand(t *testing.T) {
5383 }).
5484 ReplyError (errNetwork )
5585 // Run test
56- err := Run (context .Background (), projectRef , []string {}, true )
86+ err := Run (context .Background (), projectRef , []string {}, true , false )
5787 // Check error
5888 assert .ErrorIs (t , err , errNetwork )
5989 assert .Empty (t , apitest .ListUnmatchedRequests ())
@@ -71,7 +101,7 @@ func TestUpdateRestrictionsCommand(t *testing.T) {
71101 }).
72102 Reply (http .StatusServiceUnavailable )
73103 // Run test
74- err := Run (context .Background (), projectRef , []string {}, true )
104+ err := Run (context .Background (), projectRef , []string {}, true , false )
75105 // Check error
76106 assert .ErrorContains (t , err , "failed to apply network restrictions:" )
77107 assert .Empty (t , apitest .ListUnmatchedRequests ())
@@ -99,22 +129,22 @@ func TestValidateCIDR(t *testing.T) {
99129 Status : api .NetworkRestrictionsResponseStatus ("applied" ),
100130 })
101131 // Run test
102- err := Run (context .Background (), projectRef , []string {"10.0.0.0/8" }, true )
132+ err := Run (context .Background (), projectRef , []string {"10.0.0.0/8" }, true , false )
103133 // Check error
104134 assert .NoError (t , err )
105135 assert .Empty (t , apitest .ListUnmatchedRequests ())
106136 })
107137
108138 t .Run ("throws error on private subnet" , func (t * testing.T ) {
109139 // Run test
110- err := Run (context .Background (), projectRef , []string {"12.3.4.5/32" , "10.0.0.0/8" , "1.2.3.1/24" }, false )
140+ err := Run (context .Background (), projectRef , []string {"12.3.4.5/32" , "10.0.0.0/8" , "1.2.3.1/24" }, false , false )
111141 // Check error
112142 assert .ErrorContains (t , err , "private IP provided: 10.0.0.0/8" )
113143 })
114144
115145 t .Run ("throws error on invalid subnet" , func (t * testing.T ) {
116146 // Run test
117- err := Run (context .Background (), projectRef , []string {"12.3.4.5" , "10.0.0.0/8" , "1.2.3.1/24" }, false )
147+ err := Run (context .Background (), projectRef , []string {"12.3.4.5" , "10.0.0.0/8" , "1.2.3.1/24" }, false , false )
118148 // Check error
119149 assert .ErrorContains (t , err , "failed to parse IP: 12.3.4.5" )
120150 })
0 commit comments