我目前正在使用赛普拉斯来测试我们的Vue SPA。但是,我通过Auth0以编程方式登录时遇到问题。我已经按照this tutorial 创建了以下形式的命令:
Cypress.Commands.add('login', (overrides = {}) => {
Cypress.log({
name: 'loginViaAuth0'
})
const options = {
method: 'POST',
url: Cypress.env('auth_url'),
body: {
grant_type: 'password',
username: Cypress.env('auth_username'),
password: Cypress.env('auth_password'),
audience: Cypress.env('auth_audience'),
scope: 'openid profile email',
client_id: Cypress.env('auth_client_id'),
client_secret: Cypress.env('auth_client_secret')
}
}
cy.request(options)
})
和测试:
describe('login', () => {
it('should successfully log in', () => {
cy.login()
.then((resp) => {
return resp.body;
})
.then((body) => {
console.log(body)
const {access_token, expires_in, id_token} = body;
const auth0State = {
nonce: '',
state: 'some-random-state'
};
const callbackUrl = `/dashboard#access_token=${access_token}&scope=openid&id_token=${id_token}&expires_in=${expires_in}&token_type=Bearer&state=${auth0State.state}`;
cy.visit(callbackUrl, {
onBeforeLoad(win) {
win.document.cookie = 'com.auth0.auth.some-random-state=' + JSON.stringify(auth0State);
}
});
cy.url().should('include', '/dashboard')
})
});
这是服务器的响应:
{
access_token: "some-access-token",
expires_in: 86400,
id_token: "some-id-token",
scope: "openid profile email ..."
token_type: "Bearer"
}
然后,按照本教程中的说明,将此答案连接到URL,如测试所示。 不幸的是,我仍未登录,因此无法访问受保护的路线“仪表板”。 我希望你能告诉我我的错误在哪里。