github.com/wolfi-dev/wolfictl@v0.16.11/pkg/configs/advisory/v2/event_test.go (about) 1 package v2 2 3 import ( 4 "testing" 5 "time" 6 ) 7 8 func TestEvent_Validate(t *testing.T) { 9 testTime := Timestamp(time.Date(2022, 9, 26, 0, 0, 0, 0, time.UTC)) 10 11 tests := []struct { 12 name string 13 event Event 14 wantErr bool 15 }{ 16 { 17 name: "valid", 18 event: Event{ 19 Timestamp: testTime, 20 Type: EventTypeDetection, 21 Data: Detection{ 22 Type: DetectionTypeManual, 23 }, 24 }, 25 wantErr: false, 26 }, 27 { 28 name: "invalid timestamp", 29 event: Event{ 30 Timestamp: Timestamp{}, 31 Type: EventTypeTruePositiveDetermination, 32 Data: TruePositiveDetermination{ 33 Note: "this is a note", 34 }, 35 }, 36 wantErr: true, 37 }, 38 { 39 name: "invalid type", 40 event: Event{ 41 Timestamp: testTime, 42 Type: "foo", 43 }, 44 wantErr: true, 45 }, 46 { 47 name: "no type", 48 event: Event{ 49 Timestamp: testTime, 50 Data: AnalysisNotPlanned{}, 51 }, 52 wantErr: true, 53 }, 54 { 55 name: "invalid data for type", 56 event: Event{ 57 Timestamp: testTime, 58 Type: EventTypeFixed, 59 Data: Detection{ 60 Type: DetectionTypeManual, 61 }, 62 }, 63 wantErr: true, 64 }, 65 { 66 name: "timestamp in the future", 67 event: Event{ 68 Timestamp: Timestamp(time.Now().Add(24 * time.Hour)), 69 Type: EventTypeDetection, 70 Data: Detection{ 71 Type: DetectionTypeManual, 72 }, 73 }, 74 wantErr: true, 75 }, 76 } 77 78 for _, tt := range tests { 79 t.Run(tt.name, func(t *testing.T) { 80 if err := tt.event.Validate(); (err != nil) != tt.wantErr { 81 t.Errorf("Validate() error = %v, wantErr %v", err, tt.wantErr) 82 } 83 }) 84 } 85 }