Skip to content
Snippets Groups Projects
Select Git revision
  • af7d6c68356fd71ac622cb41b2ca04dc2f4ea09e
  • main default protected
  • refactor/zod-schema-names
  • renovate/main-redis-5.x
  • chore/update-static-data
  • feat/poetry/supersede-pep621
  • fix/markdown/linking
  • next
  • feat/gnupg
  • fix/36615b-branch-reuse-no-cache
  • 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
  • 41.71.0
  • 41.70.3
  • 41.70.2
  • 41.70.1
  • 41.70.0
  • 41.69.1
  • 41.69.0
  • 41.68.0
  • 41.67.0
  • 41.66.3
  • 41.66.2
  • 41.66.1
  • 41.66.0
  • 41.65.1
  • 41.65.0
  • 41.64.4
  • 41.64.3
  • 41.64.2
  • 41.64.1
  • 41.64.0
41 results

index.spec.ts

Blame
  • user avatar
    Sergei Zharinov authored and GitHub committed
    56e5ea07
    History
    user avatar 56e5ea07
    index.spec.ts 5.43 KiB
    import { getDigest, getPkgReleases } from '..';
    import { Fixtures } from '../../../../test/fixtures';
    import * as httpMock from '../../../../test/http-mock';
    import { EXTERNAL_HOST_ERROR } from '../../../constants/error-messages';
    import { HttpError } from '../../../util/http';
    import { CdnjsDatasource } from '.';
    
    const baseUrl = 'https://api.cdnjs.com/';
    
    const pathFor = (s: string): string =>
      `/libraries/${s.split('/').shift()}?fields=homepage,repository,versions`;
    
    const pathForDigest = (s: string, version: string): string =>
      `/libraries/${s.split('/').shift()}/${version}?fields=sri`;
    
    describe('modules/datasource/cdnjs/index', () => {
      describe('getReleases', () => {
        it('throws for empty result', async () => {
          httpMock.scope(baseUrl).get(pathFor('foo/bar')).reply(200, '}');
          await expect(
            getPkgReleases({
              datasource: CdnjsDatasource.id,
              packageName: 'foo/bar',
            }),
          ).rejects.toThrow(EXTERNAL_HOST_ERROR);
        });
    
        it('throws for error', async () => {
          httpMock.scope(baseUrl).get(pathFor('foo/bar')).replyWithError('error');
          await expect(
            getPkgReleases({
              datasource: CdnjsDatasource.id,
              packageName: 'foo/bar',
            }),
          ).rejects.toThrow(EXTERNAL_HOST_ERROR);
        });
    
        it('returns null for 404', async () => {
          httpMock.scope(baseUrl).get(pathFor('foo/bar')).reply(404);
          expect(
            await getPkgReleases({
              datasource: CdnjsDatasource.id,
              packageName: 'foo/bar',
            }),
          ).toBeNull();
        });
    
        it('returns null for empty 200 OK', async () => {
          httpMock
            .scope(baseUrl)
            .get(pathFor('doesnotexist/doesnotexist'))
            .reply(200, {});
          expect(
            await getPkgReleases({
              datasource: CdnjsDatasource.id,
              packageName: 'doesnotexist/doesnotexist',
            }),
          ).toBeNull();
        });
    
        it('throws for 401', async () => {
          httpMock.scope(baseUrl).get(pathFor('foo/bar')).reply(401);
          await expect(
            getPkgReleases({
              datasource: CdnjsDatasource.id,
              packageName: 'foo/bar',
            }),
          ).rejects.toThrow(EXTERNAL_HOST_ERROR);
        });
    
        it('throws for 429', async () => {
          httpMock.scope(baseUrl).get(pathFor('foo/bar')).reply(429);
          await expect(
            getPkgReleases({
              datasource: CdnjsDatasource.id,
              packageName: 'foo/bar',
            }),
          ).rejects.toThrow(EXTERNAL_HOST_ERROR);
        });
    
        it('throws for 5xx', async () => {
          httpMock.scope(baseUrl).get(pathFor('foo/bar')).reply(502);
          await expect(
            getPkgReleases({
              datasource: CdnjsDatasource.id,
              packageName: 'foo/bar',
            }),
          ).rejects.toThrow(EXTERNAL_HOST_ERROR);
        });
    
        it('throws for unknown error', async () => {
          httpMock.scope(baseUrl).get(pathFor('foo/bar')).replyWithError('error');
          await expect(
            getPkgReleases({
              datasource: CdnjsDatasource.id,
              packageName: 'foo/bar',
            }),
          ).rejects.toThrow(EXTERNAL_HOST_ERROR);
        });
    
        it('processes real data', async () => {
          httpMock
            .scope(baseUrl)
            .get(pathFor('d3-force/d3-force.js'))
            .reply(200, Fixtures.get('d3-force.json'));
          const res = await getPkgReleases({
            datasource: CdnjsDatasource.id,
            packageName: 'd3-force/d3-force.js',
          });
          expect(res).toMatchSnapshot();
        });
      });
    
      describe('getDigest', () => {
        it('returs null for no result', async () => {
          httpMock
            .scope(baseUrl)
            .get(pathForDigest('foo/bar', '1.2.0'))
            .reply(200, '{}');
    
          const res = await getDigest(
            {
              datasource: CdnjsDatasource.id,
              packageName: 'foo/bar',
            },
            '1.2.0',
          );
          expect(res).toBeNull();
        });
    
        it('returs null for empty sri object', async () => {
          httpMock
            .scope(baseUrl)
            .get(pathForDigest('foo/bar', '1.2.0'))
            .reply(200, JSON.stringify({ sri: {} }));
    
          const res = await getDigest(
            {
              datasource: CdnjsDatasource.id,
              packageName: 'foo/bar',
            },
            '1.2.0',
          );
          expect(res).toBeNull();
        });
    
        it('returs null if file not found', async () => {
          httpMock
            .scope(baseUrl)
            .get(pathForDigest('foo/bar', '1.2.0'))
            .reply(200, JSON.stringify({ sri: { string: 'hash' } }));
    
          const res = await getDigest(
            {
              datasource: CdnjsDatasource.id,
              packageName: 'foo/bar',
            },
            '1.2.0',
          );
          expect(res).toBeNull();
        });
    
        it('returns null for 404', async () => {
          httpMock.scope(baseUrl).get(pathForDigest('foo/bar', '1.2.0')).reply(404);
          await expect(
            getDigest(
              {
                datasource: CdnjsDatasource.id,
                packageName: 'foo/bar',
              },
              '1.2.0',
            ),
          ).rejects.toThrow(HttpError);
        });
    
        it('returns digest', async () => {
          httpMock
            .scope(baseUrl)
            .get(pathForDigest('bootstrap/js/bootstrap.min.js', '5.2.3'))
            .reply(200, Fixtures.get('sri.json'));
    
          const res = await getDigest(
            {
              datasource: CdnjsDatasource.id,
              packageName: 'bootstrap/js/bootstrap.min.js',
            },
            '5.2.3',
          );
          expect(res).toBe(
            'sha512-1/RvZTcCDEUjY/CypiMz+iqqtaoQfAITmNSJY17Myp4Ms5mdxPS5UV7iOfdZoxcGhzFbOm6sntTKJppjvuhg4g==',
          );
        });
      });
    });