@@ -2166,6 +2166,10 @@ func TestGetAuthorizedConnectionLogsOffset(t *testing.T) {
2166
2166
require .NoError (t , err )
2167
2167
// Then: No logs returned
2168
2168
require .Len (t , logs , 0 , "no logs should be returned" )
2169
+ // And: The count matches the number of logs returned
2170
+ count , err := authDb .CountConnectionLogs (memberCtx , database.CountConnectionLogsParams {})
2171
+ require .NoError (t , err )
2172
+ require .EqualValues (t , len (logs ), count )
2169
2173
})
2170
2174
2171
2175
t .Run ("SiteWideAuditor" , func (t * testing.T ) {
@@ -2184,6 +2188,10 @@ func TestGetAuthorizedConnectionLogsOffset(t *testing.T) {
2184
2188
require .NoError (t , err )
2185
2189
// Then: All logs are returned
2186
2190
require .ElementsMatch (t , connectionOnlyIDs (allLogs ), connectionOnlyIDs (logs ))
2191
+ // And: The count matches the number of logs returned
2192
+ count , err := authDb .CountConnectionLogs (siteAuditorCtx , database.CountConnectionLogsParams {})
2193
+ require .NoError (t , err )
2194
+ require .EqualValues (t , len (logs ), count )
2187
2195
})
2188
2196
2189
2197
t .Run ("SingleOrgAuditor" , func (t * testing.T ) {
@@ -2203,6 +2211,10 @@ func TestGetAuthorizedConnectionLogsOffset(t *testing.T) {
2203
2211
require .NoError (t , err )
2204
2212
// Then: Only the logs for the organization are returned
2205
2213
require .ElementsMatch (t , orgConnectionLogs [orgID ], connectionOnlyIDs (logs ))
2214
+ // And: The count matches the number of logs returned
2215
+ count , err := authDb .CountConnectionLogs (orgAuditCtx , database.CountConnectionLogsParams {})
2216
+ require .NoError (t , err )
2217
+ require .EqualValues (t , len (logs ), count )
2206
2218
})
2207
2219
2208
2220
t .Run ("TwoOrgAuditors" , func (t * testing.T ) {
@@ -2223,6 +2235,10 @@ func TestGetAuthorizedConnectionLogsOffset(t *testing.T) {
2223
2235
require .NoError (t , err )
2224
2236
// Then: All logs for both organizations are returned
2225
2237
require .ElementsMatch (t , append (orgConnectionLogs [first ], orgConnectionLogs [second ]... ), connectionOnlyIDs (logs ))
2238
+ // And: The count matches the number of logs returned
2239
+ count , err := authDb .CountConnectionLogs (multiOrgAuditCtx , database.CountConnectionLogsParams {})
2240
+ require .NoError (t , err )
2241
+ require .EqualValues (t , len (logs ), count )
2226
2242
})
2227
2243
2228
2244
t .Run ("ErroneousOrg" , func (t * testing.T ) {
@@ -2241,9 +2257,71 @@ func TestGetAuthorizedConnectionLogsOffset(t *testing.T) {
2241
2257
require .NoError (t , err )
2242
2258
// Then: No logs are returned
2243
2259
require .Len (t , logs , 0 , "no logs should be returned" )
2260
+ // And: The count matches the number of logs returned
2261
+ count , err := authDb .CountConnectionLogs (userCtx , database.CountConnectionLogsParams {})
2262
+ require .NoError (t , err )
2263
+ require .EqualValues (t , len (logs ), count )
2244
2264
})
2245
2265
}
2246
2266
2267
+ func TestCountConnectionLogs (t * testing.T ) {
2268
+ t .Parallel ()
2269
+ ctx := testutil .Context (t , testutil .WaitLong )
2270
+
2271
+ db , _ := dbtestutil .NewDB (t )
2272
+
2273
+ orgA := dbfake .Organization (t , db ).Do ()
2274
+ userA := dbgen .User (t , db , database.User {})
2275
+ tplA := dbgen .Template (t , db , database.Template {OrganizationID : orgA .Org .ID , CreatedBy : userA .ID })
2276
+ wsA := dbgen .Workspace (t , db , database.WorkspaceTable {OwnerID : userA .ID , OrganizationID : orgA .Org .ID , TemplateID : tplA .ID })
2277
+
2278
+ orgB := dbfake .Organization (t , db ).Do ()
2279
+ userB := dbgen .User (t , db , database.User {})
2280
+ tplB := dbgen .Template (t , db , database.Template {OrganizationID : orgB .Org .ID , CreatedBy : userB .ID })
2281
+ wsB := dbgen .Workspace (t , db , database.WorkspaceTable {OwnerID : userB .ID , OrganizationID : orgB .Org .ID , TemplateID : tplB .ID })
2282
+
2283
+ // Create logs for two different orgs.
2284
+ for i := 0 ; i < 20 ; i ++ {
2285
+ dbgen .ConnectionLog (t , db , database.UpsertConnectionLogParams {
2286
+ OrganizationID : wsA .OrganizationID ,
2287
+ WorkspaceOwnerID : wsA .OwnerID ,
2288
+ WorkspaceID : wsA .ID ,
2289
+ Type : database .ConnectionTypeSsh ,
2290
+ })
2291
+ }
2292
+ for i := 0 ; i < 10 ; i ++ {
2293
+ dbgen .ConnectionLog (t , db , database.UpsertConnectionLogParams {
2294
+ OrganizationID : wsB .OrganizationID ,
2295
+ WorkspaceOwnerID : wsB .OwnerID ,
2296
+ WorkspaceID : wsB .ID ,
2297
+ Type : database .ConnectionTypeSsh ,
2298
+ })
2299
+ }
2300
+
2301
+ // Count with a filter for orgA.
2302
+ countParams := database.CountConnectionLogsParams {
2303
+ OrganizationID : orgA .Org .ID ,
2304
+ }
2305
+ totalCount , err := db .CountConnectionLogs (ctx , countParams )
2306
+ require .NoError (t , err )
2307
+ require .Equal (t , int64 (20 ), totalCount )
2308
+
2309
+ // Get a paginated result for the same filter.
2310
+ getParams := database.GetConnectionLogsOffsetParams {
2311
+ OrganizationID : orgA .Org .ID ,
2312
+ LimitOpt : 5 ,
2313
+ OffsetOpt : 10 ,
2314
+ }
2315
+ logs , err := db .GetConnectionLogsOffset (ctx , getParams )
2316
+ require .NoError (t , err )
2317
+ require .Len (t , logs , 5 )
2318
+
2319
+ // The count with the filter should remain the same, independent of pagination.
2320
+ countAfterGet , err := db .CountConnectionLogs (ctx , countParams )
2321
+ require .NoError (t , err )
2322
+ require .Equal (t , int64 (20 ), countAfterGet )
2323
+ }
2324
+
2247
2325
func TestConnectionLogsOffsetFilters (t * testing.T ) {
2248
2326
t .Parallel ()
2249
2327
ctx := testutil .Context (t , testutil .WaitLong )
@@ -2482,7 +2560,24 @@ func TestConnectionLogsOffsetFilters(t *testing.T) {
2482
2560
t .Parallel ()
2483
2561
logs , err := db .GetConnectionLogsOffset (ctx , tc .params )
2484
2562
require .NoError (t , err )
2563
+ count , err := db .CountConnectionLogs (ctx , database.CountConnectionLogsParams {
2564
+ OrganizationID : tc .params .OrganizationID ,
2565
+ WorkspaceOwner : tc .params .WorkspaceOwner ,
2566
+ Type : tc .params .Type ,
2567
+ UserID : tc .params .UserID ,
2568
+ Username : tc .params .Username ,
2569
+ UserEmail : tc .params .UserEmail ,
2570
+ StartedAfter : tc .params .StartedAfter ,
2571
+ StartedBefore : tc .params .StartedBefore ,
2572
+ WorkspaceID : tc .params .WorkspaceID ,
2573
+ ConnectionID : tc .params .ConnectionID ,
2574
+ Status : tc .params .Status ,
2575
+ WorkspaceOwnerID : tc .params .WorkspaceOwnerID ,
2576
+ WorkspaceOwnerEmail : tc .params .WorkspaceOwnerEmail ,
2577
+ })
2578
+ require .NoError (t , err )
2485
2579
require .ElementsMatch (t , tc .expectedLogIDs , connectionOnlyIDs (logs ))
2580
+ require .Equal (t , len (tc .expectedLogIDs ), int (count ), "CountConnectionLogs should match the number of returned logs (no offset or limit)" )
2486
2581
})
2487
2582
}
2488
2583
}
0 commit comments