Jamie Hannaford | 984e917 | 2015-02-13 14:34:03 +0100 | [diff] [blame] | 1 | package users |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "net/http" |
| 6 | "testing" |
| 7 | |
| 8 | th "github.com/rackspace/gophercloud/testhelper" |
| 9 | fake "github.com/rackspace/gophercloud/testhelper/client" |
| 10 | ) |
| 11 | |
| 12 | const singleDB = `{"databases": [{"name": "databaseE"}]}` |
| 13 | |
| 14 | func setupHandler(t *testing.T, url, method, requestBody, responseBody string, status int) { |
| 15 | th.Mux.HandleFunc(url, func(w http.ResponseWriter, r *http.Request) { |
| 16 | th.TestMethod(t, r, method) |
| 17 | th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) |
| 18 | |
| 19 | if requestBody != "" { |
| 20 | th.TestJSONRequest(t, r, requestBody) |
| 21 | } |
| 22 | |
| 23 | w.WriteHeader(status) |
| 24 | |
| 25 | if responseBody != "" { |
| 26 | w.Header().Add("Content-Type", "application/json") |
| 27 | fmt.Fprintf(w, responseBody) |
| 28 | } |
| 29 | }) |
| 30 | } |
| 31 | |
| 32 | func HandleChangePasswordSuccessfully(t *testing.T, instanceID string) { |
| 33 | th.Mux.HandleFunc("/instances/"+instanceID+"/users", func(w http.ResponseWriter, r *http.Request) { |
| 34 | th.TestMethod(t, r, "PUT") |
| 35 | th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) |
| 36 | th.TestJSONRequest(t, r, ` |
| 37 | { |
| 38 | "users": [ |
| 39 | { |
| 40 | "name": "dbuser1", |
| 41 | "password": "newpassword" |
| 42 | }, |
| 43 | { |
| 44 | "name": "dbuser2", |
| 45 | "password": "anotherpassword" |
| 46 | } |
| 47 | ] |
| 48 | } |
| 49 | `) |
| 50 | |
| 51 | w.Header().Set("Content-Type", "application/json") |
| 52 | w.WriteHeader(http.StatusAccepted) |
| 53 | }) |
| 54 | } |
| 55 | |
| 56 | func HandleUpdateSuccessfully(t *testing.T, instanceID, userName string) { |
| 57 | th.Mux.HandleFunc("/instances/"+instanceID+"/users/"+userName, func(w http.ResponseWriter, r *http.Request) { |
| 58 | th.TestMethod(t, r, "PUT") |
| 59 | th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) |
| 60 | th.TestJSONRequest(t, r, ` |
| 61 | { |
| 62 | "user": { |
| 63 | "name": "new_username", |
| 64 | "password": "new_password" |
| 65 | } |
| 66 | } |
| 67 | `) |
| 68 | |
| 69 | w.Header().Set("Content-Type", "application/json") |
| 70 | w.WriteHeader(http.StatusAccepted) |
| 71 | }) |
| 72 | } |
| 73 | |
| 74 | func HandleGetSuccessfully(t *testing.T, instanceID, userName string) { |
| 75 | th.Mux.HandleFunc("/instances/"+instanceID+"/users/"+userName, func(w http.ResponseWriter, r *http.Request) { |
| 76 | th.TestMethod(t, r, "GET") |
| 77 | th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) |
| 78 | |
| 79 | w.Header().Add("Content-Type", "application/json") |
| 80 | |
| 81 | fmt.Fprintf(w, ` |
| 82 | { |
| 83 | "user": { |
| 84 | "name": "exampleuser", |
| 85 | "host": "foo", |
| 86 | "databases": [ |
| 87 | { |
| 88 | "name": "databaseA" |
| 89 | }, |
| 90 | { |
| 91 | "name": "databaseB" |
| 92 | } |
| 93 | ] |
| 94 | } |
| 95 | } |
| 96 | `) |
| 97 | }) |
| 98 | } |
| 99 | |
| 100 | func HandleListUserAccessSuccessfully(t *testing.T, instanceID, userName string) { |
| 101 | th.Mux.HandleFunc("/instances/"+instanceID+"/users/"+userName+"/databases", func(w http.ResponseWriter, r *http.Request) { |
| 102 | th.TestMethod(t, r, "GET") |
| 103 | th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) |
| 104 | |
| 105 | w.Header().Add("Content-Type", "application/json") |
| 106 | |
| 107 | fmt.Fprintf(w, singleDB) |
| 108 | }) |
| 109 | } |
| 110 | |
| 111 | func HandleGrantUserAccessSuccessfully(t *testing.T, instanceID, userName string) { |
| 112 | url := "/instances/" + instanceID + "/users/" + userName + "/databases" |
| 113 | setupHandler(t, url, "PUT", singleDB, "", http.StatusAccepted) |
| 114 | } |
| 115 | |
| 116 | func HandleRevokeUserAccessSuccessfully(t *testing.T, instanceID, userName, dbName string) { |
| 117 | url := "/instances/" + instanceID + "/users/" + userName + "/databases/" + dbName |
| 118 | setupHandler(t, url, "DELETE", "", "", http.StatusAccepted) |
| 119 | } |