|
| 1 | +import * as sinon from "sinon"; |
| 2 | +import { expect } from "chai"; |
| 3 | +import { Command } from "../command"; |
| 4 | +import { command as firestoreDatabasesCreate } from "./firestore-databases-create"; |
| 5 | +import * as fsi from "../firestore/api"; |
| 6 | +import * as types from "../firestore/api-types"; |
| 7 | +import { FirebaseError } from "../error"; |
| 8 | +import * as requireAuthModule from "../requireAuth"; |
| 9 | + |
| 10 | +describe("firestore:databases:create", () => { |
| 11 | + const PROJECT = "test-project"; |
| 12 | + const DATABASE = "test-database"; |
| 13 | + const LOCATION = "nam5"; |
| 14 | + |
| 15 | + let command: Command; |
| 16 | + let firestoreApiStub: sinon.SinonStubbedInstance<fsi.FirestoreApi>; |
| 17 | + let requireAuthStub: sinon.SinonStub; |
| 18 | + |
| 19 | + beforeEach(() => { |
| 20 | + command = firestoreDatabasesCreate; |
| 21 | + firestoreApiStub = sinon.createStubInstance(fsi.FirestoreApi); |
| 22 | + requireAuthStub = sinon.stub(requireAuthModule, "requireAuth"); |
| 23 | + sinon.stub(fsi, "FirestoreApi").returns(firestoreApiStub); |
| 24 | + requireAuthStub.resolves("[email protected]"); |
| 25 | + }); |
| 26 | + |
| 27 | + afterEach(() => { |
| 28 | + sinon.restore(); |
| 29 | + }); |
| 30 | + |
| 31 | + const mockDatabaseResp = (overrides: Partial<types.DatabaseResp>): types.DatabaseResp => { |
| 32 | + return { |
| 33 | + name: `projects/${PROJECT}/databases/${DATABASE}`, |
| 34 | + uid: "test-uid", |
| 35 | + createTime: "2025-07-28T12:00:00Z", |
| 36 | + updateTime: "2025-07-28T12:00:00Z", |
| 37 | + locationId: LOCATION, |
| 38 | + type: types.DatabaseType.FIRESTORE_NATIVE, |
| 39 | + databaseEdition: types.DatabaseEdition.STANDARD, |
| 40 | + concurrencyMode: "OPTIMISTIC", |
| 41 | + appEngineIntegrationMode: "DISABLED", |
| 42 | + keyPrefix: `projects/${PROJECT}/databases/${DATABASE}`, |
| 43 | + deleteProtectionState: types.DatabaseDeleteProtectionState.DISABLED, |
| 44 | + pointInTimeRecoveryEnablement: types.PointInTimeRecoveryEnablement.DISABLED, |
| 45 | + etag: "test-etag", |
| 46 | + versionRetentionPeriod: "1h", |
| 47 | + earliestVersionTime: "2025-07-28T11:00:00Z", |
| 48 | + ...overrides, |
| 49 | + }; |
| 50 | + }; |
| 51 | + |
| 52 | + it("should create a new database with the correct parameters", async () => { |
| 53 | + const options = { |
| 54 | + project: PROJECT, |
| 55 | + location: LOCATION, |
| 56 | + json: true, |
| 57 | + }; |
| 58 | + const expectedDatabase = mockDatabaseResp({}); |
| 59 | + firestoreApiStub.createDatabase.resolves(expectedDatabase); |
| 60 | + |
| 61 | + const result = await command.runner()(DATABASE, options); |
| 62 | + |
| 63 | + expect(result).to.deep.equal(expectedDatabase); |
| 64 | + expect( |
| 65 | + firestoreApiStub.createDatabase.calledOnceWith({ |
| 66 | + project: PROJECT, |
| 67 | + databaseId: DATABASE, |
| 68 | + locationId: LOCATION, |
| 69 | + type: types.DatabaseType.FIRESTORE_NATIVE, |
| 70 | + databaseEdition: types.DatabaseEdition.STANDARD, |
| 71 | + deleteProtectionState: types.DatabaseDeleteProtectionState.DISABLED, |
| 72 | + pointInTimeRecoveryEnablement: types.PointInTimeRecoveryEnablement.DISABLED, |
| 73 | + cmekConfig: undefined, |
| 74 | + }), |
| 75 | + ).to.be.true; |
| 76 | + }); |
| 77 | + |
| 78 | + it("should throw an error if location is not provided", async () => { |
| 79 | + const options = { |
| 80 | + project: PROJECT, |
| 81 | + }; |
| 82 | + |
| 83 | + await expect(command.runner()(DATABASE, options)).to.be.rejectedWith( |
| 84 | + FirebaseError, |
| 85 | + "Missing required flag --location", |
| 86 | + ); |
| 87 | + }); |
| 88 | + |
| 89 | + it("should throw an error for invalid delete protection option", async () => { |
| 90 | + const options = { |
| 91 | + project: PROJECT, |
| 92 | + location: LOCATION, |
| 93 | + deleteProtection: "INVALID", |
| 94 | + }; |
| 95 | + |
| 96 | + await expect(command.runner()(DATABASE, options)).to.be.rejectedWith( |
| 97 | + FirebaseError, |
| 98 | + "Invalid value for flag --delete-protection", |
| 99 | + ); |
| 100 | + }); |
| 101 | + |
| 102 | + it("should throw an error for invalid point-in-time recovery option", async () => { |
| 103 | + const options = { |
| 104 | + project: PROJECT, |
| 105 | + location: LOCATION, |
| 106 | + pointInTimeRecovery: "INVALID", |
| 107 | + }; |
| 108 | + |
| 109 | + await expect(command.runner()(DATABASE, options)).to.be.rejectedWith( |
| 110 | + FirebaseError, |
| 111 | + "Invalid value for flag --point-in-time-recovery", |
| 112 | + ); |
| 113 | + }); |
| 114 | + |
| 115 | + it("should throw an error for invalid edition option", async () => { |
| 116 | + const options = { |
| 117 | + project: PROJECT, |
| 118 | + location: LOCATION, |
| 119 | + edition: "INVALID", |
| 120 | + }; |
| 121 | + |
| 122 | + await expect(command.runner()(DATABASE, options)).to.be.rejectedWith( |
| 123 | + FirebaseError, |
| 124 | + "Invalid value for flag --edition", |
| 125 | + ); |
| 126 | + }); |
| 127 | + |
| 128 | + it("should create a database with enterprise edition", async () => { |
| 129 | + const options = { |
| 130 | + project: PROJECT, |
| 131 | + location: LOCATION, |
| 132 | + edition: "enterprise", |
| 133 | + json: true, |
| 134 | + }; |
| 135 | + const expectedDatabase = mockDatabaseResp({ |
| 136 | + databaseEdition: types.DatabaseEdition.ENTERPRISE, |
| 137 | + }); |
| 138 | + firestoreApiStub.createDatabase.resolves(expectedDatabase); |
| 139 | + |
| 140 | + const result = await command.runner()(DATABASE, options); |
| 141 | + |
| 142 | + expect(result).to.deep.equal(expectedDatabase); |
| 143 | + expect( |
| 144 | + firestoreApiStub.createDatabase.calledOnceWith({ |
| 145 | + project: PROJECT, |
| 146 | + databaseId: DATABASE, |
| 147 | + locationId: LOCATION, |
| 148 | + type: types.DatabaseType.FIRESTORE_NATIVE, |
| 149 | + databaseEdition: types.DatabaseEdition.ENTERPRISE, |
| 150 | + deleteProtectionState: types.DatabaseDeleteProtectionState.DISABLED, |
| 151 | + pointInTimeRecoveryEnablement: types.PointInTimeRecoveryEnablement.DISABLED, |
| 152 | + cmekConfig: undefined, |
| 153 | + }), |
| 154 | + ).to.be.true; |
| 155 | + }); |
| 156 | + |
| 157 | + it("should create a database with delete protection enabled", async () => { |
| 158 | + const options = { |
| 159 | + project: PROJECT, |
| 160 | + location: LOCATION, |
| 161 | + deleteProtection: "ENABLED", |
| 162 | + json: true, |
| 163 | + }; |
| 164 | + const expectedDatabase = mockDatabaseResp({ |
| 165 | + deleteProtectionState: types.DatabaseDeleteProtectionState.ENABLED, |
| 166 | + }); |
| 167 | + firestoreApiStub.createDatabase.resolves(expectedDatabase); |
| 168 | + |
| 169 | + const result = await command.runner()(DATABASE, options); |
| 170 | + |
| 171 | + expect(result).to.deep.equal(expectedDatabase); |
| 172 | + expect( |
| 173 | + firestoreApiStub.createDatabase.calledOnceWith({ |
| 174 | + project: PROJECT, |
| 175 | + databaseId: DATABASE, |
| 176 | + locationId: LOCATION, |
| 177 | + type: types.DatabaseType.FIRESTORE_NATIVE, |
| 178 | + databaseEdition: types.DatabaseEdition.STANDARD, |
| 179 | + deleteProtectionState: types.DatabaseDeleteProtectionState.ENABLED, |
| 180 | + pointInTimeRecoveryEnablement: types.PointInTimeRecoveryEnablement.DISABLED, |
| 181 | + cmekConfig: undefined, |
| 182 | + }), |
| 183 | + ).to.be.true; |
| 184 | + }); |
| 185 | + |
| 186 | + it("should create a database with point-in-time recovery enabled", async () => { |
| 187 | + const options = { |
| 188 | + project: PROJECT, |
| 189 | + location: LOCATION, |
| 190 | + pointInTimeRecovery: "ENABLED", |
| 191 | + json: true, |
| 192 | + }; |
| 193 | + const expectedDatabase = mockDatabaseResp({ |
| 194 | + pointInTimeRecoveryEnablement: types.PointInTimeRecoveryEnablement.ENABLED, |
| 195 | + }); |
| 196 | + firestoreApiStub.createDatabase.resolves(expectedDatabase); |
| 197 | + |
| 198 | + const result = await command.runner()(DATABASE, options); |
| 199 | + |
| 200 | + expect(result).to.deep.equal(expectedDatabase); |
| 201 | + expect( |
| 202 | + firestoreApiStub.createDatabase.calledOnceWith({ |
| 203 | + project: PROJECT, |
| 204 | + databaseId: DATABASE, |
| 205 | + locationId: LOCATION, |
| 206 | + type: types.DatabaseType.FIRESTORE_NATIVE, |
| 207 | + databaseEdition: types.DatabaseEdition.STANDARD, |
| 208 | + deleteProtectionState: types.DatabaseDeleteProtectionState.DISABLED, |
| 209 | + pointInTimeRecoveryEnablement: types.PointInTimeRecoveryEnablement.ENABLED, |
| 210 | + cmekConfig: undefined, |
| 211 | + }), |
| 212 | + ).to.be.true; |
| 213 | + }); |
| 214 | + |
| 215 | + it("should create a database with a KMS key", async () => { |
| 216 | + const KMS_KEY = "test-kms-key"; |
| 217 | + const options = { |
| 218 | + project: PROJECT, |
| 219 | + location: LOCATION, |
| 220 | + kmsKeyName: KMS_KEY, |
| 221 | + json: true, |
| 222 | + }; |
| 223 | + const expectedDatabase = mockDatabaseResp({ |
| 224 | + cmekConfig: { |
| 225 | + kmsKeyName: KMS_KEY, |
| 226 | + }, |
| 227 | + }); |
| 228 | + firestoreApiStub.createDatabase.resolves(expectedDatabase); |
| 229 | + |
| 230 | + const result = await command.runner()(DATABASE, options); |
| 231 | + |
| 232 | + expect(result).to.deep.equal(expectedDatabase); |
| 233 | + expect( |
| 234 | + firestoreApiStub.createDatabase.calledOnceWith({ |
| 235 | + project: PROJECT, |
| 236 | + databaseId: DATABASE, |
| 237 | + locationId: LOCATION, |
| 238 | + type: types.DatabaseType.FIRESTORE_NATIVE, |
| 239 | + databaseEdition: types.DatabaseEdition.STANDARD, |
| 240 | + deleteProtectionState: types.DatabaseDeleteProtectionState.DISABLED, |
| 241 | + pointInTimeRecoveryEnablement: types.PointInTimeRecoveryEnablement.DISABLED, |
| 242 | + cmekConfig: { |
| 243 | + kmsKeyName: KMS_KEY, |
| 244 | + }, |
| 245 | + }), |
| 246 | + ).to.be.true; |
| 247 | + }); |
| 248 | + |
| 249 | + it("should throw an error if the API call fails", async () => { |
| 250 | + const options = { |
| 251 | + project: PROJECT, |
| 252 | + location: LOCATION, |
| 253 | + }; |
| 254 | + const apiError = new Error("API Error"); |
| 255 | + firestoreApiStub.createDatabase.rejects(apiError); |
| 256 | + |
| 257 | + await expect(command.runner()(DATABASE, options)).to.be.rejectedWith(apiError); |
| 258 | + }); |
| 259 | +}); |
0 commit comments