-
Notifications
You must be signed in to change notification settings - Fork 0
/
response.go
108 lines (98 loc) · 3.15 KB
/
response.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
package secretmanagertest
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"testing"
"cloud.google.com/go/secretmanager/apiv1/secretmanagerpb"
"github.com/jarcoal/httpmock"
)
const projectNumber = 888888888888
func ListVersionsResponse(
t *testing.T,
projectName string,
secretName string,
states []secretmanagerpb.SecretVersion_State,
) (string, string, httpmock.Responder) {
summaries := []map[string]any{}
for i, state := range states {
version := i + 1
summaries = append(summaries, map[string]any{
"name": fmt.Sprintf("projects/%d/secrets/%s/versions/%d", projectNumber, secretName, version),
"createTime": "2001-01-01T00:00:00.000000Z",
"state": state,
"replicationStatus": map[string]any{
"automatic": map[string]any{},
},
"etag": fmt.Sprintf("\"%014d\"", version),
"clientSpecifiedPayloadChecksum": true,
})
}
var body bytes.Buffer
encorder := json.NewEncoder(&body)
encorder.SetIndent("", " ")
if err := encorder.Encode(map[string]any{
"versions": summaries,
"totalSize": len(summaries),
}); err != nil {
t.Fatal(err)
}
url := fmt.Sprintf("https://secretmanager.googleapis.com/v1/projects/%s/secrets/%s/versions", projectName, secretName)
return http.MethodGet,
url,
httpmock.NewStringResponder(http.StatusOK, body.String())
}
func DestroyVersionResponse(
t *testing.T,
projectName string,
secretName string,
version int,
) (string, string, httpmock.Responder) {
var body bytes.Buffer
encorder := json.NewEncoder(&body)
encorder.SetIndent("", " ")
if err := encorder.Encode(map[string]any{
"name": fmt.Sprintf("projects/%d/secrets/%s/versions/%d", projectNumber, secretName, version),
"createTime": "2001-01-01T00:00:00.000000Z",
"destroyTime": "2001-01-01T00:00:00.000000Z",
"state": 3,
"replicationStatus": map[string]any{
"automatic": map[string]any{},
},
"etag": fmt.Sprintf("\"8888888888888%d\"", version),
"clientSpecifiedPayloadChecksum": true,
}); err != nil {
t.Fatal(err)
}
url := fmt.Sprintf("https://secretmanager.googleapis.com/v1/projects/%d/secrets/%s/versions/%d:destroy", projectNumber, secretName, version)
return http.MethodPost,
url,
httpmock.NewStringResponder(http.StatusOK, body.String())
}
func DisableVersionResponse(
t *testing.T,
projectName string,
secretName string,
version int,
) (string, string, httpmock.Responder) {
var body bytes.Buffer
encorder := json.NewEncoder(&body)
encorder.SetIndent("", " ")
if err := encorder.Encode(map[string]any{
"name": fmt.Sprintf("projects/%d/secrets/%s/versions/%d", projectNumber, secretName, version),
"createTime": "2001-01-01T00:00:00.000000Z",
"state": 2,
"replicationStatus": map[string]any{
"automatic": map[string]any{},
},
"etag": fmt.Sprintf("\"8888888888888%d\"", version),
"clientSpecifiedPayloadChecksum": true,
}); err != nil {
t.Fatal(err)
}
url := fmt.Sprintf("https://secretmanager.googleapis.com/v1/projects/%d/secrets/%s/versions/%d:disable", projectNumber, secretName, version)
return http.MethodPost,
url,
httpmock.NewStringResponder(http.StatusOK, body.String())
}