github.com/gophercloud/gophercloud@v1.11.0/openstack/identity/v3/extensions/trusts/testing/requests_test.go (about) 1 package testing 2 3 import ( 4 "testing" 5 "time" 6 7 "github.com/gophercloud/gophercloud/openstack/identity/v3/extensions/trusts" 8 "github.com/gophercloud/gophercloud/openstack/identity/v3/tokens" 9 "github.com/gophercloud/gophercloud/pagination" 10 th "github.com/gophercloud/gophercloud/testhelper" 11 "github.com/gophercloud/gophercloud/testhelper/client" 12 ) 13 14 func TestCreateUserIDPasswordTrustID(t *testing.T) { 15 th.SetupHTTP() 16 defer th.TeardownHTTP() 17 18 ao := trusts.AuthOptsExt{ 19 TrustID: "de0945a", 20 AuthOptionsBuilder: &tokens.AuthOptions{ 21 UserID: "me", 22 Password: "squirrel!", 23 }, 24 } 25 HandleCreateTokenWithTrustID(t, ao, ` 26 { 27 "auth": { 28 "identity": { 29 "methods": ["password"], 30 "password": { 31 "user": { "id": "me", "password": "squirrel!" } 32 } 33 }, 34 "scope": { 35 "OS-TRUST:trust": { 36 "id": "de0945a" 37 } 38 } 39 } 40 } 41 `) 42 43 var actual struct { 44 tokens.Token 45 trusts.TokenExt 46 } 47 err := tokens.Create(client.ServiceClient(), ao).ExtractInto(&actual) 48 if err != nil { 49 t.Errorf("Create returned an error: %v", err) 50 } 51 expected := struct { 52 tokens.Token 53 trusts.TokenExt 54 }{ 55 tokens.Token{ 56 ExpiresAt: time.Date(2013, 02, 27, 18, 30, 59, 999999000, time.UTC), 57 }, 58 trusts.TokenExt{ 59 Trust: trusts.Trust{ 60 ID: "fe0aef", 61 Impersonation: false, 62 RedelegatedTrustID: "3ba234", 63 RedelegationCount: 2, 64 }, 65 }, 66 } 67 68 th.AssertDeepEquals(t, expected, actual) 69 } 70 71 func TestCreateTrust(t *testing.T) { 72 th.SetupHTTP() 73 defer th.TeardownHTTP() 74 HandleCreateTrust(t) 75 76 expiresAt := time.Date(2019, 12, 1, 14, 0, 0, 0, time.UTC) 77 result, err := trusts.Create(client.ServiceClient(), trusts.CreateOpts{ 78 ExpiresAt: &expiresAt, 79 AllowRedelegation: true, 80 ProjectID: "9b71012f5a4a4aef9193f1995fe159b2", 81 Roles: []trusts.Role{ 82 { 83 Name: "member", 84 }, 85 }, 86 TrusteeUserID: "ecb37e88cc86431c99d0332208cb6fbf", 87 TrustorUserID: "959ed913a32c4ec88c041c98e61cbbc3", 88 }).Extract() 89 th.AssertNoErr(t, err) 90 91 th.AssertDeepEquals(t, CreatedTrust, *result) 92 } 93 94 func TestCreateTrustNoExpire(t *testing.T) { 95 th.SetupHTTP() 96 defer th.TeardownHTTP() 97 HandleCreateTrustNoExpire(t) 98 99 result, err := trusts.Create(client.ServiceClient(), trusts.CreateOpts{ 100 AllowRedelegation: true, 101 ProjectID: "9b71012f5a4a4aef9193f1995fe159b2", 102 Roles: []trusts.Role{ 103 { 104 Name: "member", 105 }, 106 }, 107 TrusteeUserID: "ecb37e88cc86431c99d0332208cb6fbf", 108 TrustorUserID: "959ed913a32c4ec88c041c98e61cbbc3", 109 }).Extract() 110 th.AssertNoErr(t, err) 111 112 th.AssertDeepEquals(t, CreatedTrustNoExpire, *result) 113 } 114 115 func TestDeleteTrust(t *testing.T) { 116 th.SetupHTTP() 117 defer th.TeardownHTTP() 118 HandleDeleteTrust(t) 119 120 res := trusts.Delete(client.ServiceClient(), "3422b7c113894f5d90665e1a79655e23") 121 th.AssertNoErr(t, res.Err) 122 } 123 124 func TestGetTrust(t *testing.T) { 125 th.SetupHTTP() 126 defer th.TeardownHTTP() 127 HandleGetTrustSuccessfully(t) 128 129 res := trusts.Get(client.ServiceClient(), "987fe8") 130 th.AssertNoErr(t, res.Err) 131 } 132 133 func TestListTrusts(t *testing.T) { 134 th.SetupHTTP() 135 defer th.TeardownHTTP() 136 HandleListTrustsSuccessfully(t) 137 138 count := 0 139 err := trusts.List(client.ServiceClient(), nil).EachPage(func(page pagination.Page) (bool, error) { 140 count++ 141 142 actual, err := trusts.ExtractTrusts(page) 143 th.AssertNoErr(t, err) 144 145 th.CheckDeepEquals(t, ExpectedTrustsSlice, actual) 146 147 return true, nil 148 }) 149 th.AssertNoErr(t, err) 150 th.CheckEquals(t, count, 1) 151 } 152 153 func TestListTrustsAllPages(t *testing.T) { 154 th.SetupHTTP() 155 defer th.TeardownHTTP() 156 HandleListTrustsSuccessfully(t) 157 158 allPages, err := trusts.List(client.ServiceClient(), nil).AllPages() 159 th.AssertNoErr(t, err) 160 actual, err := trusts.ExtractTrusts(allPages) 161 th.AssertNoErr(t, err) 162 th.CheckDeepEquals(t, ExpectedTrustsSlice, actual) 163 } 164 165 func TestListTrustsFiltered(t *testing.T) { 166 th.SetupHTTP() 167 defer th.TeardownHTTP() 168 HandleListTrustsSuccessfully(t) 169 trustsListOpts := trusts.ListOpts{ 170 TrustorUserID: "86c0d5", 171 } 172 allPages, err := trusts.List(client.ServiceClient(), trustsListOpts).AllPages() 173 th.AssertNoErr(t, err) 174 actual, err := trusts.ExtractTrusts(allPages) 175 th.AssertNoErr(t, err) 176 th.CheckDeepEquals(t, ExpectedTrustsSlice, actual) 177 } 178 179 func TestListTrustRoles(t *testing.T) { 180 th.SetupHTTP() 181 defer th.TeardownHTTP() 182 HandleListTrustRolesSuccessfully(t) 183 184 count := 0 185 err := trusts.ListRoles(client.ServiceClient(), "987fe8").EachPage(func(page pagination.Page) (bool, error) { 186 count++ 187 188 actual, err := trusts.ExtractRoles(page) 189 th.AssertNoErr(t, err) 190 191 th.CheckDeepEquals(t, ExpectedTrustRolesSlice, actual) 192 193 return true, nil 194 }) 195 th.AssertNoErr(t, err) 196 th.CheckEquals(t, count, 1) 197 } 198 199 func TestListTrustRolesAllPages(t *testing.T) { 200 th.SetupHTTP() 201 defer th.TeardownHTTP() 202 HandleListTrustRolesSuccessfully(t) 203 204 allPages, err := trusts.ListRoles(client.ServiceClient(), "987fe8").AllPages() 205 th.AssertNoErr(t, err) 206 actual, err := trusts.ExtractRoles(allPages) 207 th.AssertNoErr(t, err) 208 th.CheckDeepEquals(t, ExpectedTrustRolesSlice, actual) 209 } 210 211 func TestGetTrustRole(t *testing.T) { 212 th.SetupHTTP() 213 defer th.TeardownHTTP() 214 HandleGetTrustRoleSuccessfully(t) 215 216 role, err := trusts.GetRole(client.ServiceClient(), "987fe8", "c1648e").Extract() 217 th.AssertNoErr(t, err) 218 219 th.AssertEquals(t, FirstRole, *role) 220 } 221 222 func TestCheckTrustRole(t *testing.T) { 223 th.SetupHTTP() 224 defer th.TeardownHTTP() 225 HandleCheckTrustRoleSuccessfully(t) 226 227 err := trusts.CheckRole(client.ServiceClient(), "987fe8", "c1648e").ExtractErr() 228 th.AssertNoErr(t, err) 229 }