-
Notifications
You must be signed in to change notification settings - Fork 532
Expand file tree
/
Copy pathdiagnostics_test.go
More file actions
286 lines (269 loc) · 8.77 KB
/
Copy pathdiagnostics_test.go
File metadata and controls
286 lines (269 loc) · 8.77 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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
package reviews
import (
"context"
"errors"
"flag"
"io"
"os"
"path/filepath"
"strings"
"testing"
"github.com/rudrankriyam/App-Store-Connect-CLI/internal/cli/shared"
)
func TestReviewValidationDiagnosticsPreserveErrorContracts(t *testing.T) {
t.Setenv("ASC_APP_ID", "")
notFoundPath := filepath.Join(t.TempDir(), "missing.json")
tests := []struct {
name string
run func() error
wantError string
wantStderr string
wantUsage bool
wantCode shared.DiagnosticCode
wantParam string
}{
{
name: "reviews missing app",
run: func() error { return ReviewsCommand().ParseAndRun(context.Background(), nil) },
wantError: "--app is required (or set ASC_APP_ID)",
wantStderr: "Error: --app is required (or set ASC_APP_ID)\n",
wantUsage: true,
wantCode: shared.DiagnosticRequiredInputMissing,
wantParam: "--app",
},
{
name: "reviews invalid limit",
run: func() error {
return executeReviewsList(context.Background(), "app-1", "json", false, 0, "", "", 201, "", false, "any", false, false, "")
},
wantError: "reviews: --limit must be between 1 and 200",
wantCode: shared.DiagnosticInvalidInput,
wantParam: "--limit",
},
{
name: "ratings invalid workers",
run: func() error {
return ReviewsRatingsCommand().ParseAndRun(context.Background(), []string{"--app", "123", "--workers", "0"})
},
wantError: flag.ErrHelp.Error(),
wantStderr: "Error: --workers must be at least 1\n",
wantUsage: true,
wantCode: shared.DiagnosticInvalidInput,
wantParam: "--workers",
},
{
name: "respond batch missing file",
run: func() error {
return ReviewsRespondBatchCommand().ParseAndRun(context.Background(), []string{"--app", "app-1", "--dry-run"})
},
wantError: "--file is required",
wantStderr: "Error: --file is required\n",
wantUsage: true,
wantCode: shared.DiagnosticRequiredInputMissing,
wantParam: "--file",
},
{
name: "respond batch file not found",
run: func() error {
return ReviewsRespondBatchCommand().ParseAndRun(context.Background(), []string{"--app", "app-1", "--file", notFoundPath, "--dry-run"})
},
wantUsage: true,
wantCode: shared.DiagnosticFileNotFound,
wantParam: "--file",
},
{
name: "attachment file not found",
run: func() error {
return ReviewDetailsAttachmentsUploadCommand().ParseAndRun(context.Background(), []string{"--review-detail", "detail-1", "--file", notFoundPath})
},
wantCode: shared.DiagnosticFileNotFound,
wantParam: "--file",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
var err error
stderr := captureReviewDiagnosticStderr(t, func() {
err = test.run()
})
if err == nil {
t.Fatal("expected error")
}
if test.wantError != "" && err.Error() != test.wantError {
t.Fatalf("error = %q, want %q", err, test.wantError)
}
if test.wantStderr != "" && !strings.HasPrefix(stderr, test.wantStderr) {
t.Fatalf("stderr = %q, want prefix %q", stderr, test.wantStderr)
}
if got := errors.Is(err, flag.ErrHelp); got != test.wantUsage {
t.Fatalf("errors.Is(flag.ErrHelp) = %t, want %t", got, test.wantUsage)
}
if !test.wantUsage && !shared.IsValidationError(err) {
t.Fatalf("error = %v, want non-usage validation marker", err)
}
diagnostic, ok := shared.DiagnosticFromError(err)
if !ok {
t.Fatal("expected structured diagnostic")
}
if diagnostic.Code != test.wantCode || diagnostic.Parameter != test.wantParam {
t.Fatalf("diagnostic = %+v, want code %q parameter %q", diagnostic, test.wantCode, test.wantParam)
}
})
}
}
func TestReviewSubmitInputFailuresKeepStructuredDiagnostics(t *testing.T) {
t.Setenv("ASC_APP_ID", "")
tests := []struct {
name string
args []string
wantStderr string
wantCode shared.DiagnosticCode
wantParam string
}{
{
name: "missing app",
args: nil,
wantStderr: "Error: --app is required (or set ASC_APP_ID)\n",
wantCode: shared.DiagnosticRequiredInputMissing,
wantParam: "--app",
},
{
name: "missing build",
args: []string{"--app", "123456789"},
wantStderr: "Error: --build is required\n",
wantCode: shared.DiagnosticRequiredInputMissing,
wantParam: "--build",
},
{
name: "missing version selector",
args: []string{"--app", "123456789", "--build", "BUILD_ID"},
wantStderr: "Error: --version or --version-id is required\n",
wantCode: shared.DiagnosticRequiredInputMissing,
wantParam: "",
},
{
name: "conflicting version selectors",
args: []string{"--app", "123456789", "--build", "BUILD_ID", "--version", "1.2.3", "--version-id", "VERSION_ID"},
wantStderr: "Error: --version and --version-id are mutually exclusive\n",
wantCode: shared.DiagnosticConflictingInput,
wantParam: "",
},
{
name: "missing confirm",
args: []string{"--app", "123456789", "--build", "BUILD_ID", "--version", "1.2.3"},
wantStderr: "Error: --confirm is required unless --dry-run is set\n",
wantCode: shared.DiagnosticRequiredInputMissing,
wantParam: "--confirm",
},
{
name: "unsupported platform",
args: []string{"--app", "123456789", "--build", "BUILD_ID", "--version", "1.2.3", "--dry-run", "--platform", "WATCH_OS"},
wantStderr: "Error: --platform must be one of: IOS, MAC_OS, TV_OS, VISION_OS\n",
wantCode: shared.DiagnosticInvalidInput,
wantParam: "--platform",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
command := ReviewSubmitCommand()
if err := command.FlagSet.Parse(test.args); err != nil {
t.Fatalf("parse flags: %v", err)
}
var err error
stderr := captureReviewDiagnosticStderr(t, func() {
err = command.Exec(context.Background(), nil)
})
if err == nil {
t.Fatal("expected error")
}
if stderr != test.wantStderr {
t.Fatalf("stderr = %q, want %q", stderr, test.wantStderr)
}
if !errors.Is(err, flag.ErrHelp) {
t.Fatalf("error = %v, want flag.ErrHelp usage contract", err)
}
diagnostic, ok := shared.DiagnosticFromError(err)
if !ok {
t.Fatalf("DiagnosticFromError(%v) found no metadata", err)
}
if diagnostic.Code != test.wantCode || diagnostic.Parameter != test.wantParam {
t.Fatalf("diagnostic = %+v, want code %q parameter %q", diagnostic, test.wantCode, test.wantParam)
}
})
}
}
func TestReviewAttachmentUnreadableSourceIsDiagnosedBeforeAuth(t *testing.T) {
attachmentPath := filepath.Join(t.TempDir(), "attachment.pdf")
if err := os.WriteFile(attachmentPath, []byte("review attachment"), 0o600); err != nil {
t.Fatalf("write attachment: %v", err)
}
if err := os.Chmod(attachmentPath, 0); err != nil {
t.Fatalf("make attachment unreadable: %v", err)
}
t.Cleanup(func() { _ = os.Chmod(attachmentPath, 0o600) })
probe, probeErr := os.Open(attachmentPath)
if probeErr == nil {
_ = probe.Close()
t.Skip("current user can open mode-000 files")
}
if !errors.Is(probeErr, os.ErrPermission) {
t.Skipf("cannot reproduce permission-denied open: %v", probeErr)
}
t.Setenv("ASC_BYPASS_KEYCHAIN", "1")
t.Setenv("ASC_CONFIG_PATH", filepath.Join(t.TempDir(), "missing-config.json"))
t.Setenv("ASC_KEY_ID", "")
t.Setenv("ASC_ISSUER_ID", "")
t.Setenv("ASC_PRIVATE_KEY", "")
t.Setenv("ASC_PRIVATE_KEY_PATH", "")
t.Setenv("ASC_PRIVATE_KEY_B64", "")
err := ReviewDetailsAttachmentsUploadCommand().ParseAndRun(context.Background(), []string{
"--review-detail", "detail-1",
"--file", attachmentPath,
})
if err == nil {
t.Fatal("expected error")
}
if !shared.IsValidationError(err) {
t.Fatalf("error = %v, want validation marker", err)
}
diagnostic, ok := shared.DiagnosticFromError(err)
if !ok {
t.Fatalf("expected structured diagnostic, got %v", err)
}
if diagnostic.Code != shared.DiagnosticFilePermissionDenied || diagnostic.Parameter != "--file" {
t.Fatalf("diagnostic = %+v, want file_permission_denied for --file", diagnostic)
}
if !strings.HasPrefix(err.Error(), "review attachments-upload: upload failed: ") {
t.Fatalf("error = %q, want preserved upload failure prefix", err)
}
}
func captureReviewDiagnosticStderr(t *testing.T, fn func()) string {
t.Helper()
original := os.Stderr
reader, writer, err := os.Pipe()
if err != nil {
t.Fatalf("create stderr pipe: %v", err)
}
readResult := make(chan []byte, 1)
readError := make(chan error, 1)
go func() {
data, readErr := io.ReadAll(reader)
readResult <- data
readError <- readErr
}()
os.Stderr = writer
defer func() { os.Stderr = original }()
fn()
if err := writer.Close(); err != nil {
t.Fatalf("close stderr writer: %v", err)
}
os.Stderr = original
data := <-readResult
if err := <-readError; err != nil {
t.Fatalf("read stderr: %v", err)
}
if err := reader.Close(); err != nil {
t.Fatalf("close stderr reader: %v", err)
}
return string(data)
}