|
| 1 | +// The MIT License (MIT) |
| 2 | +// |
| 3 | +// Copyright (c) 2017 Firebase |
| 4 | +// |
| 5 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | +// of this software and associated documentation files (the "Software"), to deal |
| 7 | +// in the Software without restriction, including without limitation the rights |
| 8 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | +// copies of the Software, and to permit persons to whom the Software is |
| 10 | +// furnished to do so, subject to the following conditions: |
| 11 | +// |
| 12 | +// The above copyright notice and this permission notice shall be included in |
| 13 | +// all copies or substantial portions of the Software. |
| 14 | +// |
| 15 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 21 | +// SOFTWARE. |
| 22 | +import { expect } from 'chai'; |
| 23 | +import * as admin from 'firebase-admin'; |
| 24 | +import * as _ from 'lodash'; |
| 25 | + |
| 26 | +import { CloudFunction } from '../../src/cloud-functions'; |
| 27 | +import * as functions from '../../src/index'; |
| 28 | +import * as remoteConfig from '../../src/providers/remoteConfig'; |
| 29 | + |
| 30 | +describe('RemoteConfig Functions', () => { |
| 31 | + function constructVersion() { |
| 32 | + return { |
| 33 | + versionNumber: 1, |
| 34 | + updateTime: '2017-07-02T18:48:58.920638Z', |
| 35 | + updateUser: { |
| 36 | + name: 'Foo Bar', |
| 37 | + |
| 38 | + }, |
| 39 | + description: 'test description', |
| 40 | + updateOrigin: 'CONSOLE', |
| 41 | + updateType: 'INCREMENTAL_UPDATE', |
| 42 | + }; |
| 43 | + } |
| 44 | + |
| 45 | + function makeEvent(data: any, context?: { [key: string]: any }) { |
| 46 | + context = context || {}; |
| 47 | + return { |
| 48 | + data: data, |
| 49 | + context: _.merge( |
| 50 | + { |
| 51 | + eventId: '123', |
| 52 | + timestamp: '2018-07-03T00:49:04.264Z', |
| 53 | + eventType: 'google.firebase.remoteconfig.update', |
| 54 | + resource: { |
| 55 | + name: 'projects/project1', |
| 56 | + service: 'service', |
| 57 | + }, |
| 58 | + }, |
| 59 | + context |
| 60 | + ), |
| 61 | + }; |
| 62 | + } |
| 63 | + |
| 64 | + describe('#onUpdate', () => { |
| 65 | + function expectedTrigger() { |
| 66 | + return { |
| 67 | + eventTrigger: { |
| 68 | + resource: 'projects/project1', |
| 69 | + eventType: 'google.firebase.remoteconfig.update', |
| 70 | + service: 'firebaseremoteconfig.googleapis.com', |
| 71 | + }, |
| 72 | + }; |
| 73 | + } |
| 74 | + |
| 75 | + before(() => { |
| 76 | + process.env.GCLOUD_PROJECT = 'project1'; |
| 77 | + }); |
| 78 | + |
| 79 | + after(() => { |
| 80 | + delete process.env.GCLOUD_PROJECT; |
| 81 | + }); |
| 82 | + |
| 83 | + it('should have the correct trigger', () => { |
| 84 | + const cloudFunction = remoteConfig.onUpdate(() => null); |
| 85 | + expect(cloudFunction.__trigger).to.deep.equal(expectedTrigger()); |
| 86 | + }); |
| 87 | + |
| 88 | + it('should allow both region and runtime options to be set', () => { |
| 89 | + const cloudFunction = functions |
| 90 | + .region('my-region') |
| 91 | + .runWith({ |
| 92 | + timeoutSeconds: 90, |
| 93 | + memory: '256MB', |
| 94 | + }) |
| 95 | + .remoteConfig.onUpdate(() => null); |
| 96 | + |
| 97 | + expect(cloudFunction.__trigger.regions).to.deep.equal(['my-region']); |
| 98 | + expect(cloudFunction.__trigger.availableMemoryMb).to.deep.equal(256); |
| 99 | + expect(cloudFunction.__trigger.timeout).to.deep.equal('90s'); |
| 100 | + }); |
| 101 | + }); |
| 102 | + |
| 103 | + describe('unwraps TemplateVersion', () => { |
| 104 | + let cloudFunctionUpdate: CloudFunction<remoteConfig.TemplateVersion>; |
| 105 | + let event: any; |
| 106 | + before(() => { |
| 107 | + process.env.GCLOUD_PROJECT = 'project1'; |
| 108 | + cloudFunctionUpdate = remoteConfig.onUpdate( |
| 109 | + (version: remoteConfig.TemplateVersion) => version |
| 110 | + ); |
| 111 | + event = { |
| 112 | + data: constructVersion(), |
| 113 | + }; |
| 114 | + }); |
| 115 | + |
| 116 | + after(() => { |
| 117 | + delete process.env.GCLOUD_PROJECT; |
| 118 | + }); |
| 119 | + |
| 120 | + it('should unwrap the version in the event', () => { |
| 121 | + return Promise.all([ |
| 122 | + cloudFunctionUpdate(event).then((data: any) => { |
| 123 | + expect(data).to.deep.equal(constructVersion()); |
| 124 | + }), |
| 125 | + ]); |
| 126 | + }); |
| 127 | + }); |
| 128 | +}); |
0 commit comments