|
1 | | -import { test } from './util/admin'; |
| 1 | +import { admin, test } from './util/admin'; |
2 | 2 | import { https } from 'firebase-functions'; |
3 | 3 | import * as chai from 'chai'; |
| 4 | +import * as createUser from '../src/https/createUser.function'; |
4 | 5 | import 'mocha'; |
5 | 6 |
|
6 | | -describe('onCreateUser', () => { |
7 | | - let onCreateUser: any; |
| 7 | +describe('createUser', () => { |
| 8 | + let userRecord: any; |
8 | 9 |
|
9 | | - before(async () => { |
10 | | - onCreateUser = require('../src/https/createUser.function'); |
| 10 | + after(async () => { |
| 11 | + await admin.auth().deleteUser(userRecord.uid); |
11 | 12 | }); |
12 | 13 |
|
13 | 14 | it('should throw an error because the email is not provided', () => { |
14 | | - const wrapped = test.wrap(onCreateUser); |
| 15 | + const wrapped = test.wrap(createUser.default); |
15 | 16 |
|
16 | 17 | const data = { |
17 | 18 | email: '', |
18 | 19 | isAdmin: false |
19 | 20 | }; |
20 | 21 |
|
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 | + ); |
24 | 28 | }); |
25 | 29 |
|
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); |
28 | 32 |
|
29 | 33 | const data = { |
30 | | - uid: '1234', |
31 | 34 | email: 'user@example.com', |
32 | 35 | isAdmin: false |
33 | 36 | }; |
34 | 37 |
|
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); |
37 | 43 | }); |
38 | 44 | }); |
39 | 45 | }); |
0 commit comments