@@ -6,14 +6,18 @@ package notify
66
77import (
88 "testing"
9+ "unicode"
910
1011 "github.com/aws/aws-sdk-go/aws"
1112 "github.com/google/go-cmp/cmp"
1213 "github.com/google/go-cmp/cmp/cmpopts"
14+ "github.com/lib/pq"
1315
1416 "github.com/aws/aws-sdk-go/service/sns"
1517 "github.com/aws/aws-sdk-go/service/sns/snsiface"
1618 log "github.com/sirupsen/logrus"
19+
20+ "github.com/adevinta/vulnerability-db/pkg/store"
1721)
1822
1923type snsMock struct {
@@ -26,7 +30,7 @@ func (m *snsMock) Publish(s *sns.PublishInput) (*sns.PublishOutput, error) {
2630 return nil , nil
2731}
2832
29- func TestSNSNotifier_Push (t * testing.T ) {
33+ func TestSNSNotifier_PushFinding (t * testing.T ) {
3034 type fields struct {
3135 conf SNSConfig
3236 sns * snsMock
@@ -36,7 +40,7 @@ func TestSNSNotifier_Push(t *testing.T) {
3640 tests := []struct {
3741 name string
3842 fields fields
39- message map [ string ] interface {}
43+ f FindingNotification
4044 want * sns.PublishInput
4145 wantErr bool
4246 }{
@@ -50,27 +54,165 @@ func TestSNSNotifier_Push(t *testing.T) {
5054 Enabled : true ,
5155 },
5256 },
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+ },
54126 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+ }` )),
56187 TopicArn : aws .String ("arn:aTopic" ),
57188 },
58189 },
59190 }
191+
60192 for _ , tt := range tests {
61193 t .Run (tt .name , func (t * testing.T ) {
62194 s := & SNSNotifier {
63195 conf : tt .fields .conf ,
64196 sns : tt .fields .sns ,
65197 logger : tt .fields .logger ,
66198 }
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 )
69201 }
70202 diff := cmp .Diff (tt .want , tt .fields .sns .notification , cmpopts .IgnoreUnexported (sns.PublishInput {}))
71203 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 )
73205 }
74206 })
75207 }
76208}
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