|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2020 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 | +// eslint-disable-next-line import/no-extraneous-dependencies |
| 19 | +import { initializeApp } from '@firebase/app-exp'; |
| 20 | +import { signInAnonymously, getAuth } from '@firebase/auth-exp'; |
| 21 | + |
| 22 | +// See https://github.com/typescript-eslint/typescript-eslint/issues/363 |
| 23 | +// eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 24 | +// import * as storage from '@firebase/storage-types'; |
| 25 | + |
| 26 | +// import { expect } from 'chai'; |
| 27 | +import { ref, uploadBytes, getStorage } from '../../exp/index'; |
| 28 | +import { StorageService } from '../../src/service'; |
| 29 | + |
| 30 | +// eslint-disable-next-line @typescript-eslint/no-require-imports |
| 31 | +const PROJECT_CONFIG = require('../../../../config/project.json'); |
| 32 | + |
| 33 | +export const PROJECT_ID = PROJECT_CONFIG.projectId; |
| 34 | +export const STORAGE_BUCKET = PROJECT_CONFIG.storageBucket; |
| 35 | +export const API_KEY = PROJECT_CONFIG.apiKey; |
| 36 | + |
| 37 | +let appCount = 0; |
| 38 | + |
| 39 | +export async function withTestInstance(): Promise<StorageService> { |
| 40 | + const app = initializeApp( |
| 41 | + { apiKey: API_KEY, projectId: PROJECT_ID, storageBucket: STORAGE_BUCKET }, |
| 42 | + 'test-app-' + appCount++ |
| 43 | + ); |
| 44 | + console.log('signing in'); |
| 45 | + try { |
| 46 | + await signInAnonymously(getAuth(app)); |
| 47 | + } catch (e) { |
| 48 | + console.log(e); |
| 49 | + } |
| 50 | + // await signInAnonymously(getAuth(app)); |
| 51 | + console.log('signed in'); |
| 52 | + return getStorage(app); |
| 53 | +} |
| 54 | + |
| 55 | +describe.only('FirebaseStorage', () => { |
| 56 | + it('can upload bytes', async () => { |
| 57 | + const storage = await withTestInstance(); |
| 58 | + const reference = ref(storage, 'public/bytes'); |
| 59 | + console.log(reference.fullPath); |
| 60 | + await uploadBytes(reference, new Uint8Array([0, 1, 3])); |
| 61 | + console.log('done'); |
| 62 | + }); |
| 63 | + |
| 64 | + // it('can upload string', () => { |
| 65 | + // return withTestInstance(async storage => { |
| 66 | + // const ref = storage.ref('public/string'); |
| 67 | + // await ref.putString('foo'); |
| 68 | + // }); |
| 69 | + // }); |
| 70 | + |
| 71 | + // it('validates operations on root', () => { |
| 72 | + // return withTestInstance(async storage => { |
| 73 | + // const ref = storage.ref(''); |
| 74 | + // try { |
| 75 | + // // eslint-disable-next-line @typescript-eslint/no-floating-promises |
| 76 | + // ref.putString('foo'); |
| 77 | + // expect.fail(); |
| 78 | + // } catch (e) { |
| 79 | + // expect(e.message).to.satisfy((v: string) => |
| 80 | + // v.match( |
| 81 | + // /The operation 'putString' cannot be performed on a root reference/ |
| 82 | + // ) |
| 83 | + // ); |
| 84 | + // } |
| 85 | + // }); |
| 86 | + // }); |
| 87 | + |
| 88 | + // it('can delete object ', () => { |
| 89 | + // return withTestInstance(async storage => { |
| 90 | + // const ref = storage.ref('public/delete'); |
| 91 | + // await ref.putString('foo'); |
| 92 | + |
| 93 | + // // getDownloadURL() succeeds for an existing object |
| 94 | + // await ref.getDownloadURL(); |
| 95 | + |
| 96 | + // await ref.delete(); |
| 97 | + // try { |
| 98 | + // // getDownloadURL() fails for a deleted object |
| 99 | + // await ref.getDownloadURL(); |
| 100 | + // expect.fail(); |
| 101 | + // } catch (e) { |
| 102 | + // expect(e.message).to.satisfy((v: string) => |
| 103 | + // v.match(/Object 'public\/delete' does not exist/) |
| 104 | + // ); |
| 105 | + // } |
| 106 | + // }); |
| 107 | + // }); |
| 108 | + |
| 109 | + // it('can get download URL', () => { |
| 110 | + // return withTestInstance(async storage => { |
| 111 | + // const ref = storage.ref('public/downloadurl'); |
| 112 | + // await ref.put(new Uint8Array([0, 1, 3])); |
| 113 | + // const url = await ref.getDownloadURL(); |
| 114 | + // expect(url).to.satisfy((v: string) => |
| 115 | + // v.match( |
| 116 | + // /https:\/\/firebasestorage\.googleapis\.com\/v0\/b\/.*\/o\/public%2Fdownloadurl/ |
| 117 | + // ) |
| 118 | + // ); |
| 119 | + // }); |
| 120 | + // }); |
| 121 | + |
| 122 | + // it('can get metadata', () => { |
| 123 | + // return withTestInstance(async storage => { |
| 124 | + // const ref = storage.ref('public/getmetadata'); |
| 125 | + // await ref.put(new Uint8Array([0, 1, 3])); |
| 126 | + // const metadata = await ref.getMetadata(); |
| 127 | + // expect(metadata.name).to.equal('getmetadata'); |
| 128 | + // }); |
| 129 | + // }); |
| 130 | + |
| 131 | + // it('can update metadata', () => { |
| 132 | + // return withTestInstance(async storage => { |
| 133 | + // const ref = storage.ref('public/updatemetadata'); |
| 134 | + // await ref.put(new Uint8Array([0, 1, 3])); |
| 135 | + // const metadata = await ref.updateMetadata({ |
| 136 | + // customMetadata: { foo: 'bar' } |
| 137 | + // }); |
| 138 | + // expect(metadata.customMetadata).to.deep.equal({ foo: 'bar' }); |
| 139 | + // }); |
| 140 | + // }); |
| 141 | + |
| 142 | + // it('can list files', () => { |
| 143 | + // return withTestInstance(async storage => { |
| 144 | + // await storage.ref('public/list/a').putString(''); |
| 145 | + // await storage.ref('public/list/b').putString(''); |
| 146 | + // await storage.ref('public/list/c/d').putString(''); |
| 147 | + // const listResult = await storage.ref('public/list').listAll(); |
| 148 | + // expect(listResult.items.map(v => v.name)).to.have.members(['a', 'b']); |
| 149 | + // expect(listResult.prefixes.map(v => v.name)).to.have.members(['c']); |
| 150 | + // }); |
| 151 | + // }); |
| 152 | +}); |
0 commit comments