-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added firebase storage testing/mock components
- refactored testing components to add storage testing components - added testing components for storage
- Loading branch information
Showing
53 changed files
with
514 additions
and
138 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -1,14 +1,14 @@ | ||
import { describeQueryDriverTests, describeAccessorDriverTests } from '@dereekb/firebase/test'; | ||
import { describeFirestoreQueryDriverTests, describeFirestoreAccessorDriverTests } from '@dereekb/firebase/test'; | ||
import { adminTestWithMockItemCollection } from '@dereekb/firebase-server/test'; | ||
|
||
jest.setTimeout(9000); | ||
|
||
describe('firestore server', () => { | ||
adminTestWithMockItemCollection((f) => { | ||
describeAccessorDriverTests(f); | ||
describeFirestoreAccessorDriverTests(f); | ||
}); | ||
|
||
adminTestWithMockItemCollection((f) => { | ||
describeQueryDriverTests(f); | ||
describeFirestoreQueryDriverTests(f); | ||
}); | ||
}); |
This file contains 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 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 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 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
33 changes: 28 additions & 5 deletions
33
packages/firebase-server/src/lib/storage/driver.accessor.ts
This file contains 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 |
---|---|---|
@@ -1,13 +1,36 @@ | ||
import { FirebaseStorage, FirebaseStorageAccessorDriver, FirebaseStorageAccessorDriverGetDownloadUrlFunction, GoogleCloudStorageFilePath, StorageFilePath } from '@dereekb/firebase'; | ||
import { SlashPathFolder } from '@dereekb/util'; | ||
import { Storage as GoogleCloudStorage } from '@google-cloud/storage'; | ||
import { FirebaseStorageAccessorDriver, FirebaseStorageAccessorFile, FirebaseStorageAccessorFolder, FirebaseStorage, StoragePath } from '@dereekb/firebase'; | ||
import { Storage as GoogleCloudStorage, File as GoogleCloudFile } from '@google-cloud/storage'; | ||
|
||
export function googleCloudStorageFileForStorageFilePath(storage: GoogleCloudStorage, path: StorageFilePath) { | ||
export function googleCloudStorageFileForStorageFilePath(storage: GoogleCloudStorage, path: StoragePath) { | ||
return storage.bucket(path.bucketId).file(path.pathString); | ||
} | ||
|
||
export interface GoogleCloudStorageAccessorFile extends FirebaseStorageAccessorFile<GoogleCloudFile> {} | ||
|
||
export function googleCloudStorageAccessorFile(storage: GoogleCloudStorage, storagePath: StoragePath): GoogleCloudStorageAccessorFile { | ||
const file = googleCloudStorageFileForStorageFilePath(storage, storagePath); | ||
|
||
return { | ||
reference: file, | ||
storagePath, | ||
getDownloadUrl: async () => file.publicUrl() | ||
}; | ||
} | ||
|
||
export interface GoogleCloudStorageAccessorFolder extends FirebaseStorageAccessorFolder<GoogleCloudFile> {} | ||
|
||
export function googleCloudStorageAccessorFolder(storage: GoogleCloudStorage, storagePath: StoragePath): GoogleCloudStorageAccessorFolder { | ||
const file = googleCloudStorageFileForStorageFilePath(storage, storagePath); | ||
|
||
return { | ||
reference: file, | ||
storagePath | ||
}; | ||
} | ||
|
||
export function googleCloudStorageFirebaseStorageAccessorDriver(): FirebaseStorageAccessorDriver { | ||
return { | ||
getDownloadUrl: async (storage: FirebaseStorage, path: StorageFilePath) => googleCloudStorageFileForStorageFilePath(storage as GoogleCloudStorage, path).publicUrl() | ||
file: (storage: FirebaseStorage, path: StoragePath) => googleCloudStorageAccessorFile(storage as GoogleCloudStorage, path), | ||
folder: (storage: FirebaseStorage, path: StoragePath) => googleCloudStorageAccessorFolder(storage as GoogleCloudStorage, path) | ||
}; | ||
} |
This file contains 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 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 |
---|---|---|
@@ -1,7 +1,23 @@ | ||
import { FirebaseStorageContextFactory, firebaseStorageContextFactory, FirestoreContextFactory, firestoreContextFactory } from '@dereekb/firebase'; | ||
import { googleCloudFirebaseStorageDrivers } from './driver'; | ||
import { Storage as FirebaseAdminStorage } from 'firebase-admin/lib/storage/storage'; | ||
import { Storage as GoogleCloudStorage } from '@google-cloud/storage'; | ||
|
||
/** | ||
* Creates a FirestoreContextFactory that uses the @google-cloud/storage package. | ||
*/ | ||
export const googleCloudFirebaseStorageContextFactory: FirebaseStorageContextFactory = firebaseStorageContextFactory(googleCloudFirebaseStorageDrivers()); | ||
|
||
interface FirebaseAdminStorageRefLike { | ||
readonly storageClient: GoogleCloudStorage; | ||
} | ||
|
||
/** | ||
* Retrieves the GoogleCloudStorage object from the input FirebaseAdmin Storage type. | ||
* | ||
* @param storage | ||
* @returns | ||
*/ | ||
export function googleCloudStorageFromFirebaseAdminStorage(storage: FirebaseAdminStorage): GoogleCloudStorage { | ||
return (storage as unknown as FirebaseAdminStorageRefLike).storageClient; | ||
} |
This file contains 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 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 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
4 changes: 2 additions & 2 deletions
4
packages/firebase-server/test/src/lib/firestore/firestore.fixture.admin.ts
This file contains 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 |
---|---|---|
@@ -1,9 +1,9 @@ | ||
import { testWithMockItemFixture } from '@dereekb/firebase/test'; | ||
import { testWithMockItemCollectionFixture } from '@dereekb/firebase/test'; | ||
import { adminFirestoreFactory } from './firestore.admin'; | ||
|
||
/** | ||
* Convenience mock instance for tests within an authorized context. | ||
* | ||
* Uses @google-cloud/firestore | ||
*/ | ||
export const adminTestWithMockItemCollection = testWithMockItemFixture()(adminFirestoreFactory); | ||
export const adminTestWithMockItemCollection = testWithMockItemCollectionFixture()(adminFirestoreFactory); |
This file contains 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 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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from './firebase'; | ||
export * from './firestore'; | ||
export * from './storage'; |
This file contains 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 @@ | ||
export * from './storage'; |
This file contains 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,66 @@ | ||
import { Storage as FirebaseAdminStorage } from 'firebase-admin/lib/storage/storage'; | ||
import { Storage as GoogleCloudStorage } from '@google-cloud/storage'; | ||
import { TestFirebaseStorageContext, TestFirebaseStorageInstance, TestFirebaseStorageContextFixture, TestingFirebaseStorageDrivers, makeTestingFirebaseStorageDrivers } from '@dereekb/firebase/test'; | ||
import { jestTestContextBuilder } from '@dereekb/util/test'; | ||
import { googleCloudFirebaseStorageDrivers } from '@dereekb/firebase-server'; | ||
import { FirebaseStorage, firebaseStorageContextFactory } from '@dereekb/firebase'; | ||
|
||
export interface GoogleCloudTestFirebaseStorageConfig { | ||
host: string; | ||
port: number; | ||
} | ||
|
||
export type GoogleCloudTestFirebaseStorageContext = TestFirebaseStorageContext; | ||
|
||
export function makeGoogleFirebaseStorageContext(drivers: TestingFirebaseStorageDrivers, firebaseStorage: FirebaseStorage): TestFirebaseStorageContext { | ||
const context = firebaseStorageContextFactory(drivers)(firebaseStorage) as GoogleCloudTestFirebaseStorageContext; | ||
context.drivers = drivers; | ||
return context; | ||
} | ||
|
||
export class GoogleCloudTestFirebaseStorageInstance extends TestFirebaseStorageInstance { | ||
constructor(drivers: TestingFirebaseStorageDrivers, firebaseStorage: FirebaseStorage) { | ||
super(makeGoogleFirebaseStorageContext(drivers, firebaseStorage)); | ||
} | ||
} | ||
|
||
export class GoogleCloudTestFirebaseStorageContextFixture extends TestFirebaseStorageContextFixture<GoogleCloudTestFirebaseStorageInstance> {} | ||
|
||
let COUNTER = 0; | ||
|
||
/** | ||
* A JestTestContextBuilderFunction for building firebase storage test context factories using @google-cloud/storage. This means SERVER TESTING ONLY. For client testing, look at @dereekb/firestore. | ||
* | ||
* This is used to build a @google-cloud/storage FirebaseStorage instance for testing and point it to the emulators. | ||
* | ||
* If you need all of Firebase (firebase-admin library), look at adminFirebaseAdminTestBuilder() instead. | ||
*/ | ||
export const googleCloudTestFirebaseStorageBuilder = jestTestContextBuilder<GoogleCloudTestFirebaseStorageInstance, GoogleCloudTestFirebaseStorageContextFixture, GoogleCloudTestFirebaseStorageConfig>({ | ||
buildConfig: (input?: Partial<GoogleCloudTestFirebaseStorageConfig>) => { | ||
const config: GoogleCloudTestFirebaseStorageConfig = { | ||
host: input?.host ?? 'localhost', | ||
port: input?.port ?? 0 | ||
}; | ||
|
||
if (!config.port) { | ||
throw new Error('Port for host is required.'); | ||
} | ||
|
||
return config; | ||
}, | ||
buildFixture: () => new GoogleCloudTestFirebaseStorageContextFixture(), | ||
setupInstance: async (config) => { | ||
const drivers = makeTestingFirebaseStorageDrivers(googleCloudFirebaseStorageDrivers()); | ||
|
||
const projectId = `firebase-storage-server-test-${new Date().getTime()}-${COUNTER++}`; | ||
const firebaseStorage = new GoogleCloudStorage({ | ||
projectId, | ||
apiEndpoint: `${config.host}:${config.port}` | ||
}); | ||
|
||
return new GoogleCloudTestFirebaseStorageInstance(drivers, firebaseStorage); | ||
}, | ||
teardownInstance: async (instance, config) => { | ||
// nothing to teardown | ||
} | ||
}); |
Oops, something went wrong.