github.com/vnpaycloud-console/gophercloud/v2@v2.0.5/openstack/identity/v3/policies/testing/requests_test.go (about) 1 package testing 2 3 import ( 4 "context" 5 "fmt" 6 "testing" 7 8 "github.com/vnpaycloud-console/gophercloud/v2/openstack/identity/v3/policies" 9 "github.com/vnpaycloud-console/gophercloud/v2/pagination" 10 th "github.com/vnpaycloud-console/gophercloud/v2/testhelper" 11 "github.com/vnpaycloud-console/gophercloud/v2/testhelper/client" 12 ) 13 14 func TestListPolicies(t *testing.T) { 15 th.SetupHTTP() 16 defer th.TeardownHTTP() 17 HandleListPoliciesSuccessfully(t) 18 19 count := 0 20 err := policies.List(client.ServiceClient(), nil).EachPage(context.TODO(), func(_ context.Context, page pagination.Page) (bool, error) { 21 count++ 22 23 actual, err := policies.ExtractPolicies(page) 24 th.AssertNoErr(t, err) 25 26 th.CheckDeepEquals(t, ExpectedPoliciesSlice, actual) 27 28 return true, nil 29 }) 30 th.AssertNoErr(t, err) 31 th.CheckEquals(t, count, 1) 32 } 33 34 func TestListPoliciesAllPages(t *testing.T) { 35 th.SetupHTTP() 36 defer th.TeardownHTTP() 37 HandleListPoliciesSuccessfully(t) 38 39 allPages, err := policies.List(client.ServiceClient(), nil).AllPages(context.TODO()) 40 th.AssertNoErr(t, err) 41 actual, err := policies.ExtractPolicies(allPages) 42 th.AssertNoErr(t, err) 43 th.CheckDeepEquals(t, ExpectedPoliciesSlice, actual) 44 } 45 46 func TestListPoliciesWithFilter(t *testing.T) { 47 th.SetupHTTP() 48 defer th.TeardownHTTP() 49 HandleListPoliciesSuccessfully(t) 50 51 listOpts := policies.ListOpts{ 52 Type: "application/json", 53 } 54 allPages, err := policies.List(client.ServiceClient(), listOpts).AllPages(context.TODO()) 55 th.AssertNoErr(t, err) 56 actual, err := policies.ExtractPolicies(allPages) 57 th.AssertNoErr(t, err) 58 th.CheckDeepEquals(t, []policies.Policy{SecondPolicy}, actual) 59 } 60 61 func TestListPoliciesFiltersCheck(t *testing.T) { 62 type test struct { 63 filterName string 64 wantErr bool 65 } 66 tests := []test{ 67 {"foo__contains", false}, 68 {"foo", true}, 69 {"foo_contains", true}, 70 {"foo__", true}, 71 {"__foo", true}, 72 } 73 74 var listOpts policies.ListOpts 75 for _, _test := range tests { 76 listOpts.Filters = map[string]string{_test.filterName: "bar"} 77 _, err := listOpts.ToPolicyListQuery() 78 79 if !_test.wantErr { 80 th.AssertNoErr(t, err) 81 } else { 82 switch _t := err.(type) { 83 case nil: 84 t.Fatal("error expected but got a nil") 85 case policies.InvalidListFilter: 86 default: 87 t.Fatalf("unexpected error type: [%T]", _t) 88 } 89 } 90 } 91 } 92 93 func TestCreatePolicy(t *testing.T) { 94 th.SetupHTTP() 95 defer th.TeardownHTTP() 96 HandleCreatePolicySuccessfully(t) 97 98 createOpts := policies.CreateOpts{ 99 Type: "application/json", 100 Blob: []byte("{'bar_user': 'role:network-user'}"), 101 Extra: map[string]any{ 102 "description": "policy for bar_user", 103 }, 104 } 105 106 actual, err := policies.Create(context.TODO(), client.ServiceClient(), createOpts).Extract() 107 th.AssertNoErr(t, err) 108 th.CheckDeepEquals(t, SecondPolicy, *actual) 109 } 110 111 func TestCreatePolicyTypeLengthCheck(t *testing.T) { 112 // strGenerator generates a string of fixed length filled with '0' 113 strGenerator := func(length int) string { 114 return fmt.Sprintf(fmt.Sprintf("%%0%dd", length), 0) 115 } 116 117 type test struct { 118 length int 119 wantErr bool 120 } 121 122 tests := []test{ 123 {100, false}, 124 {255, false}, 125 {256, true}, 126 {300, true}, 127 } 128 129 createOpts := policies.CreateOpts{ 130 Blob: []byte("{'bar_user': 'role:network-user'}"), 131 } 132 133 for _, _test := range tests { 134 createOpts.Type = strGenerator(_test.length) 135 if len(createOpts.Type) != _test.length { 136 t.Fatal("function strGenerator does not work properly") 137 } 138 139 _, err := createOpts.ToPolicyCreateMap() 140 if !_test.wantErr { 141 th.AssertNoErr(t, err) 142 } else { 143 switch _t := err.(type) { 144 case nil: 145 t.Fatal("error expected but got a nil") 146 case policies.StringFieldLengthExceedsLimit: 147 default: 148 t.Fatalf("unexpected error type: [%T]", _t) 149 } 150 } 151 } 152 } 153 154 func TestGetPolicy(t *testing.T) { 155 th.SetupHTTP() 156 defer th.TeardownHTTP() 157 HandleGetPolicySuccessfully(t) 158 159 id := "b49884da9d31494ea02aff38d4b4e701" 160 actual, err := policies.Get(context.TODO(), client.ServiceClient(), id).Extract() 161 162 th.AssertNoErr(t, err) 163 th.CheckDeepEquals(t, SecondPolicy, *actual) 164 } 165 166 func TestUpdatePolicy(t *testing.T) { 167 th.SetupHTTP() 168 defer th.TeardownHTTP() 169 HandleUpdatePolicySuccessfully(t) 170 171 updateOpts := policies.UpdateOpts{ 172 Extra: map[string]any{ 173 "description": "updated policy for bar_user", 174 }, 175 } 176 177 id := "b49884da9d31494ea02aff38d4b4e701" 178 actual, err := policies.Update(context.TODO(), client.ServiceClient(), id, updateOpts).Extract() 179 th.AssertNoErr(t, err) 180 th.CheckDeepEquals(t, SecondPolicyUpdated, *actual) 181 } 182 183 func TestUpdatePolicyTypeLengthCheck(t *testing.T) { 184 // strGenerator generates a string of fixed length filled with '0' 185 strGenerator := func(length int) string { 186 return fmt.Sprintf(fmt.Sprintf("%%0%dd", length), 0) 187 } 188 189 type test struct { 190 length int 191 wantErr bool 192 } 193 194 tests := []test{ 195 {100, false}, 196 {255, false}, 197 {256, true}, 198 {300, true}, 199 } 200 201 var updateOpts policies.UpdateOpts 202 for _, _test := range tests { 203 updateOpts.Type = strGenerator(_test.length) 204 if len(updateOpts.Type) != _test.length { 205 t.Fatal("function strGenerator does not work properly") 206 } 207 208 _, err := updateOpts.ToPolicyUpdateMap() 209 if !_test.wantErr { 210 th.AssertNoErr(t, err) 211 } else { 212 switch _t := err.(type) { 213 case nil: 214 t.Fatal("error expected but got a nil") 215 case policies.StringFieldLengthExceedsLimit: 216 default: 217 t.Fatalf("unexpected error type: [%T]", _t) 218 } 219 } 220 } 221 } 222 223 func TestDeletePolicy(t *testing.T) { 224 th.SetupHTTP() 225 defer th.TeardownHTTP() 226 HandleDeletePolicySuccessfully(t) 227 228 res := policies.Delete(context.TODO(), client.ServiceClient(), "9fe1d3") 229 th.AssertNoErr(t, res.Err) 230 }