File size: 2,668 Bytes
f0743f4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// api/test/__mocks__/openid-client.js
module.exports = {
  Issuer: {
    discover: jest.fn().mockResolvedValue({
      Client: jest.fn().mockImplementation(() => ({
        authorizationUrl: jest.fn().mockReturnValue('mock_auth_url'),
        callback: jest.fn().mockResolvedValue({
          access_token: 'mock_access_token',
          id_token: 'mock_id_token',
          claims: () => ({
            sub: 'mock_sub',
            email: 'mock@example.com',
          }),
        }),
        userinfo: jest.fn().mockResolvedValue({
          sub: 'mock_sub',
          email: 'mock@example.com',
        }),
      })),
    }),
  },
  Strategy: jest.fn().mockImplementation((options, verify) => {
    // Store verify to call it if needed, or just mock the strategy behavior
    return { name: 'openid-mock-strategy' };
  }),
  custom: {
    setHttpOptionsDefaults: jest.fn(),
  },
  // Add any other exports from openid-client that are used directly
  // For example, if your code uses `client.Issuer.discover`, then mock `Issuer`
  // If it uses `new Strategy()`, then mock `Strategy`
  // Based on openidStrategy.js, it uses:
  // const client = require('openid-client'); -> client.discovery, client.fetchUserInfo, client.genericGrantRequest
  // const { Strategy: OpenIDStrategy } = require('openid-client/passport');
  // So the mock needs to cover these.
  // The provided mock in openidStrategy.spec.js is a good reference.

  // Simpler mock based on the spec file:
  discovery: jest.fn().mockResolvedValue({
    clientId: 'fake_client_id',
    clientSecret: 'fake_client_secret',
    issuer: 'https://fake-issuer.com',
    serverMetadata: jest.fn().mockReturnValue({
      jwks_uri: 'https://fake-issuer.com/.well-known/jwks.json',
      end_session_endpoint: 'https://fake-issuer.com/logout',
    }),
    Client: jest.fn().mockImplementation(() => ({
      authorizationUrl: jest.fn().mockReturnValue('mock_auth_url'),
      callback: jest.fn().mockResolvedValue({
        access_token: 'mock_access_token',
        id_token: 'mock_id_token',
        claims: () => ({
          sub: 'mock_sub',
          email: 'mock@example.com',
        }),
      }),
      userinfo: jest.fn().mockResolvedValue({
        sub: 'mock_sub',
        email: 'mock@example.com',
      }),
      grant: jest.fn().mockResolvedValue({ access_token: 'mock_grant_token' }), // For genericGrantRequest
    })),
  }),
  fetchUserInfo: jest.fn().mockResolvedValue({
    preferred_username: 'preferred_username',
  }),
  genericGrantRequest: jest
    .fn()
    .mockResolvedValue({ access_token: 'mock_grant_access_token', expires_in: 3600 }),
  customFetch: Symbol('customFetch'),
};