Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update KMS sample #595

Merged
merged 2 commits into from
Apr 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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/`));
});