github.com/gophercloud/gophercloud@v1.11.0/internal/acceptance/openstack/identity/v3/users_test.go (about) 1 //go:build acceptance 2 // +build acceptance 3 4 package v3 5 6 import ( 7 "testing" 8 9 "github.com/gophercloud/gophercloud/internal/acceptance/clients" 10 "github.com/gophercloud/gophercloud/internal/acceptance/tools" 11 "github.com/gophercloud/gophercloud/openstack/identity/v3/groups" 12 "github.com/gophercloud/gophercloud/openstack/identity/v3/projects" 13 "github.com/gophercloud/gophercloud/openstack/identity/v3/users" 14 th "github.com/gophercloud/gophercloud/testhelper" 15 ) 16 17 func TestUsersList(t *testing.T) { 18 clients.RequireAdmin(t) 19 20 client, err := clients.NewIdentityV3Client() 21 th.AssertNoErr(t, err) 22 23 var iTrue bool = true 24 listOpts := users.ListOpts{ 25 Enabled: &iTrue, 26 } 27 28 allPages, err := users.List(client, listOpts).AllPages() 29 th.AssertNoErr(t, err) 30 31 allUsers, err := users.ExtractUsers(allPages) 32 th.AssertNoErr(t, err) 33 34 var found bool 35 for _, user := range allUsers { 36 tools.PrintResource(t, user) 37 tools.PrintResource(t, user.Extra) 38 39 if user.Name == "admin" { 40 found = true 41 } 42 } 43 44 th.AssertEquals(t, found, true) 45 46 listOpts.Filters = map[string]string{ 47 "name__contains": "dmi", 48 } 49 50 allPages, err = users.List(client, listOpts).AllPages() 51 th.AssertNoErr(t, err) 52 53 allUsers, err = users.ExtractUsers(allPages) 54 th.AssertNoErr(t, err) 55 56 found = false 57 for _, user := range allUsers { 58 tools.PrintResource(t, user) 59 tools.PrintResource(t, user.Extra) 60 61 if user.Name == "admin" { 62 found = true 63 } 64 } 65 66 th.AssertEquals(t, found, true) 67 68 listOpts.Filters = map[string]string{ 69 "name__contains": "foo", 70 } 71 72 allPages, err = users.List(client, listOpts).AllPages() 73 th.AssertNoErr(t, err) 74 75 allUsers, err = users.ExtractUsers(allPages) 76 th.AssertNoErr(t, err) 77 78 found = false 79 for _, user := range allUsers { 80 tools.PrintResource(t, user) 81 tools.PrintResource(t, user.Extra) 82 83 if user.Name == "admin" { 84 found = true 85 } 86 } 87 88 th.AssertEquals(t, found, false) 89 } 90 91 func TestUsersGet(t *testing.T) { 92 clients.RequireAdmin(t) 93 94 client, err := clients.NewIdentityV3Client() 95 th.AssertNoErr(t, err) 96 97 allPages, err := users.List(client, nil).AllPages() 98 th.AssertNoErr(t, err) 99 100 allUsers, err := users.ExtractUsers(allPages) 101 th.AssertNoErr(t, err) 102 103 user := allUsers[0] 104 p, err := users.Get(client, user.ID).Extract() 105 th.AssertNoErr(t, err) 106 107 tools.PrintResource(t, p) 108 109 th.AssertEquals(t, user.Name, p.Name) 110 } 111 112 func TestUserCRUD(t *testing.T) { 113 clients.RequireAdmin(t) 114 115 client, err := clients.NewIdentityV3Client() 116 th.AssertNoErr(t, err) 117 118 project, err := CreateProject(t, client, nil) 119 th.AssertNoErr(t, err) 120 defer DeleteProject(t, client, project.ID) 121 122 tools.PrintResource(t, project) 123 124 createOpts := users.CreateOpts{ 125 DefaultProjectID: project.ID, 126 Description: "test description", 127 Password: "foobar", 128 DomainID: "default", 129 Options: map[users.Option]interface{}{ 130 users.IgnorePasswordExpiry: true, 131 users.MultiFactorAuthRules: []interface{}{ 132 []string{"password", "totp"}, 133 []string{"password", "custom-auth-method"}, 134 }, 135 }, 136 Extra: map[string]interface{}{ 137 "email": "jsmith@example.com", 138 }, 139 } 140 141 user, err := CreateUser(t, client, &createOpts) 142 th.AssertNoErr(t, err) 143 defer DeleteUser(t, client, user.ID) 144 145 tools.PrintResource(t, user) 146 tools.PrintResource(t, user.Extra) 147 148 th.AssertEquals(t, user.Description, createOpts.Description) 149 th.AssertEquals(t, user.DomainID, createOpts.DomainID) 150 151 iFalse := false 152 name := "newtestuser" 153 description := "" 154 updateOpts := users.UpdateOpts{ 155 Name: name, 156 Description: &description, 157 Enabled: &iFalse, 158 Options: map[users.Option]interface{}{ 159 users.MultiFactorAuthRules: nil, 160 }, 161 Extra: map[string]interface{}{ 162 "disabled_reason": "DDOS", 163 }, 164 } 165 166 newUser, err := users.Update(client, user.ID, updateOpts).Extract() 167 th.AssertNoErr(t, err) 168 169 tools.PrintResource(t, newUser) 170 tools.PrintResource(t, newUser.Extra) 171 172 th.AssertEquals(t, newUser.Name, name) 173 th.AssertEquals(t, newUser.Description, description) 174 th.AssertEquals(t, newUser.Enabled, iFalse) 175 th.AssertEquals(t, newUser.Extra["disabled_reason"], "DDOS") 176 } 177 178 func TestUserChangePassword(t *testing.T) { 179 clients.RequireAdmin(t) 180 181 client, err := clients.NewIdentityV3Client() 182 th.AssertNoErr(t, err) 183 184 createOpts := users.CreateOpts{ 185 Password: "secretsecret", 186 DomainID: "default", 187 } 188 189 user, err := CreateUser(t, client, &createOpts) 190 th.AssertNoErr(t, err) 191 defer DeleteUser(t, client, user.ID) 192 193 tools.PrintResource(t, user) 194 tools.PrintResource(t, user.Extra) 195 196 changePasswordOpts := users.ChangePasswordOpts{ 197 OriginalPassword: "secretsecret", 198 Password: "new_secretsecret", 199 } 200 err = users.ChangePassword(client, user.ID, changePasswordOpts).ExtractErr() 201 th.AssertNoErr(t, err) 202 } 203 204 func TestUsersGroups(t *testing.T) { 205 clients.RequireAdmin(t) 206 207 client, err := clients.NewIdentityV3Client() 208 th.AssertNoErr(t, err) 209 210 createOpts := users.CreateOpts{ 211 Password: "foobar", 212 DomainID: "default", 213 } 214 215 user, err := CreateUser(t, client, &createOpts) 216 th.AssertNoErr(t, err) 217 defer DeleteUser(t, client, user.ID) 218 219 tools.PrintResource(t, user) 220 tools.PrintResource(t, user.Extra) 221 222 createGroupOpts := groups.CreateOpts{ 223 Name: "testgroup", 224 DomainID: "default", 225 } 226 227 // Create Group in the default domain 228 group, err := CreateGroup(t, client, &createGroupOpts) 229 th.AssertNoErr(t, err) 230 defer DeleteGroup(t, client, group.ID) 231 232 tools.PrintResource(t, group) 233 tools.PrintResource(t, group.Extra) 234 235 err = users.AddToGroup(client, group.ID, user.ID).ExtractErr() 236 th.AssertNoErr(t, err) 237 238 allGroupPages, err := users.ListGroups(client, user.ID).AllPages() 239 th.AssertNoErr(t, err) 240 241 allGroups, err := groups.ExtractGroups(allGroupPages) 242 th.AssertNoErr(t, err) 243 244 var found bool 245 for _, g := range allGroups { 246 tools.PrintResource(t, g) 247 tools.PrintResource(t, g.Extra) 248 249 if g.ID == group.ID { 250 found = true 251 } 252 } 253 254 th.AssertEquals(t, found, true) 255 256 found = false 257 allUserPages, err := users.ListInGroup(client, group.ID, nil).AllPages() 258 th.AssertNoErr(t, err) 259 260 allUsers, err := users.ExtractUsers(allUserPages) 261 th.AssertNoErr(t, err) 262 263 for _, u := range allUsers { 264 tools.PrintResource(t, user) 265 tools.PrintResource(t, user.Extra) 266 267 if u.ID == user.ID { 268 found = true 269 } 270 } 271 272 th.AssertEquals(t, found, true) 273 274 ok, err := users.IsMemberOfGroup(client, group.ID, user.ID).Extract() 275 if err != nil { 276 t.Fatalf("Unable to check whether user belongs to group: %v", err) 277 } 278 if !ok { 279 t.Fatalf("User %s is expected to be a member of group %s", user.ID, group.ID) 280 } 281 282 err = users.RemoveFromGroup(client, group.ID, user.ID).ExtractErr() 283 th.AssertNoErr(t, err) 284 285 allGroupPages, err = users.ListGroups(client, user.ID).AllPages() 286 th.AssertNoErr(t, err) 287 288 allGroups, err = groups.ExtractGroups(allGroupPages) 289 th.AssertNoErr(t, err) 290 291 found = false 292 for _, g := range allGroups { 293 tools.PrintResource(t, g) 294 tools.PrintResource(t, g.Extra) 295 296 if g.ID == group.ID { 297 found = true 298 } 299 } 300 301 th.AssertEquals(t, found, false) 302 303 found = false 304 allUserPages, err = users.ListInGroup(client, group.ID, nil).AllPages() 305 th.AssertNoErr(t, err) 306 307 allUsers, err = users.ExtractUsers(allUserPages) 308 th.AssertNoErr(t, err) 309 310 for _, u := range allUsers { 311 tools.PrintResource(t, user) 312 tools.PrintResource(t, user.Extra) 313 314 if u.ID == user.ID { 315 found = true 316 } 317 } 318 319 th.AssertEquals(t, found, false) 320 321 } 322 323 func TestUsersListProjects(t *testing.T) { 324 clients.RequireAdmin(t) 325 326 client, err := clients.NewIdentityV3Client() 327 th.AssertNoErr(t, err) 328 329 allUserPages, err := users.List(client, nil).AllPages() 330 th.AssertNoErr(t, err) 331 332 allUsers, err := users.ExtractUsers(allUserPages) 333 th.AssertNoErr(t, err) 334 335 user := allUsers[0] 336 337 allProjectPages, err := users.ListProjects(client, user.ID).AllPages() 338 th.AssertNoErr(t, err) 339 340 allProjects, err := projects.ExtractProjects(allProjectPages) 341 th.AssertNoErr(t, err) 342 343 for _, project := range allProjects { 344 tools.PrintResource(t, project) 345 } 346 }