forked from dependabot/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi_test.go
More file actions
213 lines (185 loc) · 6.67 KB
/
api_test.go
File metadata and controls
213 lines (185 loc) · 6.67 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package server
import (
"bytes"
"crypto/sha256"
"encoding/base64"
"encoding/hex"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"testing"
"github.com/dependabot/cli/internal/model"
)
func Test_decodeWrapper(t *testing.T) {
t.Run("reject extra data", func(t *testing.T) {
_, err := decodeWrapper("update_dependency_list", []byte(`data: {"unknown": "value"}`))
if err == nil {
t.Error("expected decode would error on extra data")
}
})
}
func TestAPI_ServeHTTP(t *testing.T) {
t.Run("doesn't crash when unknown endpoint is used", func(t *testing.T) {
request := httptest.NewRequest("POST", "/unexpected-endpoint", nil)
response := httptest.NewRecorder()
api := NewAPI(nil, nil)
api.ServeHTTP(response, request)
if response.Code != http.StatusNotImplemented {
t.Errorf("expected status code %d, got %d", http.StatusNotImplemented, response.Code)
}
})
}
type Wrapper[T any] struct {
Data T `json:"data"`
}
func TestAPI_CreatePullRequest_ReplacesBinaryWithHash(t *testing.T) {
var stdout bytes.Buffer
api := NewAPI(nil, &stdout)
defer api.Stop()
content := base64.StdEncoding.EncodeToString([]byte("Hello, world!"))
hash := sha256.Sum256([]byte(content))
expectedHashedContent := hex.EncodeToString(hash[:])
// Construct the request body for create_pull_request
createPullRequest := model.CreatePullRequest{
UpdatedDependencyFiles: []model.DependencyFile{
{
Content: content,
ContentEncoding: "base64",
},
},
}
var body bytes.Buffer
if err := json.NewEncoder(&body).Encode(model.UpdateWrapper{Data: createPullRequest}); err != nil {
t.Fatalf("failed to encode request body: %v", err)
}
url := "http://127.0.0.1:" + // use the API's port
fmt.Sprintf("%d/create_pull_request", api.Port())
req, err := http.NewRequest("POST", url, &body)
if err != nil {
t.Fatalf("failed to create request: %v", err)
}
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
t.Fatalf("failed to send request: %v", err)
}
defer resp.Body.Close()
if len(api.Errors) > 0 {
t.Fatalf("expected no errors, got %d errors: %v", len(api.Errors), api.Errors)
}
// The API should have replaced the content with a SHA hash in a.Actual.Output
if len(api.Actual.Output) != 1 {
t.Fatalf("expected 1 output, got %d", len(api.Actual.Output))
}
if api.Actual.Output[0].Type != "create_pull_request" {
t.Fatalf("expected output type 'create_pull_request', got '%s'", api.Actual.Output[0].Type)
}
if api.Actual.Output[0].Expect.Data.(model.CreatePullRequest).UpdatedDependencyFiles[0].Content != expectedHashedContent {
t.Errorf("expected content to be 'hello', got '%s'", api.Actual.Output[0].Expect.Data.(model.CreatePullRequest).UpdatedDependencyFiles[0].Content)
}
// stdout should contain the original content so folks can create PRs
var wrapper Wrapper[model.CreatePullRequest]
if err := json.NewDecoder(&stdout).Decode(&wrapper); err != nil {
t.Fatalf("failed to decode stdout: %v", err)
}
if wrapper.Data.UpdatedDependencyFiles[0].Content != content {
t.Errorf("expected stdout to contain the original content, got '%s'", stdout.String())
}
}
func TestAPI_CreatePullRequest_PreservesAssociatedMetadata(t *testing.T) {
var stdout bytes.Buffer
api := NewAPI(nil, &stdout)
defer api.Stop()
manifestPaths := []string{
"WorkspacePackage1.jl/SubPackageA/Project.toml",
"WorkspacePackage1.jl/SubPackageB/Project.toml",
}
lockfilePath := "WorkspacePackage1.jl/Manifest.toml"
createPullRequest := model.CreatePullRequest{
UpdatedDependencyFiles: []model.DependencyFile{
{
Name: "Manifest.toml",
Directory: "/WorkspacePackage1.jl",
Content: "manifest content",
ContentEncoding: "utf-8",
AssociatedManifestPaths: manifestPaths,
AssociatedLockfilePath: lockfilePath,
},
},
}
var body bytes.Buffer
if err := json.NewEncoder(&body).Encode(model.UpdateWrapper{Data: createPullRequest}); err != nil {
t.Fatalf("failed to encode request body: %v", err)
}
url := fmt.Sprintf("http://127.0.0.1:%d/create_pull_request", api.Port())
req, err := http.NewRequest("POST", url, &body)
if err != nil {
t.Fatalf("failed to create request: %v", err)
}
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
t.Fatalf("failed to send request: %v", err)
}
defer resp.Body.Close()
if len(api.Errors) > 0 {
t.Fatalf("expected no errors, got %d errors: %v", len(api.Errors), api.Errors)
}
if len(api.Actual.Output) != 1 {
t.Fatalf("expected 1 output, got %d", len(api.Actual.Output))
}
actual := api.Actual.Output[0].Expect.Data.(model.CreatePullRequest).UpdatedDependencyFiles[0]
if actual.AssociatedLockfilePath != lockfilePath {
t.Fatalf("expected lockfile path %q, got %q", lockfilePath, actual.AssociatedLockfilePath)
}
if len(actual.AssociatedManifestPaths) != len(manifestPaths) {
t.Fatalf("expected %d manifest paths, got %d", len(manifestPaths), len(actual.AssociatedManifestPaths))
}
for i, path := range manifestPaths {
if actual.AssociatedManifestPaths[i] != path {
t.Fatalf("expected manifest path %q at index %d, got %q", path, i, actual.AssociatedManifestPaths[i])
}
}
var wrapper Wrapper[model.CreatePullRequest]
if err := json.NewDecoder(&stdout).Decode(&wrapper); err != nil {
t.Fatalf("failed to decode stdout: %v", err)
}
stdoutFile := wrapper.Data.UpdatedDependencyFiles[0]
if stdoutFile.AssociatedLockfilePath != lockfilePath {
t.Fatalf("expected stdout lockfile path %q, got %q", lockfilePath, stdoutFile.AssociatedLockfilePath)
}
if len(stdoutFile.AssociatedManifestPaths) != len(manifestPaths) {
t.Fatalf("expected stdout manifest path count %d, got %d", len(manifestPaths), len(stdoutFile.AssociatedManifestPaths))
}
for i, path := range manifestPaths {
if stdoutFile.AssociatedManifestPaths[i] != path {
t.Fatalf("expected stdout manifest path %q at index %d, got %q", path, i, stdoutFile.AssociatedManifestPaths[i])
}
}
}
func TestAPI_compareDependencySubmissionRequest(t *testing.T) {
t.Run("ignores detector version", func(t *testing.T) {
expect := model.DependencySubmissionRequest{
Detector: map[string]any{
"version": "1.2.3",
},
}
actual := model.DependencySubmissionRequest{
Detector: map[string]any{
"version": "4.5.6",
},
}
if compareDependencySubmissionRequest(expect, actual) != nil {
t.Error("expected detector version to be ignored")
}
if expect.Detector["version"] != "1.2.3" {
t.Error("expected expect detector version to be unchanged")
}
if actual.Detector["version"] != "4.5.6" {
t.Error("expected actual detector version to be unchanged")
}
})
}