blob: d5091b9426b26061b3872e2f70d3f72912c4c309 [file] [log] [blame]
Keith Byrnebda48592016-03-23 11:37:08 +00001// +build fixtures
2
Jamie Hannaford6e4d7952014-10-29 16:18:29 +01003package users
4
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
14func 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
43func 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
77func 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
101func 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
134func 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 Quernafdc369a2014-10-31 11:50:20 -0700141
142func 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}