Skip to content
This repository was archived by the owner on Jun 24, 2025. It is now read-only.

Commit 23af88f

Browse files
committed
Fix results tests
Struct literals shouldn't be used for scanData; there are quite a few fields now so being explicit makes it more obvious and allows omitting fields. A job needs to be saved before posting job results, so add a saveJob() call to the TestJobResultsHandler test. Test submitting job results twice to ensure it fails the second time.
1 parent cf1f475 commit 23af88f

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

scan_test.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ func TestSaveJob(t *testing.T) {
9595
func TestResultData(t *testing.T) {
9696
createDB("TestResultData")
9797
defer destroyDB()
98-
want := scanData{0, 0, 0, "0001-01-01 00:00", nil}
98+
want := scanData{Total: 0, Latest: 0, New: 0, LastSeen: "0001-01-01 00:00", Results: nil}
9999
data, err := resultData("", "", "")
100100
if err != nil {
101101
t.Fatal(err)
@@ -274,6 +274,9 @@ func TestJobResultsHandler(t *testing.T) {
274274
ts := httptest.NewServer(mux)
275275
defer ts.Close()
276276

277+
// We need to save some job data before trying to submit any
278+
saveJob("192.0.2.1", "80", "tcp", "testuser@example.com")
279+
277280
req, err := http.NewRequest("PUT", ts.URL+"/results/1", data)
278281
if err != nil {
279282
t.Fatal(err)
@@ -287,6 +290,15 @@ func TestJobResultsHandler(t *testing.T) {
287290
if resp.StatusCode != http.StatusOK {
288291
t.Errorf("expected status 200, got %v", resp.StatusCode)
289292
}
293+
294+
// Do it again - submitting the same job should be an error
295+
resp, err = http.DefaultClient.Do(req)
296+
if err != nil {
297+
t.Fatal(err)
298+
}
299+
if resp.StatusCode != http.StatusBadRequest {
300+
t.Errorf("expected status 400, got %v", resp.StatusCode)
301+
}
290302
}
291303

292304
// TestTracerouteHandler tests fetching a route, ensuring it fails, uploading

0 commit comments

Comments
 (0)