Skip to content
Snippets Groups Projects
Select Git revision
  • 62d5d670bf24e077b3c3643707219493148e4fa3
  • main default protected
  • fix/36615b-branch-reuse-no-cache
  • renovate/main-redis-5.x
  • next
  • revert-31645-feat/rename-gradle-wrapper-validation-action
  • chore/punycode
  • refactor/pin-new-value
  • feat/36219--git-x509-signing
  • feat/structured-logger
  • hotfix/39.264.1
  • feat/skip-dangling
  • gh-readonly-queue/next/pr-36034-7a061c4ca1024a19e2c295d773d9642625d1c2be
  • hotfix/39.238.3
  • refactor/gitlab-auto-approve
  • feat/template-strings
  • gh-readonly-queue/next/pr-35654-137d934242c784e0c45d4b957362214f0eade1d7
  • fix/32307-global-extends-merging
  • fix/32307-global-extends-repositories
  • gh-readonly-queue/next/pr-35009-046ebf7cb84ab859f7fefceb5fa53a54ce9736f8
  • gh-readonly-queue/next/pr-35009-9d5e583b7d7251148ab0d11ee8dd38149618d162
  • 41.38.2
  • 41.38.1
  • 41.38.0
  • 41.37.12
  • 41.37.11
  • 41.37.10
  • 41.37.9
  • 41.37.8
  • 41.37.7
  • 41.37.6
  • 41.37.5
  • 41.37.4
  • 41.37.3
  • 41.37.2
  • 41.37.1
  • 41.37.0
  • 41.36.2
  • 41.36.1
  • 41.36.0
  • 41.35.2
41 results

host-rules.spec.ts

Blame
  • user avatar
    Michael Kriese authored and GitHub committed
    64f5bd5f
    History
    host-rules.spec.ts 10.63 KiB
    import { PlatformId } from '../constants';
    import { NugetDatasource } from '../modules/datasource/nuget';
    import type { HostRule } from '../types';
    import {
      add,
      clear,
      find,
      findAll,
      getAll,
      hostType,
      hosts,
    } from './host-rules';
    
    describe('util/host-rules', () => {
      beforeEach(() => {
        clear();
      });
    
      describe('add()', () => {
        it('throws if both domainName and hostName', () => {
          expect(() =>
            add({
              hostType: PlatformId.Azure,
              domainName: 'github.com',
              hostName: 'api.github.com',
            } as HostRule)
          ).toThrow();
        });
    
        it('throws if both domainName and baseUrl', () => {
          expect(() =>
            add({
              hostType: PlatformId.Azure,
              domainName: 'github.com',
              matchHost: 'https://api.github.com',
            } as HostRule)
          ).toThrow();
        });
    
        it('throws if both hostName and baseUrl', () => {
          expect(() =>
            add({
              hostType: PlatformId.Azure,
              hostName: 'api.github.com',
              matchHost: 'https://api.github.com',
            } as HostRule)
          ).toThrow();
        });
    
        it('supports baseUrl-only', () => {
          add({
            matchHost: 'https://some.endpoint',
            username: 'user1',
            password: 'pass1',
          });
          expect(find({ url: 'https://some.endpoint/v3/' })).toEqual({
            password: 'pass1',
            username: 'user1',
          });
        });
      });
    
      describe('find()', () => {
        beforeEach(() => {
          clear();
        });
    
        it('warns and returns empty for bad search', () => {
          expect(find({ abc: 'def' } as any)).toEqual({});
        });
    
        it('needs exact host matches', () => {
          add({
            hostType: NugetDatasource.id,
            hostName: 'nuget.org',
            username: 'root',
            password: 'p4$$w0rd',
            token: undefined,
          } as HostRule);
          expect(find({ hostType: NugetDatasource.id })).toEqual({});
          expect(
            find({ hostType: NugetDatasource.id, url: 'https://nuget.org' })
          ).not.toEqual({});
          expect(
            find({ hostType: NugetDatasource.id, url: 'https://not.nuget.org' })
          ).not.toEqual({});
          expect(
            find({ hostType: NugetDatasource.id, url: 'https://not-nuget.org' })
          ).toEqual({});
        });
    
        it('matches on empty rules', () => {
          add({
            enabled: true,
          });
          expect(
            find({ hostType: NugetDatasource.id, url: 'https://api.github.com' })
          ).toEqual({ enabled: true });
        });
    
        it('matches on hostType', () => {
          add({
            hostType: NugetDatasource.id,
            token: 'abc',
          });
          expect(
            find({ hostType: NugetDatasource.id, url: 'https://nuget.local/api' })
          ).toEqual({ token: 'abc' });
        });
    
        it('matches on domainName', () => {
          add({
            domainName: 'github.com',
            token: 'def',
          } as HostRule);
          expect(
            find({ hostType: NugetDatasource.id, url: 'https://api.github.com' })
              .token
          ).toBe('def');
          expect(
            find({ hostType: NugetDatasource.id, url: 'https://github.com' }).token
          ).toBe('def');
          expect(
            find({ hostType: NugetDatasource.id, url: 'https://apigithub.com' })
              .token
          ).toBeUndefined();
        });
    
        it('matches on specific path', () => {
          // Initialized platform holst rule
          add({
            hostType: PlatformId.Github,
            matchHost: 'https://api.github.com',
            token: 'abc',
          });
          // specific host rule for using other token in different org
          add({
            hostType: PlatformId.Github,
            matchHost: 'https://api.github.com/repos/org-b/',
            token: 'def',
          });
          // Initialized generic host rule for github platform
          add({
            hostType: PlatformId.Github,
            matchHost: 'https://api.github.com',
            token: 'abc',
          });
          expect(
            find({
              hostType: PlatformId.Github,
              url: 'https://api.github.com/repos/org-b/someRepo/tags?per_page=100',
            }).token
          ).toBe('def');
        });
    
        it('matches for several hostTypes when no hostType rule is configured', () => {
          add({
            matchHost: 'https://api.github.com',
            token: 'abc',
          });
          expect(
            find({
              hostType: PlatformId.Github,
              url: 'https://api.github.com/repos/org-b/someRepo/tags?per_page=100',
            }).token
          ).toBe('abc');
          expect(
            find({
              hostType: 'github-releases',
              url: 'https://api.github.com/repos/org-b/someRepo/tags?per_page=100',
            }).token
          ).toBe('abc');
        });
    
        it('matches if hostType is configured and host rule is filtered with datasource', () => {
          add({
            hostType: PlatformId.Github,
            matchHost: 'https://api.github.com',
            token: 'abc',
          });
          add({
            hostType: 'github-tags',
            matchHost: 'https://api.github.com/repos/org-b/',
            token: 'def',
          });
          expect(
            find({
              hostType: 'github-tags',
              url: 'https://api.github.com/repos/org-b/someRepo/tags?per_page=100',
            }).token
          ).toBe('def');
        });
    
        it('matches on hostName', () => {
          add({
            hostName: 'nuget.local',
            token: 'abc',
          } as HostRule);
          expect(
            find({ hostType: NugetDatasource.id, url: 'https://nuget.local/api' })
          ).toEqual({ token: 'abc' });
        });
    
        it('matches on matchHost with protocol', () => {
          add({
            matchHost: 'https://domain.com',
            token: 'def',
          });
          expect(find({ url: 'https://api.domain.com' }).token).toBeUndefined();
          expect(find({ url: 'https://domain.com' }).token).toBe('def');
          expect(
            find({
              hostType: NugetDatasource.id,
              url: 'https://domain.com/renovatebot',
            }).token
          ).toBe('def');
        });
    
        it('matches on matchHost without protocol', () => {
          add({
            matchHost: 'domain.com',
            token: 'def',
          });
          expect(find({ url: 'https://api.domain.com' }).token).toBe('def');
          expect(find({ url: 'https://domain.com' }).token).toBe('def');
          expect(find({ url: 'httpsdomain.com' }).token).toBeUndefined();
        });
    
        it('matches on matchHost with dot prefix', () => {
          add({
            matchHost: '.domain.com',
            token: 'def',
          });
          expect(find({ url: 'https://api.domain.com' }).token).toBe('def');
          expect(find({ url: 'https://domain.com' }).token).toBeUndefined();
          expect(find({ url: 'httpsdomain.com' }).token).toBeUndefined();
        });
    
        it('matches on hostType and endpoint', () => {
          add({
            hostType: NugetDatasource.id,
            matchHost: 'https://nuget.local/api',
            token: 'abc',
          });
          expect(
            find({ hostType: NugetDatasource.id, url: 'https://nuget.local/api' })
              .token
          ).toBe('abc');
        });
    
        it('matches on endpoint subresource', () => {
          add({
            hostType: NugetDatasource.id,
            matchHost: 'https://nuget.local/api',
            token: 'abc',
          });
          expect(
            find({
              hostType: NugetDatasource.id,
              url: 'https://nuget.local/api/sub-resource',
            })
          ).toEqual({ token: 'abc' });
        });
    
        it('matches shortest matchHost first', () => {
          add({
            matchHost: 'https://nuget.local/api',
            token: 'longest',
          });
          add({
            matchHost: 'https://nuget.local/',
            token: 'shortest',
          });
          expect(
            find({
              url: 'https://nuget.local/api/sub-resource',
            })
          ).toEqual({ token: 'longest' });
        });
      });
    
      describe('hosts()', () => {
        it('returns hosts', () => {
          add({
            hostType: NugetDatasource.id,
            token: 'aaaaaa',
          });
          add({
            hostType: NugetDatasource.id,
            matchHost: 'https://nuget.local/api',
            token: 'abc',
          });
          add({
            hostType: NugetDatasource.id,
            hostName: 'my.local.registry',
            token: 'def',
          } as HostRule);
          add({
            hostType: NugetDatasource.id,
            matchHost: 'another.local.registry',
            token: 'xyz',
          });
          add({
            hostType: NugetDatasource.id,
            matchHost: 'https://yet.another.local.registry',
            token: '123',
          });
          const res = hosts({
            hostType: NugetDatasource.id,
          });
          expect(res).toEqual([
            'nuget.local',
            'my.local.registry',
            'another.local.registry',
            'yet.another.local.registry',
          ]);
        });
      });
    
      describe('findAll()', () => {
        it('warns and returns empty for bad search', () => {
          expect(findAll({ abc: 'def' } as any)).toEqual([]);
        });
    
        it('needs exact host matches', () => {
          const hostRule = {
            hostType: 'nuget',
            hostName: 'nuget.org',
            username: 'root',
            password: 'p4$$w0rd',
          };
          add(hostRule);
          expect(findAll({ hostType: 'nuget' })).toEqual([
            {
              hostType: 'nuget',
              password: 'p4$$w0rd',
              resolvedHost: 'nuget.org',
              username: 'root',
              matchHost: 'nuget.org',
            },
          ]);
        });
      });
    
      describe('getAll()', () => {
        it('returns all host rules', () => {
          const hostRule1 = {
            hostType: 'nuget',
            matchHost: 'nuget.org',
            username: 'root',
            password: 'p4$$w0rd',
          };
          const hostRule2 = {
            hostType: 'github',
            matchHost: 'github.com',
            token: 'token',
          };
          add(hostRule1);
          add(hostRule2);
          expect(getAll()).toMatchObject([hostRule1, hostRule2]);
        });
      });
    
      describe('hostType()', () => {
        it('return hostType', () => {
          add({
            hostType: PlatformId.Github,
            token: 'aaaaaa',
          });
          add({
            hostType: PlatformId.Github,
            matchHost: 'github.example.com',
            token: 'abc',
          });
          add({
            hostType: 'github-changelog',
            matchHost: 'https://github.example.com/chalk/chalk',
            token: 'def',
          });
          expect(
            hostType({
              url: 'https://github.example.com/chalk/chalk',
            })
          ).toBe('github-changelog');
        });
    
        it('returns null', () => {
          add({
            hostType: PlatformId.Github,
            token: 'aaaaaa',
          });
          add({
            hostType: PlatformId.Github,
            matchHost: 'github.example.com',
            token: 'abc',
          });
          add({
            hostType: 'github-changelog',
            matchHost: 'https://github.example.com/chalk/chalk',
            token: 'def',
          });
          expect(
            hostType({
              url: 'https://github.example.com/chalk/chalk',
            })
          ).toBe('github-changelog');
          expect(
            hostType({
              url: 'https://gitlab.example.com/chalk/chalk',
            })
          ).toBeNull();
        });
      });
    });