Skip to content

Commit 17d9688

Browse files
committed
Refactor tests
1 parent e19c835 commit 17d9688

File tree

1 file changed

+85
-155
lines changed

1 file changed

+85
-155
lines changed

coderd/insights_test.go

Lines changed: 85 additions & 155 deletions
Original file line numberDiff line numberDiff line change
@@ -1286,196 +1286,126 @@ func TestTemplateInsights_RBAC(t *testing.T) {
12861286
}
12871287
}
12881288

1289-
func TestUserLatencyInsights_RBAC(t *testing.T) {
1289+
func TestGenericInsights_RBAC(t *testing.T) {
12901290
t.Parallel()
12911291

12921292
y, m, d := time.Now().UTC().Date()
12931293
today := time.Date(y, m, d, 0, 0, 0, 0, time.UTC)
12941294

1295+
type fetchInsightsFunc func(ctx context.Context, client *codersdk.Client, startTime, endTime time.Time, templateIDs ...uuid.UUID) error
1296+
12951297
type test struct {
12961298
withTemplate bool
12971299
}
12981300

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 {
13231303
_, 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,
13511306
TemplateIDs: templateIDs,
13521307
})
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,
13761314
TemplateIDs: templateIDs,
13771315
})
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+
},
13831318
}
1384-
}
13851319

1386-
func TestUserActivityInsights_RBAC(t *testing.T) {
1387-
t.Parallel()
1320+
for endpointName, endpoint := range endpoints {
1321+
endpointName := endpointName
1322+
endpoint := endpoint
13881323

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()
13911326

1392-
type test struct {
1393-
withTemplate bool
1394-
}
1327+
tests := []test{
1328+
{true},
1329+
{false},
1330+
}
13951331

1396-
tests := []test{
1397-
{true},
1398-
{false},
1399-
}
1332+
for _, tt := range tests {
1333+
tt := tt
14001334

1401-
for _, tt := range tests {
1402-
tt := tt
1335+
t.Run("AsOwner", func(t *testing.T) {
1336+
t.Parallel()
14031337

1404-
t.Run("AsOwner", func(t *testing.T) {
1405-
t.Parallel()
1338+
client := coderdtest.New(t, &coderdtest.Options{})
1339+
admin := coderdtest.CreateFirstUser(t, client)
14061340

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()
14091343

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+
}
14121350

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()
14191359

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)
14291362

1430-
client := coderdtest.New(t, &coderdtest.Options{})
1431-
admin := coderdtest.CreateFirstUser(t, client)
1363+
templateAdmin, _ := coderdtest.CreateAnotherUser(t, client, admin.OrganizationID, rbac.RoleTemplateAdmin())
14321364

1433-
templateAdmin, _ := coderdtest.CreateAnotherUser(t, client, admin.OrganizationID, rbac.RoleTemplateAdmin())
1365+
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitShort)
1366+
defer cancel()
14341367

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+
}
14371374

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()
14441383

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)
14541386

1455-
client := coderdtest.New(t, &coderdtest.Options{})
1456-
admin := coderdtest.CreateFirstUser(t, client)
1387+
regular, _ := coderdtest.CreateAnotherUser(t, client, admin.OrganizationID)
14571388

1458-
regular, _ := coderdtest.CreateAnotherUser(t, client, admin.OrganizationID)
1389+
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitShort)
1390+
defer cancel()
14591391

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+
}
14621398

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+
})
14681408
}
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())
14791409
})
14801410
}
14811411
}

0 commit comments

Comments
 (0)