/
index.ts
167 lines (161 loc) · 5.52 KB
/
index.ts
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import * as functions from 'firebase-functions';
import * as https from 'https';
import * as admin from 'firebase-admin';
import { Request, Response } from 'express';
import * as fs from 'fs'
import * as PubSub from '@google-cloud/pubsub';
const pubsub = PubSub();
export * from './pubsub-tests';
export * from './database-tests';
export * from './auth-tests';
export * from './firestore-tests';
export * from './https-tests';
export * from './remoteConfig-tests';
// export * from './storage-tests';
const numTests = Object.keys(exports).length; // Assumption: every exported function is its own test.
import 'firebase-functions'; // temporary shim until process.env.FIREBASE_CONFIG available natively in GCF(BUG 63586213)
const firebaseConfig = JSON.parse(process.env.FIREBASE_CONFIG);
admin.initializeApp();
admin.firestore().settings({ timestampsInSnapshots: true });
// TODO(klimt): Get rid of this once the JS client SDK supports callable triggers.
function callHttpsTrigger(name: string, data: any) {
return new Promise((resolve, reject) => {
const request = https.request(
{
method: 'POST',
host: 'us-central1-' + firebaseConfig.projectId + '.cloudfunctions.net',
path: '/' + name,
headers: {
'Content-Type': 'application/json',
},
},
response => {
let body = '';
response.on('data', chunk => {
body += chunk;
});
response.on('end', () => resolve(body));
}
);
request.on('error', reject);
request.write(JSON.stringify({ data }));
request.end();
});
}
export const integrationTests: any = functions
.runWith({
timeoutSeconds: 540,
})
.https.onRequest((req: Request, resp: Response) => {
let pubsub: any = require('@google-cloud/pubsub')();
const testId = admin
.database()
.ref()
.push().key;
console.log('testId is: ', testId);
fs.writeFile('/tmp/' + testId + '.txt', 'test', ()=> {});
return Promise.all([
// A database write to trigger the Firebase Realtime Database tests.
admin
.database()
.ref(`dbTests/${testId}/start`)
.set({ '.sv': 'timestamp' }),
// A Pub/Sub publish to trigger the Cloud Pub/Sub tests.
pubsub
.topic('pubsubTests')
.publisher()
.publish(Buffer.from(JSON.stringify({ testId }))),
// A user creation to trigger the Firebase Auth user creation tests.
admin
.auth()
.createUser({
email: `${testId}@fake.com`,
password: 'secret',
displayName: `${testId}`,
})
.then(userRecord => {
// A user deletion to trigger the Firebase Auth user deletion tests.
admin.auth().deleteUser(userRecord.uid);
}),
// A firestore write to trigger the Cloud Firestore tests.
admin
.firestore()
.collection('tests')
.doc(testId)
.set({ test: testId }),
// A Remote Config update to trigger the Remote Config tests.
admin.credential
.applicationDefault()
.getAccessToken()
.then((accessToken) => {
const options = {
hostname: 'firebaseremoteconfig.googleapis.com',
path: `/v1/projects/${firebaseConfig.projectId}/remoteConfig`,
method: 'PUT',
headers: {
Authorization: 'Bearer ' + accessToken.access_token,
'Content-Type': 'application/json; UTF-8',
'Accept-Encoding': 'gzip',
'If-Match': '*',
},
};
const request = https.request(options, resp => {});
request.write(JSON.stringify({ version: { description: testId } }));
request.end();
}),
// A storage upload to trigger the Storage tests
admin.storage().bucket().upload('/tmp/' + testId + '.txt'),
// Invoke a callable HTTPS trigger.
callHttpsTrigger('callableTests', { foo: 'bar', testId }),
])
.then(() => {
// On test completion, check that all tests pass and reply "PASS", or provide further details.
console.log('Waiting for all tests to report they pass...');
let ref = admin.database().ref(`testRuns/${testId}`);
return new Promise((resolve, reject) => {
let testsExecuted = 0;
ref.on('child_added', snapshot => {
testsExecuted += 1;
if (!snapshot.val().passed) {
reject(
new Error(
`test ${snapshot.key} failed; see database for details.`
)
);
return;
}
console.log(
`${snapshot.key} passed (${testsExecuted} of ${numTests})`
);
if (testsExecuted < numTests) {
// Not all tests have completed. Wait longer.
return;
}
// All tests have passed!
resolve();
});
})
.then(() => {
ref.off(); // No more need to listen.
return Promise.resolve();
})
.catch(err => {
ref.off(); // No more need to listen.
return Promise.reject(err);
});
})
.then(() => {
console.log('All tests pass!');
resp.status(200).send('PASS \n');
})
.catch(err => {
console.log(`Some tests failed: ${err}`);
resp
.status(500)
.send(
`FAIL - details at https://${
process.env.GCLOUD_PROJECT
}.firebaseio.com/testRuns/${testId}`
);
});
});