blob: 17de21db6ace48bad710b86694ba3990155951a6 [file] [log] [blame]
Jamie Hannaford984e9172015-02-13 14:34:03 +01001package users
2
3import (
4 "testing"
5
6 db "github.com/rackspace/gophercloud/openstack/db/v1/databases"
7 os "github.com/rackspace/gophercloud/openstack/db/v1/users"
8 "github.com/rackspace/gophercloud/pagination"
9 th "github.com/rackspace/gophercloud/testhelper"
10 fake "github.com/rackspace/gophercloud/testhelper/client"
11)
12
13const userName = "{userName}"
14
15func TestChangeUserPassword(t *testing.T) {
16 th.SetupHTTP()
17 defer th.TeardownHTTP()
18
19 HandleChangePasswordSuccessfully(t, instanceID)
20
21 opts := os.BatchCreateOpts{
22 os.CreateOpts{Name: "dbuser1", Password: "newpassword"},
23 os.CreateOpts{Name: "dbuser2", Password: "anotherpassword"},
24 }
25
26 err := ChangePassword(fake.ServiceClient(), instanceID, opts).ExtractErr()
27
28 th.AssertNoErr(t, err)
29}
30
31func TestUpdateUser(t *testing.T) {
32 th.SetupHTTP()
33 defer th.TeardownHTTP()
34
35 HandleUpdateSuccessfully(t, instanceID, userName)
36
37 opts := os.CreateOpts{
38 Name: "new_username",
39 Password: "new_password",
40 }
41
42 err := Update(fake.ServiceClient(), instanceID, userName, opts).ExtractErr()
43
44 th.AssertNoErr(t, err)
45}
46
47func TestGetUser(t *testing.T) {
48 th.SetupHTTP()
49 defer th.TeardownHTTP()
50
51 HandleGetSuccessfully(t, instanceID, userName)
52
53 user, err := Get(fake.ServiceClient(), instanceID, userName).Extract()
54
55 th.AssertNoErr(t, err)
56
57 expected := &User{
58 Name: "exampleuser",
59 Host: "foo",
60 Databases: []db.Database{
61 db.Database{Name: "databaseA"},
62 db.Database{Name: "databaseB"},
63 },
64 }
65
66 th.AssertDeepEquals(t, expected, user)
67}
68
69func TestUserAccessList(t *testing.T) {
70 th.SetupHTTP()
71 defer th.TeardownHTTP()
72
73 HandleListUserAccessSuccessfully(t, instanceID, userName)
74
75 expectedDBs := []db.Database{
76 db.Database{Name: "databaseE"},
77 }
78
79 pages := 0
80 err := ListAccess(fake.ServiceClient(), instanceID, userName).EachPage(func(page pagination.Page) (bool, error) {
81 pages++
82
83 actual, err := ExtractDBs(page)
84 if err != nil {
85 return false, err
86 }
87
88 th.CheckDeepEquals(t, expectedDBs, actual)
89
90 return true, nil
91 })
92
93 th.AssertNoErr(t, err)
94
95 if pages != 1 {
96 t.Errorf("Expected 1 page, saw %d", pages)
97 }
98}
99
100func TestGrantAccess(t *testing.T) {
101 th.SetupHTTP()
102 defer th.TeardownHTTP()
103
104 HandleGrantUserAccessSuccessfully(t, instanceID, userName)
105
106 opts := db.BatchCreateOpts{
107 db.CreateOpts{Name: "databaseE"},
108 }
109
110 err := GrantAccess(fake.ServiceClient(), instanceID, userName, opts).ExtractErr()
111 th.AssertNoErr(t, err)
112}
113
114func TestRevokeAccess(t *testing.T) {
115 th.SetupHTTP()
116 defer th.TeardownHTTP()
117
118 HandleRevokeUserAccessSuccessfully(t, instanceID, userName, "{dbName}")
119
120 err := RevokeAccess(fake.ServiceClient(), instanceID, userName, "{dbName}").ExtractErr()
121 th.AssertNoErr(t, err)
122}