|
| 1 | +// The MIT License (MIT) |
| 2 | +// |
| 3 | +// Copyright (c) 2018 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 all |
| 13 | +// 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 | + |
| 23 | +import { expect } from 'chai'; |
| 24 | + |
| 25 | +import { FirebaseFunctionsTest } from '../src/lifecycle'; |
| 26 | +import { mockConfig } from '../src/main'; |
| 27 | +import { afterEach } from 'mocha'; |
| 28 | + |
| 29 | +describe('lifecycle', () => { |
| 30 | + describe('#init', () => { |
| 31 | + let test = new FirebaseFunctionsTest(); |
| 32 | + afterEach(() => { |
| 33 | + test.cleanup(); |
| 34 | + }); |
| 35 | + |
| 36 | + it('sets env variables appropriately if SDK initialized without parameters', () => { |
| 37 | + test.init(); |
| 38 | + expect(process.env.FIREBASE_CONFIG).to.equal(JSON.stringify({ |
| 39 | + databaseURL: 'https://not-a-project.firebaseio.com', |
| 40 | + storageBucket: 'not-a-project.appspot.com', |
| 41 | + projectId: 'not-a-project', |
| 42 | + })); |
| 43 | + expect(process.env.GCLOUD_PROJECT).to.equal('not-a-project'); |
| 44 | + expect(process.env.GOOGLE_APPLICATION_CREDENTIALS).to.be.undefined; |
| 45 | + }); |
| 46 | + |
| 47 | + it('sets env variables appropriately if SDK initialized with parameters', () => { |
| 48 | + let firebaseConfig = { |
| 49 | + databaseURL: 'https://my-project.firebaseio.com', |
| 50 | + storageBucket: 'my-project.appspot.com', |
| 51 | + projectId: 'my-project', |
| 52 | + }; |
| 53 | + test.init(firebaseConfig, 'path/to/key.json'); |
| 54 | + |
| 55 | + expect(process.env.FIREBASE_CONFIG).to.equal(JSON.stringify(firebaseConfig)); |
| 56 | + expect(process.env.GCLOUD_PROJECT).to.equal('my-project'); |
| 57 | + expect(process.env.GOOGLE_APPLICATION_CREDENTIALS).to.equal('path/to/key.json'); |
| 58 | + }); |
| 59 | + }); |
| 60 | + |
| 61 | + describe('#cleanUp', () => { |
| 62 | + afterEach(() => { |
| 63 | + delete process.env.FIREBASE_CONFIG; |
| 64 | + delete process.env.GCLOUD_PROJECT; |
| 65 | + delete process.env.GOOGLE_APPLICATION_CREDENTIALS; |
| 66 | + delete process.env.CLOUD_RUNTIME_CONFIG; |
| 67 | + }); |
| 68 | + |
| 69 | + it('deletes all the env variables if they did not previously exist', () => { |
| 70 | + let test = new FirebaseFunctionsTest(); |
| 71 | + test.init(); |
| 72 | + mockConfig({ foo: {bar: 'faz '}}); |
| 73 | + test.cleanup(); |
| 74 | + expect(process.env.FIREBASE_CONFIG).to.be.undefined; |
| 75 | + expect(process.env.GCLOUD_PROJECT).to.be.undefined; |
| 76 | + expect(process.env.GOOGLE_APPLICATION_CREDENTIALS).to.be.undefined; |
| 77 | + expect(process.env.CLOUD_RUNTIME_CONFIG).to.be.undefined; |
| 78 | + }); |
| 79 | + |
| 80 | + it('restores env variables if they had previous values', () => { |
| 81 | + process.env.FIREBASE_CONFIG = 'oldFb'; |
| 82 | + process.env.GCLOUD_PROJECT = 'oldGc'; |
| 83 | + process.env.GOOGLE_APPLICATION_CREDENTIALS = 'oldGac'; |
| 84 | + process.env.CLOUD_RUNTIME_CONFIG = 'oldCrc'; |
| 85 | + let test = new FirebaseFunctionsTest(); |
| 86 | + |
| 87 | + test.init(); |
| 88 | + mockConfig({ foo: { bar: 'faz ' } }); |
| 89 | + test.cleanup(); |
| 90 | + |
| 91 | + expect(process.env.FIREBASE_CONFIG).to.equal('oldFb'); |
| 92 | + expect(process.env.GCLOUD_PROJECT).to.equal('oldGc'); |
| 93 | + expect(process.env.GOOGLE_APPLICATION_CREDENTIALS).to.equal('oldGac'); |
| 94 | + expect(process.env.CLOUD_RUNTIME_CONFIG).to.equal('oldCrc'); |
| 95 | + }); |
| 96 | + }); |
| 97 | +}); |
0 commit comments