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