forked from firebase/firebase-admin-node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.ts
More file actions
139 lines (120 loc) · 3.98 KB
/
setup.ts
File metadata and controls
139 lines (120 loc) · 3.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/*!
* Copyright 2018 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import * as admin from '../../lib/index';
import fs = require('fs');
import minimist = require('minimist');
import path = require('path');
import {random} from 'lodash';
import { Credential, CertCredential, GoogleOAuthAccessToken, Certificate } from '../../src/auth/credential';
/* tslint:disable:no-var-requires */
const chalk = require('chalk');
/* tslint:enable:no-var-requires */
export let databaseUrl: string;
export let storageBucket: string;
export let projectId: string;
export let apiKey: string;
export let defaultApp: admin.app.App;
export let nullApp: admin.app.App;
export let nonNullApp: admin.app.App;
export let noServiceAccountApp: admin.app.App;
export let cmdArgs: any;
before(() => {
/* tslint:disable:no-console */
let serviceAccount: any;
try {
serviceAccount = require('../resources/key.json');
} catch (error) {
console.log(chalk.red(
'The integration test suite requires a service account JSON file for a ' +
'Firebase project to be saved to `test/resources/key.json`.',
error,
));
throw error;
}
try {
apiKey = fs.readFileSync(path.join(__dirname, '../resources/apikey.txt')).toString().trim();
} catch (error) {
console.log(chalk.red(
'The integration test suite requires an API key for a ' +
'Firebase project to be saved to `test/resources/apikey.txt`.',
error,
));
throw error;
}
/* tslint:enable:no-console */
projectId = serviceAccount.project_id;
databaseUrl = 'https://' + projectId + '.firebaseio.com';
storageBucket = projectId + '.appspot.com';
defaultApp = admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: databaseUrl,
storageBucket,
});
nullApp = admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: databaseUrl,
databaseAuthVariableOverride: null,
storageBucket,
}, 'null');
nonNullApp = admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: databaseUrl,
databaseAuthVariableOverride: {
uid: generateRandomString(20),
},
storageBucket,
}, 'nonNull');
noServiceAccountApp = admin.initializeApp({
credential: new CertificatelessCredential(admin.credential.cert(serviceAccount)),
serviceAccountId: serviceAccount.client_email,
projectId,
}, 'noServiceAccount');
cmdArgs = minimist(process.argv.slice(2));
});
after(() => {
return Promise.all([
defaultApp.delete(),
nullApp.delete(),
nonNullApp.delete(),
noServiceAccountApp.delete(),
]);
});
class CertificatelessCredential implements Credential {
private readonly delegate: admin.credential.Credential;
constructor(delegate: admin.credential.Credential) {
this.delegate = delegate;
}
public getAccessToken(): Promise<GoogleOAuthAccessToken> {
return this.delegate.getAccessToken();
}
public getCertificate(): Certificate {
return null;
}
}
/**
* Generate a random string of the specified length, optionally using the specified alphabet.
*
* @param {number} length The length of the string to generate.
* @return {string} A random string of the provided length.
*/
export function generateRandomString(length: number): string {
const alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
let text = '';
for (let i = 0; i < length; i++) {
text += alphabet.charAt(random(alphabet.length - 1));
}
return text;
}