LibreChat/packages/api/src/pulls/settings.spec.ts
Marco Beretta 0bc344fcb9
🔑 feat: Let Users Connect Their Own Account for Pull Request Lookups (#16989)
* feat: Let Users Connect Their Own Account for Pull Request Lookups

* fix: Refuse Cross-Tenant Pull Request Callbacks and Surface Key Failures

* fix: Keep Pull Request Grants Through Outages and Remove Them on Disconnect

A token endpoint server error is an outage even with an OAuth error body, so
it no longer deletes the grant, and a refresh outage reaches the lookup as
unavailable instead of falling back to no token. Disconnect removes every
pr_oauth row of the user, whatever app or host is configured now. A grant that
never expires extends its retention when used, and the flow, request and
retention limits are configurable. The connection routes are built in
packages/api, mounted behind the pre-auth tenant middleware in both servers,
and answer failures with stable codes.

* fix: Keep the Pull Request OAuth App Out of Overrides

A role, group, user or tenant override can no longer change or remove pullRequests.oauth, so the connect capability and the callback see the operator's app.

* fix: Read a User's Pull Request Token Only With the Feature On

* fix: Report a Malformed Stored Pull Request Grant as a Failure

* fix: Remove a User's Stored Tokens When an Administrator Deletes the User

* fix: Answer No Pull Request for a Conversation Without a Lane When the User Token Fails

* fix: Share One Grant Read Between Concurrent Callers and Report an Expired Grant Disconnected

* fix: Remove a Pull Request Grant Only on a Definite Refusal and Past Its Retention

* fix: Version Pull Request Grants and Coordinate Refresh, Refusal and Retention Through Them

* fix: Invalidate a Refused User Token and Read It Only for a Lane That Needs It

* fix: Finish a Pull Request Connection Under the User's Effective Config Behind the Login Limiter

* fix: Type the Pull Request User Token for Isolated Declarations and Settle Retention Before Answering

* fix: Bind a Pull Request Connection Flow to the Generation a Disconnect Replaces

* fix: Start a Batch's User Token Read Only Within Its Deadline

* fix: Fence Pull Request Token Reads Against a Disconnect and Report a Failed Retention Cleanup

* fix: Fence Connection Status Like Token Reads and Keep a Newer Read Joinable

* fix: Report a Pull Request Grant Without Its App and Name the Sign-In That Made It

* fix: Read a User's Unexpired Pull Request Grants Newest First

* fix: Mark the Connection Callback as an OAuth Navigation and Report a Failed Extension

* fix: Name the Pull Request Sign-In Flow on Its Landing Page

* fix: Remove a Pull Request Grant of a Replaced App When It Is Read

* fix: Keep a Replaced App's Grant Untouched and Extend Retention From Each Day's Use

* fix: Accept a Pull Request Grant Only Under the User's Current Connection Generation

* fix: Renew the Connection Generation With a Grant Still in Use

* fix: Restore the Previous Connection When a Sign-In Cannot Store Its Grant

* fix: Decide a Sign-In by What Was Stored When a Write Does Not Answer

* fix: Roll Back a Sign-In's Generation Along With a Grant It Must Remove

* fix: Refresh an Access Token a Lookup Was Refused With Before Ending the Connection

* fix: Undo a Sign-In Only for a Confirmed Fence, Not a Failed Check

* fix: Read a User Token Again When the Connection Changes Under the Read

* fix: Apply the Current Retention Again When Another Replica Changed the Grant First

* test: Answer Each Concurrent Sign-In by Its Own Code

* fix: Skip Retention Rewrites for Grants That Expire Without a Refresh Token

* fix: Retry a Token Rejection That Loses to a Concurrent Grant Write

* fix: Skip User Token Reads for Lanes Without a Commit and Reread Status After a Reconnect
2026-10-12 15:58:23 +02:00

238 lines
8.6 KiB
TypeScript

import {
resolvePullRequestOAuth,
resolveTokenReference,
resolvePullRequestHost,
resolvePullRequestToken,
isPullRequestFeatureActive,
resolvePullRequestRecording,
resolvePullRequestProvider,
} from './settings';
describe('resolvePullRequestOAuth', () => {
const oauth = { clientId: '${ID}', clientSecret: '${SECRET}' };
const env = { ID: 'client', SECRET: 'secret' };
it('resolves the app on the provider host', () => {
expect(
resolvePullRequestOAuth({ provider: 'gitlab', host: 'GitLab.Example.com', oauth }, env),
).toEqual({
provider: 'gitlab',
host: 'gitlab.example.com',
clientId: 'client',
clientSecret: 'secret',
});
expect(resolvePullRequestOAuth({ oauth: { ...oauth, scope: ' repo ' } }, env)).toMatchObject({
provider: 'github',
host: 'github.com',
scope: 'repo',
});
});
it('is null without an app, or when a reference does not resolve', () => {
expect(resolvePullRequestOAuth({}, env)).toBeNull();
expect(resolvePullRequestOAuth({ oauth }, { ID: 'client' })).toBeNull();
expect(resolvePullRequestOAuth({ oauth: { ...oauth, clientId: 'literal' } }, env)).toBeNull();
});
it('turns the feature on without a server token or a repository list', () => {
expect(isPullRequestFeatureActive({ oauth }, env)).toBe(true);
expect(isPullRequestFeatureActive({ oauth, enabled: false }, env)).toBe(false);
});
});
describe('resolvePullRequestProvider', () => {
it('is GitHub when none is configured', () => {
expect(resolvePullRequestProvider(undefined)).toBe('github');
});
it.each(['github', 'gitlab', 'bitbucket'])('accepts %s', (provider) => {
expect(resolvePullRequestProvider(provider)).toBe(provider);
});
it('refuses a provider this server does not know', () => {
expect(resolvePullRequestProvider('gitea')).toBeNull();
});
});
describe('resolvePullRequestHost', () => {
it('is github.com when no host is configured', () => {
expect(resolvePullRequestHost(undefined)).toBe('github.com');
});
it('normalizes a configured host, which the raw YAML does not', () => {
expect(resolvePullRequestHost(' GitHub.Example.com ')).toBe('github.example.com');
expect(resolvePullRequestHost('ghe.local:8443')).toBe('ghe.local:8443');
expect(resolvePullRequestHost('ghe.local:08443')).toBe('ghe.local:8443');
});
it('drops the https default port, which a page URL never carries', () => {
expect(resolvePullRequestHost('github.com:443')).toBe('github.com');
expect(resolvePullRequestHost('GHE.Example.com:443')).toBe('ghe.example.com');
});
it.each([
'https://github.example.com',
'github.example.com/api',
'u@github.example.com',
'ghe.local:0',
'ghe.local:65536',
'ghe.local:99999',
'',
])('refuses %p rather than falling back to github.com', (host) => {
expect(resolvePullRequestHost(host)).toBeNull();
});
it("defaults to the provider's own host", () => {
expect(resolvePullRequestHost(undefined, 'gitlab')).toBe('gitlab.com');
expect(resolvePullRequestHost(undefined, 'bitbucket')).toBe('bitbucket.org');
expect(resolvePullRequestHost('GitLab.Example.com', 'gitlab')).toBe('gitlab.example.com');
});
it('refuses any Bitbucket host but Cloud, which is all this server can read', () => {
expect(resolvePullRequestHost('bitbucket.example.com', 'bitbucket')).toBeNull();
expect(resolvePullRequestHost('Bitbucket.org', 'bitbucket')).toBe('bitbucket.org');
});
});
describe('resolvePullRequestToken', () => {
it('prefers the configured reference over any fallback', () => {
expect(
resolvePullRequestToken('${MY_TOKEN}', { MY_TOKEN: 'mine', GITHUB_TOKEN: 'other' }),
).toBe('mine');
});
it('does not fall back when the configured reference does not resolve', () => {
expect(resolvePullRequestToken('${MY_TOKEN}', { GITHUB_TOKEN: 'other' })).toBeNull();
expect(resolvePullRequestToken('not-a-reference', { GITHUB_TOKEN: 'other' })).toBeNull();
});
it.each([
[
'GITHUB_PULL_REQUEST_TOKEN',
{ GITHUB_PULL_REQUEST_TOKEN: 'a', GITHUB_TOKEN: 'b', GH_TOKEN: 'c' },
'a',
],
['GITHUB_TOKEN', { GITHUB_TOKEN: ' b ', GH_TOKEN: 'c' }, 'b'],
['GH_TOKEN', { GH_TOKEN: 'c' }, 'c'],
])('falls back to %s, in that order', (_name, env, expected) => {
expect(resolvePullRequestToken(undefined, env)).toBe(expected);
});
it('is null when no token exists anywhere, or only a blank one', () => {
expect(resolvePullRequestToken(undefined, {})).toBeNull();
expect(resolvePullRequestToken(undefined, { GITHUB_TOKEN: ' ' })).toBeNull();
});
});
describe('resolveTokenReference', () => {
it.each([
['${GH_TOKEN}', { GH_TOKEN: ' abc ' }, 'abc'],
['${GH_TOKEN}', {}, null],
['literal-token', { 'literal-token': 'x' }, null],
[undefined, { GH_TOKEN: 'x' }, null],
])('resolves %s', (reference, env, expected) => {
expect(resolveTokenReference(reference, env)).toBe(expected);
});
});
describe('isPullRequestFeatureActive', () => {
const env = { GITHUB_TOKEN: 't' };
it('is on without any switch once there is a token and a list of repositories', () => {
expect(isPullRequestFeatureActive({ allowedRepositories: ['o/r'] }, env)).toBe(true);
});
it('is on with the opt-in to every repository, and no list', () => {
expect(isPullRequestFeatureActive({ allowAllRepositories: true }, env)).toBe(true);
});
it('stays dormant without a repository scope, so a worker cannot name its own', () => {
expect(isPullRequestFeatureActive({}, env)).toBe(false);
expect(isPullRequestFeatureActive(undefined, env)).toBe(false);
expect(isPullRequestFeatureActive({ allowedRepositories: [] }, env)).toBe(false);
expect(isPullRequestFeatureActive({ allowAllRepositories: false }, env)).toBe(false);
});
it('stays dormant without any token', () => {
expect(isPullRequestFeatureActive({ allowAllRepositories: true }, {})).toBe(false);
});
it('counts a configured reference as a token even before it resolves, so the failure is visible', () => {
expect(
isPullRequestFeatureActive({ token: '${MISSING}', allowAllRepositories: true }, {}),
).toBe(true);
});
it('is off when an administrator turns it off, whatever else is set', () => {
expect(isPullRequestFeatureActive({ enabled: false, allowAllRepositories: true }, env)).toBe(
false,
);
});
it('falls back to the variables of the configured provider only', () => {
const gitlab = { provider: 'gitlab' as const, allowAllRepositories: true };
expect(isPullRequestFeatureActive(gitlab, { GITHUB_TOKEN: 't' })).toBe(false);
expect(isPullRequestFeatureActive(gitlab, { GITLAB_TOKEN: 't' })).toBe(true);
expect(resolvePullRequestToken(undefined, { GITLAB_TOKEN: 'g', GH_TOKEN: 'h' }, 'gitlab')).toBe(
'g',
);
expect(
resolvePullRequestToken(undefined, { BITBUCKET_PULL_REQUEST_TOKEN: 'b' }, 'bitbucket'),
).toBe('b');
});
it('stays dormant for a provider this server does not know', () => {
expect(
isPullRequestFeatureActive(
{ provider: 'gitea' as never, token: '${T}', allowAllRepositories: true },
env,
),
).toBe(false);
});
it('stays dormant with a host that is not a host name, so the token goes nowhere', () => {
expect(
isPullRequestFeatureActive(
{ host: 'https://ghe.example.com', allowAllRepositories: true },
env,
),
).toBe(false);
expect(
isPullRequestFeatureActive({ host: 'ghe.example.com', allowAllRepositories: true }, env),
).toBe(true);
});
it('is on when explicitly enabled and fully scoped', () => {
expect(isPullRequestFeatureActive({ enabled: true, allowAllRepositories: true }, env)).toBe(
true,
);
});
});
describe('resolvePullRequestRecording', () => {
const env = { GITHUB_TOKEN: 't' };
it('records with the configured host when the feature is on', () => {
expect(
resolvePullRequestRecording({ host: 'GHE.example.com:443', allowAllRepositories: true }, env),
).toEqual({ enabled: true, host: 'ghe.example.com' });
expect(resolvePullRequestRecording({ allowAllRepositories: true }, env)).toEqual({
enabled: true,
host: 'github.com',
});
});
it('records links on the default host of the configured provider', () => {
expect(
resolvePullRequestRecording(
{ provider: 'gitlab', allowAllRepositories: true },
{ GITLAB_TOKEN: 't' },
),
).toEqual({ enabled: true, host: 'gitlab.com' });
});
it('records nothing when the feature is off', () => {
expect(resolvePullRequestRecording({ allowAllRepositories: true }, {})).toEqual({
enabled: false,
});
});
});