Jamie Hannaford | 984e917 | 2015-02-13 14:34:03 +0100 | [diff] [blame] | 1 | package users |
| 2 | |
| 3 | import ( |
| 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 Hannaford | e635b7d | 2015-02-18 14:11:46 +0100 | [diff] [blame] | 11 | "github.com/rackspace/gophercloud/testhelper/fixture" |
Jamie Hannaford | 984e917 | 2015-02-13 14:34:03 +0100 | [diff] [blame] | 12 | ) |
| 13 | |
Jamie Hannaford | e635b7d | 2015-02-18 14:11:46 +0100 | [diff] [blame] | 14 | var ( |
| 15 | userName = "{userName}" |
| 16 | _rootURL = "/instances/" + instanceID + "/users" |
| 17 | _userURL = _rootURL + "/" + userName |
| 18 | _dbURL = _userURL + "/databases" |
| 19 | ) |
Jamie Hannaford | 984e917 | 2015-02-13 14:34:03 +0100 | [diff] [blame] | 20 | |
| 21 | func TestChangeUserPassword(t *testing.T) { |
| 22 | th.SetupHTTP() |
| 23 | defer th.TeardownHTTP() |
Jamie Hannaford | e635b7d | 2015-02-18 14:11:46 +0100 | [diff] [blame] | 24 | fixture.SetupHandler(t, _rootURL, "PUT", changePwdReq, "", 202) |
Jamie Hannaford | 984e917 | 2015-02-13 14:34:03 +0100 | [diff] [blame] | 25 | |
| 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 Hannaford | 984e917 | 2015-02-13 14:34:03 +0100 | [diff] [blame] | 32 | th.AssertNoErr(t, err) |
| 33 | } |
| 34 | |
| 35 | func TestUpdateUser(t *testing.T) { |
| 36 | th.SetupHTTP() |
| 37 | defer th.TeardownHTTP() |
Jamie Hannaford | e635b7d | 2015-02-18 14:11:46 +0100 | [diff] [blame] | 38 | fixture.SetupHandler(t, _userURL, "PUT", updateReq, "", 202) |
Jamie Hannaford | 984e917 | 2015-02-13 14:34:03 +0100 | [diff] [blame] | 39 | |
Jamie Hannaford | 52dbcee | 2015-10-06 16:09:56 +0200 | [diff] [blame] | 40 | opts := UpdateOpts{ |
Jamie Hannaford | 984e917 | 2015-02-13 14:34:03 +0100 | [diff] [blame] | 41 | Name: "new_username", |
| 42 | Password: "new_password", |
| 43 | } |
| 44 | |
| 45 | err := Update(fake.ServiceClient(), instanceID, userName, opts).ExtractErr() |
Jamie Hannaford | 984e917 | 2015-02-13 14:34:03 +0100 | [diff] [blame] | 46 | th.AssertNoErr(t, err) |
| 47 | } |
| 48 | |
| 49 | func TestGetUser(t *testing.T) { |
| 50 | th.SetupHTTP() |
| 51 | defer th.TeardownHTTP() |
Jamie Hannaford | e635b7d | 2015-02-18 14:11:46 +0100 | [diff] [blame] | 52 | fixture.SetupHandler(t, _userURL, "GET", "", getResp, 200) |
Jamie Hannaford | 984e917 | 2015-02-13 14:34:03 +0100 | [diff] [blame] | 53 | |
| 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 | |
| 70 | func TestUserAccessList(t *testing.T) { |
| 71 | th.SetupHTTP() |
| 72 | defer th.TeardownHTTP() |
Jamie Hannaford | e635b7d | 2015-02-18 14:11:46 +0100 | [diff] [blame] | 73 | fixture.SetupHandler(t, _userURL+"/databases", "GET", "", listUserAccessResp, 200) |
Jamie Hannaford | 984e917 | 2015-02-13 14:34:03 +0100 | [diff] [blame] | 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) |
Jamie Hannaford | e635b7d | 2015-02-18 14:11:46 +0100 | [diff] [blame] | 94 | th.AssertEquals(t, 1, pages) |
Jamie Hannaford | 984e917 | 2015-02-13 14:34:03 +0100 | [diff] [blame] | 95 | } |
| 96 | |
Jamie Hannaford | 1b2f8cb | 2015-10-16 12:25:07 +0200 | [diff] [blame] | 97 | func 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 Hannaford | 984e917 | 2015-02-13 14:34:03 +0100 | [diff] [blame] | 139 | func TestGrantAccess(t *testing.T) { |
| 140 | th.SetupHTTP() |
| 141 | defer th.TeardownHTTP() |
Jamie Hannaford | e635b7d | 2015-02-18 14:11:46 +0100 | [diff] [blame] | 142 | fixture.SetupHandler(t, _dbURL, "PUT", grantUserAccessReq, "", 202) |
Jamie Hannaford | 984e917 | 2015-02-13 14:34:03 +0100 | [diff] [blame] | 143 | |
Jamie Hannaford | e635b7d | 2015-02-18 14:11:46 +0100 | [diff] [blame] | 144 | opts := db.BatchCreateOpts{db.CreateOpts{Name: "databaseE"}} |
Jamie Hannaford | 984e917 | 2015-02-13 14:34:03 +0100 | [diff] [blame] | 145 | err := GrantAccess(fake.ServiceClient(), instanceID, userName, opts).ExtractErr() |
| 146 | th.AssertNoErr(t, err) |
| 147 | } |
| 148 | |
| 149 | func TestRevokeAccess(t *testing.T) { |
| 150 | th.SetupHTTP() |
| 151 | defer th.TeardownHTTP() |
Jamie Hannaford | e635b7d | 2015-02-18 14:11:46 +0100 | [diff] [blame] | 152 | fixture.SetupHandler(t, _dbURL+"/{dbName}", "DELETE", "", "", 202) |
Jamie Hannaford | 984e917 | 2015-02-13 14:34:03 +0100 | [diff] [blame] | 153 | |
| 154 | err := RevokeAccess(fake.ServiceClient(), instanceID, userName, "{dbName}").ExtractErr() |
| 155 | th.AssertNoErr(t, err) |
| 156 | } |