Refactor to use new handler setup
diff --git a/openstack/db/v1/users/fixtures.go b/openstack/db/v1/users/fixtures.go
index 6bada41..25b75f1 100644
--- a/openstack/db/v1/users/fixtures.go
+++ b/openstack/db/v1/users/fixtures.go
@@ -1,91 +1,21 @@
package users
-import (
- "fmt"
- "net/http"
- "testing"
+import "fmt"
- th "github.com/rackspace/gophercloud/testhelper"
- fake "github.com/rackspace/gophercloud/testhelper/client"
+const user1 = `
+{"databases": [{"name": "databaseA"}],"name": "dbuser3"%s}
+`
+
+const user2 = `
+{"databases": [{"name": "databaseB"},{"name": "databaseC"}],"name": "dbuser4"%s}
+`
+
+var (
+ pUser1 = fmt.Sprintf(user1, `,"password":"secretsecret"`)
+ pUser2 = fmt.Sprintf(user2, `,"password":"secretsecret"`)
)
-func HandleCreateUserSuccessfully(t *testing.T, instanceID string) {
- th.Mux.HandleFunc("/instances/"+instanceID+"/users", func(w http.ResponseWriter, r *http.Request) {
- th.TestMethod(t, r, "POST")
- th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
- th.TestJSONRequest(t, r, `
-{
- "users": [
- {
- "databases": [
- {
- "name": "databaseA"
- }
- ],
- "name": "dbuser3",
- "password": "secretsecret"
- },
- {
- "databases": [
- {
- "name": "databaseB"
- },
- {
- "name": "databaseC"
- }
- ],
- "name": "dbuser4",
- "password": "secretsecret"
- }
- ]
-}
-`)
-
- w.Header().Set("Content-Type", "application/json")
- w.WriteHeader(http.StatusAccepted)
- })
-}
-
-func HandleListUsersSuccessfully(t *testing.T, instanceID string) {
- th.Mux.HandleFunc("/instances/"+instanceID+"/users", func(w http.ResponseWriter, r *http.Request) {
- th.TestMethod(t, r, "GET")
- th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
-
- w.Header().Set("Content-Type", "application/json")
- w.WriteHeader(http.StatusOK)
-
- fmt.Fprintf(w, `
-{
- "users": [
- {
- "databases": [
- {
- "name": "databaseA"
- }
- ],
- "name": "dbuser3"
- },
- {
- "databases": [
- {
- "name": "databaseB"
- },
- {
- "name": "databaseC"
- }
- ],
- "name": "dbuser4"
- }
- ]
-}
-`)
- })
-}
-
-func HandleDeleteUserSuccessfully(t *testing.T, instanceID, dbName string) {
- th.Mux.HandleFunc("/instances/"+instanceID+"/users/"+dbName, func(w http.ResponseWriter, r *http.Request) {
- th.TestMethod(t, r, "DELETE")
- th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
- w.WriteHeader(http.StatusAccepted)
- })
-}
+var (
+ createReq = fmt.Sprintf(`{"users":[%s, %s]}`, pUser1, pUser2)
+ listResp = fmt.Sprintf(`{"users":[%s, %s]}`, fmt.Sprintf(user1, ""), fmt.Sprintf(user2, ""))
+)
diff --git a/openstack/db/v1/users/requests_test.go b/openstack/db/v1/users/requests_test.go
index 00c4e58..47eccea 100644
--- a/openstack/db/v1/users/requests_test.go
+++ b/openstack/db/v1/users/requests_test.go
@@ -7,15 +7,18 @@
"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 instanceID = "{instanceID}"
+var (
+ instanceID = "{instanceID}"
+ _rootURL = "/instances/" + instanceID + "/users"
+)
func TestCreate(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
-
- HandleCreateUserSuccessfully(t, instanceID)
+ fixture.SetupHandler(t, _rootURL, "POST", createReq, "", 202)
opts := BatchCreateOpts{
CreateOpts{
@@ -42,8 +45,7 @@
func TestUserList(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
-
- HandleListUsersSuccessfully(t, instanceID)
+ fixture.SetupHandler(t, _rootURL, "GET", "", listResp, 200)
expectedUsers := []User{
User{
@@ -71,22 +73,17 @@
}
th.CheckDeepEquals(t, expectedUsers, actual)
-
return true, nil
})
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) {
th.SetupHTTP()
defer th.TeardownHTTP()
-
- HandleDeleteUserSuccessfully(t, instanceID, "{dbName}")
+ fixture.SetupHandler(t, _rootURL+"/{dbName}", "DELETE", "", "", 202)
res := Delete(fake.ServiceClient(), instanceID, "{dbName}")
th.AssertNoErr(t, res.Err)