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