|
| 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 | +}); |
0 commit comments