Skip to content

Commit

Permalink
Update KMS sample (#595)
Browse files Browse the repository at this point in the history
* Fix bug in quickstart

* Fix quickstart sample + convert test to system-test
  • Loading branch information
Ace Nassri authored Apr 4, 2018
1 parent 7233e6b commit 13f22ea
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 73 deletions.
6 changes: 3 additions & 3 deletions kms/quickstart.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
const google = require('googleapis').google;

// Your Google Cloud Platform project ID
const projectId = 'YOUR_PROJECT_ID';
const projectId = process.env.GCLOUD_PROJECT;

// Lists keys in the "global" location.
const location = 'global';
Expand Down Expand Up @@ -54,11 +54,11 @@ google.auth.getApplicationDefault((err, authClient) => {
return;
}

const keyRings = result.keyRings || [];
const keyRings = result.data.keyRings || [];

if (keyRings.length) {
console.log('Key rings:');
result.keyRings.forEach((keyRing) => console.log(keyRing.name));
keyRings.forEach((keyRing) => console.log(keyRing.name));
} else {
console.log(`No key rings found.`);
}
Expand Down
74 changes: 4 additions & 70 deletions kms/system-test/quickstart.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,77 +15,11 @@

'use strict';

const proxyquire = require(`proxyquire`).noPreserveCache();
const google = proxyquire(`googleapis`, {});
const test = require(`ava`);
const tools = require(`@google-cloud/nodejs-repo-tools`);

function list (callback) {
google.auth.getApplicationDefault((err, authClient) => {
if (err) {
callback(err);
return;
}

if (authClient.createScopedRequired && authClient.createScopedRequired()) {
authClient = authClient.createScoped([
'https://www.googleapis.com/auth/cloud-platform'
]);
}

const cloudkms = google.cloudkms({
version: 'v1',
auth: authClient
});
const params = {
parent: `projects/${process.env.GCLOUD_PROJECT}/locations/global`
};

cloudkms.projects.locations.keyRings.list(params, callback);
});
}

test.beforeEach(tools.stubConsole);
test.afterEach.always(tools.restoreConsole);

test.cb(`should list key rings`, (t) => {
const googleapisMock = {
cloudkms () {
return {
projects: {
locations: {
keyRings: {
list (params, callback) {
list((err, result) => {
if (err) {
t.end(err);
return;
}
callback(err, result);

setTimeout(() => {
try {
t.true(console.log.called);
if (result && result.keyRings && result.keyRings.length) {
t.deepEqual(console.log.getCall(0).args, [`Key rings:`]);
} else {
t.deepEqual(console.log.getCall(0).args, [`No key rings found.`]);
}
t.end();
} catch (err) {
t.end(err);
}
}, 200);
});
}
}
}
}
};
}
};

proxyquire(`../quickstart`, {
'googleapis': googleapisMock
});
test(`should list key rings`, async t => {
const output = await tools.runAsync(`node quickstart.js`);
t.regex(output, /Key rings:/);
t.true(output.includes(`/locations/global/keyRings/`));
});

0 comments on commit 13f22ea

Please sign in to comment.