问题描述
我正在尝试创建和单元测试端点来处理来自 Passport 的 Spotify 包装器的数据,我希望能够测试真实数据而不是模拟。我正在使用 supertest 和 mocha 进行测试。有什么方法可以让我的测试套件在运行所有测试之前打开浏览器登录名,以便我可以获得令牌并获取实际数据?
这里的任何帮助将不胜感激!
一些相关文件:
// tests/server.ts:
import { startServer } from '../src/api'
export const app = startServer()
//tests/service.test.ts
import request from 'supertest'
import { app } from './server'
import assert from 'assert'
describe('GET /ping',() => {
it('responds with pong',(done) => {
request(app)
.get('/ping')
.expect(200)
.then((res) => {
assert(res.body.message === 'pong')
done()
})
.catch((err) => {
done(new Error(err))
})
})
})
//src/api.ts
import express,{ Request,Response,NextFunction} from "express";
import { authApp } from './auth'
import { spotifyService } from './spotify'
import cookieSession from "cookie-session";
import passport from "passport";
import session from "express-session";
import cors from "cors";
import cookieParser from "cookie-parser"; // parse cookie header
import keys from './config/keys'
import { passport_setup } from './config/passport-setup'
export const startServer = () => {
const app = express();
const port = process.env.PORT || 8080; // default port to listen
// cookies
app.use(cookieSession({
name: "session",keys: [keys.COOKIE_KEY],maxAge: 24 * 60 * 60 * 100
}))
app.use(cookieParser());
passport_setup()
// initalize passport
app.use(passport.initialize());
// deserialize cookie from the browser
app.use(passport.session());
app.use(cors({
origin: "http://localhost:3000",// allow to server to accept request from different origin
methods: "GET,HEAD,PUT,PATCH,POST,DELETE",credentials: true // allow session cookie from browser to pass through
}));
// Add auth routes
app.use('/auth',authApp)
const authCheck = (req: Request,res: Response,next: NextFunction) => {
if (!req.user) {
res.status(401).json({
authenticated: false,message: "user has not been authenticated"
});
} else {
next();
}
};
// if it's already login,send the profile response,// otherwise,send a 401 response that the user is not authenticated
// authCheck before navigating to home page
app.get("/",authCheck,(req,res) => {
res.status(200).json({
authenticated: true,message: "user successfully authenticated",user: req.user,cookies: req.cookies
});
});
app.get('/ping',( req,res ) => {
res.send({ message: 'pong' })
})
// start the Express server
app.listen( port,() => {
console.log( `server started at http://localhost:${ port }` );
} );
return app
}
//src/auth.ts
import express from "express";
import passport from "passport";
const app = express();
const CLIENT_HOME_PAGE_URL = "http://localhost:3000";
// when login is successful,retrieve user info
app.get("/login/success",res) => {
if (req.user) {
res.json({
success: true,message: "user has successfully authenticated",cookies: req.cookies
});
}
});
// when login Failed,send Failed msg
app.get("/login/Failed",res) => {
res.status(401).json({
success: false,message: "user Failed to authenticate."
});
});
// When logout,redirect to client
app.get("/logout",res) => {
req.logout();
res.redirect(CLIENT_HOME_PAGE_URL);
});
// auth with spotify
app.get("/spotify",passport.authenticate("spotify"));
// redirect to home page after successfully login via spotify
app.get(
"/spotify/redirect",passport.authenticate("spotify",{
successRedirect: CLIENT_HOME_PAGE_URL,failureRedirect: "/auth/login/Failed"
})
);
export { app as authApp }
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)