Refactor fixtures
diff --git a/rackspace/db/v1/users/delegate_test.go b/rackspace/db/v1/users/delegate_test.go
index a5da864..c818fe1 100644
--- a/rackspace/db/v1/users/delegate_test.go
+++ b/rackspace/db/v1/users/delegate_test.go
@@ -15,8 +15,7 @@
func TestCreate(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
-
- os.HandleCreateUserSuccessfully(t, instanceID)
+ os.HandleCreate(t)
opts := os.BatchCreateOpts{
os.CreateOpts{
@@ -43,8 +42,7 @@
func TestUserList(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
-
- os.HandleListUsersSuccessfully(t, instanceID)
+ os.HandleList(t)
expectedUsers := []os.User{
os.User{
@@ -77,17 +75,13 @@
})
th.AssertNoErr(t, err)
-
- if pages != 1 {
- t.Errorf("Expected 1 page, saw %d", pages)
- }
+ th.AssertEquals(t, 1, pages)
}
-func TestDeleteInstance(t *testing.T) {
+func TestDelete(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
-
- os.HandleDeleteUserSuccessfully(t, instanceID, "{userName}")
+ os.HandleDelete(t)
res := Delete(fake.ServiceClient(), instanceID, "{userName}")
th.AssertNoErr(t, res.Err)
diff --git a/rackspace/db/v1/users/fixtures.go b/rackspace/db/v1/users/fixtures.go
index 9fdae69..5388123 100644
--- a/rackspace/db/v1/users/fixtures.go
+++ b/rackspace/db/v1/users/fixtures.go
@@ -1,39 +1,8 @@
package users
-import (
- "fmt"
- "net/http"
- "testing"
-
- th "github.com/rackspace/gophercloud/testhelper"
- fake "github.com/rackspace/gophercloud/testhelper/client"
-)
-
const singleDB = `{"databases": [{"name": "databaseE"}]}`
-func setupHandler(t *testing.T, url, method, requestBody, responseBody string, status int) {
- th.Mux.HandleFunc(url, func(w http.ResponseWriter, r *http.Request) {
- th.TestMethod(t, r, method)
- th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
-
- if requestBody != "" {
- th.TestJSONRequest(t, r, requestBody)
- }
-
- w.WriteHeader(status)
-
- if responseBody != "" {
- w.Header().Add("Content-Type", "application/json")
- fmt.Fprintf(w, responseBody)
- }
- })
-}
-
-func HandleChangePasswordSuccessfully(t *testing.T, instanceID string) {
- th.Mux.HandleFunc("/instances/"+instanceID+"/users", func(w http.ResponseWriter, r *http.Request) {
- th.TestMethod(t, r, "PUT")
- th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
- th.TestJSONRequest(t, r, `
+var changePwdReq = `
{
"users": [
{
@@ -46,74 +15,35 @@
}
]
}
-`)
+`
- w.Header().Set("Content-Type", "application/json")
- w.WriteHeader(http.StatusAccepted)
- })
-}
-
-func HandleUpdateSuccessfully(t *testing.T, instanceID, userName string) {
- th.Mux.HandleFunc("/instances/"+instanceID+"/users/"+userName, func(w http.ResponseWriter, r *http.Request) {
- th.TestMethod(t, r, "PUT")
- th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
- th.TestJSONRequest(t, r, `
+var updateReq = `
{
- "user": {
- "name": "new_username",
- "password": "new_password"
- }
+ "user": {
+ "name": "new_username",
+ "password": "new_password"
+ }
}
-`)
+`
- w.Header().Set("Content-Type", "application/json")
- w.WriteHeader(http.StatusAccepted)
- })
-}
-
-func HandleGetSuccessfully(t *testing.T, instanceID, userName string) {
- th.Mux.HandleFunc("/instances/"+instanceID+"/users/"+userName, func(w http.ResponseWriter, r *http.Request) {
- th.TestMethod(t, r, "GET")
- th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
-
- w.Header().Add("Content-Type", "application/json")
-
- fmt.Fprintf(w, `
+var getResp = `
{
- "user": {
- "name": "exampleuser",
- "host": "foo",
- "databases": [
- {
- "name": "databaseA"
- },
- {
- "name": "databaseB"
- }
- ]
- }
+ "user": {
+ "name": "exampleuser",
+ "host": "foo",
+ "databases": [
+ {
+ "name": "databaseA"
+ },
+ {
+ "name": "databaseB"
+ }
+ ]
+ }
}
-`)
- })
-}
+`
-func HandleListUserAccessSuccessfully(t *testing.T, instanceID, userName string) {
- th.Mux.HandleFunc("/instances/"+instanceID+"/users/"+userName+"/databases", func(w http.ResponseWriter, r *http.Request) {
- th.TestMethod(t, r, "GET")
- th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
-
- w.Header().Add("Content-Type", "application/json")
-
- fmt.Fprintf(w, singleDB)
- })
-}
-
-func HandleGrantUserAccessSuccessfully(t *testing.T, instanceID, userName string) {
- url := "/instances/" + instanceID + "/users/" + userName + "/databases"
- setupHandler(t, url, "PUT", singleDB, "", http.StatusAccepted)
-}
-
-func HandleRevokeUserAccessSuccessfully(t *testing.T, instanceID, userName, dbName string) {
- url := "/instances/" + instanceID + "/users/" + userName + "/databases/" + dbName
- setupHandler(t, url, "DELETE", "", "", http.StatusAccepted)
-}
+var (
+ listUserAccessResp = singleDB
+ grantUserAccessReq = singleDB
+)
diff --git a/rackspace/db/v1/users/requests_test.go b/rackspace/db/v1/users/requests_test.go
index 17de21d..f18492d 100644
--- a/rackspace/db/v1/users/requests_test.go
+++ b/rackspace/db/v1/users/requests_test.go
@@ -8,15 +8,20 @@
"github.com/rackspace/gophercloud/pagination"
th "github.com/rackspace/gophercloud/testhelper"
fake "github.com/rackspace/gophercloud/testhelper/client"
+ "github.com/rackspace/gophercloud/testhelper/fixture"
)
-const userName = "{userName}"
+var (
+ userName = "{userName}"
+ _rootURL = "/instances/" + instanceID + "/users"
+ _userURL = _rootURL + "/" + userName
+ _dbURL = _userURL + "/databases"
+)
func TestChangeUserPassword(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
-
- HandleChangePasswordSuccessfully(t, instanceID)
+ fixture.SetupHandler(t, _rootURL, "PUT", changePwdReq, "", 202)
opts := os.BatchCreateOpts{
os.CreateOpts{Name: "dbuser1", Password: "newpassword"},
@@ -24,15 +29,13 @@
}
err := ChangePassword(fake.ServiceClient(), instanceID, opts).ExtractErr()
-
th.AssertNoErr(t, err)
}
func TestUpdateUser(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
-
- HandleUpdateSuccessfully(t, instanceID, userName)
+ fixture.SetupHandler(t, _userURL, "PUT", updateReq, "", 202)
opts := os.CreateOpts{
Name: "new_username",
@@ -40,15 +43,13 @@
}
err := Update(fake.ServiceClient(), instanceID, userName, opts).ExtractErr()
-
th.AssertNoErr(t, err)
}
func TestGetUser(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
-
- HandleGetSuccessfully(t, instanceID, userName)
+ fixture.SetupHandler(t, _userURL, "GET", "", getResp, 200)
user, err := Get(fake.ServiceClient(), instanceID, userName).Extract()
@@ -69,8 +70,7 @@
func TestUserAccessList(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
-
- HandleListUserAccessSuccessfully(t, instanceID, userName)
+ fixture.SetupHandler(t, _userURL+"/databases", "GET", "", listUserAccessResp, 200)
expectedDBs := []db.Database{
db.Database{Name: "databaseE"},
@@ -91,22 +91,15 @@
})
th.AssertNoErr(t, err)
-
- if pages != 1 {
- t.Errorf("Expected 1 page, saw %d", pages)
- }
+ th.AssertEquals(t, 1, pages)
}
func TestGrantAccess(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
+ fixture.SetupHandler(t, _dbURL, "PUT", grantUserAccessReq, "", 202)
- HandleGrantUserAccessSuccessfully(t, instanceID, userName)
-
- opts := db.BatchCreateOpts{
- db.CreateOpts{Name: "databaseE"},
- }
-
+ opts := db.BatchCreateOpts{db.CreateOpts{Name: "databaseE"}}
err := GrantAccess(fake.ServiceClient(), instanceID, userName, opts).ExtractErr()
th.AssertNoErr(t, err)
}
@@ -114,8 +107,7 @@
func TestRevokeAccess(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
-
- HandleRevokeUserAccessSuccessfully(t, instanceID, userName, "{dbName}")
+ fixture.SetupHandler(t, _dbURL+"/{dbName}", "DELETE", "", "", 202)
err := RevokeAccess(fake.ServiceClient(), instanceID, userName, "{dbName}").ExtractErr()
th.AssertNoErr(t, err)