@@ -6,14 +6,18 @@ package notify
6
6
7
7
import (
8
8
"testing"
9
+ "unicode"
9
10
10
11
"github.com/aws/aws-sdk-go/aws"
11
12
"github.com/google/go-cmp/cmp"
12
13
"github.com/google/go-cmp/cmp/cmpopts"
14
+ "github.com/lib/pq"
13
15
14
16
"github.com/aws/aws-sdk-go/service/sns"
15
17
"github.com/aws/aws-sdk-go/service/sns/snsiface"
16
18
log "github.com/sirupsen/logrus"
19
+
20
+ "github.com/adevinta/vulnerability-db/pkg/store"
17
21
)
18
22
19
23
type snsMock struct {
@@ -26,7 +30,7 @@ func (m *snsMock) Publish(s *sns.PublishInput) (*sns.PublishOutput, error) {
26
30
return nil , nil
27
31
}
28
32
29
- func TestSNSNotifier_Push (t * testing.T ) {
33
+ func TestSNSNotifier_PushFinding (t * testing.T ) {
30
34
type fields struct {
31
35
conf SNSConfig
32
36
sns * snsMock
@@ -36,7 +40,7 @@ func TestSNSNotifier_Push(t *testing.T) {
36
40
tests := []struct {
37
41
name string
38
42
fields fields
39
- message map [ string ] interface {}
43
+ f FindingNotification
40
44
want * sns.PublishInput
41
45
wantErr bool
42
46
}{
@@ -50,27 +54,165 @@ func TestSNSNotifier_Push(t *testing.T) {
50
54
Enabled : true ,
51
55
},
52
56
},
53
- message : map [string ]interface {}{"a" : "b" },
57
+ f : FindingNotification {
58
+ FindingExpanded : store.FindingExpanded {
59
+ Finding : store.Finding {
60
+ ID : "FindingID-1" ,
61
+ AffectedResource : "AffectedResource-1" ,
62
+ Score : 9 ,
63
+ Status : "OPEN" ,
64
+ Details : "Details-1" ,
65
+ ImpactDetails : "ImpactDetails-1" ,
66
+ },
67
+ Issue : store.IssueLabels {
68
+ Issue : store.Issue {
69
+ ID : "IssueID-1" ,
70
+ Summary : "Summary-1" ,
71
+ CWEID : 1 ,
72
+ Description : "Description-1" ,
73
+ Recommendations : pq.StringArray {
74
+ "Recommendation-1" ,
75
+ "Recommendation-2" ,
76
+ },
77
+ ReferenceLinks : pq.StringArray {
78
+ "ReferenceLink-1" ,
79
+ "ReferenceLink-2" ,
80
+ },
81
+ },
82
+ Labels : []string {
83
+ "Label-1" ,
84
+ "Label-2" ,
85
+ },
86
+ },
87
+ Target : store.TargetTeams {
88
+ Target : store.Target {
89
+ ID : "TargetID-1" ,
90
+ Identifier : "Identifier-1" ,
91
+ },
92
+ Teams : []string {
93
+ "Team-1" ,
94
+ "Team-2" ,
95
+ },
96
+ },
97
+ Source : store.Source {
98
+ ID : "SourceID-1" ,
99
+ Instance : "SourceInstance-1" ,
100
+ Options : "SourceOptions-1" ,
101
+ SourceFamily : store.SourceFamily {
102
+ Name : "SourceName-1" ,
103
+ Component : "SourceComponent-1" ,
104
+ },
105
+ },
106
+ Resources : store.Resources {
107
+ {
108
+ Name : "ResourceName-1" ,
109
+ Attributes : []string {
110
+ "Attr-1" ,
111
+ "Attr-2" ,
112
+ },
113
+ Resources : []map [string ]string {
114
+ {
115
+ "Attr-1" : "1" ,
116
+ "Attr-2" : "2" ,
117
+ },
118
+ },
119
+ },
120
+ },
121
+ TotalExposure : 10 ,
122
+ CurrentExposure : 5 ,
123
+ },
124
+ Tag : "tag-1" ,
125
+ },
54
126
want : & sns.PublishInput {
55
- Message : aws .String (`{"a":"b"}` ),
127
+ Message : aws .String (removeSpaces (
128
+ `{
129
+ "id": "FindingID-1",
130
+ "affected_resource": "AffectedResource-1",
131
+ "score": 9,
132
+ "status": "OPEN",
133
+ "details": "Details-1",
134
+ "impact_details": "ImpactDetails-1",
135
+ "issue": {
136
+ "id": "IssueID-1",
137
+ "summary": "Summary-1",
138
+ "cwe_id": 1,
139
+ "description": "Description-1",
140
+ "recommendations": [
141
+ "Recommendation-1",
142
+ "Recommendation-2"
143
+ ],
144
+ "reference_links": [
145
+ "ReferenceLink-1",
146
+ "ReferenceLink-2"
147
+ ],
148
+ "labels": [
149
+ "Label-1",
150
+ "Label-2"
151
+ ]
152
+ },
153
+ "target": {
154
+ "id": "TargetID-1",
155
+ "identifier": "Identifier-1",
156
+ "teams": [
157
+ "Team-1",
158
+ "Team-2"
159
+ ]
160
+ },
161
+ "source": {
162
+ "id": "SourceID-1",
163
+ "instance": "SourceInstance-1",
164
+ "options": "SourceOptions-1",
165
+ "time": "0001-01-01T00:00:00Z",
166
+ "name": "SourceName-1",
167
+ "component": "SourceComponent-1"
168
+ },
169
+ "resources": [
170
+ {
171
+ "name": "ResourceName-1",
172
+ "attributes": [
173
+ "Attr-1",
174
+ "Attr-2"
175
+ ],
176
+ "resources": [
177
+ {
178
+ "Attr-1": "1",
179
+ "Attr-2": "2"
180
+ }
181
+ ]
182
+ }
183
+ ],
184
+ "total_exposure": 10,
185
+ "current_exposure": 5
186
+ }` )),
56
187
TopicArn : aws .String ("arn:aTopic" ),
57
188
},
58
189
},
59
190
}
191
+
60
192
for _ , tt := range tests {
61
193
t .Run (tt .name , func (t * testing.T ) {
62
194
s := & SNSNotifier {
63
195
conf : tt .fields .conf ,
64
196
sns : tt .fields .sns ,
65
197
logger : tt .fields .logger ,
66
198
}
67
- if err := s .Push (tt .message ); (err != nil ) != tt .wantErr {
68
- t .Errorf ("SNSNotifier.Push() error = %v, wantErr %v" , err , tt .wantErr )
199
+ if err := s .PushFinding (tt .f ); (err != nil ) != tt .wantErr {
200
+ t .Errorf ("got error: %v but wanted: %v" , err , tt .wantErr )
69
201
}
70
202
diff := cmp .Diff (tt .want , tt .fields .sns .notification , cmpopts .IgnoreUnexported (sns.PublishInput {}))
71
203
if diff != "" {
72
- t .Errorf ("want!= got. Diffs: %s" , diff )
204
+ t .Errorf ("SNS payload does not match with expected one. Diff: \n %s" , diff )
73
205
}
74
206
})
75
207
}
76
208
}
209
+
210
+ func removeSpaces (s string ) string {
211
+ rr := make ([]rune , 0 , len (s ))
212
+ for _ , r := range s {
213
+ if ! unicode .IsSpace (r ) {
214
+ rr = append (rr , r )
215
+ }
216
+ }
217
+ return string (rr )
218
+ }
0 commit comments