-
Notifications
You must be signed in to change notification settings - Fork 1
/
check_test.go
382 lines (371 loc) · 9.81 KB
/
check_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
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
package handlers
import (
"bytes"
"encoding/json"
"net/http"
"net/http/httptest"
"os"
"testing"
)
func TestCheckMyWork(t *testing.T) {
os.Setenv("FABRICATOR_DATA_MOUNT", "/tmp")
files := []struct {
name string
permissions os.FileMode
expectAccess bool
}{
{"/tmp/test_readable.txt", 0644, true}, // Readable globally
{"/tmp/test_writable.txt", 0666, true}, // Writable globally
{"/tmp/test_private.txt", 0600, false}, // Not accessible globally
}
// Create test files
for _, file := range files {
if err := os.WriteFile(file.name, []byte("test content"), file.permissions); err != nil {
t.Fatalf("Failed to create test file %s: %v", file.name, err)
}
}
defer func() {
for _, file := range files {
_ = os.Remove(file.name)
}
}()
tests := []struct {
name string
method string
body interface{}
statusCode int
response string
}{
{
name: "Method not allowed",
method: http.MethodGet,
body: nil,
statusCode: http.StatusMethodNotAllowed,
response: "Method not allowed\n",
},
{
name: "Not authorized",
method: http.MethodPost,
body: nil,
statusCode: http.StatusUnauthorized,
response: "Authorization header missing\n",
},
{
name: "Empty request body",
method: http.MethodPost,
body: nil,
statusCode: http.StatusBadRequest,
response: "Request body is empty\n",
},
{
name: "Invalid JSON body",
method: http.MethodPost,
body: "invalid_json",
statusCode: http.StatusBadRequest,
response: "Error parsing CSV\n",
},
{
name: "No rows in CSV to process",
method: http.MethodPost,
body: [][]string{{"Title", "Object Model"}},
statusCode: http.StatusOK,
response: `{"A":"No rows in CSV to process"}`,
},
{
name: "Valid CSV data with no errors",
method: http.MethodPost,
body: [][]string{
{"Title", "Object Model", "Full Title"},
{"foo", "bar", "Full Test Title"},
},
statusCode: http.StatusOK,
response: "{}", // Empty errors map
},
{
name: "Missing title",
method: http.MethodPost,
body: [][]string{
{"Title", "Object Model", "Full Title"},
{"", "bar", "foo"},
},
statusCode: http.StatusOK,
response: `{"A2":"Missing value"}`,
},
{
name: "Missing resource type",
method: http.MethodPost,
body: [][]string{
{"Title", "Object Model", "Full Title", "Resource Type"},
{"foo", "bar", "foo", ""},
},
statusCode: http.StatusOK,
response: `{"D2":"Must have a resource type"}`,
},
{
name: "Missing resource type OK",
method: http.MethodPost,
body: [][]string{
{"Title", "Object Model", "Full Title", "Resource Type"},
{"foo", "Page", "foo", ""},
},
statusCode: http.StatusOK,
response: `{}`,
},
{
name: "Missing model",
method: http.MethodPost,
body: [][]string{
{"Title", "Object Model", "Full Title"},
{"foo", "", "bar"},
},
statusCode: http.StatusOK,
response: `{"B2":"Missing value"}`,
},
{
name: "Missing full title",
method: http.MethodPost,
body: [][]string{
{"Title", "Object Model", "Full Title"},
{"foo", "bar", ""},
},
statusCode: http.StatusOK,
response: `{"C2":"Missing value"}`,
},
{
name: "Non-existent file",
method: http.MethodPost,
body: [][]string{
{"Title", "Object Model", "Full Title", "File Path"},
{"foo", "bar", "foo", "/tmp/file/does/not/exist"},
},
statusCode: http.StatusOK,
response: `{"D2":"File does not exist in islandora_staging"}`,
},
{
name: "Missing file",
method: http.MethodPost,
body: [][]string{
{"Title", "Object Model", "Full Title", "File Path"},
{"foo", "Image", "foo", ""},
},
statusCode: http.StatusOK,
response: `{}`,
},
{
name: "OK file",
method: http.MethodPost,
body: [][]string{
{"Title", "Object Model", "Full Title", "File Path"},
{"foo", "Image", "foo", "test_readable.txt"},
},
statusCode: http.StatusOK,
response: `{}`,
},
{
name: "OK file (rw)",
method: http.MethodPost,
body: [][]string{
{"Title", "Object Model", "Full Title", "File Path"},
{"foo", "Image", "foo", "test_writable.txt"},
},
statusCode: http.StatusOK,
response: `{}`,
},
{
name: "Unreadable file",
method: http.MethodPost,
body: [][]string{
{"Title", "Object Model", "Full Title", "File Path"},
{"foo", "Image", "foo", "test_private.txt"},
},
statusCode: http.StatusOK,
response: `{"D2":"File does not exist in islandora_staging"}`,
},
{
name: "Missing file OK",
method: http.MethodPost,
body: [][]string{
{"Title", "Object Model", "Full Title", "File Path"},
{"foo", "Paged Content", "foo", ""},
},
statusCode: http.StatusOK,
response: `{}`,
},
{
name: "Missing supplemental file",
method: http.MethodPost,
body: [][]string{
{"Title", "Object Model", "Full Title", "Supplemental File"},
{"foo", "bar", "foo", "/tmp/file/does/not/exist"},
},
statusCode: http.StatusOK,
response: `{"D2":"File does not exist in islandora_staging"}`,
},
// Parent Collection and PPI
{
name: "Parent Collection not integer",
method: http.MethodPost,
body: [][]string{
{"Title", "Object Model", "Full Title", "Parent Collection"},
{"foo", "bar", "foo", "NotAnInteger"},
},
statusCode: http.StatusOK,
response: `{"D2":"Must be an integer"}`,
},
{
name: "Parent Collection not found",
method: http.MethodPost,
body: [][]string{
{"Title", "Object Model", "Full Title", "Parent Collection"},
{"foo", "bar", "foo", "0"},
},
statusCode: http.StatusOK,
response: `{"D2":"Could not identify parent collection 0"}`,
},
{
name: "Parent Collection exists",
method: http.MethodPost,
body: [][]string{
{"Title", "Object Model", "Full Title", "Parent Collection"},
{"foo", "bar", "foo", "2"},
},
statusCode: http.StatusOK,
response: `{}`,
},
{
name: "Invalid URL",
method: http.MethodPost,
body: [][]string{
{"Title", "Object Model", "Full Title", "Catalog or ArchivesSpace URL"},
{"foo", "bar", "foo", "invalid-url"},
},
statusCode: http.StatusOK,
response: `{"D2":"Invalid URL"}`,
},
{
name: "Duplicate Upload ID",
method: http.MethodPost,
body: [][]string{
{"Title", "Object Model", "Full Title", "Upload ID"},
{"Test Title 1", "bar", "foo", "123"},
{"Test Title 2", "bar", "foo", "123"},
},
statusCode: http.StatusOK,
response: `{"D3":"Duplicate upload ID"}`,
},
{
name: "Invalid EDTF value",
method: http.MethodPost,
body: [][]string{
{"Title", "Object Model", "Full Title", "Creation Date"},
{"foo", "bar", "foo", "1/2/2022"},
},
statusCode: http.StatusOK,
response: `{"D2":"Invalid EDTF value"}`,
},
{
name: "Invalid DOI",
method: http.MethodPost,
body: [][]string{
{"Title", "Object Model", "Full Title", "DOI"},
{"foo", "bar", "foo", "1.2.3.4"},
},
statusCode: http.StatusOK,
response: `{"D2":"Invalid DOI"}`,
},
{
name: "Unknown Parent ID",
method: http.MethodPost,
body: [][]string{
{"Title", "Object Model", "Full Title", "Page/Item Parent ID", "Upload ID"},
{"foo", "bar", "foo", "123", "456"},
},
statusCode: http.StatusOK,
response: `{"D2":"Unknown parent ID"}`,
},
{
name: "Upload ID equals Parent ID",
method: http.MethodPost,
body: [][]string{
{"Title", "Object Model", "Full Title", "Upload ID", "Page/Item Parent ID"},
{"foo", "bar", "foo", "123", "123"},
},
statusCode: http.StatusOK,
response: `{"E2":"Upload ID and parent ID can not be equal"}`,
},
// Contributor
{
name: "Contributor not in proper format",
method: http.MethodPost,
body: [][]string{
{"Title", "Object Model", "Full Title", "Contributor"},
{"foo", "bar", "foo", `{"name":"bad-format"}`},
},
statusCode: http.StatusOK,
response: `{"D2":"Contributor name not in proper format"}`,
},
{
name: "Paged Content need collection",
method: http.MethodPost,
body: [][]string{
{"Title", "Object Model", "Full Title", "Parent Collection"},
{"foo", "Paged Content", "foo", ""},
},
statusCode: http.StatusOK,
response: `{"D2":"Paged content must have a parent collection"}`,
},
{
name: "Page needs Parent ID",
method: http.MethodPost,
body: [][]string{
{"Title", "Object Model", "Full Title", "Page/Item Parent ID"},
{"foo", "Page", "foo", ""},
},
statusCode: http.StatusOK,
response: `{"D2":"Pages must have a parent id"}`,
},
{
name: "Do not require title/model on updates",
method: http.MethodPost,
body: [][]string{
{"Title", "Object Model", "Full Title", "Node ID"},
{"", "", "", "2"},
},
statusCode: http.StatusOK,
response: `{}`,
},
}
sharedSecret := "foo"
os.Setenv("SHARED_SECRET", sharedSecret)
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var reqBody *bytes.Buffer
if tt.body != nil {
bodyBytes, err := json.Marshal(tt.body)
if err != nil {
t.Fatalf("Failed to marshal body: %v", err)
}
reqBody = bytes.NewBuffer(bodyBytes)
} else {
reqBody = bytes.NewBuffer(nil)
}
req := httptest.NewRequest(tt.method, "/check-my-work", reqBody)
if tt.name == "Not authorized" {
req.Header.Set("X-Secret", "nope")
} else {
req.Header.Set("X-Secret", sharedSecret)
}
rec := httptest.NewRecorder()
// Call the handler
CheckMyWork(rec, req)
// Assert response status code
if rec.Code != tt.statusCode {
t.Errorf("Expected status code %d, got %d", tt.statusCode, rec.Code)
}
// Assert response body
if rec.Body.String() != tt.response {
t.Errorf("Expected response body: %q, got %q", tt.response, rec.Body.String())
}
})
}
}