From 655b19b024a5c048acb84d956dc009c421a1aa99 Mon Sep 17 00:00:00 2001 From: kdai7 <35241631+kdai7@users.noreply.github.com> Date: Tue, 17 Jan 2023 15:44:50 -0500 Subject: [PATCH] use nock for unit tests (#397) * use nock * Update rr-tests.js Co-authored-by: Katie Dai --- package-lock.json | 14 ++-- package.json | 2 +- test/rr-tests.js | 163 ++++++++++++++++++++++++++++++++-------------- 3 files changed, 122 insertions(+), 57 deletions(-) diff --git a/package-lock.json b/package-lock.json index 074f5ca1..4b591e33 100644 --- a/package-lock.json +++ b/package-lock.json @@ -32,7 +32,7 @@ "eslint": "^8.23.0", "markdownlint": "^0.26.2", "mocha": "^10.0.0", - "nock": "^13.2.9", + "nock": "^13.3.0", "npm-run-all": "^4.1.5", "nyc": "^15.1.0", "sinon": "^14.0.0", @@ -3982,9 +3982,9 @@ } }, "node_modules/nock": { - "version": "13.2.9", - "resolved": "https://registry.npmjs.org/nock/-/nock-13.2.9.tgz", - "integrity": "sha512-1+XfJNYF1cjGB+TKMWi29eZ0b82QOvQs2YoLNzbpWGqFMtRQHTa57osqdGj4FrFPgkO4D4AZinzUJR9VvW3QUA==", + "version": "13.3.0", + "resolved": "https://registry.npmjs.org/nock/-/nock-13.3.0.tgz", + "integrity": "sha512-HHqYQ6mBeiMc+N038w8LkMpDCRquCHWeNmN3v6645P3NhN2+qXOBqvPqo7Rt1VyCMzKhJ733wZqw5B7cQVFNPg==", "dev": true, "dependencies": { "debug": "^4.1.0", @@ -9153,9 +9153,9 @@ } }, "nock": { - "version": "13.2.9", - "resolved": "https://registry.npmjs.org/nock/-/nock-13.2.9.tgz", - "integrity": "sha512-1+XfJNYF1cjGB+TKMWi29eZ0b82QOvQs2YoLNzbpWGqFMtRQHTa57osqdGj4FrFPgkO4D4AZinzUJR9VvW3QUA==", + "version": "13.3.0", + "resolved": "https://registry.npmjs.org/nock/-/nock-13.3.0.tgz", + "integrity": "sha512-HHqYQ6mBeiMc+N038w8LkMpDCRquCHWeNmN3v6645P3NhN2+qXOBqvPqo7Rt1VyCMzKhJ733wZqw5B7cQVFNPg==", "dev": true, "requires": { "debug": "^4.1.0", diff --git a/package.json b/package.json index 38fc4036..a1c771a7 100644 --- a/package.json +++ b/package.json @@ -62,7 +62,7 @@ "eslint": "^8.23.0", "markdownlint": "^0.26.2", "mocha": "^10.0.0", - "nock": "^13.2.9", + "nock": "^13.3.0", "npm-run-all": "^4.1.5", "nyc": "^15.1.0", "sinon": "^14.0.0", diff --git a/test/rr-tests.js b/test/rr-tests.js index 7771357b..3b68c4de 100644 --- a/test/rr-tests.js +++ b/test/rr-tests.js @@ -15,10 +15,11 @@ */ const assert = require('chai').assert; -const request = require('request-promise-native'); const sinon = require('sinon'); +const nock = require('nock'); const xml2js = require('xml2js'); const hash = require('object-hash'); +const clone = require('clone'); const { KubeClass } = require('@razee/kubernetes-util'); const { BaseDownloadController, MockKubeResourceMeta} = require('@razee/razeedeploy-core'); const Factory = require('../src/BackendServiceFactory'); @@ -28,51 +29,42 @@ const RemoteResourceS3Controller = require('../src/RemoteResourceS3Controller'); describe('#RemoteResource', async function() { beforeEach(function() { - sinon.stub(request, 'get').callsFake(getFilesStub); sinon.stub(BaseDownloadController.prototype, 'added').callsFake(addedStub); }); afterEach(function(done) { sinon.restore(); + nock.cleanAll(); done(); }); - function getFilesStub() { - // the download urls are real, but the requests are stubbed - let files = [{ name: 'test-config.yaml', download_url: 'https://raw.githubusercontent.com/razee-io/RemoteResource/master/test/test-configs/test-config.yaml'}, - { name: 'teset-config-1.yaml', download_url: 'https://raw.githubusercontent.com/razee-io/RemoteResource/master/test/test-configs/test-config-1.yaml'}, - { name: 'test-config-2.yaml', download_url: 'https://raw.githubusercontent.com/razee-io/RemoteResource/master/test/test-configs/test-config-2.yaml'}]; - - files = JSON.stringify(files); - return files; - } - function addedStub() { return; } - function s3downloadStub() { - const body = { - ListBucketResult: { - '$': { xmlns: 'http://s3.amazonaws.com/doc/2006-03-01/' }, - Name: [ 'bucket' ], - Contents: [ - { - Key: [ 'test-config.yaml' ] - }, - { - Key: [ 'test-config-1.yaml' ] - }, - { - Key: [ 'test-config-2.yaml' ] - } - ] - } - }; - const builder = new xml2js.Builder(); - const xml = builder.buildObject(body); - return { statusCode: 200, body: xml}; - } + const files = JSON.stringify([{ name: 'test-config.yaml', download_url: 'https://raw.githubusercontent.com/razee-io/RemoteResource/master/test/test-configs/test-config.yaml'}, + { name: 'teset-config-1.yaml', download_url: 'https://raw.githubusercontent.com/razee-io/RemoteResource/master/test/test-configs/test-config-1.yaml'}, + { name: 'test-config-2.yaml', download_url: 'https://raw.githubusercontent.com/razee-io/RemoteResource/master/test/test-configs/test-config-2.yaml'}]); + + const body = { + ListBucketResult: { + '$': { xmlns: 'http://s3.amazonaws.com/doc/2006-03-01/' }, + Name: [ 'bucket' ], + Contents: [ + { + Key: [ 'test-config.yaml' ] + }, + { + Key: [ 'test-config-1.yaml' ] + }, + { + Key: [ 'test-config-2.yaml' ] + } + ] + } + }; + const builder = new xml2js.Builder(); + const xml = builder.buildObject(body); function setupController(eventData) { const log = require('../src/bunyan-api').createLogger('RemoteResource'); @@ -80,7 +72,7 @@ describe('#RemoteResource', async function() { const resourceMeta = new MockKubeResourceMeta('deploy.razee.io/v1alpha2', 'RemoteResource'); const params = { kubeResourceMeta: resourceMeta, - eventData: eventData, + eventData: clone(eventData), kubeClass: kc, logger: log }; @@ -135,7 +127,7 @@ describe('#RemoteResource', async function() { git: { provider: 'github', repo: 'https://github.com/razee-io/RemoteResource.git', - ref: 'tests', + ref: 'main', filePath: 'test/test-configs/test-config.yaml' } } @@ -165,7 +157,7 @@ describe('#RemoteResource', async function() { git: { provider: 'github', repo: 'https://github.com/razee-io/RemoteResource.git', - ref: 'tests', + ref: 'main', filePath: 'test/test-configs/*.yaml' } } @@ -189,10 +181,12 @@ describe('#RemoteResource', async function() { impersonateUser: 'razeedeploy' }, backendService: 's3', - iam: { - url: 'https://iam.cloud.ibm.com/identity/token', - grantType: 'urn:ibm:params:oauth:grant-type:apikey', - apiKey: 'testApiKey' + auth: { + iam: { + url: 'https://iam.cloud.ibm.com/identity/token', + grantType: 'urn:ibm:params:oauth:grant-type:apikey', + apiKey: 'testApiKey' + } }, requests: [ { @@ -219,10 +213,12 @@ describe('#RemoteResource', async function() { impersonateUser: 'razeedeploy' }, backendService: 's3', - iam: { - url: 'https://iam.cloud.ibm.com/identity/token', - grantType: 'urn:ibm:params:oauth:grant-type:apikey', - apiKey: 'testApiKey' + auth: { + iam: { + url: 'https://iam.cloud.ibm.com/identity/token', + grantType: 'urn:ibm:params:oauth:grant-type:apikey', + apiKey: 'testApiKey' + } }, requests: [ { @@ -258,7 +254,11 @@ describe('#RemoteResource', async function() { }); it('RRGitController single file request', async function() { - // RRGitController added() should correctly assemble request option + // RRGitController added() should correctly assemble request option + nock('https://api.github.com') + .get('/repos/razee-io/RemoteResource/contents/test/test-configs?ref=main') + .reply(200, files); + const controller = setupController(eventDataGit); assert(controller instanceof RemoteResourceGitController); await controller.added(); @@ -269,8 +269,27 @@ describe('#RemoteResource', async function() { assert(requests[0].splitRequestId); }); + it('RRGitController get files error', async function() { + // RRGitController should error if files not found + nock('https://api.github.com') + .get('/repos/razee-io/RemoteResource/contents/test/test-configs?ref=main') + .reply(404, 'file not found'); + try { + const controller = setupController(eventDataGit); + assert(controller instanceof RemoteResourceGitController); + await controller.added(); + assert.fail('should have thrown an error'); + } catch (e) { + assert.equal(e, '404 - "file not found"'); + } + }); + it('RRGitController multiple files request', async function() { // RRGitController added() should correctly assemble request options for multiple files + nock('https://api.github.com') + .get('/repos/razee-io/RemoteResource/contents/test/test-configs?ref=main') + .reply(200, files); + const controller = setupController(eventDataGit1); assert(controller instanceof RemoteResourceGitController); const sri = hash(eventDataGit1.object.spec.requests[0]); @@ -288,17 +307,23 @@ describe('#RemoteResource', async function() { it('RRS3Controller single file request', async function() { // RRS3Controller added() should correctly assemble request option const controller = setupController(eventDataS3); - sinon.stub(controller, 'download').callsFake(s3downloadStub); assert(controller instanceof RemoteResourceS3Controller); await controller.added(); - assert.equal(controller.data, eventDataS3); + assert.deepEqual(controller.data, eventDataS3); }); it('RRS3Controller multiple files request', async function() { // RRS3Controller added() should correctly assemble request options for multiple files + nock('https://iam.cloud.ibm.com/identity/token') + .post('?grant_type=urn:ibm:params:oauth:grant-type:apikey&apikey=testApiKey') + .reply(200, { access_token: 'testAccessToken'}); + + nock('https://s3.us.cloud-object-storage.appdomain.cloud') + .get('/bucket?prefix=') + .reply(200, xml); + const controller = setupController(eventDataS3_1); - sinon.stub(controller, 'download').callsFake(s3downloadStub); assert(controller instanceof RemoteResourceS3Controller); const sri = hash(eventDataS3_1_fixedUrl_request); //S3 fixes url before hashing for splitRequestId await controller.added(); @@ -310,4 +335,44 @@ describe('#RemoteResource', async function() { assert.equal(requests[i].splitRequestId, sri); // splitRequestIds should be the same to ensure applying all files in the group is attempted } }); + + it('RRS3Controller access token error', async function() { + // RRS3Controller added() should correctly assemble request options for multiple files + nock('https://iam.cloud.ibm.com/identity/token') + .post('?grant_type=urn:ibm:params:oauth:grant-type:apikey&apikey=testApiKey') + .reply(404); + + nock('https://s3.us.cloud-object-storage.appdomain.cloud') + .get('/bucket?prefix=') + .reply(200, xml); + + try { + const controller = setupController(eventDataS3_1); + assert(controller instanceof RemoteResourceS3Controller); + await controller.added(); + assert.fail('should have thrown an error'); + } catch(e) { + assert.equal(e.status, 404); + } + }); + + it('RRS3Controller get files error', async function() { + // RRS3Controller added() should correctly assemble request options for multiple files + nock('https://iam.cloud.ibm.com/identity/token') + .post('?grant_type=urn:ibm:params:oauth:grant-type:apikey&apikey=testApiKey') + .reply(200, { access_token: 'testAccessToken'}); + + nock('https://s3.us.cloud-object-storage.appdomain.cloud') + .get('/bucket?prefix=') + .reply(404); + + try { + const controller = setupController(eventDataS3_1); + assert(controller instanceof RemoteResourceS3Controller); + await controller.added(); + assert.fail('should have thrown an error'); + } catch(e) { + assert.equal(e.statusCode, 404); + } + }); });