blob: 2f2dca700e6869c879fdc962f69e4a2d74bc0d71 [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"
Jamie Hannaforde635b7d2015-02-18 14:11:46 +010011 "github.com/rackspace/gophercloud/testhelper/fixture"
Jamie Hannaford984e9172015-02-13 14:34:03 +010012)
13
Jamie Hannaforde635b7d2015-02-18 14:11:46 +010014var (
15 userName = "{userName}"
16 _rootURL = "/instances/" + instanceID + "/users"
17 _userURL = _rootURL + "/" + userName
18 _dbURL = _userURL + "/databases"
19)
Jamie Hannaford984e9172015-02-13 14:34:03 +010020
21func TestChangeUserPassword(t *testing.T) {
22 th.SetupHTTP()
23 defer th.TeardownHTTP()
Jamie Hannaforde635b7d2015-02-18 14:11:46 +010024 fixture.SetupHandler(t, _rootURL, "PUT", changePwdReq, "", 202)
Jamie Hannaford984e9172015-02-13 14:34:03 +010025
26 opts := os.BatchCreateOpts{
27 os.CreateOpts{Name: "dbuser1", Password: "newpassword"},
28 os.CreateOpts{Name: "dbuser2", Password: "anotherpassword"},
29 }
30
31 err := ChangePassword(fake.ServiceClient(), instanceID, opts).ExtractErr()
Jamie Hannaford984e9172015-02-13 14:34:03 +010032 th.AssertNoErr(t, err)
33}
34
35func TestUpdateUser(t *testing.T) {
36 th.SetupHTTP()
37 defer th.TeardownHTTP()
Jamie Hannaforde635b7d2015-02-18 14:11:46 +010038 fixture.SetupHandler(t, _userURL, "PUT", updateReq, "", 202)
Jamie Hannaford984e9172015-02-13 14:34:03 +010039
Jamie Hannaford52dbcee2015-10-06 16:09:56 +020040 opts := UpdateOpts{
Jamie Hannaford984e9172015-02-13 14:34:03 +010041 Name: "new_username",
42 Password: "new_password",
43 }
44
45 err := Update(fake.ServiceClient(), instanceID, userName, opts).ExtractErr()
Jamie Hannaford984e9172015-02-13 14:34:03 +010046 th.AssertNoErr(t, err)
47}
48
49func TestGetUser(t *testing.T) {
50 th.SetupHTTP()
51 defer th.TeardownHTTP()
Jamie Hannaforde635b7d2015-02-18 14:11:46 +010052 fixture.SetupHandler(t, _userURL, "GET", "", getResp, 200)
Jamie Hannaford984e9172015-02-13 14:34:03 +010053
54 user, err := Get(fake.ServiceClient(), instanceID, userName).Extract()
55
56 th.AssertNoErr(t, err)
57
58 expected := &User{
59 Name: "exampleuser",
60 Host: "foo",
61 Databases: []db.Database{
62 db.Database{Name: "databaseA"},
63 db.Database{Name: "databaseB"},
64 },
65 }
66
67 th.AssertDeepEquals(t, expected, user)
68}
69
70func TestUserAccessList(t *testing.T) {
71 th.SetupHTTP()
72 defer th.TeardownHTTP()
Jamie Hannaforde635b7d2015-02-18 14:11:46 +010073 fixture.SetupHandler(t, _userURL+"/databases", "GET", "", listUserAccessResp, 200)
Jamie Hannaford984e9172015-02-13 14:34:03 +010074
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)
Jamie Hannaforde635b7d2015-02-18 14:11:46 +010094 th.AssertEquals(t, 1, pages)
Jamie Hannaford984e9172015-02-13 14:34:03 +010095}
96
Jamie Hannaford1b2f8cb2015-10-16 12:25:07 +020097func TestUserList(t *testing.T) {
98 th.SetupHTTP()
99 defer th.TeardownHTTP()
100
101 fixture.SetupHandler(t, "/instances/"+instanceID+"/users", "GET", "", listResp, 200)
102
103 expectedUsers := []User{
104 User{
105 Databases: []db.Database{
106 db.Database{Name: "databaseA"},
107 },
108 Name: "dbuser1",
109 Host: "localhost",
110 },
111 User{
112 Databases: []db.Database{
113 db.Database{Name: "databaseB"},
114 db.Database{Name: "databaseC"},
115 },
116 Name: "dbuser2",
117 Host: "localhost",
118 },
119 }
120
121 pages := 0
122 err := List(fake.ServiceClient(), instanceID).EachPage(func(page pagination.Page) (bool, error) {
123 pages++
124
125 actual, err := ExtractUsers(page)
126 if err != nil {
127 return false, err
128 }
129
130 th.CheckDeepEquals(t, expectedUsers, actual)
131
132 return true, nil
133 })
134
135 th.AssertNoErr(t, err)
136 th.AssertEquals(t, 1, pages)
137}
138
Jamie Hannaford984e9172015-02-13 14:34:03 +0100139func TestGrantAccess(t *testing.T) {
140 th.SetupHTTP()
141 defer th.TeardownHTTP()
Jamie Hannaforde635b7d2015-02-18 14:11:46 +0100142 fixture.SetupHandler(t, _dbURL, "PUT", grantUserAccessReq, "", 202)
Jamie Hannaford984e9172015-02-13 14:34:03 +0100143
Jamie Hannaforde635b7d2015-02-18 14:11:46 +0100144 opts := db.BatchCreateOpts{db.CreateOpts{Name: "databaseE"}}
Jamie Hannaford984e9172015-02-13 14:34:03 +0100145 err := GrantAccess(fake.ServiceClient(), instanceID, userName, opts).ExtractErr()
146 th.AssertNoErr(t, err)
147}
148
149func TestRevokeAccess(t *testing.T) {
150 th.SetupHTTP()
151 defer th.TeardownHTTP()
Jamie Hannaforde635b7d2015-02-18 14:11:46 +0100152 fixture.SetupHandler(t, _dbURL+"/{dbName}", "DELETE", "", "", 202)
Jamie Hannaford984e9172015-02-13 14:34:03 +0100153
154 err := RevokeAccess(fake.ServiceClient(), instanceID, userName, "{dbName}").ExtractErr()
155 th.AssertNoErr(t, err)
156}