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 | |
| 40 | opts := os.CreateOpts{ |
| 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 | |
| 97 | func TestGrantAccess(t *testing.T) { |
| 98 | th.SetupHTTP() |
| 99 | defer th.TeardownHTTP() |
Jamie Hannaford | e635b7d | 2015-02-18 14:11:46 +0100 | [diff] [blame^] | 100 | fixture.SetupHandler(t, _dbURL, "PUT", grantUserAccessReq, "", 202) |
Jamie Hannaford | 984e917 | 2015-02-13 14:34:03 +0100 | [diff] [blame] | 101 | |
Jamie Hannaford | e635b7d | 2015-02-18 14:11:46 +0100 | [diff] [blame^] | 102 | opts := db.BatchCreateOpts{db.CreateOpts{Name: "databaseE"}} |
Jamie Hannaford | 984e917 | 2015-02-13 14:34:03 +0100 | [diff] [blame] | 103 | err := GrantAccess(fake.ServiceClient(), instanceID, userName, opts).ExtractErr() |
| 104 | th.AssertNoErr(t, err) |
| 105 | } |
| 106 | |
| 107 | func TestRevokeAccess(t *testing.T) { |
| 108 | th.SetupHTTP() |
| 109 | defer th.TeardownHTTP() |
Jamie Hannaford | e635b7d | 2015-02-18 14:11:46 +0100 | [diff] [blame^] | 110 | fixture.SetupHandler(t, _dbURL+"/{dbName}", "DELETE", "", "", 202) |
Jamie Hannaford | 984e917 | 2015-02-13 14:34:03 +0100 | [diff] [blame] | 111 | |
| 112 | err := RevokeAccess(fake.ServiceClient(), instanceID, userName, "{dbName}").ExtractErr() |
| 113 | th.AssertNoErr(t, err) |
| 114 | } |