github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/identity/v3/roles/testing/requests_test.go (about) 1 package testing 2 3 import ( 4 "testing" 5 6 "github.com/huaweicloud/golangsdk/openstack/identity/v3/roles" 7 "github.com/huaweicloud/golangsdk/pagination" 8 th "github.com/huaweicloud/golangsdk/testhelper" 9 "github.com/huaweicloud/golangsdk/testhelper/client" 10 ) 11 12 func TestListRoles(t *testing.T) { 13 th.SetupHTTP() 14 defer th.TeardownHTTP() 15 HandleListRolesSuccessfully(t) 16 17 count := 0 18 err := roles.List(client.ServiceClient(), nil).EachPage(func(page pagination.Page) (bool, error) { 19 count++ 20 21 actual, err := roles.ExtractRoles(page) 22 th.AssertNoErr(t, err) 23 24 th.CheckDeepEquals(t, ExpectedRolesSlice, actual) 25 26 return true, nil 27 }) 28 th.AssertNoErr(t, err) 29 th.CheckEquals(t, count, 1) 30 } 31 32 func TestListRolesAllPages(t *testing.T) { 33 th.SetupHTTP() 34 defer th.TeardownHTTP() 35 HandleListRolesSuccessfully(t) 36 37 allPages, err := roles.List(client.ServiceClient(), nil).AllPages() 38 th.AssertNoErr(t, err) 39 actual, err := roles.ExtractRoles(allPages) 40 th.AssertNoErr(t, err) 41 th.CheckDeepEquals(t, ExpectedRolesSlice, actual) 42 th.AssertEquals(t, ExpectedRolesSlice[1].Extra["description"], "read-only support role") 43 } 44 45 func TestGetRole(t *testing.T) { 46 th.SetupHTTP() 47 defer th.TeardownHTTP() 48 HandleGetRoleSuccessfully(t) 49 50 actual, err := roles.Get(client.ServiceClient(), "9fe1d3").Extract() 51 52 th.AssertNoErr(t, err) 53 th.CheckDeepEquals(t, SecondRole, *actual) 54 } 55 56 func TestCreateRole(t *testing.T) { 57 th.SetupHTTP() 58 defer th.TeardownHTTP() 59 HandleCreateRoleSuccessfully(t) 60 61 createOpts := roles.CreateOpts{ 62 Name: "support", 63 DomainID: "1789d1", 64 Extra: map[string]interface{}{ 65 "description": "read-only support role", 66 }, 67 } 68 69 actual, err := roles.Create(client.ServiceClient(), createOpts).Extract() 70 th.AssertNoErr(t, err) 71 th.CheckDeepEquals(t, SecondRole, *actual) 72 } 73 74 func TestUpdateRole(t *testing.T) { 75 th.SetupHTTP() 76 defer th.TeardownHTTP() 77 HandleUpdateRoleSuccessfully(t) 78 79 updateOpts := roles.UpdateOpts{ 80 Extra: map[string]interface{}{ 81 "description": "admin read-only support role", 82 }, 83 } 84 85 actual, err := roles.Update(client.ServiceClient(), "9fe1d3", updateOpts).Extract() 86 th.AssertNoErr(t, err) 87 th.CheckDeepEquals(t, SecondRoleUpdated, *actual) 88 } 89 90 func TestDeleteRole(t *testing.T) { 91 th.SetupHTTP() 92 defer th.TeardownHTTP() 93 HandleDeleteRoleSuccessfully(t) 94 95 res := roles.Delete(client.ServiceClient(), "9fe1d3") 96 th.AssertNoErr(t, res.Err) 97 } 98 99 func TestAssign(t *testing.T) { 100 th.SetupHTTP() 101 defer th.TeardownHTTP() 102 HandleAssignSuccessfully(t) 103 104 err := roles.Assign(client.ServiceClient(), "{role_id}", roles.AssignOpts{ 105 UserID: "{user_id}", 106 ProjectID: "{project_id}", 107 }).ExtractErr() 108 th.AssertNoErr(t, err) 109 110 err = roles.Assign(client.ServiceClient(), "{role_id}", roles.AssignOpts{ 111 UserID: "{user_id}", 112 DomainID: "{domain_id}", 113 }).ExtractErr() 114 th.AssertNoErr(t, err) 115 116 err = roles.Assign(client.ServiceClient(), "{role_id}", roles.AssignOpts{ 117 GroupID: "{group_id}", 118 ProjectID: "{project_id}", 119 }).ExtractErr() 120 th.AssertNoErr(t, err) 121 122 err = roles.Assign(client.ServiceClient(), "{role_id}", roles.AssignOpts{ 123 GroupID: "{group_id}", 124 DomainID: "{domain_id}", 125 }).ExtractErr() 126 th.AssertNoErr(t, err) 127 } 128 129 func TestUnassign(t *testing.T) { 130 th.SetupHTTP() 131 defer th.TeardownHTTP() 132 HandleUnassignSuccessfully(t) 133 134 err := roles.Unassign(client.ServiceClient(), "{role_id}", roles.UnassignOpts{ 135 UserID: "{user_id}", 136 ProjectID: "{project_id}", 137 }).ExtractErr() 138 th.AssertNoErr(t, err) 139 140 err = roles.Unassign(client.ServiceClient(), "{role_id}", roles.UnassignOpts{ 141 UserID: "{user_id}", 142 DomainID: "{domain_id}", 143 }).ExtractErr() 144 th.AssertNoErr(t, err) 145 146 err = roles.Unassign(client.ServiceClient(), "{role_id}", roles.UnassignOpts{ 147 GroupID: "{group_id}", 148 ProjectID: "{project_id}", 149 }).ExtractErr() 150 th.AssertNoErr(t, err) 151 152 err = roles.Unassign(client.ServiceClient(), "{role_id}", roles.UnassignOpts{ 153 GroupID: "{group_id}", 154 DomainID: "{domain_id}", 155 }).ExtractErr() 156 th.AssertNoErr(t, err) 157 }