|
| 1 | +import * as sinon from "sinon"; |
| 2 | +import { expect } from "chai"; |
| 3 | +import * as ensureApiEnabled from "../ensureApiEnabled"; |
| 4 | +import * as iam from "../gcp/iam"; |
| 5 | +import * as rm from "../gcp/resourceManager"; |
| 6 | +import * as prompt from "../prompt"; |
| 7 | +import * as utils from "../utils"; |
| 8 | +import { FirebaseError } from "../error"; |
| 9 | +import * as apptesting from "./ensureProjectConfigured"; |
| 10 | + |
| 11 | +describe("ensureProjectConfigured", () => { |
| 12 | + const sandbox: sinon.SinonSandbox = sinon.createSandbox(); |
| 13 | + |
| 14 | + let serviceAccountHasRolesStub: sinon.SinonStub; |
| 15 | + let confirmStub: sinon.SinonStub; |
| 16 | + let ensureApiEnabledStub: sinon.SinonStub; |
| 17 | + let createServiceAccountStub: sinon.SinonStub; |
| 18 | + let addServiceAccountToRolesStub: sinon.SinonStub; |
| 19 | + let logWarningStub: sinon.SinonStub; |
| 20 | + |
| 21 | + beforeEach(() => { |
| 22 | + serviceAccountHasRolesStub = sandbox.stub(rm, "serviceAccountHasRoles"); |
| 23 | + confirmStub = sandbox.stub(prompt, "confirm"); |
| 24 | + ensureApiEnabledStub = sandbox.stub(ensureApiEnabled, "ensure"); |
| 25 | + createServiceAccountStub = sandbox.stub(iam, "createServiceAccount"); |
| 26 | + addServiceAccountToRolesStub = sandbox.stub(rm, "addServiceAccountToRoles"); |
| 27 | + logWarningStub = sandbox.stub(utils, "logWarning"); |
| 28 | + }); |
| 29 | + |
| 30 | + afterEach(() => { |
| 31 | + sandbox.verifyAndRestore(); |
| 32 | + }); |
| 33 | + |
| 34 | + const projectId = "test-project"; |
| 35 | + const serviceAccount = "firebaseapptesting-test-runner@test-project.iam.gserviceaccount.com"; |
| 36 | + const TEST_RUNNER_ROLE = "roles/firebaseapptesting.testRunner"; |
| 37 | + |
| 38 | + it("should ensure all necessary APIs are enabled", async () => { |
| 39 | + serviceAccountHasRolesStub.resolves(true); |
| 40 | + ensureApiEnabledStub.resolves(); |
| 41 | + |
| 42 | + await apptesting.ensureProjectConfigured(projectId); |
| 43 | + |
| 44 | + expect(ensureApiEnabledStub).to.be.calledThrice; |
| 45 | + expect(ensureApiEnabledStub).to.be.calledWith(projectId, sinon.match.any, "storage", false); |
| 46 | + expect(ensureApiEnabledStub).to.be.calledWith(projectId, sinon.match.any, "run", false); |
| 47 | + expect(ensureApiEnabledStub).to.be.calledWith( |
| 48 | + projectId, |
| 49 | + sinon.match.any, |
| 50 | + "artifactregistry", |
| 51 | + false, |
| 52 | + ); |
| 53 | + }); |
| 54 | + |
| 55 | + it("should do nothing if service account is already configured", async () => { |
| 56 | + ensureApiEnabledStub.resolves(); |
| 57 | + serviceAccountHasRolesStub.resolves(true); |
| 58 | + |
| 59 | + await apptesting.ensureProjectConfigured(projectId); |
| 60 | + |
| 61 | + expect(serviceAccountHasRolesStub).to.be.calledWith( |
| 62 | + projectId, |
| 63 | + serviceAccount, |
| 64 | + [TEST_RUNNER_ROLE], |
| 65 | + true, |
| 66 | + ); |
| 67 | + expect(confirmStub).to.not.have.been.called; |
| 68 | + expect(createServiceAccountStub).to.not.have.been.called; |
| 69 | + expect(addServiceAccountToRolesStub).to.not.have.been.called; |
| 70 | + }); |
| 71 | + |
| 72 | + it("should provision service account if user confirms", async () => { |
| 73 | + ensureApiEnabledStub.resolves(); |
| 74 | + serviceAccountHasRolesStub.resolves(false); |
| 75 | + confirmStub.resolves(true); |
| 76 | + createServiceAccountStub.resolves(); |
| 77 | + addServiceAccountToRolesStub.resolves(); |
| 78 | + |
| 79 | + await apptesting.ensureProjectConfigured(projectId); |
| 80 | + |
| 81 | + expect(serviceAccountHasRolesStub).to.be.calledWith( |
| 82 | + projectId, |
| 83 | + serviceAccount, |
| 84 | + [TEST_RUNNER_ROLE], |
| 85 | + true, |
| 86 | + ); |
| 87 | + expect(confirmStub).to.be.calledOnce; |
| 88 | + expect(createServiceAccountStub).to.be.calledWith( |
| 89 | + projectId, |
| 90 | + "firebaseapptesting-test-runner", |
| 91 | + sinon.match.string, |
| 92 | + sinon.match.string, |
| 93 | + ); |
| 94 | + expect(addServiceAccountToRolesStub).to.be.calledWith( |
| 95 | + projectId, |
| 96 | + serviceAccount, |
| 97 | + [TEST_RUNNER_ROLE], |
| 98 | + true, |
| 99 | + ); |
| 100 | + }); |
| 101 | + |
| 102 | + it("should throw error if user denies service account creation", async () => { |
| 103 | + ensureApiEnabledStub.resolves(); |
| 104 | + serviceAccountHasRolesStub.resolves(false); |
| 105 | + confirmStub.resolves(false); |
| 106 | + |
| 107 | + await expect(apptesting.ensureProjectConfigured(projectId)).to.be.rejectedWith( |
| 108 | + FirebaseError, |
| 109 | + /Firebase App Testing requires a service account/, |
| 110 | + ); |
| 111 | + |
| 112 | + expect(confirmStub).to.be.calledOnce; |
| 113 | + expect(createServiceAccountStub).to.not.have.been.called; |
| 114 | + expect(addServiceAccountToRolesStub).to.not.have.been.called; |
| 115 | + }); |
| 116 | + |
| 117 | + it("should handle service account already exists error", async () => { |
| 118 | + ensureApiEnabledStub.resolves(); |
| 119 | + serviceAccountHasRolesStub.resolves(false); |
| 120 | + confirmStub.resolves(true); |
| 121 | + createServiceAccountStub.rejects(new FirebaseError("Already exists", { status: 409 })); |
| 122 | + addServiceAccountToRolesStub.resolves(); |
| 123 | + |
| 124 | + await apptesting.ensureProjectConfigured(projectId); |
| 125 | + |
| 126 | + expect(createServiceAccountStub).to.be.calledOnce; |
| 127 | + expect(addServiceAccountToRolesStub).to.be.calledOnce; |
| 128 | + }); |
| 129 | + |
| 130 | + it("should handle addServiceAccountToRoles 400 error", async () => { |
| 131 | + ensureApiEnabledStub.resolves(); |
| 132 | + serviceAccountHasRolesStub.resolves(false); |
| 133 | + confirmStub.resolves(true); |
| 134 | + createServiceAccountStub.resolves(); |
| 135 | + addServiceAccountToRolesStub.rejects(new FirebaseError("Bad request", { status: 400 })); |
| 136 | + |
| 137 | + await apptesting.ensureProjectConfigured(projectId); |
| 138 | + |
| 139 | + expect(addServiceAccountToRolesStub).to.be.calledOnce; |
| 140 | + expect(logWarningStub).to.be.calledWith( |
| 141 | + `Your App Testing runner service account, "${serviceAccount}", is still being provisioned in the background. If you encounter an error, please try again after a few moments.`, |
| 142 | + ); |
| 143 | + }); |
| 144 | +}); |
0 commit comments