|
| 1 | +/** |
| 2 | + * Copyright 2018, Google LLC. |
| 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 | + |
| 16 | +'use strict'; |
| 17 | + |
| 18 | +const uuid = require('uuid'); |
| 19 | +const test = require('ava'); |
| 20 | +const utils = require('@google-cloud/nodejs-repo-tools'); |
| 21 | + |
| 22 | +const program = require('../'); |
| 23 | + |
| 24 | +test.beforeEach(utils.stubConsole); |
| 25 | +test.afterEach.always(utils.restoreConsole); |
| 26 | + |
| 27 | +test.serial('should monitor Firebase RTDB', t => { |
| 28 | + const dataId = uuid.v4(); |
| 29 | + const resourceId = uuid.v4(); |
| 30 | + |
| 31 | + const data = { |
| 32 | + admin: true, |
| 33 | + delta: { |
| 34 | + id: dataId |
| 35 | + } |
| 36 | + }; |
| 37 | + const context = { |
| 38 | + resource: resourceId |
| 39 | + }; |
| 40 | + |
| 41 | + program.helloRTDB(data, context); |
| 42 | + |
| 43 | + t.true(console.log.firstCall.args[0].includes(resourceId)); |
| 44 | + t.deepEqual(console.log.secondCall.args, ['Admin?: true']); |
| 45 | + t.true(console.log.getCall(3).args[0].includes(dataId)); |
| 46 | +}); |
| 47 | + |
| 48 | +test.serial('should monitor Firestore', t => { |
| 49 | + const resourceId = uuid.v4(); |
| 50 | + |
| 51 | + const context = { |
| 52 | + resource: resourceId |
| 53 | + }; |
| 54 | + const data = { |
| 55 | + oldValue: { uuid: uuid.v4() }, |
| 56 | + value: { uuid: uuid.v4() } |
| 57 | + }; |
| 58 | + |
| 59 | + program.helloFirestore(data, context); |
| 60 | + |
| 61 | + t.true(console.log.firstCall.args[0].includes(resourceId)); |
| 62 | + t.true(console.log.calledWith(JSON.stringify(data.oldValue, null, 2))); |
| 63 | + t.true(console.log.calledWith(JSON.stringify(data.value, null, 2))); |
| 64 | +}); |
| 65 | + |
| 66 | +test.serial('should monitor Auth', t => { |
| 67 | + const userId = uuid.v4(); |
| 68 | + const dateString = (new Date()).toISOString(); |
| 69 | + const emailString = `${uuid.v4()}@${uuid.v4()}.com`; |
| 70 | + |
| 71 | + const data = { |
| 72 | + uid: userId, |
| 73 | + metadata: { |
| 74 | + createdAt: dateString |
| 75 | + }, |
| 76 | + email: emailString |
| 77 | + }; |
| 78 | + |
| 79 | + program.helloAuth(data, null); |
| 80 | + |
| 81 | + t.true(console.log.firstCall.args[0].includes(userId)); |
| 82 | + t.true(console.log.secondCall.args[0].includes(dateString)); |
| 83 | + t.true(console.log.thirdCall.args[0].includes(emailString)); |
| 84 | +}); |
0 commit comments