Mar*_*lik 5 auth0 cypress auth0-lock
我有一个隐藏在 Auth0 锁后面的单页应用程序,使用@auth0/auth0-spa-js。我想使用 Cypress 对其进行测试,因此我决定关注Auth0官方博客文章以及 Johnny Reilly博客文章。
我能够使用建议的请求从 auth0 成功检索有效的 JWT 令牌。我不知道该怎么办:(
我面临的问题是,上述两种方法都依赖于应用程序在本地存储 JWT 令牌(在 cookie 或 localstorage 中)。然而,@auth0/auth0-spa-js使用不同的方法,我假设所有相关的 cookie/localstorage 都存储在 auth0 域中。
你有什么想法,如果有办法绕过它吗?
我在 github 上发现了一个已解决的问题@auth0/auth0-spa-js。cwmrowe建议的方法似乎有效
oauth/token解决方案是使用 e2e 测试端生成的令牌来模拟端点的响应。
这种方法似乎对我们有用
我正在复制cwmrowe提供的 示例代码
Cypress.Commands.add(
'login',
(username, password, appState = { target: '/' }) => {
cy.log(`Logging in as ${username}`);
const options = {
method: 'POST',
url: Cypress.env('Auth0TokenUrl'),
body: {
grant_type: 'password',
username,
password,
audience: Cypress.env('Auth0Audience'),
scope: 'openid profile email',
client_id: Cypress.env('Auth0ClientId'),
client_secret: Cypress.env('Auth0ClientSecret')
}
};
cy.request(options).then(({ body }) => {
const { access_token, expires_in, id_token } = body;
cy.server();
// intercept Auth0 request for token and return what we have
cy.route({
url: 'oauth/token',
method: 'POST',
response: {
access_token,
expires_in,
id_token,
token_type: 'Bearer'
}
});
// Auth0 SPA SDK will check for value in cookie to get appState
// and validate nonce (which has been removed for simplicity)
const stateId = 'test';
const encodedAppState = encodeURI(JSON.stringify(appState));
cy.setCookie(
`a0.spajs.txs.${stateId}`,
`{%22appState%22:${encodedAppState}%2C%22scope%22:%22openid%20profile%20email%22%2C%22audience%22:%22default%22}`
);
const callbackUrl = `/auth/callback?code=test-code&state=${stateId}`;
return cy.visit(callbackUrl);
});
}
);
declare namespace Cypress {
interface Chainable<Subject> {
login(
username: string,
password: string,
appState?: any
): Chainable<Subject>;
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3911 次 |
| 最近记录: |