Firebase函数身份验证用户onCreate uni测试

时间:2020-01-05 03:49:24

标签: javascript google-cloud-firestore firebase-authentication google-cloud-functions sinon

如何实现对以下通话的存根?

我设法将用户对象存根

admin.firestore().collection('roles').where('role', '==', 'unassigned');

功能

exports.userRegistration = functions.auth.user()
.onCreate((user, context) => {    
    let unassignedRole;
    let roleRef = admin.firestore().collection('roles').where('role', '==', 'unassigned');
    await roleRef.get()
    .then(snapshot => {
        snapshot.forEach(doc => {
            unassignedRole = doc.ref;
        });
    })
    .catch(err => {
        console.log('Error getting documents', err);
    });

    await admin.firestore().collection('users').add({
        firstName: user.firstName || "",
        lastName: user.lastName || "",
        fullName: user.displayName || "",
        profilePic: user.photoURL || "",
        email: user.email,
        dateRegistered: admin.firestore.FieldValue.serverTimestamp(),
        role: unassignedRole
    });
});

单元测试

describe('Cloud Functions', () => {
    let myFunctions, adminInitStub;

    before(() => {
        adminInitStub = sinon.stub(admin, 'initializeApp');
        myFunctions = require('../index');
    });

    after(() => {
        adminInitStub.restore();
        test.cleanup();
    });

    describe('userRegistration', () => {
        it('should write the newly registered user to /users', () => {
            const mockUser = test.auth.makeUserRecord({
                email: "test@email.com"
            });

            const wrapped = test.wrap(myFunctions.userRegistration);
        })
    });
});

0 个答案:

没有答案