-
Notifications
You must be signed in to change notification settings - Fork 349
Expand file tree
/
Copy pathsecrets_test.go
More file actions
151 lines (140 loc) · 4 KB
/
secrets_test.go
File metadata and controls
151 lines (140 loc) · 4 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package api_test
import (
"context"
"errors"
"fmt"
"io"
"net/http"
"net/http/httptest"
"path"
"testing"
"github.com/buildkite/agent/v3/api"
"github.com/buildkite/agent/v3/logger"
"github.com/google/go-cmp/cmp"
)
func TestGetSecret(t *testing.T) {
t.Parallel()
const (
jobID = "job-id"
nonJobID = "non-job-id"
secretKey = "NOT_TEST_SECRET"
nonSecretKey = "TEST_SECRET"
secretValue = "super-secret"
secretUUID = "secret-id"
accessToken = "llamas"
nonAccessToken = "alpacas"
)
ctx := context.Background()
for _, test := range []struct {
name string
accessToken string
getSecretRequest *api.GetSecretRequest
expectedSecret *api.Secret
expectedError error
expectedCode int
}{
{
name: "success",
accessToken: accessToken,
getSecretRequest: &api.GetSecretRequest{
Key: secretKey,
JobID: jobID,
},
expectedSecret: &api.Secret{
Key: secretKey,
Value: secretValue,
UUID: secretUUID,
},
expectedError: nil,
expectedCode: http.StatusOK,
},
{
name: "unauthorized",
accessToken: nonAccessToken,
getSecretRequest: &api.GetSecretRequest{
Key: secretKey,
JobID: jobID,
},
expectedError: errors.New("Unauthorized: got alpacas, want llamas"),
expectedCode: http.StatusUnauthorized,
},
{
name: "job_not_found",
accessToken: accessToken,
getSecretRequest: &api.GetSecretRequest{
Key: secretKey,
JobID: nonJobID,
},
expectedError: fmt.Errorf("Not Found: method = GET, url = /jobs/%s/secrets?key=%s", nonJobID, secretKey),
expectedCode: http.StatusNotFound,
},
{
name: "secret_not_found",
accessToken: accessToken,
getSecretRequest: &api.GetSecretRequest{
Key: nonSecretKey,
JobID: jobID,
},
expectedError: fmt.Errorf("Not Found: method = GET, url = /jobs/%s/secrets?key=%s", jobID, nonSecretKey),
expectedCode: http.StatusNotFound,
},
} {
t.Run(test.name, func(t *testing.T) {
t.Parallel()
secretPath := path.Join("/jobs", jobID, "secrets")
buildkiteAPI := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
if got, want := authToken(req), accessToken; got != want {
http.Error(
rw,
fmt.Sprintf(`{"message": "Unauthorized: got %s, want %s"}`, got, want),
http.StatusUnauthorized,
)
return
}
if req.URL.Path == secretPath && req.URL.Query().Get("key") == secretKey {
_, err := io.WriteString(
rw, fmt.Sprintf(`{"key":%q,"value":%q,"uuid":%q}`, secretKey, secretValue, secretUUID),
)
if err != nil {
t.Fatalf("io.WriteString(rw, %q) error = %v, want nil", fmt.Sprintf(`{"key":%q,"value":%q,"uuid":%q}`, secretKey, secretValue, secretUUID), err)
}
return
}
http.Error(
rw,
fmt.Sprintf(
`{"message":"Not Found: method = %s, url = %s"}`,
req.Method,
req.URL.String(),
),
http.StatusNotFound,
)
}))
t.Cleanup(buildkiteAPI.Close)
// Initial client with a registration token
client := api.NewClient(logger.Discard, api.Config{
UserAgent: "Test",
Endpoint: buildkiteAPI.URL,
Token: test.accessToken,
DebugHTTP: true,
})
secret, resp, err := client.GetSecret(ctx, test.getSecretRequest)
if got := resp.StatusCode == test.expectedCode; !got {
t.Errorf("expected status code %d, got %d", test.expectedCode, resp.StatusCode)
}
if test.expectedError == nil {
if diff := cmp.Diff(test.expectedSecret, secret); diff != "" {
t.Fatalf("test.expectedSecret diff (-got +want):\n%s", diff)
}
} else if aerr := new(api.ErrorResponse); errors.As(err, &aerr) {
if diff := cmp.Diff(test.expectedError.Error(), aerr.Message); diff != "" {
t.Fatalf("test.expectedError.Error() diff (-got +want):\n%s", diff)
}
} else {
if want := test.expectedError; !errors.Is(err, want) {
t.Fatalf("client.GetSecret(ctx, test.getSecretRequest) error = %v, want %v", err, want)
}
}
})
}
}