我正在尝试使用 jest(测试环境:节点)通过集成测试,以获得使用 csurf 进行 csrf 保护的登录表单(使用 cookie 选项)。
我已经从登录表单和 set-cookie 标头中提取了 csrfToken,但测试仍然失败,并出现 403 - 无效 csrf 令牌。
我看不出问题出在哪里,希望能朝着正确的方向前进。
测试文件:
const request = require('supertest');
const {User} = require('../../server/models/user');
const cheerio = require('cheerio');
const app = require('../../app');
let user, csrfToken, password, cookies;
beforeEach( async () => {
user = await new User({
firstName: "Name",
lastName: "Surname",
email: "email@example.com",
password: "password",
isAdmin : true
}).save();
});
afterEach( async () => {
await User.deleteMany();
});
describe('/login', () => {
describe('GET /', () => {
const exec = async () => {
const res = await request(app).get(`/login`);
let $ = cheerio.load(res.text);
csrfToken = $('[name=_csrf]').val();
return res;
};
it('should return the login form', async () => {
const res = await exec();
expect(res.status).toBe(200);
expect(res.text).toMatch(/Sign In/);
});
});
describe('POST /', () => {
const getLoginCsrfs = async () => {
const res = await request(app).get(`/login`);
let $ = cheerio.load(res.text);
csrfToken = $('[name=_csrf]').val();
cookies = res.headers['set-cookie'];
return res;
};
const postLogin = async () => {
return request(app).post(`/login`)
.set('Cookie', cookies)
.send({ email: user.email,
password: password,
_csrf: csrfToken
});
};
it('should return 401 without incorrect user info', async () => {
await getLoginCsrfs();
password = 'wrongpassword';
const res = await postLogin();
expect(res.status).toBe(401)
});
it('should return 403 without csrf token/header credentials', async () => {
await getLoginCsrfs();
csrfToken = '';
cookies = '';
password = 'password';
const res = await postLogin();
expect(res.status).toBe(403)
});
it('should return 200 with correct credentials', async () => {
await getLoginCsrfs();
password = 'password';
const res = await postLogin();
expect(res.status).toBe(200)
});
});
});
FAIL tests/integration/login.test.js
/login
GET /
✓ should return the login form (300ms)
POST /
✕ should return 401 without incorrect user info (150ms)
✓ should return 403 without csrf token/header credentials (130ms)
✕ should return 200 with correct credentials (131ms)
● /login › POST / › should return 401 without incorrect user info
expect(received).toBe(expected) // Object.is equality
Expected: 401
Received: 403
61 | password = 'wrongpassword';
62 | const res = await postLogin();
> 63 | expect(res.status).toBe(401)
| ^
64 | });
65 |
66 | it('should return 403 without csrf token/header credentials', async () => {
at Object.toBe (tests/integration/login.test.js:63:26)
● /login › POST / › should return 200 with correct credentials
expect(received).toBe(expected) // Object.is equality
Expected: 200
Received: 403
77 | password = 'password';
78 | const res = await postLogin();
> 79 | expect(res.status).toBe(200)
| ^
80 | });
81 | });
82 | });
at Object.toBe (tests/integration/login.test.js:79:26)