Jamie Hannaford | 2a13024 | 2014-10-28 11:19:46 +0100 | [diff] [blame] | 1 | package users |
Jamie Hannaford | 6a076d8 | 2014-10-29 10:57:36 +0100 | [diff] [blame] | 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 | |
Jamie Hannaford | 10cf2bd | 2014-10-30 12:21:14 +0100 | [diff] [blame] | 12 | func MockListUserResponse(t *testing.T) { |
Jamie Hannaford | 6a076d8 | 2014-10-29 10:57:36 +0100 | [diff] [blame] | 13 | th.Mux.HandleFunc("/users", func(w http.ResponseWriter, r *http.Request) { |
| 14 | th.TestMethod(t, r, "GET") |
| 15 | th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) |
| 16 | |
| 17 | w.Header().Add("Content-Type", "application/json") |
| 18 | w.WriteHeader(http.StatusOK) |
| 19 | |
| 20 | fmt.Fprintf(w, ` |
| 21 | { |
| 22 | "users":[ |
| 23 | { |
| 24 | "id": "u1000", |
Jamie Hannaford | 929bd00 | 2014-10-29 11:14:25 +0100 | [diff] [blame] | 25 | "name": "John Smith", |
Jamie Hannaford | 6a076d8 | 2014-10-29 10:57:36 +0100 | [diff] [blame] | 26 | "username": "jqsmith", |
| 27 | "email": "john.smith@example.org", |
Jamie Hannaford | 929bd00 | 2014-10-29 11:14:25 +0100 | [diff] [blame] | 28 | "enabled": true, |
| 29 | "tenant_id": "12345" |
Jamie Hannaford | 6a076d8 | 2014-10-29 10:57:36 +0100 | [diff] [blame] | 30 | }, |
| 31 | { |
| 32 | "id": "u1001", |
Jamie Hannaford | 929bd00 | 2014-10-29 11:14:25 +0100 | [diff] [blame] | 33 | "name": "Jane Smith", |
Jamie Hannaford | 6a076d8 | 2014-10-29 10:57:36 +0100 | [diff] [blame] | 34 | "username": "jqsmith", |
Jamie Hannaford | 929bd00 | 2014-10-29 11:14:25 +0100 | [diff] [blame] | 35 | "email": "jane.smith@example.org", |
| 36 | "enabled": true, |
| 37 | "tenant_id": "12345" |
Jamie Hannaford | 6a076d8 | 2014-10-29 10:57:36 +0100 | [diff] [blame] | 38 | } |
| 39 | ] |
| 40 | } |
| 41 | `) |
| 42 | }) |
| 43 | } |
Jamie Hannaford | 9c7bb8e | 2014-10-29 11:47:34 +0100 | [diff] [blame] | 44 | |
Jamie Hannaford | 10cf2bd | 2014-10-30 12:21:14 +0100 | [diff] [blame] | 45 | func mockCreateUserResponse(t *testing.T) { |
Jamie Hannaford | 9c7bb8e | 2014-10-29 11:47:34 +0100 | [diff] [blame] | 46 | th.Mux.HandleFunc("/users", func(w http.ResponseWriter, r *http.Request) { |
| 47 | th.TestMethod(t, r, "POST") |
| 48 | th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) |
| 49 | |
Jamie Hannaford | 7e04adf | 2014-10-29 13:47:58 +0100 | [diff] [blame] | 50 | th.TestJSONRequest(t, r, ` |
| 51 | { |
| 52 | "user": { |
| 53 | "name": "new_user", |
| 54 | "tenant_id": "12345", |
| 55 | "enabled": false, |
| 56 | "email": "new_user@foo.com" |
| 57 | } |
| 58 | } |
| 59 | `) |
| 60 | |
Jamie Hannaford | 9c7bb8e | 2014-10-29 11:47:34 +0100 | [diff] [blame] | 61 | w.Header().Add("Content-Type", "application/json") |
| 62 | w.WriteHeader(http.StatusOK) |
| 63 | |
| 64 | fmt.Fprintf(w, ` |
| 65 | { |
| 66 | "user": { |
| 67 | "name": "new_user", |
| 68 | "tenant_id": "12345", |
| 69 | "enabled": false, |
| 70 | "email": "new_user@foo.com", |
| 71 | "id": "c39e3de9be2d4c779f1dfd6abacc176d" |
| 72 | } |
| 73 | } |
| 74 | `) |
| 75 | }) |
| 76 | } |
Jamie Hannaford | 2ad98bd | 2014-10-29 13:26:47 +0100 | [diff] [blame] | 77 | |
Jamie Hannaford | 10cf2bd | 2014-10-30 12:21:14 +0100 | [diff] [blame] | 78 | func mockGetUserResponse(t *testing.T) { |
Jamie Hannaford | 2ad98bd | 2014-10-29 13:26:47 +0100 | [diff] [blame] | 79 | th.Mux.HandleFunc("/users/new_user", func(w http.ResponseWriter, r *http.Request) { |
| 80 | th.TestMethod(t, r, "GET") |
| 81 | th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) |
| 82 | |
| 83 | w.Header().Add("Content-Type", "application/json") |
| 84 | w.WriteHeader(http.StatusOK) |
| 85 | |
| 86 | fmt.Fprintf(w, ` |
| 87 | { |
| 88 | "user": { |
| 89 | "name": "new_user", |
| 90 | "tenant_id": "12345", |
| 91 | "enabled": false, |
| 92 | "email": "new_user@foo.com", |
| 93 | "id": "c39e3de9be2d4c779f1dfd6abacc176d" |
| 94 | } |
| 95 | } |
| 96 | `) |
| 97 | }) |
| 98 | } |
Jamie Hannaford | 7e04adf | 2014-10-29 13:47:58 +0100 | [diff] [blame] | 99 | |
Jamie Hannaford | 10cf2bd | 2014-10-30 12:21:14 +0100 | [diff] [blame] | 100 | func mockUpdateUserResponse(t *testing.T) { |
Jamie Hannaford | 7e04adf | 2014-10-29 13:47:58 +0100 | [diff] [blame] | 101 | th.Mux.HandleFunc("/users/c39e3de9be2d4c779f1dfd6abacc176d", func(w http.ResponseWriter, r *http.Request) { |
| 102 | th.TestMethod(t, r, "PUT") |
| 103 | th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) |
| 104 | |
| 105 | th.TestJSONRequest(t, r, ` |
| 106 | { |
| 107 | "user": { |
| 108 | "name": "new_name", |
| 109 | "enabled": true, |
| 110 | "email": "new_email@foo.com" |
| 111 | } |
| 112 | } |
| 113 | `) |
| 114 | |
| 115 | w.Header().Add("Content-Type", "application/json") |
| 116 | w.WriteHeader(http.StatusOK) |
| 117 | |
| 118 | fmt.Fprintf(w, ` |
| 119 | { |
| 120 | "user": { |
| 121 | "name": "new_name", |
| 122 | "tenant_id": "12345", |
| 123 | "enabled": true, |
| 124 | "email": "new_email@foo.com", |
| 125 | "id": "c39e3de9be2d4c779f1dfd6abacc176d" |
| 126 | } |
| 127 | } |
| 128 | `) |
| 129 | }) |
| 130 | } |
Jamie Hannaford | 8b9a800 | 2014-10-29 14:20:24 +0100 | [diff] [blame] | 131 | |
Jamie Hannaford | 10cf2bd | 2014-10-30 12:21:14 +0100 | [diff] [blame] | 132 | func mockDeleteUserResponse(t *testing.T) { |
Jamie Hannaford | 8b9a800 | 2014-10-29 14:20:24 +0100 | [diff] [blame] | 133 | th.Mux.HandleFunc("/users/c39e3de9be2d4c779f1dfd6abacc176d", func(w http.ResponseWriter, r *http.Request) { |
| 134 | th.TestMethod(t, r, "DELETE") |
| 135 | th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) |
| 136 | w.WriteHeader(http.StatusNoContent) |
| 137 | }) |
| 138 | } |
Jamie Hannaford | e680e42 | 2014-10-29 14:55:57 +0100 | [diff] [blame] | 139 | |
Jamie Hannaford | 10cf2bd | 2014-10-30 12:21:14 +0100 | [diff] [blame] | 140 | func mockListRolesResponse(t *testing.T) { |
Jamie Hannaford | b136b18 | 2014-10-30 15:20:10 +0100 | [diff] [blame] | 141 | th.Mux.HandleFunc("/tenants/1d8b6120dcc640fda4fc9194ffc80273/users/c39e3de9be2d4c779f1dfd6abacc176d/roles", func(w http.ResponseWriter, r *http.Request) { |
Jamie Hannaford | e680e42 | 2014-10-29 14:55:57 +0100 | [diff] [blame] | 142 | th.TestMethod(t, r, "GET") |
| 143 | th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) |
| 144 | |
| 145 | w.Header().Add("Content-Type", "application/json") |
| 146 | w.WriteHeader(http.StatusOK) |
| 147 | |
| 148 | fmt.Fprintf(w, ` |
| 149 | { |
| 150 | "roles": [ |
| 151 | { |
| 152 | "id": "9fe2ff9ee4384b1894a90878d3e92bab", |
| 153 | "name": "foo_role" |
| 154 | }, |
| 155 | { |
| 156 | "id": "1ea3d56793574b668e85960fbf651e13", |
| 157 | "name": "admin" |
| 158 | } |
| 159 | ] |
| 160 | } |
| 161 | `) |
| 162 | }) |
| 163 | } |