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