|
| 1 | +// Copyright 2020 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +'use strict'; |
| 16 | + |
| 17 | +const { |
| 18 | + SecurityCenterClient, |
| 19 | +} = require('@google-cloud/security-center').v1p1beta1; |
| 20 | +const uuidv1 = require('uuid/v1'); |
| 21 | +const {assert} = require('chai'); |
| 22 | +const {describe, it, before, after} = require('mocha'); |
| 23 | +const {execSync} = require('child_process'); |
| 24 | +const exec = cmd => execSync(cmd, {encoding: 'utf8'}); |
| 25 | + |
| 26 | +const organizationId = process.env['GCLOUD_ORGANIZATION']; |
| 27 | +const orgName = 'organizations/' + organizationId; |
| 28 | +const pubsubTopic = 'projects/project-a-id/topics/notifications-sample-topic'; |
| 29 | + |
| 30 | +describe('Client with Notifications', async () => { |
| 31 | + const createConfig = 'notif-config-test-node-create' + uuidv1(); |
| 32 | + const deleteConfig = 'notif-config-test-node-delete' + uuidv1(); |
| 33 | + const getConfig = 'notif-config-test-node-get' + uuidv1(); |
| 34 | + const listConfig = 'notif-config-test-node-list' + uuidv1(); |
| 35 | + const updateConfig = 'notif-config-test-node-update' + uuidv1(); |
| 36 | + |
| 37 | + before(async () => { |
| 38 | + const client = new SecurityCenterClient(); |
| 39 | + async function createNotificationConfig(configId) { |
| 40 | + /*eslint no-unused-vars: ["error", { "varsIgnorePattern": "^_" }]*/ |
| 41 | + const [_response] = await client.createNotificationConfig({ |
| 42 | + parent: orgName, |
| 43 | + configId: configId, |
| 44 | + notificationConfig: { |
| 45 | + description: 'Sample config for node.js', |
| 46 | + pubsubTopic: pubsubTopic, |
| 47 | + eventType: 'FINDING', |
| 48 | + streamingConfig: {filter: 'state = "ACTIVE"'}, |
| 49 | + }, |
| 50 | + }); |
| 51 | + } |
| 52 | + |
| 53 | + await createNotificationConfig(deleteConfig); |
| 54 | + await createNotificationConfig(getConfig); |
| 55 | + await createNotificationConfig(listConfig); |
| 56 | + await createNotificationConfig(updateConfig); |
| 57 | + }); |
| 58 | + |
| 59 | + after(async () => { |
| 60 | + const client = new SecurityCenterClient(); |
| 61 | + async function deleteNotificationConfig(configId) { |
| 62 | + const name = client.notificationConfigPath(organizationId, configId); |
| 63 | + await client.deleteNotificationConfig({name: name}); |
| 64 | + } |
| 65 | + |
| 66 | + await deleteNotificationConfig(createConfig); |
| 67 | + await deleteNotificationConfig(getConfig); |
| 68 | + await deleteNotificationConfig(listConfig); |
| 69 | + await deleteNotificationConfig(updateConfig); |
| 70 | + }); |
| 71 | + |
| 72 | + it('client can create config', () => { |
| 73 | + const output = exec( |
| 74 | + `node v1p1beta1/createNotificationConfig.js ${organizationId} ${createConfig} ${pubsubTopic}` |
| 75 | + ); |
| 76 | + assert.include(output, createConfig); |
| 77 | + assert.match(output, /Notification config creation succeeded/); |
| 78 | + assert.notMatch(output, /undefined/); |
| 79 | + }); |
| 80 | + |
| 81 | + it('client can delete config', () => { |
| 82 | + const output = exec( |
| 83 | + `node v1p1beta1/deleteNotificationConfig.js ${organizationId} ${deleteConfig}` |
| 84 | + ); |
| 85 | + assert.include(output, 'Notification config deleted'); |
| 86 | + assert.notMatch(output, /undefined/); |
| 87 | + }); |
| 88 | + |
| 89 | + it('client can get config', () => { |
| 90 | + const output = exec( |
| 91 | + `node v1p1beta1/getNotificationConfig.js ${organizationId} ${getConfig}` |
| 92 | + ); |
| 93 | + assert.include(output, getConfig); |
| 94 | + assert.match(output, /Notification config/); |
| 95 | + assert.notMatch(output, /undefined/); |
| 96 | + }); |
| 97 | + |
| 98 | + it('client can list configs', () => { |
| 99 | + const output = exec( |
| 100 | + `node v1p1beta1/listNotificationConfigs.js ${organizationId}` |
| 101 | + ); |
| 102 | + assert.include(output, listConfig); |
| 103 | + assert.match(output, /Received Notification configs/); |
| 104 | + assert.notMatch(output, /undefined/); |
| 105 | + }); |
| 106 | + |
| 107 | + it('client can update configs', () => { |
| 108 | + const output = exec( |
| 109 | + `node v1p1beta1/updateNotificationConfig.js ${organizationId} ${updateConfig} ${pubsubTopic}` |
| 110 | + ); |
| 111 | + assert.include(output, updateConfig); |
| 112 | + assert.match(output, /notification config update succeeded/); |
| 113 | + assert.notMatch(output, /undefined/); |
| 114 | + }); |
| 115 | +}); |
0 commit comments