@@ -1286,196 +1286,126 @@ func TestTemplateInsights_RBAC(t *testing.T) {
1286
1286
}
1287
1287
}
1288
1288
1289
- func TestUserLatencyInsights_RBAC (t * testing.T ) {
1289
+ func TestGenericInsights_RBAC (t * testing.T ) {
1290
1290
t .Parallel ()
1291
1291
1292
1292
y , m , d := time .Now ().UTC ().Date ()
1293
1293
today := time .Date (y , m , d , 0 , 0 , 0 , 0 , time .UTC )
1294
1294
1295
+ type fetchInsightsFunc func (ctx context.Context , client * codersdk.Client , startTime , endTime time.Time , templateIDs ... uuid.UUID ) error
1296
+
1295
1297
type test struct {
1296
1298
withTemplate bool
1297
1299
}
1298
1300
1299
- tests := []test {
1300
- {true },
1301
- {false },
1302
- }
1303
-
1304
- for _ , tt := range tests {
1305
- tt := tt
1306
-
1307
- t .Run ("AsOwner" , func (t * testing.T ) {
1308
- t .Parallel ()
1309
-
1310
- client := coderdtest .New (t , & coderdtest.Options {})
1311
- admin := coderdtest .CreateFirstUser (t , client )
1312
-
1313
- ctx , cancel := context .WithTimeout (context .Background (), testutil .WaitShort )
1314
- defer cancel ()
1315
-
1316
- var templateIDs []uuid.UUID
1317
- if tt .withTemplate {
1318
- version := coderdtest .CreateTemplateVersion (t , client , admin .OrganizationID , nil )
1319
- template := coderdtest .CreateTemplate (t , client , admin .OrganizationID , version .ID )
1320
- templateIDs = append (templateIDs , template .ID )
1321
- }
1322
-
1301
+ endpoints := map [string ]fetchInsightsFunc {
1302
+ "UserLatency" : func (ctx context.Context , client * codersdk.Client , startTime , endTime time.Time , templateIDs ... uuid.UUID ) error {
1323
1303
_ , err := client .UserLatencyInsights (ctx , codersdk.UserLatencyInsightsRequest {
1324
- StartTime : today ,
1325
- EndTime : time .Now ().UTC ().Truncate (time .Hour ).Add (time .Hour ), // Round up to include the current hour.
1326
- TemplateIDs : templateIDs ,
1327
- })
1328
- require .NoError (t , err )
1329
- })
1330
- t .Run ("AsTemplateAdmin" , func (t * testing.T ) {
1331
- t .Parallel ()
1332
-
1333
- client := coderdtest .New (t , & coderdtest.Options {})
1334
- admin := coderdtest .CreateFirstUser (t , client )
1335
-
1336
- templateAdmin , _ := coderdtest .CreateAnotherUser (t , client , admin .OrganizationID , rbac .RoleTemplateAdmin ())
1337
-
1338
- ctx , cancel := context .WithTimeout (context .Background (), testutil .WaitShort )
1339
- defer cancel ()
1340
-
1341
- var templateIDs []uuid.UUID
1342
- if tt .withTemplate {
1343
- version := coderdtest .CreateTemplateVersion (t , client , admin .OrganizationID , nil )
1344
- template := coderdtest .CreateTemplate (t , client , admin .OrganizationID , version .ID )
1345
- templateIDs = append (templateIDs , template .ID )
1346
- }
1347
-
1348
- _ , err := templateAdmin .UserLatencyInsights (ctx , codersdk.UserLatencyInsightsRequest {
1349
- StartTime : today ,
1350
- EndTime : time .Now ().UTC ().Truncate (time .Hour ).Add (time .Hour ), // Round up to include the current hour.
1304
+ StartTime : startTime ,
1305
+ EndTime : endTime ,
1351
1306
TemplateIDs : templateIDs ,
1352
1307
})
1353
- require .NoError (t , err )
1354
- })
1355
- t .Run ("AsRegularUser" , func (t * testing.T ) {
1356
- t .Parallel ()
1357
-
1358
- client := coderdtest .New (t , & coderdtest.Options {})
1359
- admin := coderdtest .CreateFirstUser (t , client )
1360
-
1361
- regular , _ := coderdtest .CreateAnotherUser (t , client , admin .OrganizationID )
1362
-
1363
- ctx , cancel := context .WithTimeout (context .Background (), testutil .WaitShort )
1364
- defer cancel ()
1365
-
1366
- var templateIDs []uuid.UUID
1367
- if tt .withTemplate {
1368
- version := coderdtest .CreateTemplateVersion (t , client , admin .OrganizationID , nil )
1369
- template := coderdtest .CreateTemplate (t , client , admin .OrganizationID , version .ID )
1370
- templateIDs = append (templateIDs , template .ID )
1371
- }
1372
-
1373
- _ , err := regular .UserLatencyInsights (ctx , codersdk.UserLatencyInsightsRequest {
1374
- StartTime : today ,
1375
- EndTime : time .Now ().UTC ().Truncate (time .Hour ).Add (time .Hour ), // Round up to include the current hour.
1308
+ return err
1309
+ },
1310
+ "UserActivity" : func (ctx context.Context , client * codersdk.Client , startTime , endTime time.Time , templateIDs ... uuid.UUID ) error {
1311
+ _ , err := client .UserActivityInsights (ctx , codersdk.UserActivityInsightsRequest {
1312
+ StartTime : startTime ,
1313
+ EndTime : endTime ,
1376
1314
TemplateIDs : templateIDs ,
1377
1315
})
1378
- require .Error (t , err )
1379
- var apiErr * codersdk.Error
1380
- require .ErrorAs (t , err , & apiErr )
1381
- require .Equal (t , http .StatusNotFound , apiErr .StatusCode ())
1382
- })
1316
+ return err
1317
+ },
1383
1318
}
1384
- }
1385
1319
1386
- func TestUserActivityInsights_RBAC (t * testing.T ) {
1387
- t .Parallel ()
1320
+ for endpointName , endpoint := range endpoints {
1321
+ endpointName := endpointName
1322
+ endpoint := endpoint
1388
1323
1389
- y , m , d := time . Now (). UTC (). Date ()
1390
- today := time . Date ( y , m , d , 0 , 0 , 0 , 0 , time . UTC )
1324
+ t . Run ( fmt . Sprintf ( "With%sEndpoint" , endpointName ), func ( t * testing. T ) {
1325
+ t . Parallel ( )
1391
1326
1392
- type test struct {
1393
- withTemplate bool
1394
- }
1327
+ tests := []test {
1328
+ {true },
1329
+ {false },
1330
+ }
1395
1331
1396
- tests := []test {
1397
- {true },
1398
- {false },
1399
- }
1332
+ for _ , tt := range tests {
1333
+ tt := tt
1400
1334
1401
- for _ , tt := range tests {
1402
- tt := tt
1335
+ t . Run ( "AsOwner" , func ( t * testing. T ) {
1336
+ t . Parallel ()
1403
1337
1404
- t . Run ( "AsOwner" , func ( t * testing. T ) {
1405
- t . Parallel ( )
1338
+ client := coderdtest . New ( t , & coderdtest. Options {})
1339
+ admin := coderdtest . CreateFirstUser ( t , client )
1406
1340
1407
- client := coderdtest . New ( t , & coderdtest. Options {} )
1408
- admin := coderdtest . CreateFirstUser ( t , client )
1341
+ ctx , cancel := context . WithTimeout ( context . Background (), testutil . WaitShort )
1342
+ defer cancel ( )
1409
1343
1410
- ctx , cancel := context .WithTimeout (context .Background (), testutil .WaitShort )
1411
- defer cancel ()
1344
+ var templateIDs []uuid.UUID
1345
+ if tt .withTemplate {
1346
+ version := coderdtest .CreateTemplateVersion (t , client , admin .OrganizationID , nil )
1347
+ template := coderdtest .CreateTemplate (t , client , admin .OrganizationID , version .ID )
1348
+ templateIDs = append (templateIDs , template .ID )
1349
+ }
1412
1350
1413
- var templateIDs []uuid.UUID
1414
- if tt .withTemplate {
1415
- version := coderdtest .CreateTemplateVersion (t , client , admin .OrganizationID , nil )
1416
- template := coderdtest .CreateTemplate (t , client , admin .OrganizationID , version .ID )
1417
- templateIDs = append (templateIDs , template .ID )
1418
- }
1351
+ err := endpoint (ctx , client ,
1352
+ today ,
1353
+ time .Now ().UTC ().Truncate (time .Hour ).Add (time .Hour ), // Round up to include the current hour.
1354
+ templateIDs ... )
1355
+ require .NoError (t , err )
1356
+ })
1357
+ t .Run ("AsTemplateAdmin" , func (t * testing.T ) {
1358
+ t .Parallel ()
1419
1359
1420
- _ , err := client .UserActivityInsights (ctx , codersdk.UserActivityInsightsRequest {
1421
- StartTime : today ,
1422
- EndTime : time .Now ().UTC ().Truncate (time .Hour ).Add (time .Hour ), // Round up to include the current hour.
1423
- TemplateIDs : templateIDs ,
1424
- })
1425
- require .NoError (t , err )
1426
- })
1427
- t .Run ("AsTemplateAdmin" , func (t * testing.T ) {
1428
- t .Parallel ()
1360
+ client := coderdtest .New (t , & coderdtest.Options {})
1361
+ admin := coderdtest .CreateFirstUser (t , client )
1429
1362
1430
- client := coderdtest .New (t , & coderdtest.Options {})
1431
- admin := coderdtest .CreateFirstUser (t , client )
1363
+ templateAdmin , _ := coderdtest .CreateAnotherUser (t , client , admin .OrganizationID , rbac .RoleTemplateAdmin ())
1432
1364
1433
- templateAdmin , _ := coderdtest .CreateAnotherUser (t , client , admin .OrganizationID , rbac .RoleTemplateAdmin ())
1365
+ ctx , cancel := context .WithTimeout (context .Background (), testutil .WaitShort )
1366
+ defer cancel ()
1434
1367
1435
- ctx , cancel := context .WithTimeout (context .Background (), testutil .WaitShort )
1436
- defer cancel ()
1368
+ var templateIDs []uuid.UUID
1369
+ if tt .withTemplate {
1370
+ version := coderdtest .CreateTemplateVersion (t , client , admin .OrganizationID , nil )
1371
+ template := coderdtest .CreateTemplate (t , client , admin .OrganizationID , version .ID )
1372
+ templateIDs = append (templateIDs , template .ID )
1373
+ }
1437
1374
1438
- var templateIDs []uuid.UUID
1439
- if tt .withTemplate {
1440
- version := coderdtest .CreateTemplateVersion (t , client , admin .OrganizationID , nil )
1441
- template := coderdtest .CreateTemplate (t , client , admin .OrganizationID , version .ID )
1442
- templateIDs = append (templateIDs , template .ID )
1443
- }
1375
+ err := endpoint (ctx , templateAdmin ,
1376
+ today ,
1377
+ time .Now ().UTC ().Truncate (time .Hour ).Add (time .Hour ), // Round up to include the current hour.
1378
+ templateIDs ... )
1379
+ require .NoError (t , err )
1380
+ })
1381
+ t .Run ("AsRegularUser" , func (t * testing.T ) {
1382
+ t .Parallel ()
1444
1383
1445
- _ , err := templateAdmin .UserActivityInsights (ctx , codersdk.UserActivityInsightsRequest {
1446
- StartTime : today ,
1447
- EndTime : time .Now ().UTC ().Truncate (time .Hour ).Add (time .Hour ), // Round up to include the current hour.
1448
- TemplateIDs : templateIDs ,
1449
- })
1450
- require .NoError (t , err )
1451
- })
1452
- t .Run ("AsRegularUser" , func (t * testing.T ) {
1453
- t .Parallel ()
1384
+ client := coderdtest .New (t , & coderdtest.Options {})
1385
+ admin := coderdtest .CreateFirstUser (t , client )
1454
1386
1455
- client := coderdtest .New (t , & coderdtest.Options {})
1456
- admin := coderdtest .CreateFirstUser (t , client )
1387
+ regular , _ := coderdtest .CreateAnotherUser (t , client , admin .OrganizationID )
1457
1388
1458
- regular , _ := coderdtest .CreateAnotherUser (t , client , admin .OrganizationID )
1389
+ ctx , cancel := context .WithTimeout (context .Background (), testutil .WaitShort )
1390
+ defer cancel ()
1459
1391
1460
- ctx , cancel := context .WithTimeout (context .Background (), testutil .WaitShort )
1461
- defer cancel ()
1392
+ var templateIDs []uuid.UUID
1393
+ if tt .withTemplate {
1394
+ version := coderdtest .CreateTemplateVersion (t , client , admin .OrganizationID , nil )
1395
+ template := coderdtest .CreateTemplate (t , client , admin .OrganizationID , version .ID )
1396
+ templateIDs = append (templateIDs , template .ID )
1397
+ }
1462
1398
1463
- var templateIDs []uuid.UUID
1464
- if tt .withTemplate {
1465
- version := coderdtest .CreateTemplateVersion (t , client , admin .OrganizationID , nil )
1466
- template := coderdtest .CreateTemplate (t , client , admin .OrganizationID , version .ID )
1467
- templateIDs = append (templateIDs , template .ID )
1399
+ err := endpoint (ctx , regular ,
1400
+ today ,
1401
+ time .Now ().UTC ().Truncate (time .Hour ).Add (time .Hour ), // Round up to include the current hour.
1402
+ templateIDs ... )
1403
+ require .Error (t , err )
1404
+ var apiErr * codersdk.Error
1405
+ require .ErrorAs (t , err , & apiErr )
1406
+ require .Equal (t , http .StatusNotFound , apiErr .StatusCode ())
1407
+ })
1468
1408
}
1469
-
1470
- _ , err := regular .UserActivityInsights (ctx , codersdk.UserActivityInsightsRequest {
1471
- StartTime : today ,
1472
- EndTime : time .Now ().UTC ().Truncate (time .Hour ).Add (time .Hour ), // Round up to include the current hour.
1473
- TemplateIDs : templateIDs ,
1474
- })
1475
- require .Error (t , err )
1476
- var apiErr * codersdk.Error
1477
- require .ErrorAs (t , err , & apiErr )
1478
- require .Equal (t , http .StatusNotFound , apiErr .StatusCode ())
1479
1409
})
1480
1410
}
1481
1411
}
0 commit comments