|
| 1 | +// The MIT License (MIT) |
| 2 | +// |
| 3 | +// Copyright (c) 2025 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 | +import * as sinon from "sinon"; |
| 25 | + |
| 26 | +import * as params from "../../src/params"; |
| 27 | +import * as options from "../../src/v2/options"; |
| 28 | +import { ResetValue, RESET_VALUE } from "../../src/common/options"; |
| 29 | +import { GCLOUD_PROJECT } from "../init"; |
| 30 | + |
| 31 | +describe("optionsToEndpoint", () => { |
| 32 | + afterEach(() => { |
| 33 | + sinon.restore(); |
| 34 | + }); |
| 35 | + |
| 36 | + it("should convert serviceAccount in shorthand form with @ to full email", () => { |
| 37 | + const opts: options.GlobalOptions = { |
| 38 | + serviceAccount: "my-service@", |
| 39 | + }; |
| 40 | + |
| 41 | + const result = options.optionsToEndpoint(opts); |
| 42 | + |
| 43 | + expect(result.serviceAccountEmail).to.equal( |
| 44 | + `my-service@${GCLOUD_PROJECT}.iam.gserviceaccount.com` |
| 45 | + ); |
| 46 | + }); |
| 47 | + |
| 48 | + it("should pass through serviceAccount that already has full email format", () => { |
| 49 | + const opts: options.GlobalOptions = { |
| 50 | + serviceAccount: "[email protected]", |
| 51 | + }; |
| 52 | + |
| 53 | + const result = options.optionsToEndpoint(opts); |
| 54 | + |
| 55 | + expect(result.serviceAccountEmail).to.equal("[email protected]"); |
| 56 | + }); |
| 57 | + |
| 58 | + it("should convert 'default' serviceAccount to null", () => { |
| 59 | + const opts: options.GlobalOptions = { |
| 60 | + serviceAccount: "default", |
| 61 | + }; |
| 62 | + |
| 63 | + const result = options.optionsToEndpoint(opts); |
| 64 | + |
| 65 | + expect(result.serviceAccountEmail).to.be.null; |
| 66 | + }); |
| 67 | + |
| 68 | + it("should throw error when using shorthand without GCLOUD_PROJECT", () => { |
| 69 | + delete process.env.GCLOUD_PROJECT; |
| 70 | + const opts: options.GlobalOptions = { |
| 71 | + serviceAccount: "my-service@", |
| 72 | + }; |
| 73 | + |
| 74 | + expect(() => options.optionsToEndpoint(opts)).to.throw( |
| 75 | + "Unable to determine email for service account 'my-service@' because process.env.GCLOUD_PROJECT is not set." |
| 76 | + ); |
| 77 | + process.env.GCLOUD_PROJECT = GCLOUD_PROJECT; |
| 78 | + }); |
| 79 | + |
| 80 | + it("should throw error for invalid serviceAccount format", () => { |
| 81 | + const opts: options.GlobalOptions = { |
| 82 | + serviceAccount: "invalid-format", |
| 83 | + }; |
| 84 | + |
| 85 | + expect(() => options.optionsToEndpoint(opts)).to.throw( |
| 86 | + "Invalid option for serviceAccount: 'invalid-format'. Valid options are 'default', a service account email, or '{serviceAccountName}@'" |
| 87 | + ); |
| 88 | + }); |
| 89 | + |
| 90 | + it("should return if given RESET_VALUE for serviceAccount", () => { |
| 91 | + const opts: options.GlobalOptions = { |
| 92 | + serviceAccount: RESET_VALUE, |
| 93 | + }; |
| 94 | + |
| 95 | + const result = options.optionsToEndpoint(opts); |
| 96 | + |
| 97 | + expect(result.serviceAccountEmail).to.be.instanceof(ResetValue); |
| 98 | + }); |
| 99 | + |
| 100 | + it("should pass through Expression for serviceAccount", () => { |
| 101 | + const param = params.defineString("sa"); |
| 102 | + const opts: options.GlobalOptions = { |
| 103 | + serviceAccount: param, |
| 104 | + }; |
| 105 | + |
| 106 | + const result = options.optionsToEndpoint(opts); |
| 107 | + |
| 108 | + expect(result.serviceAccountEmail).to.equal(param); |
| 109 | + }); |
| 110 | +}); |
0 commit comments