Как написать интеграционный тест для потока oAuth от Google с помощью паспорта? - PullRequest
0 голосов
/ 18 апреля 2019

У меня есть следующий код как controller:

require('dotenv').config();
const passport = require('passport');
const models = require('../models');
const GoogleStrategy = require('passport-google-oauth20').Strategy;


const strategyCallback = async (accessToken, refreshToken, profile, cb) => {
    // To be replaced with googleId once other models are merged to dev
    const email = profile.emails[0].value;
    try {
        let user = await models.user.findOne({ where: { email } });
        if (!user) {
            // Other fields will be added once the rest of the models are merged to dev
            user = await models.user.create({ email });
        } else {
            return cb(null, profile);
        }
    } catch(e) {
        return cb(e, false);
    }
}

passport.use(new GoogleStrategy({
    clientID: process.env.GOOGLE_CLIENT_ID,
    clientSecret: process.env.GOOGLE_SECRET,
    callbackURL: process.env.GOOGLE_CALLBACK
    },
    strategyCallback
));


const authenticate = passport.authenticate('google', {
    scope: ['profile', 'email']
});

const authenticateCallback = passport.authenticate('google', {
    session: false,
    successRedirect: '/users',
    failureRedirect: '/auth/failed'
});

const isLoggedIn = async (req, res) => {
    if (!req.user) {
        res.status(404).json({error: 'User not logged in!'});
    }
}

const logOut = async (req, res) => {
    req.logout();
    res.redirect('/');
}

module.exports = {
    authenticate,
    authenticateCallback,
    isLoggedIn,
    logOut
};

Я использую jest для тестирования и passport-google-oauth20 вместе с Sequelize с Postgresql для БД.

Я могу войти в систему и создать пользователя в базе данных, процесс работает, когда я тестирую это в браузере, но я не уверен, как на самом деле написать интеграционный тест для этого. Я посмотрел на несколько пакетов, но там не так много.

Должен ли я просто сделать где-нибудь макет http-запроса? Действительно не знаете, как это проверить!

Да, и я использую только accessTokens для аутентификации.

...