我如何用摩卡单元测试passportjs?

时间:2014-03-10 15:49:50

标签: javascript mocha passport.js

我想用mocha测试这个方法,我不知道从哪里开始? 路线:

 app.post('/signup', passport.authenticate('local-signup', {
                failureRedirect: '/#/',
                failureFlash: true
            }),
            function(req, res) {
                res.jsonp(req.user);
            });

以下是我服务的定义:

passport.use('local-signup', new LocalStrategy({
            // by default, local strategy uses username and password, we will override with email
            usernameField: 'email',
            passwordField: 'password',
            pseudoField: 'pseudo',

            passReqToCallback: true // allows us to pass back the entire request to the callback
        },
        function(req, email, password, pseudo, done) {

            // asynchronous
            // User.findOne wont fire unless data is sent back
            process.nextTick(function() {

                // find a user whose email is the same as the forms email
                // we are checking to see if the user trying to login already exists
                User.findOne({
                    'local.email': email
                }, function(err, user) {
                    // if there are any errors, return the error
                    if (err)
                        return done(err);

                    // check to see if theres already a user with that email
                    if (user) {
                        console.log('That email is already taken');
                        //var newUser = new User();
                        return done(404, null);
                        // return done(null, false, req.flash('signupMessage', 'That email is already taken.'));
                    } else {

                        // if there is no user with that email
                        // create the user
                        console.log('creation new user');
                        var newUser = new User();
                        // set the user's local credentials
                        newUser.local.email = email;
                        newUser.local.password = newUser.generateHash(password);
                        newUser.local.pseudo = pseudo;
                        console.log(pseudo);
                        console.log(newUser.local);
                        // save the user
                        console.log('going to save in bdd');
                        newUser.save(function(err) {
                            if (err)
                                throw err;
                            return done(null, newUser);
                        });
                    }

                });

            });

        }));

我只是需要一些帮助来了解我如何测试这种方法以及如何调用它。

0 个答案:

没有答案