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