|
| 1 | +jest.mock('@librechat/data-schemas', () => ({ |
| 2 | + logger: { error: jest.fn(), debug: jest.fn(), warn: jest.fn() }, |
| 3 | +})); |
| 4 | +jest.mock('~/server/services/GraphTokenService', () => ({ |
| 5 | + getGraphApiToken: jest.fn(), |
| 6 | +})); |
| 7 | +jest.mock('~/server/services/AuthService', () => ({ |
| 8 | + requestPasswordReset: jest.fn(), |
| 9 | + setOpenIDAuthTokens: jest.fn(), |
| 10 | + resetPassword: jest.fn(), |
| 11 | + setAuthTokens: jest.fn(), |
| 12 | + registerUser: jest.fn(), |
| 13 | +})); |
| 14 | +jest.mock('~/strategies', () => ({ getOpenIdConfig: jest.fn() })); |
| 15 | +jest.mock('~/models', () => ({ |
| 16 | + deleteAllUserSessions: jest.fn(), |
| 17 | + getUserById: jest.fn(), |
| 18 | + findSession: jest.fn(), |
| 19 | + updateUser: jest.fn(), |
| 20 | + findUser: jest.fn(), |
| 21 | +})); |
| 22 | +jest.mock('@librechat/api', () => ({ |
| 23 | + isEnabled: jest.fn(), |
| 24 | + findOpenIDUser: jest.fn(), |
| 25 | +})); |
| 26 | + |
| 27 | +const { isEnabled } = require('@librechat/api'); |
| 28 | +const { getGraphApiToken } = require('~/server/services/GraphTokenService'); |
| 29 | +const { graphTokenController } = require('./AuthController'); |
| 30 | + |
| 31 | +describe('graphTokenController', () => { |
| 32 | + let req, res; |
| 33 | + |
| 34 | + beforeEach(() => { |
| 35 | + jest.clearAllMocks(); |
| 36 | + isEnabled.mockReturnValue(true); |
| 37 | + |
| 38 | + req = { |
| 39 | + user: { |
| 40 | + openidId: 'oid-123', |
| 41 | + provider: 'openid', |
| 42 | + federatedTokens: { |
| 43 | + access_token: 'federated-access-token', |
| 44 | + id_token: 'federated-id-token', |
| 45 | + }, |
| 46 | + }, |
| 47 | + headers: { authorization: 'Bearer app-jwt-which-is-id-token' }, |
| 48 | + query: { scopes: 'https://graph.microsoft.com/.default' }, |
| 49 | + }; |
| 50 | + |
| 51 | + res = { |
| 52 | + status: jest.fn().mockReturnThis(), |
| 53 | + json: jest.fn(), |
| 54 | + }; |
| 55 | + |
| 56 | + getGraphApiToken.mockResolvedValue({ |
| 57 | + access_token: 'graph-access-token', |
| 58 | + token_type: 'Bearer', |
| 59 | + expires_in: 3600, |
| 60 | + }); |
| 61 | + }); |
| 62 | + |
| 63 | + it('should pass federatedTokens.access_token as OBO assertion, not the auth header bearer token', async () => { |
| 64 | + await graphTokenController(req, res); |
| 65 | + |
| 66 | + expect(getGraphApiToken).toHaveBeenCalledWith( |
| 67 | + req.user, |
| 68 | + 'federated-access-token', |
| 69 | + 'https://graph.microsoft.com/.default', |
| 70 | + ); |
| 71 | + expect(getGraphApiToken).not.toHaveBeenCalledWith( |
| 72 | + expect.anything(), |
| 73 | + 'app-jwt-which-is-id-token', |
| 74 | + expect.anything(), |
| 75 | + ); |
| 76 | + }); |
| 77 | + |
| 78 | + it('should return the graph token response on success', async () => { |
| 79 | + await graphTokenController(req, res); |
| 80 | + |
| 81 | + expect(res.json).toHaveBeenCalledWith({ |
| 82 | + access_token: 'graph-access-token', |
| 83 | + token_type: 'Bearer', |
| 84 | + expires_in: 3600, |
| 85 | + }); |
| 86 | + }); |
| 87 | + |
| 88 | + it('should return 403 when user is not authenticated via Entra ID', async () => { |
| 89 | + req.user.provider = 'google'; |
| 90 | + req.user.openidId = undefined; |
| 91 | + |
| 92 | + await graphTokenController(req, res); |
| 93 | + |
| 94 | + expect(res.status).toHaveBeenCalledWith(403); |
| 95 | + expect(getGraphApiToken).not.toHaveBeenCalled(); |
| 96 | + }); |
| 97 | + |
| 98 | + it('should return 403 when OPENID_REUSE_TOKENS is not enabled', async () => { |
| 99 | + isEnabled.mockReturnValue(false); |
| 100 | + |
| 101 | + await graphTokenController(req, res); |
| 102 | + |
| 103 | + expect(res.status).toHaveBeenCalledWith(403); |
| 104 | + expect(getGraphApiToken).not.toHaveBeenCalled(); |
| 105 | + }); |
| 106 | + |
| 107 | + it('should return 400 when scopes query param is missing', async () => { |
| 108 | + req.query.scopes = undefined; |
| 109 | + |
| 110 | + await graphTokenController(req, res); |
| 111 | + |
| 112 | + expect(res.status).toHaveBeenCalledWith(400); |
| 113 | + expect(getGraphApiToken).not.toHaveBeenCalled(); |
| 114 | + }); |
| 115 | + |
| 116 | + it('should return 401 when federatedTokens.access_token is missing', async () => { |
| 117 | + req.user.federatedTokens = {}; |
| 118 | + |
| 119 | + await graphTokenController(req, res); |
| 120 | + |
| 121 | + expect(res.status).toHaveBeenCalledWith(401); |
| 122 | + expect(getGraphApiToken).not.toHaveBeenCalled(); |
| 123 | + }); |
| 124 | + |
| 125 | + it('should return 401 when federatedTokens is absent entirely', async () => { |
| 126 | + req.user.federatedTokens = undefined; |
| 127 | + |
| 128 | + await graphTokenController(req, res); |
| 129 | + |
| 130 | + expect(res.status).toHaveBeenCalledWith(401); |
| 131 | + expect(getGraphApiToken).not.toHaveBeenCalled(); |
| 132 | + }); |
| 133 | + |
| 134 | + it('should return 500 when getGraphApiToken throws', async () => { |
| 135 | + getGraphApiToken.mockRejectedValue(new Error('OBO exchange failed')); |
| 136 | + |
| 137 | + await graphTokenController(req, res); |
| 138 | + |
| 139 | + expect(res.status).toHaveBeenCalledWith(500); |
| 140 | + expect(res.json).toHaveBeenCalledWith({ |
| 141 | + message: 'Failed to obtain Microsoft Graph token', |
| 142 | + }); |
| 143 | + }); |
| 144 | +}); |
0 commit comments