|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2024 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 | +import { deleteUser, getAuth, signInAnonymously } from 'firebase/auth'; |
| 19 | +import { deleteApp, initializeApp } from 'firebase/app'; |
| 20 | +import { firebaseConfig } from 'lib/firebase'; |
| 21 | +import { deleteObject, getDownloadURL, getStorage, ref, uploadString } from 'firebase/storage'; |
| 22 | +import { OK, FAILED, waitForUserSignIn } from 'lib/util'; |
| 23 | + |
| 24 | +export type TestResults = { |
| 25 | + initializeAppResult: string, |
| 26 | + initializeAuthResult: string, |
| 27 | + authUserSignedInResult: string, |
| 28 | + initializeStorageResult: string, |
| 29 | + createStorageRefResult: string, |
| 30 | + uploadStringResult: string, |
| 31 | + getDownloadUrlResult: string, |
| 32 | + fetchCompletedResult: string, |
| 33 | + getResponseTextResult: string, |
| 34 | + getDataMatchesExepctedResult: string, |
| 35 | + deleteReferenceResult: string, |
| 36 | + deleteUserResult: string, |
| 37 | + authSignedOutResult, |
| 38 | + deleteAppResult: string |
| 39 | +}; |
| 40 | + |
| 41 | +export function initializeTestResults(): TestResults { |
| 42 | + return { |
| 43 | + initializeAppResult: FAILED, |
| 44 | + initializeAuthResult: FAILED, |
| 45 | + authUserSignedInResult: FAILED, |
| 46 | + initializeStorageResult: FAILED, |
| 47 | + createStorageRefResult: FAILED, |
| 48 | + uploadStringResult: FAILED, |
| 49 | + getDownloadUrlResult: FAILED, |
| 50 | + fetchCompletedResult: FAILED, |
| 51 | + getResponseTextResult: FAILED, |
| 52 | + getDataMatchesExepctedResult: FAILED, |
| 53 | + deleteReferenceResult: FAILED, |
| 54 | + deleteUserResult: FAILED, |
| 55 | + authSignedOutResult: FAILED, |
| 56 | + deleteAppResult: FAILED |
| 57 | + }; |
| 58 | +} |
| 59 | + |
| 60 | +export async function testStorage(isServer: boolean = false): Promise<TestResults> { |
| 61 | + const result: TestResults = initializeTestResults(); |
| 62 | + if (isServer) { |
| 63 | + console.log("server app"); |
| 64 | + } |
| 65 | + |
| 66 | + try { |
| 67 | + const firebaseApp = initializeApp(firebaseConfig); |
| 68 | + if (firebaseApp === null) { |
| 69 | + return result; |
| 70 | + } |
| 71 | + result.initializeAppResult = OK; |
| 72 | + |
| 73 | + const auth = getAuth(firebaseApp); |
| 74 | + await auth.authStateReady(); |
| 75 | + result.initializeAuthResult = OK; |
| 76 | + |
| 77 | + await signInAnonymously(auth); |
| 78 | + await waitForUserSignIn(auth); |
| 79 | + if (auth.currentUser) { |
| 80 | + result.authUserSignedInResult = OK; |
| 81 | + |
| 82 | + const storage = getStorage(firebaseApp); |
| 83 | + if (storage) { |
| 84 | + result.initializeStorageResult = OK; |
| 85 | + const reference = ref(storage, '/next-js-test.txt'); |
| 86 | + if (reference) { |
| 87 | + const storageText = 'next-js-test-string'; |
| 88 | + result.createStorageRefResult = OK; |
| 89 | + await uploadString(reference, storageText); |
| 90 | + result.uploadStringResult = OK; |
| 91 | + const url = await getDownloadURL(reference); |
| 92 | + if (url) { |
| 93 | + result.getDownloadUrlResult = OK; |
| 94 | + const response = await fetch(url); |
| 95 | + result.fetchCompletedResult = OK; |
| 96 | + const data = await response.text(); |
| 97 | + result.getResponseTextResult = OK; |
| 98 | + if (data === storageText) { |
| 99 | + result.getDataMatchesExepctedResult = OK; |
| 100 | + } |
| 101 | + } |
| 102 | + await deleteObject(reference); |
| 103 | + result.deleteReferenceResult = OK; |
| 104 | + } |
| 105 | + } |
| 106 | + await deleteUser(auth.currentUser); |
| 107 | + result.deleteUserResult = OK; |
| 108 | + |
| 109 | + await auth.signOut(); |
| 110 | + result.authSignedOutResult = OK; |
| 111 | + } |
| 112 | + deleteApp(firebaseApp); |
| 113 | + result.deleteAppResult = OK; |
| 114 | + } catch (e) { |
| 115 | + console.error("Caught error: ", e); |
| 116 | + } |
| 117 | + return result; |
| 118 | +} |
0 commit comments