• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

bpatrik / pigallery2 / 17528940173

07 Sep 2025 01:06PM UTC coverage: 65.007% (-0.01%) from 65.017%
17528940173

push

github

bpatrik
Fix session manager tests #1015

1254 of 2192 branches covered (57.21%)

Branch coverage included in aggregate %.

4648 of 6887 relevant lines covered (67.49%)

4425.53 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

84.38
/src/backend/model/database/SessionManager.ts
1
import * as crypto from 'crypto';
1✔
2
import {ContextUser, SessionContext} from '../SessionContext';
1✔
3
import {SearchQueryUtils} from '../../../common/SearchQueryUtils';
1✔
4
import {Config} from '../../../common/config/private/Config';
1✔
5
import {ANDSearchQuery, SearchQueryDTO, SearchQueryTypes} from '../../../common/entities/SearchQueryDTO';
1✔
6
import {SharingEntity} from './enitites/SharingEntity';
7
import {ObjectManagers} from '../ObjectManagers';
1✔
8
import {Logger} from '../../Logger';
1✔
9

10
const LOG_TAG = '[SessionManager]';
1✔
11

12
export class SessionManager {
1✔
13

14
  public static readonly NO_PROJECTION_KEY = 'pr:' + crypto.createHash('md5').update('No Key').digest('hex');
1✔
15

16
  private getQueryForUser(user: ContextUser) {
17
    let blockQuery = user.overrideAllowBlockList ? user.blockQuery : Config.Users.blockQuery;
320✔
18
    const allowQuery = user.overrideAllowBlockList ? user.allowQuery : Config.Users.allowQuery;
320✔
19

20
    if (SearchQueryUtils.isQueryEmpty(allowQuery) && SearchQueryUtils.isQueryEmpty(blockQuery)) {
320✔
21
      return null;
277✔
22
    }
23

24
    if (!SearchQueryUtils.isQueryEmpty(blockQuery)) {
43✔
25
      blockQuery = SearchQueryUtils.negate(blockQuery);
6✔
26
    }
27
    let query = !SearchQueryUtils.isQueryEmpty(allowQuery) ? allowQuery : blockQuery;
43✔
28
    if (!SearchQueryUtils.isQueryEmpty(allowQuery) && !SearchQueryUtils.isQueryEmpty(blockQuery)) {
43✔
29
      query = {
4✔
30
        type: SearchQueryTypes.AND,
31
        list: [
32
          allowQuery,
33
          blockQuery
34
        ]
35
      } as ANDSearchQuery;
36
    }
37
    return query;
43✔
38

39
  }
40

41
  public buildAllowListForSharing(sharing: SharingEntity): SearchQueryDTO {
42
    const creatorQuery = this.getQueryForUser(sharing.creator);
16✔
43
    let finalQuery = sharing.searchQuery;
16✔
44
    if (creatorQuery) {
16✔
45
      finalQuery = {
4✔
46
        type: SearchQueryTypes.AND,
47
        list: [
48
          creatorQuery,
49
          sharing.searchQuery
50
        ]
51
      } as ANDSearchQuery;
52
    }
53
    return finalQuery;
16✔
54
  }
55

56
  public createProjectionKey(q: SearchQueryDTO) {
57
    const canonical = SearchQueryUtils.stringifyForComparison(q);
44✔
58
    return 'pr:' + crypto.createHash('md5').update(canonical).digest('hex');
44✔
59
  }
60

61
  public async buildContext(user: ContextUser): Promise<SessionContext> {
62
    const context = new SessionContext();
304✔
63
    context.user = user;
304✔
64
    context.user.projectionKey = SessionManager.NO_PROJECTION_KEY;
304✔
65
    let finalQuery = this.getQueryForUser(user);
304✔
66

67
    if (finalQuery) {
304✔
68
      // Build the Brackets-based query
69
      context.projectionQuery = await ObjectManagers.getInstance().SearchManager.prepareAndBuildWhereQuery(finalQuery);
39✔
70
      context.hasDirectoryProjection = ObjectManagers.getInstance().SearchManager.hasDirectoryQuery(finalQuery);
39✔
71
      context.user.projectionKey = this.createProjectionKey(finalQuery);
39✔
72
      if (SearchQueryUtils.isQueryEmpty(finalQuery)) {
39!
73
        Logger.silly(LOG_TAG, 'Empty Projection query.');
×
74
      } else {
75
        Logger.silly(LOG_TAG, 'Projection query: ' + JSON.stringify(finalQuery));
39✔
76
      }
77
    }
78
    return context;
304✔
79
  }
80

81
  async getAvailableUserSessions(): Promise<SessionContext[]> {
82
    // List all users and build a session context for each
83
    const users = await ObjectManagers.getInstance().UserManager.find({} as any);
×
84
    const sessions: SessionContext[] = [];
×
85
    for (const u of users as unknown as ContextUser[]) {
×
86
      try {
×
87
        const ctx = await this.buildContext(u);
×
88
        sessions.push(ctx);
×
89
      } catch (e) {
90
        // Log and continue on individual user context build failure to ensure we return other sessions
91
        Logger.warn(LOG_TAG, 'Failed to build session context for user id=' + (u as any)?.id + ': ' + (e as Error)?.message);
×
92
      }
93
    }
94
    return sessions;
×
95
  }
96
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc