Skip to content

Commit 0da66a3

Browse files
committed
[WIP] onUpdate, onDelete & onCreate tests in .ts
1 parent e481a4b commit 0da66a3

8 files changed

Lines changed: 181 additions & 114 deletions

File tree

functions/.runtimeconfig.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"env": {
3+
"testingConfig": {
4+
"projectId": "react-firebase-admin-eeac2",
5+
"serviceAccountKey": "../serviceAccountKey.json",
6+
"storageBucket": "react-firebase-admin-eeac2.appspot.com",
7+
"databaseURL": "https://react-firebase-admin-eeac2.firebaseio.com"
8+
}
9+
},
10+
"signinlink": {
11+
"url": "https://react-firebase-admin-eeac2.firebaseapp.com/login"
12+
}
13+
}

functions/env.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"testingConfig": {
3+
"databaseURL": "https://react-firebase-admin-eeac2.firebaseio.com",
4+
"storageBucket": "react-firebase-admin-eeac2.appspot.com",
5+
"projectId": "react-firebase-admin-eeac2",
6+
"serviceAccountKey": "../serviceAccountKey.json"
7+
}
8+
}

functions/package.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"deploy": "firebase deploy --only functions",
1010
"logs": "firebase functions:log",
1111
"setup-firebase": "node setupProject.js",
12-
"test": "mocha --reporter spec --timeout 10000"
12+
"test": "mocha -r ts-node/register test/**/*.test.ts --timeout 10000"
1313
},
1414
"engines": {
1515
"node": "10"
@@ -26,7 +26,13 @@
2626
"camelcase": "^5.3.1"
2727
},
2828
"devDependencies": {
29+
"@types/chai": "^4.2.11",
30+
"@types/chai-as-promised": "^7.1.2",
31+
"@types/mocha": "^7.0.2",
32+
"chai": "^4.2.0",
33+
"chai-as-promised": "^7.1.1",
2934
"mocha": "^7.2.0",
35+
"ts-node": "^8.10.2",
3036
"tslint": "^5.12.0",
3137
"typescript": "^3.8.0"
3238
},

functions/test/createUser.test.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { test } from './util/admin';
2+
import { https } from 'firebase-functions';
3+
import * as chai from 'chai';
4+
import 'mocha';
5+
6+
describe('onCreateUser', () => {
7+
let onCreateUser: any;
8+
9+
before(async () => {
10+
onCreateUser = require('../src/https/createUser.function');
11+
});
12+
13+
it('should throw an error because the email is not provided', () => {
14+
const wrapped = test.wrap(onCreateUser);
15+
16+
const data = {
17+
email: '',
18+
isAdmin: false
19+
};
20+
21+
return wrapped(data).then(() => {
22+
chai.expect(https.HttpsError, 'auth/invalid-email');
23+
});
24+
});
25+
26+
it('should create the user into the database', () => {
27+
const wrapped = test.wrap(onCreateUser);
28+
29+
const data = {
30+
uid: '1234',
31+
email: 'user@example.com',
32+
isAdmin: false
33+
};
34+
35+
return wrapped(data).then((res: any) => {
36+
chai.assert.equal(data.uid, res.uid);
37+
});
38+
});
39+
});

functions/test/index.test.js

Lines changed: 0 additions & 113 deletions
This file was deleted.

functions/test/onDelete.test.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { admin, test } from './util/admin';
2+
import * as chai from 'chai';
3+
import * as chaiAsPromised from 'chai-as-promised';
4+
import 'mocha';
5+
6+
chai.use(chaiAsPromised);
7+
8+
describe('onDelete', () => {
9+
let onDelete: any;
10+
let userRecord: any;
11+
12+
before(async () => {
13+
const user = {
14+
uid: '1234',
15+
email: 'user@example.com',
16+
password: 'secretPassword'
17+
};
18+
onDelete = require('../src/db/users/onDelete.function');
19+
userRecord = await admin.auth().createUser(user);
20+
});
21+
22+
it('should delete the user from the authentication section', () => {
23+
const wrapped = test.wrap(onDelete);
24+
25+
return wrapped(
26+
{},
27+
{
28+
params: {
29+
uid: userRecord.uid
30+
}
31+
}
32+
).then(async () => {
33+
await chai
34+
.expect(admin.auth().getUser(userRecord.uid))
35+
.to.be.rejectedWith(
36+
Error,
37+
'There is no user record corresponding to the provided identifier.'
38+
);
39+
});
40+
});
41+
});

functions/test/onUpdate.test.ts

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import { admin, test } from './util/admin';
2+
import * as chai from 'chai';
3+
import 'mocha';
4+
5+
describe('onUpdate', () => {
6+
let onUpdate: any;
7+
let userRecord: any;
8+
9+
before(async () => {
10+
onUpdate = require('../src/db/users/onUpdate.function');
11+
12+
const user = {
13+
uid: '1234',
14+
email: 'user@example.com',
15+
password: 'secretPassword'
16+
};
17+
const customClaims = {
18+
isAdmin: false
19+
};
20+
21+
userRecord = await admin.auth().createUser(user);
22+
await admin.auth().setCustomUserClaims(userRecord.uid, customClaims);
23+
});
24+
25+
after(async () => {
26+
await admin.auth().deleteUser(userRecord.uid);
27+
});
28+
29+
it('should update the user information in the database', () => {
30+
const wrapped = test.wrap(onUpdate);
31+
32+
const beforeSnap = test.database.makeDataSnapshot(
33+
{ email: 'user@example.com', isAdmin: false },
34+
`users/${userRecord.uid}`
35+
);
36+
const afterSnap = test.database.makeDataSnapshot(
37+
{ email: 'user@example.com', isAdmin: true },
38+
`users/${userRecord.uid}`
39+
);
40+
41+
const change = test.makeChange(beforeSnap, afterSnap);
42+
43+
return wrapped(change, {
44+
params: {
45+
uid: userRecord.uid
46+
}
47+
}).then(() => {
48+
return admin
49+
.auth()
50+
.getUser(userRecord.uid)
51+
.then(snap => {
52+
chai.assert.isTrue(snap.customClaims!.isAdmin);
53+
});
54+
});
55+
});
56+
});

functions/test/util/admin.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import * as functions from 'firebase-functions';
2+
import * as testConfig from 'firebase-functions-test';
3+
import * as admin from 'firebase-admin';
4+
5+
const config = functions.config().env.testingConfig;
6+
7+
const projectConfig = {
8+
databaseURL: config.databaseURL,
9+
storageBucket: config.storageBucket,
10+
projectId: config.projectId
11+
};
12+
13+
const test = testConfig(projectConfig, config.serviceAccountKey);
14+
15+
admin.initializeApp();
16+
17+
export { admin, test };

0 commit comments

Comments
 (0)