blob: 5589b1b2214e1c9f9445567075f59ef57574c6ce [file] [log] [blame]
Jamie Hannafordcebff1b2014-10-30 14:31:44 +01001// +build acceptance identity roles
2
3package v2
4
5import (
6 "testing"
7
Joe Topjian918f5732016-08-15 08:47:08 -06008 "github.com/gophercloud/gophercloud/acceptance/clients"
Jon Perritt27249f42016-02-18 10:35:59 -06009 "github.com/gophercloud/gophercloud/openstack/identity/v2/extensions/admin/roles"
Joe Topjian918f5732016-08-15 08:47:08 -060010 "github.com/gophercloud/gophercloud/openstack/identity/v2/users"
Jamie Hannafordcebff1b2014-10-30 14:31:44 +010011)
12
Joe Topjian918f5732016-08-15 08:47:08 -060013func TestRolesAddToUser(t *testing.T) {
14 client, err := clients.NewIdentityV2AdminClient()
15 if err != nil {
16 t.Fatalf("Unable to obtain an identity client: %v", err)
17 }
Jamie Hannafordcebff1b2014-10-30 14:31:44 +010018
Joe Topjian918f5732016-08-15 08:47:08 -060019 tenant, err := FindTenant(t, client)
20 if err != nil {
21 t.Fatalf("Unable to get a tenant: %v", err)
22 }
Jamie Hannafordcebff1b2014-10-30 14:31:44 +010023
Joe Topjian918f5732016-08-15 08:47:08 -060024 role, err := FindRole(t, client)
25 if err != nil {
26 t.Fatalf("Unable to get a role: %v", err)
27 }
Jamie Hannafordcebff1b2014-10-30 14:31:44 +010028
Joe Topjian918f5732016-08-15 08:47:08 -060029 user, err := CreateUser(t, client, tenant)
30 if err != nil {
31 t.Fatalf("Unable to create a user: %v", err)
32 }
33 defer DeleteUser(t, client, user)
Jamie Hannafordcebff1b2014-10-30 14:31:44 +010034
Joe Topjian918f5732016-08-15 08:47:08 -060035 err = AddUserRole(t, client, tenant, user, role)
36 if err != nil {
37 t.Fatalf("Unable to add role to user: %v", err)
38 }
39 defer DeleteUserRole(t, client, tenant, user, role)
40
41 allPages, err := users.ListRoles(client, tenant.ID, user.ID).AllPages()
42 if err != nil {
43 t.Fatalf("Unable to obtain roles for user: %v", err)
44 }
45
46 allRoles, err := users.ExtractRoles(allPages)
47 if err != nil {
48 t.Fatalf("Unable to extract roles: %v", err)
49 }
50
51 t.Logf("Roles of user %s:", user.Name)
52 for _, role := range allRoles {
53 PrintUserRole(t, &role)
54 }
Jamie Hannafordcebff1b2014-10-30 14:31:44 +010055}
56
Joe Topjian918f5732016-08-15 08:47:08 -060057func TestRolesList(t *testing.T) {
58 client, err := clients.NewIdentityV2AdminClient()
59 if err != nil {
60 t.Fatalf("Unable to create an identity client: %v", err)
61 }
Jamie Hannafordcebff1b2014-10-30 14:31:44 +010062
Joe Topjian918f5732016-08-15 08:47:08 -060063 allPages, err := roles.List(client).AllPages()
64 if err != nil {
65 t.Fatalf("Unable to list all roles: %v", err)
66 }
Jamie Hannafordcebff1b2014-10-30 14:31:44 +010067
Joe Topjian918f5732016-08-15 08:47:08 -060068 allRoles, err := roles.ExtractRoles(allPages)
69 if err != nil {
70 t.Fatalf("Unable to extract roles: %v", err)
71 }
Jamie Hannafordcebff1b2014-10-30 14:31:44 +010072
Joe Topjian918f5732016-08-15 08:47:08 -060073 for _, r := range allRoles {
74 PrintRole(t, &r)
75 }
Jamie Hannafordcebff1b2014-10-30 14:31:44 +010076}