blob: df3f7743a02509a6551ffec1a21cc3b3f84be50d [file] [log] [blame]
Keith Byrnebda48592016-03-23 11:37:08 +00001// +build fixtures
2
Jamie Hannaford2a130242014-10-28 11:19:46 +01003package users
Jamie Hannaford6a076d82014-10-29 10:57:36 +01004
5import (
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 Hannaford10cf2bd2014-10-30 12:21:14 +010014func MockListUserResponse(t *testing.T) {
Jamie Hannaford6a076d82014-10-29 10:57:36 +010015 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 Hannaford929bd002014-10-29 11:14:25 +010027 "name": "John Smith",
Jamie Hannaford6a076d82014-10-29 10:57:36 +010028 "username": "jqsmith",
29 "email": "john.smith@example.org",
Jamie Hannaford929bd002014-10-29 11:14:25 +010030 "enabled": true,
31 "tenant_id": "12345"
Jamie Hannaford6a076d82014-10-29 10:57:36 +010032 },
33 {
34 "id": "u1001",
Jamie Hannaford929bd002014-10-29 11:14:25 +010035 "name": "Jane Smith",
Jamie Hannaford6a076d82014-10-29 10:57:36 +010036 "username": "jqsmith",
Jamie Hannaford929bd002014-10-29 11:14:25 +010037 "email": "jane.smith@example.org",
38 "enabled": true,
39 "tenant_id": "12345"
Jamie Hannaford6a076d82014-10-29 10:57:36 +010040 }
41 ]
42}
43 `)
44 })
45}
Jamie Hannaford9c7bb8e2014-10-29 11:47:34 +010046
Jamie Hannaford10cf2bd2014-10-30 12:21:14 +010047func mockCreateUserResponse(t *testing.T) {
Jamie Hannaford9c7bb8e2014-10-29 11:47:34 +010048 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 Hannaford7e04adf2014-10-29 13:47:58 +010052 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 Hannaford9c7bb8e2014-10-29 11:47:34 +010063 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 Hannaford2ad98bd2014-10-29 13:26:47 +010079
Jamie Hannaford10cf2bd2014-10-30 12:21:14 +010080func mockGetUserResponse(t *testing.T) {
Jamie Hannaford2ad98bd2014-10-29 13:26:47 +010081 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 Hannaford7e04adf2014-10-29 13:47:58 +0100101
Jamie Hannaford10cf2bd2014-10-30 12:21:14 +0100102func mockUpdateUserResponse(t *testing.T) {
Jamie Hannaford7e04adf2014-10-29 13:47:58 +0100103 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 Hannaford8b9a8002014-10-29 14:20:24 +0100133
Jamie Hannaford10cf2bd2014-10-30 12:21:14 +0100134func mockDeleteUserResponse(t *testing.T) {
Jamie Hannaford8b9a8002014-10-29 14:20:24 +0100135 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 Hannaforde680e422014-10-29 14:55:57 +0100141
Jamie Hannaford10cf2bd2014-10-30 12:21:14 +0100142func mockListRolesResponse(t *testing.T) {
Jamie Hannafordb136b182014-10-30 15:20:10 +0100143 th.Mux.HandleFunc("/tenants/1d8b6120dcc640fda4fc9194ffc80273/users/c39e3de9be2d4c779f1dfd6abacc176d/roles", func(w http.ResponseWriter, r *http.Request) {
Jamie Hannaforde680e422014-10-29 14:55:57 +0100144 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}