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