1package testing
2
3import (
4	"fmt"
5	"net/http"
6	"testing"
7	"time"
8
9	"github.com/gophercloud/gophercloud/openstack/keymanager/v1/acls"
10	th "github.com/gophercloud/gophercloud/testhelper"
11	"github.com/gophercloud/gophercloud/testhelper/client"
12)
13
14const GetResponse = `
15{
16  "read": {
17    "created": "2018-06-22T17:54:24",
18    "project-access": false,
19    "updated": "2018-06-22T17:54:24",
20    "users": [
21      "GG27dVwR9gBMnsOaRoJ1DFJmZfdVjIdW"
22    ]
23  }
24}`
25
26const SetRequest = `
27{
28  "read": {
29    "project-access": false,
30    "users": [
31      "GG27dVwR9gBMnsOaRoJ1DFJmZfdVjIdW"
32    ]
33  }
34}`
35
36const SecretSetResponse = `
37{
38  "acl_ref": "http://barbican:9311/v1/secrets/4befede0-fbde-4480-982c-b160c1014a47/acl"
39}`
40
41const ContainerSetResponse = `
42{
43  "acl_ref": "http://barbican:9311/v1/containers/4befede0-fbde-4480-982c-b160c1014a47/acl"
44}`
45
46var ExpectedACL = acls.ACL{
47	"read": acls.ACLDetails{
48		Created:       time.Date(2018, 6, 22, 17, 54, 24, 0, time.UTC),
49		ProjectAccess: false,
50		Updated:       time.Date(2018, 6, 22, 17, 54, 24, 0, time.UTC),
51		Users: []string{
52			"GG27dVwR9gBMnsOaRoJ1DFJmZfdVjIdW",
53		},
54	},
55}
56
57var ExpectedSecretACLRef = acls.ACLRef("http://barbican:9311/v1/secrets/4befede0-fbde-4480-982c-b160c1014a47/acl")
58
59var ExpectedContainerACLRef = acls.ACLRef("http://barbican:9311/v1/containers/4befede0-fbde-4480-982c-b160c1014a47/acl")
60
61const UpdateRequest = `
62{
63  "read": {
64    "users": []
65  }
66}`
67
68// HandleGetSecretACLSuccessfully creates an HTTP handler at `/secrets/uuid/acl`
69// on the test handler mux that responds with an acl.
70func HandleGetSecretACLSuccessfully(t *testing.T) {
71	th.Mux.HandleFunc("/secrets/1b8068c4-3bb6-4be6-8f1e-da0d1ea0b67c/acl", func(w http.ResponseWriter, r *http.Request) {
72		th.TestMethod(t, r, "GET")
73		th.TestHeader(t, r, "Accept", "application/json")
74		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
75
76		w.Header().Set("Content-Type", "application/json")
77		w.WriteHeader(http.StatusOK)
78		fmt.Fprintf(w, GetResponse)
79	})
80}
81
82// HandleGetContainerACLSuccessfully creates an HTTP handler at `/secrets/uuid/acl`
83// on the test handler mux that responds with an acl.
84func HandleGetContainerACLSuccessfully(t *testing.T) {
85	th.Mux.HandleFunc("/containers/1b8068c4-3bb6-4be6-8f1e-da0d1ea0b67c/acl", func(w http.ResponseWriter, r *http.Request) {
86		th.TestMethod(t, r, "GET")
87		th.TestHeader(t, r, "Accept", "application/json")
88		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
89
90		w.Header().Set("Content-Type", "application/json")
91		w.WriteHeader(http.StatusOK)
92		fmt.Fprintf(w, GetResponse)
93	})
94}
95
96// HandleSetSecretACLSuccessfully creates an HTTP handler at `/secrets` on the
97// test handler mux that tests secret creation.
98func HandleSetSecretACLSuccessfully(t *testing.T) {
99	th.Mux.HandleFunc("/secrets/1b8068c4-3bb6-4be6-8f1e-da0d1ea0b67c/acl", func(w http.ResponseWriter, r *http.Request) {
100		th.TestMethod(t, r, "PUT")
101		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
102		th.TestJSONRequest(t, r, SetRequest)
103
104		w.WriteHeader(http.StatusOK)
105		fmt.Fprintf(w, SecretSetResponse)
106	})
107}
108
109// HandleSetContainerACLSuccessfully creates an HTTP handler at `/secrets` on the
110// test handler mux that tests secret creation.
111func HandleSetContainerACLSuccessfully(t *testing.T) {
112	th.Mux.HandleFunc("/containers/1b8068c4-3bb6-4be6-8f1e-da0d1ea0b67c/acl", func(w http.ResponseWriter, r *http.Request) {
113		th.TestMethod(t, r, "PUT")
114		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
115		th.TestJSONRequest(t, r, SetRequest)
116
117		w.WriteHeader(http.StatusOK)
118		fmt.Fprintf(w, ContainerSetResponse)
119	})
120}
121
122// HandleUpdateSecretACLSuccessfully creates an HTTP handler at `/secrets` on the
123// test handler mux that tests secret creation.
124func HandleUpdateSecretACLSuccessfully(t *testing.T) {
125	th.Mux.HandleFunc("/secrets/1b8068c4-3bb6-4be6-8f1e-da0d1ea0b67c/acl", func(w http.ResponseWriter, r *http.Request) {
126		th.TestMethod(t, r, "PATCH")
127		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
128		th.TestJSONRequest(t, r, UpdateRequest)
129
130		w.WriteHeader(http.StatusOK)
131		fmt.Fprintf(w, SecretSetResponse)
132	})
133}
134
135// HandleUpdateContainerACLSuccessfully creates an HTTP handler at `/secrets` on the
136// test handler mux that tests secret creation.
137func HandleUpdateContainerACLSuccessfully(t *testing.T) {
138	th.Mux.HandleFunc("/containers/1b8068c4-3bb6-4be6-8f1e-da0d1ea0b67c/acl", func(w http.ResponseWriter, r *http.Request) {
139		th.TestMethod(t, r, "PATCH")
140		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
141		th.TestJSONRequest(t, r, UpdateRequest)
142
143		w.WriteHeader(http.StatusOK)
144		fmt.Fprintf(w, ContainerSetResponse)
145	})
146}
147
148// HandleDeleteSecretACLSuccessfully creates an HTTP handler at `/secrets` on the
149// test handler mux that tests secret deletion.
150func HandleDeleteSecretACLSuccessfully(t *testing.T) {
151	th.Mux.HandleFunc("/secrets/1b8068c4-3bb6-4be6-8f1e-da0d1ea0b67c/acl", func(w http.ResponseWriter, r *http.Request) {
152		th.TestMethod(t, r, "DELETE")
153		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
154
155		w.WriteHeader(http.StatusOK)
156	})
157}
158
159// HandleDeleteContainerACLSuccessfully creates an HTTP handler at `/secrets` on the
160// test handler mux that tests secret deletion.
161func HandleDeleteContainerACLSuccessfully(t *testing.T) {
162	th.Mux.HandleFunc("/containers/1b8068c4-3bb6-4be6-8f1e-da0d1ea0b67c/acl", func(w http.ResponseWriter, r *http.Request) {
163		th.TestMethod(t, r, "DELETE")
164		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
165
166		w.WriteHeader(http.StatusOK)
167	})
168}
169