Skip to content

Commit

Permalink
use nock for unit tests (#397)
Browse files Browse the repository at this point in the history
* use nock

* Update rr-tests.js

Co-authored-by: Katie Dai <[email protected]>
  • Loading branch information
kdai7 and Katie Dai authored Jan 17, 2023
1 parent a71440f commit 655b19b
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 57 deletions.
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
163 changes: 114 additions & 49 deletions test/rr-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -28,59 +29,50 @@ 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');
const kc = new KubeClass();
const resourceMeta = new MockKubeResourceMeta('deploy.razee.io/v1alpha2', 'RemoteResource');
const params = {
kubeResourceMeta: resourceMeta,
eventData: eventData,
eventData: clone(eventData),
kubeClass: kc,
logger: log
};
Expand Down Expand Up @@ -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'
}
}
Expand Down Expand Up @@ -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'
}
}
Expand All @@ -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: [
{
Expand All @@ -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: [
{
Expand Down Expand Up @@ -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();
Expand All @@ -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]);
Expand All @@ -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();
Expand All @@ -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);
}
});
});

0 comments on commit 655b19b

Please sign in to comment.