Files
Sebastian Poxhofer 853290db9c refactor(http): read platform families instead of the host type aliases (#46347)
`lib/util/http/auth.ts` and `lib/util/http/host-rules.ts` were the last
readers of the `*_API_USING_HOST_TYPES` constants, which the previous
commits left as aliases of `PLATFORM_FAMILIES`. Both now read the table
directly and the aliases are deleted, so each family's host types are
declared in exactly one place.

Also covers the `forgejo` fallback in `findMatchingRule()`, which had no
test: the shared setup only registers a `gitea` host rule, so the
forgejo branch was never entered.

Behaviour changes: none. The aliases pointed at the same arrays.
2026-09-21 15:27:53 +00:00

226 lines
4.8 KiB
TypeScript

import { applyAuthorization } from './auth.ts';
import type { GotOptions } from './types.ts';
describe('util/http/auth', () => {
describe('applyAuthorization', () => {
it('does nothing', () => {
const opts: GotOptions = {
headers: { authorization: 'token' },
};
applyAuthorization(opts);
expect(opts).toMatchInlineSnapshot(`
{
"headers": {
"authorization": "token",
},
}
`);
});
it('gitea password', () => {
const opts: GotOptions = {
headers: {},
hostType: 'gitea',
password: 'XXXX',
};
applyAuthorization(opts);
expect(opts).toMatchInlineSnapshot(`
{
"headers": {
"authorization": "Basic OlhYWFg=",
},
"hostType": "gitea",
"password": "XXXX",
}
`);
});
it('gittea token', () => {
const opts: GotOptions = {
headers: {},
token: 'XXXX',
hostType: 'gitea',
};
applyAuthorization(opts);
expect(opts).toMatchInlineSnapshot(`
{
"headers": {
"authorization": "Bearer XXXX",
},
"hostType": "gitea",
"token": "XXXX",
}
`);
});
it('github token', () => {
const opts: GotOptions = {
headers: {},
token: 'XXX',
hostType: 'github',
};
applyAuthorization(opts);
expect(opts).toEqual({
headers: {
authorization: 'token XXX',
},
hostType: 'github',
token: 'XXX',
});
});
it('github token for datasource using github api', () => {
const opts: GotOptions = {
headers: {},
token: 'ZZZZ',
hostType: 'github-releases',
};
applyAuthorization(opts);
expect(opts).toMatchInlineSnapshot(`
{
"headers": {
"authorization": "token ZZZZ",
},
"hostType": "github-releases",
"token": "ZZZZ",
}
`);
});
it('github app token with hostType not in PLATFORM_FAMILIES.github.apiUsingHostTypes', () => {
const opts: GotOptions = {
headers: {},
token: 'x-access-token:ghs_123test',
hostType: 'github-digest',
};
expect(applyAuthorization(opts)).toMatchObject({
headers: {
authorization: 'Bearer ghs_123test',
},
});
});
it(`gitlab personal access token`, () => {
const opts: GotOptions = {
headers: {},
// Personal Access Token is exactly 20 characters long
token: '0123456789012345test',
hostType: 'gitlab',
};
applyAuthorization(opts);
expect(opts).toMatchInlineSnapshot(`
{
"headers": {
"Private-token": "0123456789012345test",
},
"hostType": "gitlab",
"token": "0123456789012345test",
}
`);
});
it(`gitlab oauth token`, () => {
const opts: GotOptions = {
headers: {},
token:
'a40bdd925a0c0b9c4cdd19d101c0df3b2bcd063ab7ad6706f03bcffcec01test',
hostType: 'gitlab',
};
applyAuthorization(opts);
expect(opts).toMatchInlineSnapshot(`
{
"headers": {
"authorization": "Bearer a40bdd925a0c0b9c4cdd19d101c0df3b2bcd063ab7ad6706f03bcffcec01test",
},
"hostType": "gitlab",
"token": "a40bdd925a0c0b9c4cdd19d101c0df3b2bcd063ab7ad6706f03bcffcec01test",
}
`);
});
it(`npm basic token`, () => {
const opts: GotOptions = {
headers: {},
token: 'test',
hostType: 'npm',
context: {
authType: 'Basic',
},
};
applyAuthorization(opts);
expect(opts).toEqual({
context: {
authType: 'Basic',
},
headers: {
authorization: 'Basic test',
},
hostType: 'npm',
token: 'test',
});
});
it(`bare token`, () => {
const opts: GotOptions = {
headers: {},
token: 'test',
context: {
authType: 'Token-Only',
},
};
applyAuthorization(opts);
expect(opts).toEqual({
context: {
authType: 'Token-Only',
},
headers: {
authorization: 'test',
},
token: 'test',
});
});
it(`honors authType`, () => {
const opts: GotOptions = {
headers: {},
token: 'test',
context: {
authType: 'Bearer',
},
hostType: 'custom',
};
applyAuthorization(opts);
expect(opts).toEqual({
context: {
authType: 'Bearer',
},
headers: {
authorization: 'Bearer test',
},
hostType: 'custom',
token: 'test',
});
});
});
});