blob: ba243fe02bf1926f9835c84a170df5ba5ade8bc3 [file] [log] [blame]
Jamie Hannafordcebff1b2014-10-30 14:31:44 +01001// +build acceptance identity roles
2
3package v2
4
5import (
6 "testing"
7
8 "github.com/rackspace/gophercloud"
9 "github.com/rackspace/gophercloud/openstack/identity/v2/extensions/admin/roles"
10 "github.com/rackspace/gophercloud/pagination"
11 th "github.com/rackspace/gophercloud/testhelper"
12)
13
14func TestRoles(t *testing.T) {
15 client := authenticatedClient(t)
16
17 tenantID := findTenant(t, client)
18 userID := createUser(t, client, tenantID)
19 roleID := listRoles(t, client)
20
21 addUserRole(t, client, tenantID, userID, roleID)
22
23 deleteUserRole(t, client, tenantID, userID, roleID)
24
25 deleteUser(t, client, userID)
26}
27
28func listRoles(t *testing.T, client *gophercloud.ServiceClient) string {
29 var roleID string
30
31 err := roles.List(client).EachPage(func(page pagination.Page) (bool, error) {
32 roleList, err := roles.ExtractRoles(page)
33 th.AssertNoErr(t, err)
34
35 for _, role := range roleList {
36 t.Logf("Listing role: ID [%s] Name [%s]", role.ID, role.Name)
37 roleID = role.ID
38 }
39
40 return true, nil
41 })
42
43 th.AssertNoErr(t, err)
44
45 return roleID
46}
47
48func addUserRole(t *testing.T, client *gophercloud.ServiceClient, tenantID, userID, roleID string) {
49 err := roles.AddUserRole(client, tenantID, userID, roleID).ExtractErr()
50 th.AssertNoErr(t, err)
51 t.Logf("Added role %s to user %s", roleID, userID)
52}
53
54func deleteUserRole(t *testing.T, client *gophercloud.ServiceClient, tenantID, userID, roleID string) {
55 err := roles.DeleteUserRole(client, tenantID, userID, roleID).ExtractErr()
56 th.AssertNoErr(t, err)
57 t.Logf("Removed role %s from user %s", roleID, userID)
58}