forked from firebase/firebase-admin-node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.ts
More file actions
99 lines (90 loc) · 3.38 KB
/
utils.ts
File metadata and controls
99 lines (90 loc) · 3.38 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
/*!
* Copyright 2017 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 _ from 'lodash';
import * as sinon from 'sinon';
import * as mocks from '../resources/mocks';
import {FirebaseNamespace} from '../../src/firebase-namespace';
import {FirebaseApp, FirebaseAppOptions, FirebaseAppInternals, FirebaseAccessToken} from '../../src/firebase-app';
import { HttpError, HttpResponse } from '../../src/utils/api-request';
/**
* Returns a new FirebaseApp instance with the provided options.
*
* @param {object} options The options for the FirebaseApp instance to create.
* @return {FirebaseApp} A new FirebaseApp instance with the provided options.
*/
export function createAppWithOptions(options: object) {
const mockFirebaseNamespaceInternals = new FirebaseNamespace().INTERNAL;
return new FirebaseApp(options as FirebaseAppOptions, mocks.appName, mockFirebaseNamespaceInternals);
}
/** @return {string} A randomly generated access token string. */
export function generateRandomAccessToken(): string {
return 'access_token_' + _.random(999999999);
}
/**
* Creates a stub for retrieving an access token from a FirebaseApp. All services should use this
* method for stubbing the OAuth2 flow during unit tests.
*
* @param {string} accessToken The access token string to return.
* @param {FirebaseApp} app The app instance to stub. If not specified, the stub will affect all apps.
* @return {sinon.SinonStub} A Sinon stub.
*/
export function stubGetAccessToken(accessToken?: string, app?: FirebaseApp): sinon.SinonStub {
if (typeof accessToken === 'undefined') {
accessToken = generateRandomAccessToken();
}
const result: FirebaseAccessToken = {
accessToken,
expirationTime: Date.now() + 3600,
};
if (app) {
return sinon.stub(app.INTERNAL, 'getToken').resolves(result);
} else {
return sinon.stub(FirebaseAppInternals.prototype, 'getToken').resolves(result);
}
}
/**
* Creates a mock HTTP response from the given data and parameters.
*
* @param {object | string} data Data to be included in the response body.
* @param {number=} status HTTP status code (defaults to 200).
* @param {*=} headers HTTP headers to be included in the ersponse.
* @return {HttpResponse} An HTTP response object.
*/
export function responseFrom(data: object | string, status: number = 200, headers: any = {}): HttpResponse {
let responseData: any;
let responseText: string;
if (typeof data === 'object') {
responseData = data;
responseText = JSON.stringify(data);
} else {
try {
responseData = JSON.parse(data);
} catch (error) {
responseData = null;
}
responseText = data as string;
}
return {
status,
headers,
data: responseData,
text: responseText,
isJson: () => responseData != null,
};
}
export function errorFrom(data: any, status: number = 500): HttpError {
return new HttpError(responseFrom(data, status));
}