github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/networking/v2/extensions/rbacpolicies/testing/requests_test.go (about) 1 package testing 2 3 import ( 4 "fmt" 5 "net/http" 6 "testing" 7 8 fake "github.com/huaweicloud/golangsdk/openstack/networking/v2/common" 9 "github.com/huaweicloud/golangsdk/openstack/networking/v2/extensions/rbacpolicies" 10 "github.com/huaweicloud/golangsdk/pagination" 11 th "github.com/huaweicloud/golangsdk/testhelper" 12 ) 13 14 func TestCreate(t *testing.T) { 15 th.SetupHTTP() 16 defer th.TeardownHTTP() 17 18 th.Mux.HandleFunc("/v2.0/rbac-policies", func(w http.ResponseWriter, r *http.Request) { 19 th.TestMethod(t, r, "POST") 20 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 21 th.TestHeader(t, r, "Content-Type", "application/json") 22 th.TestHeader(t, r, "Accept", "application/json") 23 th.TestJSONRequest(t, r, CreateRequest) 24 w.Header().Add("Content-Type", "application/json") 25 w.WriteHeader(http.StatusCreated) 26 27 fmt.Fprintf(w, CreateResponse) 28 }) 29 30 options := rbacpolicies.CreateOpts{ 31 Action: rbacpolicies.ActionAccessShared, 32 ObjectType: "network", 33 TargetTenant: "6e547a3bcfe44702889fdeff3c3520c3", 34 ObjectID: "240d22bf-bd17-4238-9758-25f72610ecdc", 35 } 36 rbacResult, err := rbacpolicies.Create(fake.ServiceClient(), options).Extract() 37 th.AssertNoErr(t, err) 38 39 th.AssertDeepEquals(t, &rbacPolicy1, rbacResult) 40 } 41 42 func TestGet(t *testing.T) { 43 th.SetupHTTP() 44 defer th.TeardownHTTP() 45 46 th.Mux.HandleFunc("/v2.0/rbac-policies/2cf7523a-93b5-4e69-9360-6c6bf986bb7c", func(w http.ResponseWriter, r *http.Request) { 47 th.TestMethod(t, r, "GET") 48 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 49 50 w.Header().Add("Content-Type", "application/json") 51 w.WriteHeader(http.StatusOK) 52 53 fmt.Fprintf(w, GetResponse) 54 }) 55 56 n, err := rbacpolicies.Get(fake.ServiceClient(), "2cf7523a-93b5-4e69-9360-6c6bf986bb7c").Extract() 57 th.AssertNoErr(t, err) 58 th.CheckDeepEquals(t, &rbacPolicy1, n) 59 } 60 61 func TestList(t *testing.T) { 62 th.SetupHTTP() 63 defer th.TeardownHTTP() 64 65 th.Mux.HandleFunc("/v2.0/rbac-policies", func(w http.ResponseWriter, r *http.Request) { 66 th.TestMethod(t, r, "GET") 67 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 68 69 w.Header().Add("Content-Type", "application/json") 70 w.WriteHeader(http.StatusOK) 71 72 fmt.Fprintf(w, ListResponse) 73 }) 74 75 client := fake.ServiceClient() 76 count := 0 77 78 rbacpolicies.List(client, rbacpolicies.ListOpts{}).EachPage(func(page pagination.Page) (bool, error) { 79 count++ 80 actual, err := rbacpolicies.ExtractRBACPolicies(page) 81 if err != nil { 82 t.Errorf("Failed to extract rbac policies: %v", err) 83 return false, err 84 } 85 86 th.CheckDeepEquals(t, ExpectedRBACPoliciesSlice, actual) 87 88 return true, nil 89 }) 90 91 if count != 1 { 92 t.Errorf("Expected 1 page, got %d", count) 93 } 94 } 95 96 func TestListWithAllPages(t *testing.T) { 97 th.SetupHTTP() 98 defer th.TeardownHTTP() 99 100 th.Mux.HandleFunc("/v2.0/rbac-policies", func(w http.ResponseWriter, r *http.Request) { 101 th.TestMethod(t, r, "GET") 102 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 103 104 w.Header().Add("Content-Type", "application/json") 105 w.WriteHeader(http.StatusOK) 106 107 fmt.Fprintf(w, ListResponse) 108 }) 109 110 client := fake.ServiceClient() 111 112 type newRBACPolicy struct { 113 rbacpolicies.RBACPolicy 114 } 115 116 var allRBACpolicies []newRBACPolicy 117 118 allPages, err := rbacpolicies.List(client, rbacpolicies.ListOpts{}).AllPages() 119 th.AssertNoErr(t, err) 120 121 err = rbacpolicies.ExtractRBACPolicesInto(allPages, &allRBACpolicies) 122 th.AssertNoErr(t, err) 123 124 th.AssertEquals(t, allRBACpolicies[0].ObjectType, "network") 125 th.AssertEquals(t, allRBACpolicies[0].Action, rbacpolicies.ActionAccessShared) 126 127 th.AssertEquals(t, allRBACpolicies[1].ProjectID, "1ae27ce0a2a54cc6ae06dc62dd0ec832") 128 th.AssertEquals(t, allRBACpolicies[1].TargetTenant, "1a547a3bcfe44702889fdeff3c3520c3") 129 130 } 131 132 func TestDelete(t *testing.T) { 133 th.SetupHTTP() 134 defer th.TeardownHTTP() 135 136 th.Mux.HandleFunc("/v2.0/rbac-policies/71d55b18-d2f8-4c76-a5e6-e0a3dd114361", func(w http.ResponseWriter, r *http.Request) { 137 th.TestMethod(t, r, "DELETE") 138 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 139 w.WriteHeader(http.StatusNoContent) 140 }) 141 142 res := rbacpolicies.Delete(fake.ServiceClient(), "71d55b18-d2f8-4c76-a5e6-e0a3dd114361").ExtractErr() 143 th.AssertNoErr(t, res) 144 } 145 146 func TestUpdate(t *testing.T) { 147 th.SetupHTTP() 148 defer th.TeardownHTTP() 149 150 th.Mux.HandleFunc("/v2.0/rbac-policies/2cf7523a-93b5-4e69-9360-6c6bf986bb7c", func(w http.ResponseWriter, r *http.Request) { 151 th.TestMethod(t, r, "PUT") 152 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 153 th.TestHeader(t, r, "Content-Type", "application/json") 154 th.TestHeader(t, r, "Accept", "application/json") 155 th.TestJSONRequest(t, r, UpdateRequest) 156 157 w.Header().Add("Content-Type", "application/json") 158 w.WriteHeader(http.StatusOK) 159 160 fmt.Fprintf(w, UpdateResponse) 161 }) 162 163 options := rbacpolicies.UpdateOpts{TargetTenant: "9d766060b6354c9e8e2da44cab0e8f38"} 164 rbacResult, err := rbacpolicies.Update(fake.ServiceClient(), "2cf7523a-93b5-4e69-9360-6c6bf986bb7c", options).Extract() 165 th.AssertNoErr(t, err) 166 167 th.AssertEquals(t, rbacResult.TargetTenant, "9d766060b6354c9e8e2da44cab0e8f38") 168 th.AssertEquals(t, rbacResult.ID, "2cf7523a-93b5-4e69-9360-6c6bf986bb7c") 169 }