@firebase/rules-unit-testing 2.0.2-canary.fad3c4765 → 2.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +6 -0
- package/dist/esm/index.d.ts +19 -0
- package/dist/esm/index.esm.js +607 -0
- package/dist/esm/index.esm.js.map +1 -0
- package/dist/esm/package.json +1 -0
- package/dist/esm/src/impl/discovery.d.ts +47 -0
- package/dist/esm/src/impl/rules.d.ts +29 -0
- package/dist/esm/src/impl/test_environment.d.ts +43 -0
- package/dist/esm/src/impl/url.d.ts +32 -0
- package/dist/esm/src/initialize.d.ts +39 -0
- package/dist/esm/src/public_types/index.d.ts +228 -0
- package/dist/esm/src/util.d.ts +75 -0
- package/dist/esm/test/dummy.test.d.ts +17 -0
- package/dist/esm/test/impl/discovery.test.d.ts +17 -0
- package/dist/esm/test/impl/rules.test.d.ts +17 -0
- package/dist/esm/test/setup.d.ts +19 -0
- package/dist/esm/test/test_utils.d.ts +18 -0
- package/dist/esm/test/util.test.d.ts +17 -0
- package/dist/index.cjs.js +1 -1
- package/dist/index.cjs.js.map +1 -1
- package/dist/tsdoc-metadata.json +11 -11
- package/package.json +15 -4
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.esm.js","sources":["../../src/impl/url.ts","../../src/impl/discovery.ts","../../src/impl/test_environment.ts","../../src/impl/rules.ts","../../src/initialize.ts","../../src/util.ts"],"sourcesContent":["/**\n * @license\n * Copyright 2021 Google LLC\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { HostAndPort } from '../public_types';\n\n/**\n * Return a connectable hostname, replacing wildcard 0.0.0.0 or :: with loopback\n * addresses 127.0.0.1 / ::1 correspondingly. See below for why this is needed:\n * https://github.com/firebase/firebase-tools-ui/issues/286\n *\n * This assumes emulators are running on the same device as fallbackHost (e.g.\n * hub), which should hold if both are started from the same CLI command.\n * @private\n */\nexport function fixHostname(host: string, fallbackHost?: string): string {\n host = host.replace('[', '').replace(']', ''); // Remove IPv6 brackets\n if (host === '0.0.0.0') {\n host = fallbackHost || '127.0.0.1';\n } else if (host === '::') {\n host = fallbackHost || '::1';\n }\n return host;\n}\n\n/**\n * Create a URL with host, port, and path. Handles IPv6 bracketing correctly.\n * @private\n */\nexport function makeUrl(hostAndPort: HostAndPort | string, path: string): URL {\n if (typeof hostAndPort === 'object') {\n const { host, port } = hostAndPort;\n if (host.includes(':')) {\n hostAndPort = `[${host}]:${port}`;\n } else {\n hostAndPort = `${host}:${port}`;\n }\n }\n const url = new URL(`http://${hostAndPort}/`);\n url.pathname = path;\n return url;\n}\n","/**\n * @license\n * Copyright 2021 Google LLC\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { EmulatorConfig, HostAndPort } from '../public_types';\nimport nodeFetch from 'node-fetch';\nimport { makeUrl, fixHostname } from './url';\n\n/**\n * Use the Firebase Emulator hub to discover other running emulators.\n *\n * @param hub the host and port where the Emulator Hub is running\n * @private\n */\nexport async function discoverEmulators(\n hub: HostAndPort,\n fetch: typeof nodeFetch = nodeFetch\n): Promise<DiscoveredEmulators> {\n const res = await fetch(makeUrl(hub, '/emulators'));\n if (!res.ok) {\n throw new Error(\n `HTTP Error ${res.status} when attempting to reach Emulator Hub at ${res.url}, are you sure it is running?`\n );\n }\n\n const emulators: DiscoveredEmulators = {};\n\n const data = await res.json();\n\n if (data.database) {\n emulators.database = {\n host: data.database.host,\n port: data.database.port\n };\n }\n\n if (data.firestore) {\n emulators.firestore = {\n host: data.firestore.host,\n port: data.firestore.port\n };\n }\n\n if (data.storage) {\n emulators.storage = {\n host: data.storage.host,\n port: data.storage.port\n };\n }\n\n if (data.hub) {\n emulators.hub = {\n host: data.hub.host,\n port: data.hub.port\n };\n }\n return emulators;\n}\n\n/**\n * @private\n */\nexport interface DiscoveredEmulators {\n database?: HostAndPort;\n firestore?: HostAndPort;\n storage?: HostAndPort;\n hub?: HostAndPort;\n}\n\n/**\n * @private\n */\nexport function getEmulatorHostAndPort(\n emulator: keyof DiscoveredEmulators,\n conf?: EmulatorConfig,\n discovered?: DiscoveredEmulators\n) {\n if (conf && ('host' in conf || 'port' in conf)) {\n const { host, port } = conf;\n if (host || port) {\n if (!host || !port) {\n throw new Error(\n `Invalid configuration ${emulator}.host=${host} and ${emulator}.port=${port}. ` +\n 'If either parameter is supplied, both must be defined.'\n );\n }\n if (discovered && !discovered[emulator]) {\n console.warn(\n `Warning: config for the ${emulator} emulator is specified, but the Emulator hub ` +\n 'reports it as not running. This may lead to errors such as connection refused.'\n );\n }\n return {\n host: fixHostname(conf.host, discovered?.hub?.host),\n port: conf.port\n };\n }\n }\n const envVar = EMULATOR_HOST_ENV_VARS[emulator];\n const fallback = discovered?.[emulator] || emulatorFromEnvVar(envVar);\n if (fallback) {\n if (discovered && !discovered[emulator]) {\n console.warn(\n `Warning: the environment variable ${envVar} is set, but the Emulator hub reports the ` +\n `${emulator} emulator as not running. This may lead to errors such as connection refused.`\n );\n }\n return {\n host: fixHostname(fallback.host, discovered?.hub?.host),\n port: fallback.port\n };\n }\n}\n\n// Visible for testing.\nexport const EMULATOR_HOST_ENV_VARS = {\n 'database': 'FIREBASE_DATABASE_EMULATOR_HOST',\n 'firestore': 'FIRESTORE_EMULATOR_HOST',\n 'hub': 'FIREBASE_EMULATOR_HUB',\n 'storage': 'FIREBASE_STORAGE_EMULATOR_HOST'\n};\n\nfunction emulatorFromEnvVar(envVar: string): HostAndPort | undefined {\n const hostAndPort = process.env[envVar];\n if (!hostAndPort) {\n return undefined;\n }\n\n let parsed: URL;\n try {\n parsed = new URL(`http://${hostAndPort}`);\n } catch {\n throw new Error(\n `Invalid format in environment variable ${envVar}=${hostAndPort} (expected host:port)`\n );\n }\n let host = parsed.hostname;\n const port = Number(parsed.port || '80');\n if (!Number.isInteger(port)) {\n throw new Error(\n `Invalid port in environment variable ${envVar}=${hostAndPort}`\n );\n }\n return { host, port };\n}\n","/**\n * @license\n * Copyright 2021 Google LLC\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport fetch from 'node-fetch';\nimport firebase from 'firebase/compat/app';\nimport 'firebase/compat/firestore';\nimport 'firebase/compat/database';\nimport 'firebase/compat/storage';\n\nimport {\n HostAndPort,\n RulesTestContext,\n RulesTestEnvironment,\n TokenOptions\n} from '../public_types';\n\nimport { DiscoveredEmulators } from './discovery';\nimport { makeUrl } from './url';\n\n/**\n * An implementation of {@code RulesTestEnvironment}. This is private to hide the constructor,\n * which should never be directly called by the developer.\n * @private\n */\nexport class RulesTestEnvironmentImpl implements RulesTestEnvironment {\n private contexts = new Set<RulesTestContextImpl>();\n private destroyed = false;\n\n constructor(\n readonly projectId: string,\n readonly emulators: DiscoveredEmulators\n ) {}\n\n authenticatedContext(\n user_id: string,\n tokenOptions?: TokenOptions\n ): RulesTestContext {\n this.checkNotDestroyed();\n return this.createContext({\n ...tokenOptions,\n sub: user_id,\n user_id: user_id\n });\n }\n\n unauthenticatedContext(): RulesTestContext {\n this.checkNotDestroyed();\n return this.createContext(/* authToken = */ undefined);\n }\n\n async withSecurityRulesDisabled(\n callback: (context: RulesTestContext) => Promise<void>\n ): Promise<void> {\n this.checkNotDestroyed();\n // The \"owner\" token is recognized by the emulators as a special value that bypasses Security\n // Rules. This should only ever be used in withSecurityRulesDisabled.\n // If you're reading this and thinking about doing this in your own app / tests / scripts, think\n // twice. Instead, just use withSecurityRulesDisabled for unit testing OR connect your Firebase\n // Admin SDKs to the emulators for integration testing via environment variables.\n // See: https://firebase.google.com/docs/emulator-suite/connect_firestore#admin_sdks\n const context = this.createContext('owner');\n try {\n await callback(context);\n } finally {\n // We eagarly clean up this context to actively prevent misuse outside of the callback, e.g.\n // storing the context in a variable.\n context.cleanup();\n this.contexts.delete(context);\n }\n }\n\n private createContext(\n authToken: string | firebase.EmulatorMockTokenOptions | undefined\n ): RulesTestContextImpl {\n const context = new RulesTestContextImpl(\n this.projectId,\n this.emulators,\n authToken\n );\n this.contexts.add(context);\n return context;\n }\n\n clearDatabase(): Promise<void> {\n this.checkNotDestroyed();\n return this.withSecurityRulesDisabled(context => {\n return context.database().ref('/').set(null);\n });\n }\n\n async clearFirestore(): Promise<void> {\n this.checkNotDestroyed();\n assertEmulatorRunning(this.emulators, 'firestore');\n\n const resp = await fetch(\n makeUrl(\n this.emulators.firestore,\n `/emulator/v1/projects/${this.projectId}/databases/(default)/documents`\n ),\n {\n method: 'DELETE'\n }\n );\n\n if (!resp.ok) {\n throw new Error(await resp.text());\n }\n }\n\n clearStorage(): Promise<void> {\n this.checkNotDestroyed();\n return this.withSecurityRulesDisabled(async context => {\n const { items } = await context.storage().ref().listAll();\n await Promise.all(\n items.map(item => {\n return item.delete();\n })\n );\n });\n }\n\n async cleanup(): Promise<void> {\n this.destroyed = true;\n this.contexts.forEach(context => {\n context.envDestroyed = true;\n context.cleanup();\n });\n this.contexts.clear();\n }\n\n private checkNotDestroyed() {\n if (this.destroyed) {\n throw new Error(\n 'This RulesTestEnvironment has already been cleaned up. ' +\n '(This may indicate a leak or missing `await` in your test cases. If you do intend to ' +\n 'perform more tests, please call cleanup() later or create another RulesTestEnvironment.)'\n );\n }\n }\n}\n/**\n * An implementation of {@code RulesTestContext}. This is private to hide the constructor,\n * which should never be directly called by the developer.\n * @private\n */\nclass RulesTestContextImpl implements RulesTestContext {\n private app?: firebase.app.App;\n private destroyed = false;\n envDestroyed = false;\n\n constructor(\n readonly projectId: string,\n readonly emulators: DiscoveredEmulators,\n readonly authToken: firebase.EmulatorMockTokenOptions | string | undefined\n ) {}\n\n cleanup() {\n this.destroyed = true;\n this.app?.delete();\n\n this.app = undefined;\n }\n\n firestore(\n settings?: firebase.firestore.Settings\n ): firebase.firestore.Firestore {\n assertEmulatorRunning(this.emulators, 'firestore');\n const firestore = this.getApp().firestore();\n if (settings) {\n firestore.settings(settings);\n }\n firestore.useEmulator(\n this.emulators.firestore.host,\n this.emulators.firestore.port,\n { mockUserToken: this.authToken }\n );\n return firestore;\n }\n database(databaseURL?: string): firebase.database.Database {\n assertEmulatorRunning(this.emulators, 'database');\n if (!databaseURL) {\n const url = makeUrl(this.emulators.database, '');\n // Make sure to set the namespace equal to projectId -- otherwise the RTDB SDK will by default\n // use `${projectId}-default-rtdb`, which is treated as a different DB by the RTDB emulator\n // (and thus WON'T apply any rules set for the `projectId` DB during initialization).\n url.searchParams.append('ns', this.projectId);\n databaseURL = url.toString();\n }\n const database = this.getApp().database(databaseURL);\n database.useEmulator(\n this.emulators.database.host,\n this.emulators.database.port,\n { mockUserToken: this.authToken }\n );\n return database;\n }\n storage(bucketUrl = `gs://${this.projectId}`): firebase.storage.Storage {\n assertEmulatorRunning(this.emulators, 'storage');\n const storage = this.getApp().storage(bucketUrl);\n storage.useEmulator(\n this.emulators.storage.host,\n this.emulators.storage.port,\n { mockUserToken: this.authToken }\n );\n return storage;\n }\n\n private getApp(): firebase.app.App {\n if (this.envDestroyed) {\n throw new Error(\n 'This RulesTestContext is no longer valid because its RulesTestEnvironment has been ' +\n 'cleaned up. (This may indicate a leak or missing `await` in your test cases.)'\n );\n }\n if (this.destroyed) {\n throw new Error(\n 'This RulesTestContext is no longer valid. When using withSecurityRulesDisabled, ' +\n 'make sure to perform all operations on the context within the callback function and ' +\n 'return a Promise that resolves when the operations are done.'\n );\n }\n if (!this.app) {\n this.app = firebase.initializeApp(\n { projectId: this.projectId },\n `_Firebase_RulesUnitTesting_${Date.now()}_${Math.random()}`\n );\n }\n return this.app;\n }\n}\n\nexport function assertEmulatorRunning<E extends keyof DiscoveredEmulators>(\n emulators: DiscoveredEmulators,\n emulator: E\n): asserts emulators is Record<E, HostAndPort> {\n if (!emulators[emulator]) {\n if (emulators.hub) {\n throw new Error(\n `The ${emulator} emulator is not running (according to Emulator hub). To force ` +\n 'connecting anyway, please specify its host and port in initializeTestEnvironment({...}).'\n );\n } else {\n throw new Error(\n `The host and port of the ${emulator} emulator must be specified. (You may wrap the test ` +\n \"script with `firebase emulators:exec './your-test-script'` to enable automatic \" +\n `discovery, or specify manually via initializeTestEnvironment({${emulator}: {host, port}}).`\n );\n }\n }\n}\n","/**\n * @license\n * Copyright 2021 Google LLC\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { HostAndPort } from '../public_types';\nimport { makeUrl } from './url';\nimport fetch from 'node-fetch';\n\n/**\n * @private\n */\nexport async function loadDatabaseRules(\n hostAndPort: HostAndPort,\n databaseName: string,\n rules: string\n): Promise<void> {\n const url = makeUrl(hostAndPort, '/.settings/rules.json');\n url.searchParams.append('ns', databaseName);\n const resp = await fetch(url, {\n method: 'PUT',\n headers: { Authorization: 'Bearer owner' },\n body: rules\n });\n\n if (!resp.ok) {\n throw new Error(await resp.text());\n }\n}\n\n/**\n * @private\n */\nexport async function loadFirestoreRules(\n hostAndPort: HostAndPort,\n projectId: string,\n rules: string\n): Promise<void> {\n const resp = await fetch(\n makeUrl(hostAndPort, `/emulator/v1/projects/${projectId}:securityRules`),\n {\n method: 'PUT',\n body: JSON.stringify({\n rules: {\n files: [{ content: rules }]\n }\n })\n }\n );\n\n if (!resp.ok) {\n throw new Error(await resp.text());\n }\n}\n\n/**\n * @private\n */\nexport async function loadStorageRules(\n hostAndPort: HostAndPort,\n rules: string\n): Promise<void> {\n const resp = await fetch(makeUrl(hostAndPort, '/internal/setRules'), {\n method: 'PUT',\n headers: {\n 'Content-Type': 'application/json'\n },\n body: JSON.stringify({\n rules: {\n files: [{ name: 'storage.rules', content: rules }]\n }\n })\n });\n if (!resp.ok) {\n throw new Error(await resp.text());\n }\n}\n","/**\n * @license\n * Copyright 2021 Google LLC\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { RulesTestEnvironment, TestEnvironmentConfig } from './public_types';\nimport {\n DiscoveredEmulators,\n discoverEmulators,\n getEmulatorHostAndPort\n} from './impl/discovery';\nimport {\n assertEmulatorRunning,\n RulesTestEnvironmentImpl\n} from './impl/test_environment';\nimport {\n loadDatabaseRules,\n loadFirestoreRules,\n loadStorageRules\n} from './impl/rules';\n\n/**\n * Initializes a test environment for rules unit testing. Call this function first for test setup.\n *\n * Requires emulators to be running. This function tries to discover those emulators via environment\n * variables or through the Firebase Emulator hub if hosts and ports are unspecified. It is strongly\n * recommended to specify security rules for emulators used for testing. See minimal example below.\n *\n * @param config - the configuration for emulators. Most fields are optional if they can be discovered\n * @returns a promise that resolves with an environment ready for testing, or rejects on error.\n * @public\n * @example\n * ```javascript\n * const testEnv = await initializeTestEnvironment({\n * firestore: {\n * rules: fs.readFileSync(\"/path/to/firestore.rules\", \"utf8\"), // Load rules from file\n * // host and port can be omitted if they can be discovered from the hub.\n * },\n * // ...\n * });\n * ```\n */\nexport async function initializeTestEnvironment(\n config: TestEnvironmentConfig\n): Promise<RulesTestEnvironment> {\n const projectId = config.projectId || process.env.GCLOUD_PROJECT;\n if (!projectId) {\n throw new Error(\n 'Missing projectId option or env var GCLOUD_PROJECT! Please specify the projectId either ' +\n 'way.\\n(A demo-* projectId is strongly recommended for unit tests, such as \"demo-test\".)'\n );\n }\n const hub = getEmulatorHostAndPort('hub', config.hub);\n let discovered = hub ? { ...(await discoverEmulators(hub)), hub } : undefined;\n\n const emulators: DiscoveredEmulators = {};\n if (hub) {\n emulators.hub = hub;\n }\n\n for (const emulator of SUPPORTED_EMULATORS) {\n const hostAndPort = getEmulatorHostAndPort(\n emulator,\n config[emulator],\n discovered\n );\n if (hostAndPort) {\n emulators[emulator] = hostAndPort;\n }\n }\n\n if (config.database?.rules) {\n assertEmulatorRunning(emulators, 'database');\n await loadDatabaseRules(\n emulators.database,\n projectId,\n config.database.rules\n );\n }\n if (config.firestore?.rules) {\n assertEmulatorRunning(emulators, 'firestore');\n await loadFirestoreRules(\n emulators.firestore,\n projectId,\n config.firestore.rules\n );\n }\n if (config.storage?.rules) {\n assertEmulatorRunning(emulators, 'storage');\n await loadStorageRules(emulators.storage, config.storage.rules);\n }\n\n return new RulesTestEnvironmentImpl(projectId, emulators);\n}\n\nconst SUPPORTED_EMULATORS = ['database', 'firestore', 'storage'] as const;\n","/**\n * @license\n * Copyright 2021 Google LLC\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport {\n EMULATOR_HOST_ENV_VARS,\n getEmulatorHostAndPort\n} from './impl/discovery';\nimport { fixHostname, makeUrl } from './impl/url';\nimport { HostAndPort } from './public_types';\nimport fetch from 'node-fetch';\n\n/**\n * Run a setup function with background Cloud Functions triggers disabled. This can be used to\n * import data into the Realtime Database or Cloud Firestore emulator without triggering locally\n * emulated Cloud Functions.\n *\n * This method only works with Firebase CLI version 8.13.0 or higher. This overload works only if\n * the Emulator hub host:port is specified by the environment variable FIREBASE_EMULATOR_HUB.\n *\n * @param fn - a function which may be sync or async (returns a promise)\n * @public\n */\nexport async function withFunctionTriggersDisabled<TResult>(\n fn: () => TResult | Promise<TResult>\n): Promise<TResult>;\n\n/**\n * Run a setup function with background Cloud Functions triggers disabled. This can be used to\n * import data into the Realtime Database or Cloud Firestore emulator without triggering locally\n * emulated Cloud Functions.\n *\n * This method only works with Firebase CLI version 8.13.0 or higher. The Emulator hub must be\n * running, which host and port are specified in this overload.\n *\n * @param fn - a function which may be sync or async (returns a promise)\n * @param hub - the host and port of the Emulator Hub (ex: `{host: 'localhost', port: 4400}`)\n * @public\n */\nexport async function withFunctionTriggersDisabled<TResult>(\n hub: { host: string; port: number },\n fn: () => TResult | Promise<TResult>\n): Promise<TResult>;\n\nexport async function withFunctionTriggersDisabled<TResult>(\n fnOrHub: { host: string; port: number } | (() => TResult | Promise<TResult>),\n maybeFn?: () => TResult | Promise<TResult>\n): Promise<TResult> {\n let hub: HostAndPort | undefined;\n if (typeof fnOrHub === 'function') {\n maybeFn = fnOrHub;\n hub = getEmulatorHostAndPort('hub');\n } else {\n hub = getEmulatorHostAndPort('hub', fnOrHub);\n if (!maybeFn) {\n throw new Error('The callback function must be specified!');\n }\n }\n if (!hub) {\n throw new Error(\n 'Please specify the Emulator Hub host and port via arguments or set the environment ' +\n `varible ${EMULATOR_HOST_ENV_VARS.hub}!`\n );\n }\n\n hub.host = fixHostname(hub.host);\n makeUrl(hub, '/functions/disableBackgroundTriggers');\n // Disable background triggers\n const disableRes = await fetch(\n makeUrl(hub, '/functions/disableBackgroundTriggers'),\n {\n method: 'PUT'\n }\n );\n if (!disableRes.ok) {\n throw new Error(\n `HTTP Error ${disableRes.status} when disabling functions triggers, are you using firebase-tools 8.13.0 or higher?`\n );\n }\n\n // Run the user's function\n let result: TResult | undefined = undefined;\n try {\n result = await maybeFn();\n } finally {\n // Re-enable background triggers\n const enableRes = await fetch(\n makeUrl(hub, '/functions/enableBackgroundTriggers'),\n {\n method: 'PUT'\n }\n );\n\n if (!enableRes.ok) {\n throw new Error(\n `HTTP Error ${enableRes.status} when enabling functions triggers, are you using firebase-tools 8.13.0 or higher?`\n );\n }\n }\n\n // Return the user's function result\n return result;\n}\n\n/**\n * Assert the promise to be rejected with a \"permission denied\" error.\n *\n * Useful to assert a certain request to be denied by Security Rules. See example below.\n * This function recognizes permission-denied errors from Database, Firestore, and Storage JS SDKs.\n *\n * @param pr - the promise to be asserted\n * @returns a Promise that is fulfilled if pr is rejected with \"permission denied\". If pr is\n * rejected with any other error or resolved, the returned promise rejects.\n * @public\n * @example\n * ```javascript\n * const unauthed = testEnv.unauthenticatedContext();\n * await assertFails(get(doc(unauthed.firestore(), '/private/doc'), { ... });\n * ```\n */\nexport function assertFails(pr: Promise<any>): Promise<any> {\n return pr.then(\n () => {\n return Promise.reject(\n new Error('Expected request to fail, but it succeeded.')\n );\n },\n (err: any) => {\n const errCode = err?.code?.toLowerCase() || '';\n const errMessage = err?.message?.toLowerCase() || '';\n const isPermissionDenied =\n errCode === 'permission-denied' ||\n errCode === 'permission_denied' ||\n errMessage.indexOf('permission_denied') >= 0 ||\n errMessage.indexOf('permission denied') >= 0 ||\n // Storage permission errors contain message: (storage/unauthorized)\n errMessage.indexOf('unauthorized') >= 0;\n\n if (!isPermissionDenied) {\n return Promise.reject(\n new Error(\n `Expected PERMISSION_DENIED but got unexpected error: ${err}`\n )\n );\n }\n return err;\n }\n );\n}\n\n/**\n * Assert the promise to be rejected with a \"permission denied\" error.\n *\n * This is a no-op function returning the passed promise as-is, but can be used for documentational\n * purposes in test code to emphasize that a certain request should succeed (e.g. allowed by rules).\n *\n * @public\n * @example\n * ```javascript\n * const alice = testEnv.authenticatedContext('alice');\n * await assertSucceeds(get(doc(alice.firestore(), '/doc/readable/by/alice'), { ... });\n * ```\n */\nexport function assertSucceeds<T>(pr: Promise<T>): Promise<T> {\n return pr;\n}\n"],"names":["fetch","nodeFetch"],"mappings":";;;;;;AAAA;;;;;;;;;;;;;;;AAeG;AAIH;;;;;;;;AAQG;AACa,SAAA,WAAW,CAAC,IAAY,EAAE,YAAqB,EAAA;AAC7D,IAAA,IAAI,GAAG,IAAI,CAAC,OAAO,CAAC,GAAG,EAAE,EAAE,CAAC,CAAC,OAAO,CAAC,GAAG,EAAE,EAAE,CAAC,CAAC;IAC9C,IAAI,IAAI,KAAK,SAAS,EAAE;AACtB,QAAA,IAAI,GAAG,YAAY,IAAI,WAAW,CAAC;AACpC,KAAA;SAAM,IAAI,IAAI,KAAK,IAAI,EAAE;AACxB,QAAA,IAAI,GAAG,YAAY,IAAI,KAAK,CAAC;AAC9B,KAAA;AACD,IAAA,OAAO,IAAI,CAAC;AACd,CAAC;AAED;;;AAGG;AACa,SAAA,OAAO,CAAC,WAAiC,EAAE,IAAY,EAAA;AACrE,IAAA,IAAI,OAAO,WAAW,KAAK,QAAQ,EAAE;AACnC,QAAA,MAAM,EAAE,IAAI,EAAE,IAAI,EAAE,GAAG,WAAW,CAAC;AACnC,QAAA,IAAI,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,EAAE;AACtB,YAAA,WAAW,GAAG,CAAI,CAAA,EAAA,IAAI,CAAK,EAAA,EAAA,IAAI,EAAE,CAAC;AAC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AC;AACJ,CAAC;AAED;;;;;;;;;;;;AAYG;AACG,SAAU,cAAc,CAAI,EAAc,EAAA;AAC9C,IAAA,OAAO,EAAE,CAAC;AACZ;;;;"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"type":"module"}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2021 Google LLC
|
|
4
|
+
*
|
|
5
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
* you may not use this file except in compliance with the License.
|
|
7
|
+
* You may obtain a copy of the License at
|
|
8
|
+
*
|
|
9
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
*
|
|
11
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
* See the License for the specific language governing permissions and
|
|
15
|
+
* limitations under the License.
|
|
16
|
+
*/
|
|
17
|
+
import { EmulatorConfig, HostAndPort } from '../public_types';
|
|
18
|
+
import nodeFetch from 'node-fetch';
|
|
19
|
+
/**
|
|
20
|
+
* Use the Firebase Emulator hub to discover other running emulators.
|
|
21
|
+
*
|
|
22
|
+
* @param hub the host and port where the Emulator Hub is running
|
|
23
|
+
* @private
|
|
24
|
+
*/
|
|
25
|
+
export declare function discoverEmulators(hub: HostAndPort, fetch?: typeof nodeFetch): Promise<DiscoveredEmulators>;
|
|
26
|
+
/**
|
|
27
|
+
* @private
|
|
28
|
+
*/
|
|
29
|
+
export interface DiscoveredEmulators {
|
|
30
|
+
database?: HostAndPort;
|
|
31
|
+
firestore?: HostAndPort;
|
|
32
|
+
storage?: HostAndPort;
|
|
33
|
+
hub?: HostAndPort;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* @private
|
|
37
|
+
*/
|
|
38
|
+
export declare function getEmulatorHostAndPort(emulator: keyof DiscoveredEmulators, conf?: EmulatorConfig, discovered?: DiscoveredEmulators): {
|
|
39
|
+
host: string;
|
|
40
|
+
port: number;
|
|
41
|
+
} | undefined;
|
|
42
|
+
export declare const EMULATOR_HOST_ENV_VARS: {
|
|
43
|
+
database: string;
|
|
44
|
+
firestore: string;
|
|
45
|
+
hub: string;
|
|
46
|
+
storage: string;
|
|
47
|
+
};
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2021 Google LLC
|
|
4
|
+
*
|
|
5
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
* you may not use this file except in compliance with the License.
|
|
7
|
+
* You may obtain a copy of the License at
|
|
8
|
+
*
|
|
9
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
*
|
|
11
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
* See the License for the specific language governing permissions and
|
|
15
|
+
* limitations under the License.
|
|
16
|
+
*/
|
|
17
|
+
import { HostAndPort } from '../public_types';
|
|
18
|
+
/**
|
|
19
|
+
* @private
|
|
20
|
+
*/
|
|
21
|
+
export declare function loadDatabaseRules(hostAndPort: HostAndPort, databaseName: string, rules: string): Promise<void>;
|
|
22
|
+
/**
|
|
23
|
+
* @private
|
|
24
|
+
*/
|
|
25
|
+
export declare function loadFirestoreRules(hostAndPort: HostAndPort, projectId: string, rules: string): Promise<void>;
|
|
26
|
+
/**
|
|
27
|
+
* @private
|
|
28
|
+
*/
|
|
29
|
+
export declare function loadStorageRules(hostAndPort: HostAndPort, rules: string): Promise<void>;
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2021 Google LLC
|
|
4
|
+
*
|
|
5
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
* you may not use this file except in compliance with the License.
|
|
7
|
+
* You may obtain a copy of the License at
|
|
8
|
+
*
|
|
9
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
*
|
|
11
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
* See the License for the specific language governing permissions and
|
|
15
|
+
* limitations under the License.
|
|
16
|
+
*/
|
|
17
|
+
import 'firebase/compat/firestore';
|
|
18
|
+
import 'firebase/compat/database';
|
|
19
|
+
import 'firebase/compat/storage';
|
|
20
|
+
import { HostAndPort, RulesTestContext, RulesTestEnvironment, TokenOptions } from '../public_types';
|
|
21
|
+
import { DiscoveredEmulators } from './discovery';
|
|
22
|
+
/**
|
|
23
|
+
* An implementation of {@code RulesTestEnvironment}. This is private to hide the constructor,
|
|
24
|
+
* which should never be directly called by the developer.
|
|
25
|
+
* @private
|
|
26
|
+
*/
|
|
27
|
+
export declare class RulesTestEnvironmentImpl implements RulesTestEnvironment {
|
|
28
|
+
readonly projectId: string;
|
|
29
|
+
readonly emulators: DiscoveredEmulators;
|
|
30
|
+
private contexts;
|
|
31
|
+
private destroyed;
|
|
32
|
+
constructor(projectId: string, emulators: DiscoveredEmulators);
|
|
33
|
+
authenticatedContext(user_id: string, tokenOptions?: TokenOptions): RulesTestContext;
|
|
34
|
+
unauthenticatedContext(): RulesTestContext;
|
|
35
|
+
withSecurityRulesDisabled(callback: (context: RulesTestContext) => Promise<void>): Promise<void>;
|
|
36
|
+
private createContext;
|
|
37
|
+
clearDatabase(): Promise<void>;
|
|
38
|
+
clearFirestore(): Promise<void>;
|
|
39
|
+
clearStorage(): Promise<void>;
|
|
40
|
+
cleanup(): Promise<void>;
|
|
41
|
+
private checkNotDestroyed;
|
|
42
|
+
}
|
|
43
|
+
export declare function assertEmulatorRunning<E extends keyof DiscoveredEmulators>(emulators: DiscoveredEmulators, emulator: E): asserts emulators is Record<E, HostAndPort>;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2021 Google LLC
|
|
4
|
+
*
|
|
5
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
* you may not use this file except in compliance with the License.
|
|
7
|
+
* You may obtain a copy of the License at
|
|
8
|
+
*
|
|
9
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
*
|
|
11
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
* See the License for the specific language governing permissions and
|
|
15
|
+
* limitations under the License.
|
|
16
|
+
*/
|
|
17
|
+
import { HostAndPort } from '../public_types';
|
|
18
|
+
/**
|
|
19
|
+
* Return a connectable hostname, replacing wildcard 0.0.0.0 or :: with loopback
|
|
20
|
+
* addresses 127.0.0.1 / ::1 correspondingly. See below for why this is needed:
|
|
21
|
+
* https://github.com/firebase/firebase-tools-ui/issues/286
|
|
22
|
+
*
|
|
23
|
+
* This assumes emulators are running on the same device as fallbackHost (e.g.
|
|
24
|
+
* hub), which should hold if both are started from the same CLI command.
|
|
25
|
+
* @private
|
|
26
|
+
*/
|
|
27
|
+
export declare function fixHostname(host: string, fallbackHost?: string): string;
|
|
28
|
+
/**
|
|
29
|
+
* Create a URL with host, port, and path. Handles IPv6 bracketing correctly.
|
|
30
|
+
* @private
|
|
31
|
+
*/
|
|
32
|
+
export declare function makeUrl(hostAndPort: HostAndPort | string, path: string): URL;
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2021 Google LLC
|
|
4
|
+
*
|
|
5
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
* you may not use this file except in compliance with the License.
|
|
7
|
+
* You may obtain a copy of the License at
|
|
8
|
+
*
|
|
9
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
*
|
|
11
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
* See the License for the specific language governing permissions and
|
|
15
|
+
* limitations under the License.
|
|
16
|
+
*/
|
|
17
|
+
import { RulesTestEnvironment, TestEnvironmentConfig } from './public_types';
|
|
18
|
+
/**
|
|
19
|
+
* Initializes a test environment for rules unit testing. Call this function first for test setup.
|
|
20
|
+
*
|
|
21
|
+
* Requires emulators to be running. This function tries to discover those emulators via environment
|
|
22
|
+
* variables or through the Firebase Emulator hub if hosts and ports are unspecified. It is strongly
|
|
23
|
+
* recommended to specify security rules for emulators used for testing. See minimal example below.
|
|
24
|
+
*
|
|
25
|
+
* @param config - the configuration for emulators. Most fields are optional if they can be discovered
|
|
26
|
+
* @returns a promise that resolves with an environment ready for testing, or rejects on error.
|
|
27
|
+
* @public
|
|
28
|
+
* @example
|
|
29
|
+
* ```javascript
|
|
30
|
+
* const testEnv = await initializeTestEnvironment({
|
|
31
|
+
* firestore: {
|
|
32
|
+
* rules: fs.readFileSync("/path/to/firestore.rules", "utf8"), // Load rules from file
|
|
33
|
+
* // host and port can be omitted if they can be discovered from the hub.
|
|
34
|
+
* },
|
|
35
|
+
* // ...
|
|
36
|
+
* });
|
|
37
|
+
* ```
|
|
38
|
+
*/
|
|
39
|
+
export declare function initializeTestEnvironment(config: TestEnvironmentConfig): Promise<RulesTestEnvironment>;
|
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2021 Google LLC
|
|
4
|
+
*
|
|
5
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
* you may not use this file except in compliance with the License.
|
|
7
|
+
* You may obtain a copy of the License at
|
|
8
|
+
*
|
|
9
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
*
|
|
11
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
* See the License for the specific language governing permissions and
|
|
15
|
+
* limitations under the License.
|
|
16
|
+
*/
|
|
17
|
+
import { FirebaseSignInProvider } from '@firebase/util';
|
|
18
|
+
import firebase from 'firebase/compat/app';
|
|
19
|
+
import 'firebase/compat/database';
|
|
20
|
+
import 'firebase/compat/firestore';
|
|
21
|
+
import 'firebase/compat/storage';
|
|
22
|
+
/**
|
|
23
|
+
* More options for the mock user token to be used for testing, including developer-specfied custom
|
|
24
|
+
* claims or optional overrides for Firebase Auth token payloads.
|
|
25
|
+
* @public
|
|
26
|
+
*/
|
|
27
|
+
export declare type TokenOptions = {
|
|
28
|
+
/** The token issue time, in seconds since epoch */
|
|
29
|
+
iat?: number;
|
|
30
|
+
/** The token expiry time, normally 'iat' + 3600 */
|
|
31
|
+
exp?: number;
|
|
32
|
+
/** The time the user authenticated, normally 'iat' */
|
|
33
|
+
auth_time?: number;
|
|
34
|
+
/** The sign in provider, only set when the provider is 'anonymous' */
|
|
35
|
+
provider_id?: 'anonymous';
|
|
36
|
+
/** The user's primary email */
|
|
37
|
+
email?: string;
|
|
38
|
+
/** The user's email verification status */
|
|
39
|
+
email_verified?: boolean;
|
|
40
|
+
/** The user's primary phone number */
|
|
41
|
+
phone_number?: string;
|
|
42
|
+
/** The user's display name */
|
|
43
|
+
name?: string;
|
|
44
|
+
/** The user's profile photo URL */
|
|
45
|
+
picture?: string;
|
|
46
|
+
/** Information on all identities linked to this user */
|
|
47
|
+
firebase?: {
|
|
48
|
+
/** The primary sign-in provider */
|
|
49
|
+
sign_in_provider: FirebaseSignInProvider;
|
|
50
|
+
/** A map of providers to the user's list of unique identifiers from each provider */
|
|
51
|
+
identities?: {
|
|
52
|
+
[provider in FirebaseSignInProvider]?: string[];
|
|
53
|
+
};
|
|
54
|
+
};
|
|
55
|
+
/** Set to PROJECT_ID by default. In rare cases, you may want to specify an override. */
|
|
56
|
+
aud?: string;
|
|
57
|
+
/** Set to https://securetoken.google.com/PROJECT_ID by default. In rare cases, you may want to specify an override. */
|
|
58
|
+
iss?: string;
|
|
59
|
+
/** Custom claims set by the developer */
|
|
60
|
+
[claim: string]: unknown;
|
|
61
|
+
/** DO NOT USE. The user ID MUST be specified as the first param in authenticatedContext(uid, options) instead. */
|
|
62
|
+
uid?: never;
|
|
63
|
+
/** DO NOT USE. The user ID MUST be specified as the first param in authenticatedContext(uid, options) instead. */
|
|
64
|
+
sub?: never;
|
|
65
|
+
/** DO NOT USE. The user ID MUST be specified as the first param in authenticatedContext(uid, options) instead. */
|
|
66
|
+
user_id?: never;
|
|
67
|
+
};
|
|
68
|
+
/**
|
|
69
|
+
* Configuration of the unit testing environment, including emulators.
|
|
70
|
+
* @public
|
|
71
|
+
*/
|
|
72
|
+
export interface TestEnvironmentConfig {
|
|
73
|
+
/**
|
|
74
|
+
* The project ID of the test environment. Can also be specified via the environment variable GCLOUD_PROJECT.
|
|
75
|
+
*
|
|
76
|
+
* A "demo-*" project ID is strongly recommended, especially for unit testing.
|
|
77
|
+
* See: https://firebase.google.com/docs/emulator-suite/connect_firestore#choose_a_firebase_project
|
|
78
|
+
*/
|
|
79
|
+
projectId?: string;
|
|
80
|
+
/**
|
|
81
|
+
* The Firebase Emulator hub. Can also be specified via the environment variable FIREBASE_EMULATOR_HUB.
|
|
82
|
+
* If specified either way, other running emulators can be automatically discovered, and thus do
|
|
83
|
+
* not to be explicity specified.
|
|
84
|
+
*/
|
|
85
|
+
hub?: HostAndPort;
|
|
86
|
+
/**
|
|
87
|
+
* The Database emulator. Its host and port can also be discovered automatically through the hub
|
|
88
|
+
* (see field "hub") or specified via the environment variable FIREBASE_DATABASE_EMULATOR_HOST.
|
|
89
|
+
*/
|
|
90
|
+
database?: EmulatorConfig;
|
|
91
|
+
/**
|
|
92
|
+
* The Firestore emulator. Its host and port can also be discovered automatically through the hub
|
|
93
|
+
* (see field "hub") or specified via the environment variable FIRESTORE_EMULATOR_HOST.
|
|
94
|
+
*/
|
|
95
|
+
firestore?: EmulatorConfig;
|
|
96
|
+
/**
|
|
97
|
+
* The Storage emulator. Its host and port can also be discovered automatically through the hub
|
|
98
|
+
* (see field "hub") or specified via the environment variable FIREBASE_STORAGE_EMULATOR_HOST.
|
|
99
|
+
*/
|
|
100
|
+
storage?: EmulatorConfig;
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* An object containing the hostname and port number of an emulator.
|
|
104
|
+
* @public
|
|
105
|
+
*/
|
|
106
|
+
export interface HostAndPort {
|
|
107
|
+
/**
|
|
108
|
+
* The host of the emulator. Can be omitted if discovered automatically through the hub or
|
|
109
|
+
* specified via environment variables. See `TestEnvironmentConfig` for details.
|
|
110
|
+
*/
|
|
111
|
+
host: string;
|
|
112
|
+
/**
|
|
113
|
+
* The port of the emulator. Can be omitted if discovered automatically through the hub or
|
|
114
|
+
* specified via environment variables. See `TestEnvironmentConfig` for details.
|
|
115
|
+
*/
|
|
116
|
+
port: number;
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Configuration for a given emulator.
|
|
120
|
+
* @public
|
|
121
|
+
*/
|
|
122
|
+
export declare type EmulatorConfig = {
|
|
123
|
+
/** The security rules source code under test for this emulator. Strongly recommended. */
|
|
124
|
+
rules?: string;
|
|
125
|
+
} & (HostAndPort | {});
|
|
126
|
+
/**
|
|
127
|
+
* An object used to control the rules unit test environment. Can be used to create RulesTestContext
|
|
128
|
+
* for different authentication situations.
|
|
129
|
+
* @public
|
|
130
|
+
*/
|
|
131
|
+
export interface RulesTestEnvironment {
|
|
132
|
+
/** The project ID specified or discovered at test environment creation. */
|
|
133
|
+
readonly projectId: string;
|
|
134
|
+
/**
|
|
135
|
+
* A readonly copy of the emulator config specified or discovered at test environment creation.
|
|
136
|
+
*/
|
|
137
|
+
readonly emulators: {
|
|
138
|
+
database?: HostAndPort;
|
|
139
|
+
firestore?: HostAndPort;
|
|
140
|
+
storage?: HostAndPort;
|
|
141
|
+
};
|
|
142
|
+
/**
|
|
143
|
+
* Create a `RulesTestContext` which behaves like an authenticated Firebase Auth user.
|
|
144
|
+
*
|
|
145
|
+
* Requests created via the returned context will have a mock Firebase Auth token attached.
|
|
146
|
+
*
|
|
147
|
+
* @param user_id - the User ID of the user. Specifies the value of "user_id" and "sub" on the token
|
|
148
|
+
* @param tokenOptions - custom claims or overrides for Firebase Auth token payloads
|
|
149
|
+
*
|
|
150
|
+
* @example
|
|
151
|
+
* ```javascript
|
|
152
|
+
* const alice = testEnv.authenticatedContext('alice');
|
|
153
|
+
* await assertSucceeds(get(doc(alice.firestore(), '/doc/readable/by/alice'), { ... });
|
|
154
|
+
* ```
|
|
155
|
+
*/
|
|
156
|
+
authenticatedContext(user_id: string, tokenOptions?: TokenOptions): RulesTestContext;
|
|
157
|
+
/**
|
|
158
|
+
* Create a `RulesTestContext` which behaves like client that is NOT logged in via Firebase
|
|
159
|
+
* Auth.
|
|
160
|
+
*
|
|
161
|
+
* Requests created via the returned context will not have Firebase Auth tokens attached.
|
|
162
|
+
*
|
|
163
|
+
* @example
|
|
164
|
+
* ```javascript
|
|
165
|
+
* const unauthed = testEnv.unauthenticatedContext();
|
|
166
|
+
* await assertFails(get(doc(unauthed.firestore(), '/private/doc'), { ... });
|
|
167
|
+
* ```
|
|
168
|
+
*/
|
|
169
|
+
unauthenticatedContext(): RulesTestContext;
|
|
170
|
+
withSecurityRulesDisabled(callback: (context: RulesTestContext) => Promise<void>): Promise<void>;
|
|
171
|
+
/**
|
|
172
|
+
* Clear all data in the Realtime Database emulator namespace.
|
|
173
|
+
*/
|
|
174
|
+
clearDatabase(): Promise<void>;
|
|
175
|
+
/**
|
|
176
|
+
* Clear data in the Firestore that belongs to the `projectId` in the Firestore emulator.
|
|
177
|
+
*/
|
|
178
|
+
clearFirestore(): Promise<void>;
|
|
179
|
+
/**
|
|
180
|
+
* Clear Storage files and metadata in all buckets in the Storage emulator.
|
|
181
|
+
*/
|
|
182
|
+
clearStorage(): Promise<void>;
|
|
183
|
+
/**
|
|
184
|
+
* At the very end of your test code, call the cleanup function. Destroy all RulesTestContexts
|
|
185
|
+
* created in test environment and clean up the underlying resources, allowing a clean exit.
|
|
186
|
+
*
|
|
187
|
+
* This method does not change the state in emulators in any way. To reset data between tests,
|
|
188
|
+
* see `clearDatabase()`, `clearFirestore()` and `clearStorage()`.
|
|
189
|
+
*/
|
|
190
|
+
cleanup(): Promise<void>;
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
193
|
+
* A test context that represents a client. Can be used to access emulators for rules unit testing.
|
|
194
|
+
* @public
|
|
195
|
+
*/
|
|
196
|
+
export interface RulesTestContext {
|
|
197
|
+
/**
|
|
198
|
+
* Get a {@link @firebase/firestore#Firestore} instance for this test context. The returned Firebase JS Client SDK instance
|
|
199
|
+
* can be used with the client SDK APIs (v9 modular or v9 compat).
|
|
200
|
+
*
|
|
201
|
+
* See: {@link @firebase/firestore#Firestore}
|
|
202
|
+
* @param settings - a settings object to configure the {@link @firebase/firestore#Firestore} instance
|
|
203
|
+
* @returns a `Firestore` instance configured to connect to the emulator
|
|
204
|
+
* @public
|
|
205
|
+
*/
|
|
206
|
+
firestore(settings?: firebase.firestore.Settings): firebase.firestore.Firestore;
|
|
207
|
+
/**
|
|
208
|
+
* Get a {@link @firebase/database#Database} instance for this test context. The returned Firebase JS Client SDK instance
|
|
209
|
+
* can be used with the client SDK APIs (v9 modular or v9 compat).
|
|
210
|
+
*
|
|
211
|
+
* See: {@link @firebase/database#Database}
|
|
212
|
+
* @param databaseURL - the URL of the Realtime Database instance. If specified, returns an instance
|
|
213
|
+
* for an emulated version of the namespace with parameters extracted from URL
|
|
214
|
+
* @returns a `Database` instance configured to connect to the emulator. It never connects to
|
|
215
|
+
* production even if a production `databaseURL` is specified
|
|
216
|
+
*/
|
|
217
|
+
database(databaseURL?: string): firebase.database.Database;
|
|
218
|
+
/**
|
|
219
|
+
* Get a {@link @firebase/storage#FirebaseStorage} instance for this test context. The returned Firebase JS Client SDK instance
|
|
220
|
+
* can be used with the client SDK APIs (v9 modular or v9 compat).
|
|
221
|
+
*
|
|
222
|
+
* See: {@link @firebase/storage#FirebaseStorage}
|
|
223
|
+
* @param settings - the gs:// url to the Firebase Storage Bucket for testing. If specified,
|
|
224
|
+
* returns a `Storage` instance for an emulated version of the bucket name
|
|
225
|
+
* @returns a `Storage` instance configured to connect to the emulator
|
|
226
|
+
*/
|
|
227
|
+
storage(bucketUrl?: string): firebase.storage.Storage;
|
|
228
|
+
}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2021 Google LLC
|
|
4
|
+
*
|
|
5
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
* you may not use this file except in compliance with the License.
|
|
7
|
+
* You may obtain a copy of the License at
|
|
8
|
+
*
|
|
9
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
*
|
|
11
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
* See the License for the specific language governing permissions and
|
|
15
|
+
* limitations under the License.
|
|
16
|
+
*/
|
|
17
|
+
/**
|
|
18
|
+
* Run a setup function with background Cloud Functions triggers disabled. This can be used to
|
|
19
|
+
* import data into the Realtime Database or Cloud Firestore emulator without triggering locally
|
|
20
|
+
* emulated Cloud Functions.
|
|
21
|
+
*
|
|
22
|
+
* This method only works with Firebase CLI version 8.13.0 or higher. This overload works only if
|
|
23
|
+
* the Emulator hub host:port is specified by the environment variable FIREBASE_EMULATOR_HUB.
|
|
24
|
+
*
|
|
25
|
+
* @param fn - a function which may be sync or async (returns a promise)
|
|
26
|
+
* @public
|
|
27
|
+
*/
|
|
28
|
+
export declare function withFunctionTriggersDisabled<TResult>(fn: () => TResult | Promise<TResult>): Promise<TResult>;
|
|
29
|
+
/**
|
|
30
|
+
* Run a setup function with background Cloud Functions triggers disabled. This can be used to
|
|
31
|
+
* import data into the Realtime Database or Cloud Firestore emulator without triggering locally
|
|
32
|
+
* emulated Cloud Functions.
|
|
33
|
+
*
|
|
34
|
+
* This method only works with Firebase CLI version 8.13.0 or higher. The Emulator hub must be
|
|
35
|
+
* running, which host and port are specified in this overload.
|
|
36
|
+
*
|
|
37
|
+
* @param fn - a function which may be sync or async (returns a promise)
|
|
38
|
+
* @param hub - the host and port of the Emulator Hub (ex: `{host: 'localhost', port: 4400}`)
|
|
39
|
+
* @public
|
|
40
|
+
*/
|
|
41
|
+
export declare function withFunctionTriggersDisabled<TResult>(hub: {
|
|
42
|
+
host: string;
|
|
43
|
+
port: number;
|
|
44
|
+
}, fn: () => TResult | Promise<TResult>): Promise<TResult>;
|
|
45
|
+
/**
|
|
46
|
+
* Assert the promise to be rejected with a "permission denied" error.
|
|
47
|
+
*
|
|
48
|
+
* Useful to assert a certain request to be denied by Security Rules. See example below.
|
|
49
|
+
* This function recognizes permission-denied errors from Database, Firestore, and Storage JS SDKs.
|
|
50
|
+
*
|
|
51
|
+
* @param pr - the promise to be asserted
|
|
52
|
+
* @returns a Promise that is fulfilled if pr is rejected with "permission denied". If pr is
|
|
53
|
+
* rejected with any other error or resolved, the returned promise rejects.
|
|
54
|
+
* @public
|
|
55
|
+
* @example
|
|
56
|
+
* ```javascript
|
|
57
|
+
* const unauthed = testEnv.unauthenticatedContext();
|
|
58
|
+
* await assertFails(get(doc(unauthed.firestore(), '/private/doc'), { ... });
|
|
59
|
+
* ```
|
|
60
|
+
*/
|
|
61
|
+
export declare function assertFails(pr: Promise<any>): Promise<any>;
|
|
62
|
+
/**
|
|
63
|
+
* Assert the promise to be rejected with a "permission denied" error.
|
|
64
|
+
*
|
|
65
|
+
* This is a no-op function returning the passed promise as-is, but can be used for documentational
|
|
66
|
+
* purposes in test code to emphasize that a certain request should succeed (e.g. allowed by rules).
|
|
67
|
+
*
|
|
68
|
+
* @public
|
|
69
|
+
* @example
|
|
70
|
+
* ```javascript
|
|
71
|
+
* const alice = testEnv.authenticatedContext('alice');
|
|
72
|
+
* await assertSucceeds(get(doc(alice.firestore(), '/doc/readable/by/alice'), { ... });
|
|
73
|
+
* ```
|
|
74
|
+
*/
|
|
75
|
+
export declare function assertSucceeds<T>(pr: Promise<T>): Promise<T>;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2019 Google LLC
|
|
4
|
+
*
|
|
5
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
* you may not use this file except in compliance with the License.
|
|
7
|
+
* You may obtain a copy of the License at
|
|
8
|
+
*
|
|
9
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
*
|
|
11
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
* See the License for the specific language governing permissions and
|
|
15
|
+
* limitations under the License.
|
|
16
|
+
*/
|
|
17
|
+
export {};
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2021 Google LLC
|
|
4
|
+
*
|
|
5
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
* you may not use this file except in compliance with the License.
|
|
7
|
+
* You may obtain a copy of the License at
|
|
8
|
+
*
|
|
9
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
*
|
|
11
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
* See the License for the specific language governing permissions and
|
|
15
|
+
* limitations under the License.
|
|
16
|
+
*/
|
|
17
|
+
export {};
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2021 Google LLC
|
|
4
|
+
*
|
|
5
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
* you may not use this file except in compliance with the License.
|
|
7
|
+
* You may obtain a copy of the License at
|
|
8
|
+
*
|
|
9
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
*
|
|
11
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
* See the License for the specific language governing permissions and
|
|
15
|
+
* limitations under the License.
|
|
16
|
+
*/
|
|
17
|
+
export {};
|