Skip to content

Commit 220275f

Browse files
committed
Fixed tests on cloud functions on createUser and on function import
1 parent 0da66a3 commit 220275f

3 files changed

Lines changed: 25 additions & 21 deletions

File tree

functions/test/createUser.test.ts

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,45 @@
1-
import { test } from './util/admin';
1+
import { admin, test } from './util/admin';
22
import { https } from 'firebase-functions';
33
import * as chai from 'chai';
4+
import * as createUser from '../src/https/createUser.function';
45
import 'mocha';
56

6-
describe('onCreateUser', () => {
7-
let onCreateUser: any;
7+
describe('createUser', () => {
8+
let userRecord: any;
89

9-
before(async () => {
10-
onCreateUser = require('../src/https/createUser.function');
10+
after(async () => {
11+
await admin.auth().deleteUser(userRecord.uid);
1112
});
1213

1314
it('should throw an error because the email is not provided', () => {
14-
const wrapped = test.wrap(onCreateUser);
15+
const wrapped = test.wrap(createUser.default);
1516

1617
const data = {
1718
email: '',
1819
isAdmin: false
1920
};
2021

21-
return wrapped(data).then(() => {
22-
chai.expect(https.HttpsError, 'auth/invalid-email');
23-
});
22+
return chai
23+
.expect(wrapped(data))
24+
.to.be.rejectedWith(
25+
https.HttpsError,
26+
'auth/invalid-email'
27+
);
2428
});
2529

26-
it('should create the user into the database', () => {
27-
const wrapped = test.wrap(onCreateUser);
30+
it('should create the user in auth with correct email and custom claims', () => {
31+
const wrapped = test.wrap(createUser.default);
2832

2933
const data = {
30-
uid: '1234',
3134
email: 'user@example.com',
3235
isAdmin: false
3336
};
3437

35-
return wrapped(data).then((res: any) => {
36-
chai.assert.equal(data.uid, res.uid);
38+
return wrapped(data).then(async (res: any) => {
39+
userRecord = await admin.auth().getUser(res.uid);
40+
41+
chai.assert.equal(data.email, userRecord.email);
42+
chai.assert.equal(data.isAdmin, userRecord.customClaims!.isAdmin);
3743
});
3844
});
3945
});

functions/test/onDelete.test.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { admin, test } from './util/admin';
22
import * as chai from 'chai';
33
import * as chaiAsPromised from 'chai-as-promised';
4+
import * as onDelete from '../src/db/users/onDelete.function';
45
import 'mocha';
56

67
chai.use(chaiAsPromised);
78

89
describe('onDelete', () => {
9-
let onDelete: any;
1010
let userRecord: any;
1111

1212
before(async () => {
@@ -15,12 +15,11 @@ describe('onDelete', () => {
1515
email: 'user@example.com',
1616
password: 'secretPassword'
1717
};
18-
onDelete = require('../src/db/users/onDelete.function');
1918
userRecord = await admin.auth().createUser(user);
2019
});
2120

2221
it('should delete the user from the authentication section', () => {
23-
const wrapped = test.wrap(onDelete);
22+
const wrapped = test.wrap(onDelete.default);
2423

2524
return wrapped(
2625
{},

functions/test/onUpdate.test.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
import { admin, test } from './util/admin';
22
import * as chai from 'chai';
3+
import * as onUpdate from '../src/db/users/onUpdate.function';
34
import 'mocha';
45

56
describe('onUpdate', () => {
6-
let onUpdate: any;
77
let userRecord: any;
88

99
before(async () => {
10-
onUpdate = require('../src/db/users/onUpdate.function');
11-
1210
const user = {
1311
uid: '1234',
1412
email: 'user@example.com',
@@ -24,10 +22,11 @@ describe('onUpdate', () => {
2422

2523
after(async () => {
2624
await admin.auth().deleteUser(userRecord.uid);
25+
test.cleanup();
2726
});
2827

2928
it('should update the user information in the database', () => {
30-
const wrapped = test.wrap(onUpdate);
29+
const wrapped = test.wrap(onUpdate.default);
3130

3231
const beforeSnap = test.database.makeDataSnapshot(
3332
{ email: 'user@example.com', isAdmin: false },

0 commit comments

Comments
 (0)