blob: f18492d744a79a81d13255505b60f51958319bd0 [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
40 opts := os.CreateOpts{
41 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
97func TestGrantAccess(t *testing.T) {
98 th.SetupHTTP()
99 defer th.TeardownHTTP()
Jamie Hannaforde635b7d2015-02-18 14:11:46 +0100100 fixture.SetupHandler(t, _dbURL, "PUT", grantUserAccessReq, "", 202)
Jamie Hannaford984e9172015-02-13 14:34:03 +0100101
Jamie Hannaforde635b7d2015-02-18 14:11:46 +0100102 opts := db.BatchCreateOpts{db.CreateOpts{Name: "databaseE"}}
Jamie Hannaford984e9172015-02-13 14:34:03 +0100103 err := GrantAccess(fake.ServiceClient(), instanceID, userName, opts).ExtractErr()
104 th.AssertNoErr(t, err)
105}
106
107func TestRevokeAccess(t *testing.T) {
108 th.SetupHTTP()
109 defer th.TeardownHTTP()
Jamie Hannaforde635b7d2015-02-18 14:11:46 +0100110 fixture.SetupHandler(t, _dbURL+"/{dbName}", "DELETE", "", "", 202)
Jamie Hannaford984e9172015-02-13 14:34:03 +0100111
112 err := RevokeAccess(fake.ServiceClient(), instanceID, userName, "{dbName}").ExtractErr()
113 th.AssertNoErr(t, err)
114}