-
Notifications
You must be signed in to change notification settings - Fork 940
Storage Emulator support in rules-unit-testing #4863
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
65abed5
Storage Emulator support in rules-unit-testing
samtstern 1284c94
Minor fixes
samtstern 010ae6f
Update deps and API
samtstern 95d1ade
Fix tests
samtstern 061102b
Prettier
samtstern 739cbca
Fix database tests
samtstern 1a387ea
Fix host parsing
samtstern a4048c7
Fix typo
samtstern 6e9bb22
Skip storage test in CI
samtstern fe58758
Delete yarn.lock
yuchenshi 9a885a5
Revert yarn.lock changes.
yuchenshi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@firebase/rules-unit-testing': minor | ||
--- | ||
|
||
Add support for Storage emulator to rules-unit-testing |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,9 @@ | |
"functions": { | ||
"port": 9004 | ||
}, | ||
"storage": { | ||
"port": 9199 | ||
}, | ||
"ui": { | ||
"enabled": false | ||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,11 +31,16 @@ const DATABASE_ADDRESS_ENV: string = 'FIREBASE_DATABASE_EMULATOR_HOST'; | |
/** The default address for the local database emulator. */ | ||
const DATABASE_ADDRESS_DEFAULT: string = 'localhost:9000'; | ||
|
||
/** If any of environment variable is set, use it for the Firestore emulator. */ | ||
/** If this environment variable is set, use it for the Firestore emulator. */ | ||
const FIRESTORE_ADDRESS_ENV: string = 'FIRESTORE_EMULATOR_HOST'; | ||
/** The default address for the local Firestore emulator. */ | ||
const FIRESTORE_ADDRESS_DEFAULT: string = 'localhost:8080'; | ||
|
||
/** If this environment variable is set, use it for the Storage emulator. */ | ||
const STORAGE_ADDRESS_ENV: string = 'STORAGE_EMULATOR_HOST'; | ||
/** The default address for the local Firestore emulator. */ | ||
const STORAGE_ADDRESS_DEFAULT: string = 'localhost:9199'; | ||
|
||
/** Environment variable to locate the Emulator Hub */ | ||
const HUB_HOST_ENV: string = 'FIREBASE_EMULATOR_HUB'; | ||
/** The default address for the Emulator Hub */ | ||
|
@@ -47,6 +52,9 @@ let _databaseHost: string | undefined = undefined; | |
/** The actual address for the Firestore emulator */ | ||
let _firestoreHost: string | undefined = undefined; | ||
|
||
/** The actual address for the Storage emulator */ | ||
let _storageHost: string | undefined = undefined; | ||
|
||
/** The actual address for the Emulator Hub */ | ||
let _hubHost: string | undefined = undefined; | ||
|
||
|
@@ -133,6 +141,10 @@ export type FirebaseEmulatorOptions = { | |
host: string; | ||
port: number; | ||
}; | ||
storage?: { | ||
host: string; | ||
port: number; | ||
}; | ||
hub?: { | ||
host: string; | ||
port: number; | ||
|
@@ -193,6 +205,7 @@ export function apps(): firebase.app.App[] { | |
export type AppOptions = { | ||
databaseName?: string; | ||
projectId?: string; | ||
storageBucket?: string; | ||
auth?: TokenOptions; | ||
}; | ||
/** Construct an App authenticated with options.auth. */ | ||
|
@@ -201,19 +214,20 @@ export function initializeTestApp(options: AppOptions): firebase.app.App { | |
? createUnsecuredJwt(options.auth, options.projectId) | ||
: undefined; | ||
|
||
return initializeApp(jwt, options.databaseName, options.projectId); | ||
return initializeApp(jwt, options.databaseName, options.projectId, options.storageBucket); | ||
} | ||
|
||
export type AdminAppOptions = { | ||
databaseName?: string; | ||
projectId?: string; | ||
storageBucket?: string; | ||
}; | ||
/** Construct an App authenticated as an admin user. */ | ||
export function initializeAdminApp(options: AdminAppOptions): firebase.app.App { | ||
const admin = require('firebase-admin'); | ||
|
||
const app = admin.initializeApp( | ||
getAppOptions(options.databaseName, options.projectId), | ||
getAppOptions(options.databaseName, options.projectId, options.storageBucket), | ||
getRandomAppName() | ||
); | ||
|
||
|
@@ -248,6 +262,10 @@ export function useEmulators(options: FirebaseEmulatorOptions): void { | |
_firestoreHost = getAddress(options.firestore.host, options.firestore.port); | ||
} | ||
|
||
if (options.storage) { | ||
_storageHost = getAddress(options.storage.host, options.storage.port); | ||
} | ||
|
||
if (options.hub) { | ||
_hubHost = getAddress(options.hub.host, options.hub.port); | ||
} | ||
|
@@ -301,6 +319,13 @@ export async function discoverEmulators( | |
}; | ||
} | ||
|
||
if (data.storage) { | ||
options.storage = { | ||
host: data.storage.host, | ||
port: data.storage.port | ||
}; | ||
} | ||
|
||
if (data.hub) { | ||
options.hub = { | ||
host: data.hub.host, | ||
|
@@ -351,6 +376,22 @@ function getFirestoreHost() { | |
return _firestoreHost; | ||
} | ||
|
||
function getStorageHost() { | ||
if (!_storageHost) { | ||
const fromEnv = process.env[STORAGE_ADDRESS_ENV]; | ||
if (fromEnv) { | ||
_storageHost = fromEnv; | ||
} else { | ||
console.warn( | ||
`Warning: ${STORAGE_ADDRESS_ENV} not set, using default value ${STORAGE_ADDRESS_DEFAULT}` | ||
); | ||
_storageHost = STORAGE_ADDRESS_DEFAULT; | ||
} | ||
} | ||
|
||
return _storageHost; | ||
} | ||
|
||
function getHubHost() { | ||
if (!_hubHost) { | ||
const fromEnv = process.env[HUB_HOST_ENV]; | ||
|
@@ -367,34 +408,53 @@ function getHubHost() { | |
return _hubHost; | ||
} | ||
|
||
function parseHost(host: string): { hostname: string, port: number } { | ||
const u = new URL(host); | ||
return { | ||
hostname: u.hostname, | ||
port: Number.parseInt(u.port, 10) | ||
} | ||
} | ||
|
||
function getRandomAppName(): string { | ||
return 'app-' + new Date().getTime() + '-' + Math.random(); | ||
} | ||
|
||
function getDatabaseUrl(databaseName: string) { | ||
return `http://${getDatabaseHost()}?ns=${databaseName}`; | ||
} | ||
|
||
function getAppOptions( | ||
databaseName?: string, | ||
projectId?: string | ||
projectId?: string, | ||
storageBucket?: string, | ||
): { [key: string]: string } { | ||
let appOptions: { [key: string]: string } = {}; | ||
|
||
if (databaseName) { | ||
appOptions[ | ||
'databaseURL' | ||
] = `http://${getDatabaseHost()}?ns=${databaseName}`; | ||
] = getDatabaseUrl(databaseName); | ||
} | ||
|
||
if (projectId) { | ||
appOptions['projectId'] = projectId; | ||
} | ||
|
||
if (storageBucket) { | ||
appOptions['storgeBucket'] = storageBucket; | ||
} | ||
|
||
return appOptions; | ||
} | ||
|
||
function initializeApp( | ||
accessToken?: string, | ||
databaseName?: string, | ||
projectId?: string | ||
projectId?: string, | ||
storageBucket?: string, | ||
): firebase.app.App { | ||
const appOptions = getAppOptions(databaseName, projectId); | ||
const appOptions = getAppOptions(databaseName, projectId, storageBucket); | ||
const app = firebase.initializeApp(appOptions, getRandomAppName()); | ||
if (accessToken) { | ||
const mockAuthComponent = new Component( | ||
|
@@ -417,17 +477,22 @@ function initializeApp( | |
); | ||
} | ||
if (databaseName) { | ||
const { hostname, port } = parseHost(getDatabaseHost()); | ||
app.database().useEmulator(hostname, port); | ||
|
||
// Toggle network connectivity to force a reauthentication attempt. | ||
// This mitigates a minor race condition where the client can send the | ||
// first database request before authenticating. | ||
app.database().goOffline(); | ||
app.database().goOnline(); | ||
} | ||
if (projectId) { | ||
app.firestore().settings({ | ||
host: getFirestoreHost(), | ||
ssl: false | ||
}); | ||
const { hostname, port } = parseHost(getFirestoreHost()); | ||
app.firestore().useEmulator(hostname, port); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Moving to consistently use |
||
} | ||
if (storageBucket) { | ||
const { hostname, port } = parseHost(getStorageHost()); | ||
app.storage().useEmulator(hostname, port); | ||
} | ||
/** | ||
Mute warnings for the previously-created database and whatever other | ||
|
@@ -498,6 +563,39 @@ export async function loadFirestoreRules( | |
} | ||
} | ||
|
||
export type LoadStorageRulesOptions = { | ||
storageBucket: string; | ||
rules: string; | ||
}; | ||
export async function loadStorageRules( | ||
options: LoadStorageRulesOptions | ||
): Promise<void> { | ||
if (!options.storageBucket) { | ||
throw new Error('storageBucket not specified'); | ||
} | ||
|
||
if (!options.rules) { | ||
throw new Error('must provide rules to loadStorageRules'); | ||
} | ||
|
||
// TODO: This endpoint is not yet implemented! Will need a change in firebase-tools | ||
// to make this real. | ||
const resp = await requestPromise(request.put, { | ||
method: 'PUT', | ||
uri: `http://${getStorageHost()}/internal/setRules`, | ||
body: JSON.stringify({ | ||
storageBucket: options.storageBucket, | ||
rules: { | ||
files: [{ content: options.rules }] | ||
} | ||
}) | ||
}); | ||
|
||
if (resp.statusCode !== 200) { | ||
throw new Error(JSON.parse(resp.body.error)); | ||
} | ||
} | ||
|
||
export type ClearFirestoreDataOptions = { | ||
projectId: string; | ||
}; | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.