@@ -44,8 +44,18 @@ func (f *fakePublisher) Publish(data *auditor.EventPayload) error {
4444 return nil
4545}
4646
47- func newTestDispatcher (p eventPublisher ) * AuditDispatcher {
48- return & AuditDispatcher {publisher : p , log : servicelogger .ScopedHelper (log .DefaultLogger , "test" )}
47+ // newTestDispatcher builds an AuditDispatcher backed by the given fake. A nil
48+ // fake leaves the underlying publisher disabled, mirroring NewAuditDispatcher.
49+ func newTestDispatcher (p * fakePublisher ) * AuditDispatcher {
50+ var pub auditor.Publisher
51+ if p != nil {
52+ pub = p
53+ }
54+
55+ return & AuditDispatcher {
56+ dispatcher : auditor .NewDispatcher (pub , log .DefaultLogger ),
57+ log : servicelogger .ScopedHelper (log .DefaultLogger , "test" ),
58+ }
4959}
5060
5161func testUploadedEntry () auditor.LogEntry {
@@ -63,51 +73,53 @@ const testOrgID = "1089bb36-e27b-428b-8009-d015c8737c54"
6373
6474func TestAuditDispatcherDispatch (t * testing.T ) {
6575 tests := []struct {
66- name string
67- dispatcher * AuditDispatcher
76+ name string
77+ // publisher is the fake backing the dispatcher; nil means the dispatcher
78+ // is disabled (no NATS). nilDispatcher exercises the nil-receiver path.
79+ publisher * fakePublisher
80+ nilDispatcher bool
6881 entry auditor.LogEntry
6982 claims * casJWT.Claims
7083 wantPublished int
7184 }{
7285 {
73- name : "nil dispatcher is a no-op" ,
74- dispatcher : nil ,
75- entry : testUploadedEntry (),
76- claims : & casJWT.Claims {OrgID : testOrgID },
86+ name : "nil dispatcher is a no-op" ,
87+ nilDispatcher : true ,
88+ entry : testUploadedEntry (),
89+ claims : & casJWT.Claims {OrgID : testOrgID },
7790 },
7891 {
79- name : "nil publisher is a no-op" ,
80- dispatcher : newTestDispatcher (nil ),
81- entry : testUploadedEntry (),
82- claims : & casJWT.Claims {OrgID : testOrgID },
92+ name : "nil publisher is a no-op" ,
93+ entry : testUploadedEntry (),
94+ claims : & casJWT.Claims {OrgID : testOrgID },
8395 },
8496 {
85- name : "internal control plane traffic is skipped" ,
86- dispatcher : newTestDispatcher ( & fakePublisher {}) ,
87- entry : testUploadedEntry (),
88- claims : & casJWT.Claims {OrgID : testOrgID , SourceInternal : true },
97+ name : "internal control plane traffic is skipped" ,
98+ publisher : & fakePublisher {},
99+ entry : testUploadedEntry (),
100+ claims : & casJWT.Claims {OrgID : testOrgID , SourceInternal : true },
89101 },
90102 {
91- name : "invalid org id is skipped" ,
92- dispatcher : newTestDispatcher ( & fakePublisher {}) ,
93- entry : testUploadedEntry (),
94- claims : & casJWT.Claims {OrgID : "not-an-uuid" },
103+ name : "invalid org id is skipped" ,
104+ publisher : & fakePublisher {},
105+ entry : testUploadedEntry (),
106+ claims : & casJWT.Claims {OrgID : "not-an-uuid" },
95107 },
96108 {
97- name : "invalid entry is skipped" ,
98- dispatcher : newTestDispatcher ( & fakePublisher {}) ,
99- entry : & events.CASArtifactUploaded {CASArtifactBase : & events.CASArtifactBase {}},
100- claims : & casJWT.Claims {OrgID : testOrgID },
109+ name : "invalid entry is skipped" ,
110+ publisher : & fakePublisher {},
111+ entry : & events.CASArtifactUploaded {CASArtifactBase : & events.CASArtifactBase {}},
112+ claims : & casJWT.Claims {OrgID : testOrgID },
101113 },
102114 {
103- name : "publish errors are swallowed" ,
104- dispatcher : newTestDispatcher ( & fakePublisher {err : errors .New ("nats is down" )}) ,
105- entry : testUploadedEntry (),
106- claims : & casJWT.Claims {OrgID : testOrgID },
115+ name : "publish errors are swallowed" ,
116+ publisher : & fakePublisher {err : errors .New ("nats is down" )},
117+ entry : testUploadedEntry (),
118+ claims : & casJWT.Claims {OrgID : testOrgID },
107119 },
108120 {
109121 name : "client traffic is published" ,
110- dispatcher : newTestDispatcher ( & fakePublisher {}) ,
122+ publisher : & fakePublisher {},
111123 entry : testUploadedEntry (),
112124 claims : & casJWT.Claims {OrgID : testOrgID },
113125 wantPublished : 1 ,
@@ -116,20 +128,24 @@ func TestAuditDispatcherDispatch(t *testing.T) {
116128
117129 for _ , tc := range tests {
118130 t .Run (tc .name , func (t * testing.T ) {
131+ var d * AuditDispatcher
132+ if ! tc .nilDispatcher {
133+ d = newTestDispatcher (tc .publisher )
134+ }
135+
119136 // must never panic nor return an error
120- tc . dispatcher .Dispatch (tc .entry , tc .claims )
137+ d .Dispatch (tc .entry , tc .claims )
121138
122- if tc .dispatcher == nil || tc . dispatcher . publisher == nil {
139+ if tc .publisher == nil {
123140 return
124141 }
125- fake := tc .dispatcher .publisher .(* fakePublisher )
126142
127- require .Len (t , fake .published , tc .wantPublished )
143+ require .Len (t , tc . publisher .published , tc .wantPublished )
128144 if tc .wantPublished == 0 {
129145 return
130146 }
131147
132- got := fake .published [0 ]
148+ got := tc . publisher .published [0 ]
133149 assert .Equal (t , auditor .AuditEventType , got .EventType )
134150 assert .Equal (t , events .CASArtifactUploadedActionType , got .Data .ActionType )
135151 assert .Equal (t , events .CASArtifactType , got .Data .TargetType )
0 commit comments