Jamie Hannaford | 6e4d795 | 2014-10-29 16:18:29 +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 | func mockListResponse(t *testing.T) { |
| 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", |
| 25 | "username": "jqsmith", |
| 26 | "email": "john.smith@example.org", |
| 27 | "enabled": true |
| 28 | }, |
| 29 | { |
| 30 | "id": "u1001", |
| 31 | "username": "jqsmith", |
| 32 | "email": "jane.smith@example.org", |
| 33 | "enabled": true |
| 34 | } |
| 35 | ] |
| 36 | } |
| 37 | `) |
| 38 | }) |
| 39 | } |
| 40 | |
| 41 | func mockCreateUser(t *testing.T) { |
| 42 | th.Mux.HandleFunc("/users", func(w http.ResponseWriter, r *http.Request) { |
| 43 | th.TestMethod(t, r, "POST") |
| 44 | th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) |
| 45 | |
| 46 | th.TestJSONRequest(t, r, ` |
| 47 | { |
| 48 | "user": { |
| 49 | "username": "new_user", |
| 50 | "enabled": false, |
| 51 | "email": "new_user@foo.com", |
| 52 | "OS-KSADM:password": "foo" |
| 53 | } |
| 54 | } |
| 55 | `) |
| 56 | |
| 57 | w.Header().Add("Content-Type", "application/json") |
| 58 | w.WriteHeader(http.StatusOK) |
| 59 | |
| 60 | fmt.Fprintf(w, ` |
| 61 | { |
| 62 | "user": { |
| 63 | "RAX-AUTH:defaultRegion": "DFW", |
| 64 | "RAX-AUTH:domainId": "5830280", |
| 65 | "id": "123456", |
| 66 | "username": "new_user", |
| 67 | "email": "new_user@foo.com", |
| 68 | "enabled": false |
| 69 | } |
| 70 | } |
| 71 | `) |
| 72 | }) |
| 73 | } |
| 74 | |
| 75 | func mockGetUser(t *testing.T) { |
| 76 | th.Mux.HandleFunc("/users/new_user", func(w http.ResponseWriter, r *http.Request) { |
| 77 | th.TestMethod(t, r, "GET") |
| 78 | th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) |
| 79 | |
| 80 | w.Header().Add("Content-Type", "application/json") |
| 81 | w.WriteHeader(http.StatusOK) |
| 82 | |
| 83 | fmt.Fprintf(w, ` |
| 84 | { |
| 85 | "user": { |
| 86 | "RAX-AUTH:defaultRegion": "DFW", |
| 87 | "RAX-AUTH:domainId": "5830280", |
| 88 | "RAX-AUTH:multiFactorEnabled": "true", |
| 89 | "id": "c39e3de9be2d4c779f1dfd6abacc176d", |
| 90 | "username": "jqsmith", |
| 91 | "email": "john.smith@example.org", |
| 92 | "enabled": true |
| 93 | } |
| 94 | } |
| 95 | `) |
| 96 | }) |
| 97 | } |
| 98 | |
| 99 | func mockUpdateUser(t *testing.T) { |
| 100 | th.Mux.HandleFunc("/users/c39e3de9be2d4c779f1dfd6abacc176d", func(w http.ResponseWriter, r *http.Request) { |
| 101 | th.TestMethod(t, r, "POST") |
| 102 | th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) |
| 103 | |
| 104 | th.TestJSONRequest(t, r, ` |
| 105 | { |
| 106 | "user": { |
| 107 | "email": "new_email@foo.com", |
| 108 | "enabled": true |
| 109 | } |
| 110 | } |
| 111 | `) |
| 112 | |
| 113 | w.Header().Add("Content-Type", "application/json") |
| 114 | w.WriteHeader(http.StatusOK) |
| 115 | |
| 116 | fmt.Fprintf(w, ` |
| 117 | { |
| 118 | "user": { |
| 119 | "RAX-AUTH:defaultRegion": "DFW", |
| 120 | "RAX-AUTH:domainId": "5830280", |
| 121 | "RAX-AUTH:multiFactorEnabled": "true", |
| 122 | "id": "123456", |
| 123 | "username": "jqsmith", |
| 124 | "email": "new_email@foo.com", |
| 125 | "enabled": true |
| 126 | } |
| 127 | } |
| 128 | `) |
| 129 | }) |
| 130 | } |
| 131 | |
| 132 | func mockDeleteUser(t *testing.T) { |
| 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 | } |
Paul Querna | fdc369a | 2014-10-31 11:50:20 -0700 | [diff] [blame^] | 139 | |
| 140 | func mockResetAPIKey(t *testing.T) { |
| 141 | th.Mux.HandleFunc("/users/99/OS-KSADM/credentials/RAX-KSKEY:apiKeyCredentials/RAX-AUTH/reset", func(w http.ResponseWriter, r *http.Request) { |
| 142 | th.TestMethod(t, r, "POST") |
| 143 | th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) |
| 144 | w.Header().Add("Content-Type", "application/json") |
| 145 | w.WriteHeader(http.StatusOK) |
| 146 | fmt.Fprintf(w, ` |
| 147 | { |
| 148 | "RAX-KSKEY:apiKeyCredentials": { |
| 149 | "username": "joesmith", |
| 150 | "apiKey": "mooH1eiLahd5ahYood7r" |
| 151 | } |
| 152 | }`) |
| 153 | }) |
| 154 | } |