|
| 1 | +// Copyright 2019 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 | +// http://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 {PubSub} = require('@google-cloud/pubsub'); |
| 18 | +const {Storage} = require('@google-cloud/storage'); |
| 19 | +const {assert} = require('chai'); |
| 20 | +const {before, after, it} = require('mocha'); |
| 21 | +const cp = require('child_process'); |
| 22 | +const uuid = require('uuid'); |
| 23 | + |
| 24 | +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); |
| 25 | + |
| 26 | +const storage = new Storage(); |
| 27 | +const bucketName = `nodejs-storage-samples-${uuid.v4()}`; |
| 28 | +const bucket = storage.bucket(bucketName); |
| 29 | +const notificationId = '1'; |
| 30 | +const notification = bucket.notification(notificationId); |
| 31 | +const topicName = `nodejs-storage-samples-${uuid.v4()}`; |
| 32 | +const pubsub = new PubSub(); |
| 33 | +const topic = pubsub.topic(topicName); |
| 34 | + |
| 35 | +before(async () => { |
| 36 | + await bucket.create(); |
| 37 | + await topic.create(); |
| 38 | + const [gcsServiceAccount] = await storage.getServiceAccount(); |
| 39 | + await topic.iam.setPolicy({ |
| 40 | + bindings: [ |
| 41 | + { |
| 42 | + role: 'roles/pubsub.editor', |
| 43 | + members: [`serviceAccount:${gcsServiceAccount.emailAddress}`], |
| 44 | + }, |
| 45 | + ], |
| 46 | + }); |
| 47 | +}); |
| 48 | + |
| 49 | +after(async () => { |
| 50 | + await bucket.delete().catch(console.error); |
| 51 | + await topic.delete().catch(console.error); |
| 52 | +}); |
| 53 | + |
| 54 | +it('should create a notification', async () => { |
| 55 | + const output = execSync( |
| 56 | + `node createNotification.js ${bucketName} ${topicName}` |
| 57 | + ); |
| 58 | + assert.match(output, /Notification subscription created./); |
| 59 | + const [exists] = await notification.exists(); |
| 60 | + assert.strictEqual(exists, true); |
| 61 | +}); |
| 62 | + |
| 63 | +it('should list notifications', async () => { |
| 64 | + const output = execSync(`node listNotifications.js ${bucketName}`); |
| 65 | + assert.match(output, /Notifications:/); |
| 66 | + assert.match(output, new RegExp(notificationId)); |
| 67 | +}); |
| 68 | + |
| 69 | +it('should get metadata', async () => { |
| 70 | + const metadata = await notification.getMetadata(); |
| 71 | + const output = execSync( |
| 72 | + `node getMetadataNotifications.js ${bucketName} ${notificationId}` |
| 73 | + ); |
| 74 | + assert.match(output, /ID:/); |
| 75 | + assert.match(output, new RegExp(metadata.id)); |
| 76 | + assert.match(output, /Topic:/); |
| 77 | + assert.match(output, new RegExp(metadata.topic)); |
| 78 | + assert.match(output, /Event Types:/); |
| 79 | + assert.match(output, new RegExp(metadata.event_types)); |
| 80 | + assert.match(output, /Custom Attributes:/); |
| 81 | + assert.match(output, new RegExp(metadata.custom_attributes)); |
| 82 | + assert.match(output, /Payload Format:/); |
| 83 | + assert.match(output, new RegExp(metadata.payload_format)); |
| 84 | + assert.match(output, /Object Name Prefix:/); |
| 85 | + assert.match(output, new RegExp(metadata.object_name_prefix)); |
| 86 | + assert.match(output, /Etag:/); |
| 87 | + assert.match(output, /Self Link:/); |
| 88 | + assert.match(output, new RegExp(metadata.selfLink)); |
| 89 | + assert.match(output, /Kind:/); |
| 90 | + assert.match(output, new RegExp(metadata.kind)); |
| 91 | +}); |
| 92 | + |
| 93 | +it('should delete a notification', async () => { |
| 94 | + const output = execSync( |
| 95 | + `node deleteNotification.js ${bucketName} ${notificationId}` |
| 96 | + ); |
| 97 | + assert.match(output, new RegExp(`Notification ${notificationId} deleted.`)); |
| 98 | + const [exists] = await notification.exists(); |
| 99 | + assert.strictEqual(exists, false); |
| 100 | +}); |
0 commit comments