1
- package main
1
+ package cron
2
2
3
3
import (
4
4
"context"
5
5
"fmt"
6
- "io"
7
- "net/http"
8
- "net/http/httptest"
9
6
"os"
10
7
"strconv"
11
8
"strings"
@@ -17,7 +14,6 @@ import (
17
14
"github.com/fleetdm/fleet/v4/server/fleet"
18
15
"github.com/fleetdm/fleet/v4/server/mock"
19
16
kitlog "github.com/go-kit/log"
20
-
21
17
"github.com/stretchr/testify/require"
22
18
)
23
19
@@ -207,16 +203,19 @@ func TestCalendarEventsMultipleHosts(t *testing.T) {
207
203
calendar .ClearMockEvents ()
208
204
})
209
205
210
- // TODO(lucas): Test!
211
- webhookServer := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
212
- require .Equal (t , "POST" , r .Method )
213
- requestBodyBytes , err := io .ReadAll (r .Body )
214
- require .NoError (t , err )
215
- t .Logf ("webhook request: %s\n " , requestBodyBytes )
216
- }))
217
- t .Cleanup (func () {
218
- webhookServer .Close ()
219
- })
206
+ //
207
+ // Test setup
208
+ //
209
+ // team1:
210
+ //
211
+ // policyID1 (calendar)
212
+ // policyID2 (calendar)
213
+ //
214
+ // hostID1 has [email protected] not passing policies.
215
+ // hostID2 has [email protected] passing policies.
216
+ // hostID3 does not have example.com email and is not passing policies.
217
+ // hostID4 does not have example.com email and is passing policies.
218
+ //
220
219
221
220
ds .AppConfigFunc = func (ctx context.Context ) (* fleet.AppConfig , error ) {
222
221
return & fleet.AppConfig {
@@ -242,7 +241,7 @@ func TestCalendarEventsMultipleHosts(t *testing.T) {
242
241
Integrations : fleet.TeamIntegrations {
243
242
GoogleCalendar : & fleet.TeamGoogleCalendarIntegration {
244
243
Enable : true ,
245
- WebhookURL : webhookServer . URL ,
244
+ WebhookURL : "https://foo.example.com" ,
246
245
},
247
246
},
248
247
},
@@ -268,12 +267,13 @@ func TestCalendarEventsMultipleHosts(t *testing.T) {
268
267
269
268
hostID1 ,
userEmail1 := uint (
100 ),
"[email protected] "
270
269
hostID2 ,
userEmail2 := uint (
101 ),
"[email protected] "
271
- hostID3 , userEmail3 := uint (
102 )
, "[email protected] "
272
- hostID4 , userEmail4 := uint (
103 )
, "[email protected] "
270
+ hostID3 := uint (102 )
271
+ hostID4 := uint (103 )
273
272
274
273
ds .GetTeamHostsPolicyMembershipsFunc = func (
275
274
ctx context.Context , domain string , teamID uint , policyIDs []uint ,
276
275
) ([]fleet.HostPolicyMembershipData , error ) {
276
+ require .Equal (t , "example.com" , domain )
277
277
require .Equal (t , teamID1 , teamID )
278
278
require .Equal (t , []uint {policyID1 , policyID2 }, policyIDs )
279
279
return []fleet.HostPolicyMembershipData {
@@ -289,12 +289,12 @@ func TestCalendarEventsMultipleHosts(t *testing.T) {
289
289
},
290
290
{
291
291
HostID : hostID3 ,
292
- Email : userEmail3 ,
292
+ Email : "" , // because it does not belong to example.com
293
293
Passing : false ,
294
294
},
295
295
{
296
296
HostID : hostID4 ,
297
- Email : userEmail4 ,
297
+ Email : "" , // because it does not belong to example.com
298
298
Passing : true ,
299
299
},
300
300
}, nil
@@ -304,33 +304,54 @@ func TestCalendarEventsMultipleHosts(t *testing.T) {
304
304
return nil , nil , notFoundErr {}
305
305
}
306
306
307
+ var eventsMu sync.Mutex
308
+ calendarEvents := make (map [string ]* fleet.CalendarEvent )
309
+ hostCalendarEvents := make (map [uint ]* fleet.HostCalendarEvent )
310
+
307
311
ds .CreateOrUpdateCalendarEventFunc = func (ctx context.Context ,
308
312
email string ,
309
313
startTime , endTime time.Time ,
310
314
data []byte ,
311
315
hostID uint ,
312
316
webhookStatus fleet.CalendarWebhookStatus ,
313
317
) (* fleet.CalendarEvent , error ) {
314
- switch email {
315
- case userEmail1 :
316
- require .Equal (t , hostID1 , hostID )
317
- case userEmail2 :
318
- require .Equal (t , hostID2 , hostID )
319
- case userEmail3 :
320
- require .Equal (t , hostID3 , hostID )
321
- case userEmail4 :
322
- require .Equal (t , hostID4 , hostID )
323
- }
318
+ require .Equal (t , hostID1 , hostID )
319
+ require .Equal (t , userEmail1 , email )
324
320
require .Equal (t , fleet .CalendarWebhookStatusNone , webhookStatus )
325
321
require .NotEmpty (t , data )
326
322
require .NotZero (t , startTime )
327
323
require .NotZero (t , endTime )
328
- // Currently, the returned calendar event is unused.
324
+
325
+ eventsMu .Lock ()
326
+ calendarEventID := uint (len (calendarEvents ) + 1 )
327
+ calendarEvents [email ] = & fleet.CalendarEvent {
328
+ ID : calendarEventID ,
329
+ Email : email ,
330
+ StartTime : startTime ,
331
+ EndTime : endTime ,
332
+ Data : data ,
333
+ }
334
+ hostCalendarEventID := uint (len (hostCalendarEvents ) + 1 )
335
+ hostCalendarEvents [hostID ] = & fleet.HostCalendarEvent {
336
+ ID : hostCalendarEventID ,
337
+ HostID : hostID ,
338
+ CalendarEventID : calendarEventID ,
339
+ WebhookStatus : webhookStatus ,
340
+ }
341
+ eventsMu .Unlock ()
329
342
return nil , nil
330
343
}
331
344
332
345
err := cronCalendarEvents (ctx , ds , logger )
333
346
require .NoError (t , err )
347
+
348
+ eventsMu .Lock ()
349
+ require .Len (t , calendarEvents , 1 )
350
+ require .Len (t , hostCalendarEvents , 1 )
351
+ eventsMu .Unlock ()
352
+
353
+ createdCalendarEvents := calendar .ListGoogleMockEvents ()
354
+ require .Len (t , createdCalendarEvents , 1 )
334
355
}
335
356
336
357
type notFoundErr struct {}
@@ -356,17 +377,6 @@ func TestCalendarEvents1KHosts(t *testing.T) {
356
377
calendar .ClearMockEvents ()
357
378
})
358
379
359
- // TODO(lucas): Use for the test.
360
- webhookServer := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
361
- require .Equal (t , "POST" , r .Method )
362
- requestBodyBytes , err := io .ReadAll (r .Body )
363
- require .NoError (t , err )
364
- t .Logf ("webhook request: %s\n " , requestBodyBytes )
365
- }))
366
- t .Cleanup (func () {
367
- webhookServer .Close ()
368
- })
369
-
370
380
ds .AppConfigFunc = func (ctx context.Context ) (* fleet.AppConfig , error ) {
371
381
return & fleet.AppConfig {
372
382
Integrations : fleet.Integrations {
@@ -395,7 +405,7 @@ func TestCalendarEvents1KHosts(t *testing.T) {
395
405
Integrations : fleet.TeamIntegrations {
396
406
GoogleCalendar : & fleet.TeamGoogleCalendarIntegration {
397
407
Enable : true ,
398
- WebhookURL : webhookServer . URL ,
408
+ WebhookURL : "https://foo.example.com" ,
399
409
},
400
410
},
401
411
},
@@ -406,7 +416,7 @@ func TestCalendarEvents1KHosts(t *testing.T) {
406
416
Integrations : fleet.TeamIntegrations {
407
417
GoogleCalendar : & fleet.TeamGoogleCalendarIntegration {
408
418
Enable : true ,
409
- WebhookURL : webhookServer . URL ,
419
+ WebhookURL : "https://foo.example.com" ,
410
420
},
411
421
},
412
422
},
@@ -417,7 +427,7 @@ func TestCalendarEvents1KHosts(t *testing.T) {
417
427
Integrations : fleet.TeamIntegrations {
418
428
GoogleCalendar : & fleet.TeamGoogleCalendarIntegration {
419
429
Enable : true ,
420
- WebhookURL : webhookServer . URL ,
430
+ WebhookURL : "https://foo.example.com" ,
421
431
},
422
432
},
423
433
},
@@ -428,7 +438,7 @@ func TestCalendarEvents1KHosts(t *testing.T) {
428
438
Integrations : fleet.TeamIntegrations {
429
439
GoogleCalendar : & fleet.TeamGoogleCalendarIntegration {
430
440
Enable : true ,
431
- WebhookURL : webhookServer . URL ,
441
+ WebhookURL : "https://foo.example.com" ,
432
442
},
433
443
},
434
444
},
@@ -439,7 +449,7 @@ func TestCalendarEvents1KHosts(t *testing.T) {
439
449
Integrations : fleet.TeamIntegrations {
440
450
GoogleCalendar : & fleet.TeamGoogleCalendarIntegration {
441
451
Enable : true ,
442
- WebhookURL : webhookServer . URL ,
452
+ WebhookURL : "https://foo.example.com" ,
443
453
},
444
454
},
445
455
},
0 commit comments