-
Notifications
You must be signed in to change notification settings - Fork 2
/
barbican_test.go
110 lines (97 loc) · 3.07 KB
/
barbican_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package main
import (
"fmt"
"net/http"
"testing"
th "github.com/gophercloud/gophercloud/testhelper"
"github.com/gophercloud/gophercloud/testhelper/client"
)
func TestFetchKey(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
HandleListSecretKey(t)
HandleGetSecretKey(t)
HandleGetPayloadKey(t)
key, nonce, err := fetchKey(client.ServiceClient(), "test")
if err != nil {
t.Fatalf("test failed : %v", err)
}
if key != "s928HkkJKVCGO1q1aIFq1iWG3ZDh6LB7utsZ1mRqjKg=" {
t.Fatalf("got wrong key in payload : %v vs %v", key, GetPayloadResponse)
}
if nonce != "cWcmxHPcuG0O0hY3" {
t.Fatalf("got wrong nonce in payload : %v vs %v", nonce, GetPayloadResponse)
}
}
// GetResponse provides a Get result.
const GetResponse = `
{
"algorithm": "aes",
"bit_length": 256,
"content_types": {
"default": "text/plain"
},
"created": "2018-06-21T02:49:48",
"creator_id": "5c70d99f4a8641c38f8084b32b5e5c0e",
"expiration": null,
"mode": "cbc",
"name": "test",
"secret_ref": "http://barbican:9311/v1/secrets/1b8068c4-3bb6-4be6-8f1e-da0d1ea0b67c",
"secret_type": "opaque",
"status": "ACTIVE",
"updated": "2018-06-21T02:49:48"
}`
// GetPayloadResponse provides a payload result.
const GetPayloadResponse = `s928HkkJKVCGO1q1aIFq1iWG3ZDh6LB7utsZ1mRqjKg=
cWcmxHPcuG0O0hY3`
const ListResponse = `
{
"secrets": [
{
"algorithm": "aes",
"bit_length": 256,
"content_types": {
"default": "text/plain"
},
"created": "2018-06-21T02:49:48",
"creator_id": "5c70d99f4a8641c38f8084b32b5e5c0e",
"expiration": null,
"mode": "cbc",
"name": "test",
"secret_ref": "http://barbican:9311/v1/secrets/1b8068c4-3bb6-4be6-8f1e-da0d1ea0b67c",
"secret_type": "opaque",
"status": "ACTIVE",
"updated": "2018-06-21T02:49:48"
}
],
"total": 1
}`
func HandleListSecretKey(t *testing.T) {
th.Mux.HandleFunc("/secrets", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "GET")
th.TestHeader(t, r, "Accept", "application/json")
th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, ListResponse)
})
}
func HandleGetSecretKey(t *testing.T) {
th.Mux.HandleFunc("/secrets/1b8068c4-3bb6-4be6-8f1e-da0d1ea0b67c", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "GET")
th.TestHeader(t, r, "Accept", "application/json")
th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, GetResponse)
})
}
func HandleGetPayloadKey(t *testing.T) {
th.Mux.HandleFunc("/secrets/1b8068c4-3bb6-4be6-8f1e-da0d1ea0b67c/payload", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "GET")
th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
w.Header().Set("Content-Type", "text/plain")
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, GetPayloadResponse)
})
}