From 394f0bb7416ff6031bf7eb14498a85f00a6305df Mon Sep 17 00:00:00 2001
From: Michael Kriese <michael.kriese@visualon.de>
Date: Mon, 9 Aug 2021 19:37:28 +0200
Subject: [PATCH] test: fix http mocks (#11191)

---
 lib/datasource/clojure/index.spec.ts          |  5 +++++
 lib/datasource/crate/index.spec.ts            |  2 ++
 lib/datasource/helm/index.spec.ts             |  5 +++++
 lib/datasource/packagist/index.spec.ts        |  5 +++++
 lib/datasource/pod/index.spec.ts              | 10 +++++++++
 lib/datasource/rubygems/index.spec.ts         |  4 ++++
 lib/manager/bazel/update.spec.ts              |  6 ++++++
 lib/platform/bitbucket/index.spec.ts          |  9 ++++++++
 lib/platform/gitlab/index.spec.ts             |  4 +++-
 lib/util/merge-confidence/index.spec.ts       |  8 +++++++
 lib/workers/pr/changelog/github.spec.ts       | 21 +++++++++++++++++++
 lib/workers/pr/changelog/gitlab.spec.ts       |  5 +++++
 lib/workers/pr/changelog/index.spec.ts        |  2 ++
 .../pr/changelog/release-notes.spec.ts        |  6 ++++++
 test/http-mock.ts                             |  4 ++++
 15 files changed, 95 insertions(+), 1 deletion(-)

diff --git a/lib/datasource/clojure/index.spec.ts b/lib/datasource/clojure/index.spec.ts
index 546bb2e441..8fa371a74e 100644
--- a/lib/datasource/clojure/index.spec.ts
+++ b/lib/datasource/clojure/index.spec.ts
@@ -233,6 +233,11 @@ describe(getName(), () => {
     const pom = loadFixture('pom.scm-prefix.xml', upath.join('..', 'maven'));
     mockGenericPackage({ pom });
 
+    httpMock
+      .scope('https://repo.maven.apache.org')
+      .get('/maven2/org/example/package/maven-metadata.xml')
+      .reply(200, '###');
+
     const { sourceUrl } = await get();
 
     expect(sourceUrl).toEqual('https://github.com/example/test');
diff --git a/lib/datasource/crate/index.spec.ts b/lib/datasource/crate/index.spec.ts
index b62822b12e..47ba345702 100644
--- a/lib/datasource/crate/index.spec.ts
+++ b/lib/datasource/crate/index.spec.ts
@@ -97,6 +97,8 @@ describe(getName(), () => {
     });
 
     it('returns null for missing registry url', async () => {
+      // FIXME: should not call default registry?
+      httpMock.scope(baseUrl).get('/no/n_/non_existent_crate').reply(404, {});
       expect(
         await getPkgReleases({
           datasource,
diff --git a/lib/datasource/helm/index.spec.ts b/lib/datasource/helm/index.spec.ts
index 83ce27950c..1cb2105cec 100644
--- a/lib/datasource/helm/index.spec.ts
+++ b/lib/datasource/helm/index.spec.ts
@@ -22,6 +22,11 @@ describe(getName(), () => {
       ).toBeNull();
     });
     it('returns null if repository was not provided', async () => {
+      // FIXME: should it call default rtegisty?
+      httpMock
+        .scope('https://charts.helm.sh')
+        .get('/stable/index.yaml')
+        .reply(404);
       expect(
         await getPkgReleases({
           datasource: HelmDatasource.id,
diff --git a/lib/datasource/packagist/index.spec.ts b/lib/datasource/packagist/index.spec.ts
index 4e7e13b990..57f7536b43 100644
--- a/lib/datasource/packagist/index.spec.ts
+++ b/lib/datasource/packagist/index.spec.ts
@@ -33,6 +33,10 @@ describe(getName(), () => {
     });
 
     it('supports custom registries', async () => {
+      httpMock
+        .scope('https://composer.renovatebot.com')
+        .get('/packages.json')
+        .reply(404);
       config = {
         registryUrls: ['https://composer.renovatebot.com'],
       };
@@ -44,6 +48,7 @@ describe(getName(), () => {
       });
       expect(res).toBeNull();
     });
+
     it('supports plain packages', async () => {
       const packagesOnly = {
         packages: {
diff --git a/lib/datasource/pod/index.spec.ts b/lib/datasource/pod/index.spec.ts
index 66db34635d..b246373338 100644
--- a/lib/datasource/pod/index.spec.ts
+++ b/lib/datasource/pod/index.spec.ts
@@ -22,6 +22,11 @@ describe(getName(), () => {
     });
 
     it('returns null for invalid inputs', async () => {
+      // FIXME: why get request?
+      httpMock
+        .scope(cocoapodsHost)
+        .get('/all_pods_versions_3_8_5.txt')
+        .reply(404);
       expect(
         await getPkgReleases({
           datasource: pod.id,
@@ -31,6 +36,11 @@ describe(getName(), () => {
       ).toBeNull();
     });
     it('returns null for empty result', async () => {
+      // FIXME: why get request?
+      httpMock
+        .scope(cocoapodsHost)
+        .get('/all_pods_versions_a_c_b.txt')
+        .reply(404);
       expect(await getPkgReleases(config)).toBeNull();
     });
     it('returns null for 404', async () => {
diff --git a/lib/datasource/rubygems/index.spec.ts b/lib/datasource/rubygems/index.spec.ts
index 83a07492d7..8d935229cc 100644
--- a/lib/datasource/rubygems/index.spec.ts
+++ b/lib/datasource/rubygems/index.spec.ts
@@ -167,6 +167,10 @@ describe(getName(), () => {
         .reply(200, railsInfo)
         .get('/api/v1/versions/rails.json')
         .reply(500, {});
+      httpMock
+        .scope('https://firstparty.com/basepath')
+        .get('/api/v1/gems/rails.json')
+        .reply(500);
       expect(await getPkgReleases(params)).toBeNull();
     });
   });
diff --git a/lib/manager/bazel/update.spec.ts b/lib/manager/bazel/update.spec.ts
index 3be8723119..9d5e913d1a 100644
--- a/lib/manager/bazel/update.spec.ts
+++ b/lib/manager/bazel/update.spec.ts
@@ -193,6 +193,12 @@ describe(getName(), () => {
         currentValue: '0.6.0',
         newValue: '0.8.0',
       };
+
+      httpMock
+        .scope('https://github.com')
+        .get('/bazelbuild/bazel-skyfoo/archive/0.8.0.tar.gz')
+        .reply(500);
+
       const res = await updateDependency({
         fileContent: content,
         upgrade,
diff --git a/lib/platform/bitbucket/index.spec.ts b/lib/platform/bitbucket/index.spec.ts
index cad5cd8e66..993a936f9f 100644
--- a/lib/platform/bitbucket/index.spec.ts
+++ b/lib/platform/bitbucket/index.spec.ts
@@ -100,6 +100,7 @@ describe(getName(), () => {
       );
     });
     it('should init', async () => {
+      httpMock.scope(baseUrl).get('/2.0/user').reply(200);
       expect(
         await bitbucket.initPlatform({
           username: 'abc',
@@ -611,6 +612,10 @@ describe(getName(), () => {
 
   describe('ensureComment()', () => {
     it('does not throw', async () => {
+      httpMock
+        .scope(baseUrl)
+        .get('/2.0/repositories/undefined/pullrequests/3/comments?pagelen=100')
+        .reply(500);
       expect(
         await bitbucket.ensureComment({
           number: 3,
@@ -623,6 +628,10 @@ describe(getName(), () => {
 
   describe('ensureCommentRemoval()', () => {
     it('does not throw', async () => {
+      httpMock
+        .scope(baseUrl)
+        .get('/2.0/repositories/undefined/pullrequests/3/comments?pagelen=100')
+        .reply(500);
       expect(
         await bitbucket.ensureCommentRemoval({ number: 3, topic: 'topic' })
       ).toMatchSnapshot();
diff --git a/lib/platform/gitlab/index.spec.ts b/lib/platform/gitlab/index.spec.ts
index 63c849fe31..4cbe3169ce 100644
--- a/lib/platform/gitlab/index.spec.ts
+++ b/lib/platform/gitlab/index.spec.ts
@@ -1028,7 +1028,9 @@ describe(getName(), () => {
           .get('/api/v4/users?username=someuser')
           .reply(200, [{ id: 1 }])
           .get('/api/v4/users?username=someotheruser')
-          .reply(200, [{ id: 2 }]);
+          .reply(200, [{ id: 2 }])
+          .put('/api/v4/projects/undefined/merge_requests/42')
+          .reply(200);
 
         await gitlab.addReviewers(42, ['someuser', 'foo', 'someotheruser']);
         expect(scope.isDone()).toBeTrue();
diff --git a/lib/util/merge-confidence/index.spec.ts b/lib/util/merge-confidence/index.spec.ts
index ca3eda4652..99f6d009e0 100644
--- a/lib/util/merge-confidence/index.spec.ts
+++ b/lib/util/merge-confidence/index.spec.ts
@@ -162,6 +162,14 @@ describe(getName(), () => {
           'minor'
         )
       ).toBe('neutral');
+
+      // FIXME: no cache hit
+      httpMock
+        .scope('https://badges.renovateapi.com')
+        .get(
+          `/packages/${datasource}/${depName}-new/${newVersion}/confidence.api/${currentVersion}`
+        )
+        .reply(403);
       // memory cache
       expect(
         await getMergeConfidenceLevel(
diff --git a/lib/workers/pr/changelog/github.spec.ts b/lib/workers/pr/changelog/github.spec.ts
index 0280a83bf9..19cec8280c 100644
--- a/lib/workers/pr/changelog/github.spec.ts
+++ b/lib/workers/pr/changelog/github.spec.ts
@@ -1,3 +1,4 @@
+import * as httpMock from '../../../../test/http-mock';
 import { getName } from '../../../../test/util';
 import { PLATFORM_TYPE_GITHUB } from '../../../constants/platforms';
 import * as hostRules from '../../../util/host-rules';
@@ -30,6 +31,11 @@ const upgrade: BranchUpgradeConfig = {
 };
 
 describe(getName(), () => {
+  afterEach(() => {
+    // FIXME: add missing http mocks
+    httpMock.clear(false);
+  });
+
   describe('getChangeLogJSON', () => {
     beforeEach(() => {
       hostRules.clear();
@@ -39,6 +45,7 @@ describe(getName(), () => {
         token: 'abc',
       });
     });
+
     it('returns null if @types', async () => {
       expect(
         await getChangeLogJSON({
@@ -47,6 +54,7 @@ describe(getName(), () => {
         })
       ).toBeNull();
     });
+
     it('returns null if no currentVersion', async () => {
       expect(
         await getChangeLogJSON({
@@ -55,6 +63,7 @@ describe(getName(), () => {
         })
       ).toBeNull();
     });
+
     it('returns null if currentVersion equals newVersion', async () => {
       expect(
         await getChangeLogJSON({
@@ -64,6 +73,7 @@ describe(getName(), () => {
         })
       ).toBeNull();
     });
+
     it('skips invalid repos', async () => {
       expect(
         await getChangeLogJSON({
@@ -72,6 +82,7 @@ describe(getName(), () => {
         })
       ).toBeNull();
     });
+
     it('works without Github', async () => {
       // FIXME: explicit assert condition
       expect(
@@ -80,6 +91,7 @@ describe(getName(), () => {
         })
       ).toMatchSnapshot();
     });
+
     it('uses GitHub tags', async () => {
       // FIXME: explicit assert condition
       expect(
@@ -88,6 +100,7 @@ describe(getName(), () => {
         })
       ).toMatchSnapshot();
     });
+
     it('filters unnecessary warns', async () => {
       // FIXME: explicit assert condition
       expect(
@@ -97,6 +110,7 @@ describe(getName(), () => {
         })
       ).toMatchSnapshot();
     });
+
     it('supports node engines', async () => {
       // FIXME: explicit assert condition
       expect(
@@ -106,6 +120,7 @@ describe(getName(), () => {
         })
       ).toMatchSnapshot();
     });
+
     it('handles no sourceUrl', async () => {
       expect(
         await getChangeLogJSON({
@@ -114,6 +129,7 @@ describe(getName(), () => {
         })
       ).toBeNull();
     });
+
     it('handles invalid sourceUrl', async () => {
       expect(
         await getChangeLogJSON({
@@ -122,6 +138,7 @@ describe(getName(), () => {
         })
       ).toBeNull();
     });
+
     it('handles missing Github token', async () => {
       expect(
         await getChangeLogJSON({
@@ -130,6 +147,7 @@ describe(getName(), () => {
         })
       ).toEqual({ error: ChangeLogError.MissingGithubToken });
     });
+
     it('handles no releases', async () => {
       expect(
         await getChangeLogJSON({
@@ -138,6 +156,7 @@ describe(getName(), () => {
         })
       ).toBeNull();
     });
+
     it('handles not enough releases', async () => {
       expect(
         await getChangeLogJSON({
@@ -146,6 +165,7 @@ describe(getName(), () => {
         })
       ).toBeNull();
     });
+
     it('supports github enterprise and github.com changelog', async () => {
       hostRules.add({
         hostType: PLATFORM_TYPE_GITHUB,
@@ -160,6 +180,7 @@ describe(getName(), () => {
         })
       ).toMatchSnapshot();
     });
+
     it('supports github enterprise and github enterprise changelog', async () => {
       hostRules.add({
         hostType: PLATFORM_TYPE_GITHUB,
diff --git a/lib/workers/pr/changelog/gitlab.spec.ts b/lib/workers/pr/changelog/gitlab.spec.ts
index 7885d932ab..4a74ab94d1 100644
--- a/lib/workers/pr/changelog/gitlab.spec.ts
+++ b/lib/workers/pr/changelog/gitlab.spec.ts
@@ -32,6 +32,11 @@ const upgrade: BranchUpgradeConfig = {
 const matchHost = 'https://gitlab.com/';
 
 describe(getName(), () => {
+  afterEach(() => {
+    // FIXME: add missing http mocks
+    httpMock.clear(false);
+  });
+
   describe('getChangeLogJSON', () => {
     beforeEach(() => {
       hostRules.clear();
diff --git a/lib/workers/pr/changelog/index.spec.ts b/lib/workers/pr/changelog/index.spec.ts
index 34193a687e..85a2f07cf8 100644
--- a/lib/workers/pr/changelog/index.spec.ts
+++ b/lib/workers/pr/changelog/index.spec.ts
@@ -147,6 +147,8 @@ describe(getName(), () => {
           depType: 'engines',
         })
       ).toMatchSnapshot();
+      // FIXME: missing mocks
+      httpMock.clear(false);
     });
     it('handles no sourceUrl', async () => {
       expect(
diff --git a/lib/workers/pr/changelog/release-notes.spec.ts b/lib/workers/pr/changelog/release-notes.spec.ts
index 8753b20628..766afc35bb 100644
--- a/lib/workers/pr/changelog/release-notes.spec.ts
+++ b/lib/workers/pr/changelog/release-notes.spec.ts
@@ -228,6 +228,11 @@ describe(getName(), () => {
     it.each([[''], ['v'], ['other-']])(
       'gets null from repository without gitlab/github in domain %s',
       async (prefix) => {
+        // FIXME: Should not call `api.lol.lol` ?
+        httpMock
+          .scope('https://api.lol.lol')
+          .get('/repos/some/other-repository/releases?per_page=100')
+          .reply(404);
         const res = await getReleaseNotes(
           'some/other-repository',
           '1.0.1',
@@ -241,6 +246,7 @@ describe(getName(), () => {
   });
   describe('getReleaseNotesMd()', () => {
     it('handles not found', async () => {
+      httpMock.scope('https://api.github.com').get('/repos/chalk').reply(404);
       const res = await getReleaseNotesMd(
         'chalk',
         '2.0.0',
diff --git a/test/http-mock.ts b/test/http-mock.ts
index 321d6517d0..76e455aba4 100644
--- a/test/http-mock.ts
+++ b/test/http-mock.ts
@@ -46,11 +46,15 @@ export function clear(throwOnPending = true): void {
   const pending = nock.pendingMocks();
   nock.abortPendingRequests();
   nock.cleanAll();
+  const missing = missingLog;
   requestLog = [];
   missingLog = [];
   if (!isDone && throwOnPending) {
     throw new Error(`Pending mocks!\n * ${pending.join('\n * ')}`);
   }
+  if (missing.length && throwOnPending) {
+    throw new Error(`Missing mocks!\n * ${missing.join('\n * ')}`);
+  }
 }
 
 export function scope(basePath: BasePath, options?: nock.Options): nock.Scope {
-- 
GitLab