import { OktaAuth } from '@okta/okta-auth-js'
Cypress.Commands.add('oktaLoginApi', (username, password) => {
cy.request({
method: 'POST',
url: `https://${Cypress.env('okta_domain')}/api/v1/authn`,
body: {
username,
password
},
headers: {
"Content-Type": "application/json",
"Accept": "application/json"
}
}).then(({ body }) => {
const user = body._embedded.user
const config = {
issuer: `https://${Cypress.env('okta_domain')}/oauth2/default`,
clientId: Cypress.env('okta_client_id'),
redirectUri: 'http://localhost:3000/login/callback',
scope: ['openid', 'email', 'profile'], // TODO: look into
}
const authClient = new OktaAuth(config)
cy.log("hello"+authClient.token)
return authClient.token
.getWithoutPrompt({ sessionToken: body.sessionToken })
.then(({ tokens }) => {
const userItem = {
token: tokens.accessToken.value,
user: {
sub: user.id,
email: user.profile.login,
given_name: user.profile.firstName,
family_name: user.profile.lastName,
preferred_username: user.profile.login,
},
}
window.localStorage.setItem('oktaCypress', JSON.stringify(userItem))
log.snapshot('after')
log.end()
}, error => {
console.log(error)
})
})
})
giving error
CypressError
cy.then() timed out after waiting 4000ms.
Your callback function returned a promise that never resolved.
The callback function was:
({
body
}) => {
const user = body._embedded.user;
const config = {
issuer: https://${Cypress.env('okta_domain')}/oauth2/default,
clientId: Cypress.env('okta_client_id'),
redirectUri: 'http://localhost:3000/login/callback',
scope: ['openid', 'email', 'profile'] // TODO: look into
};
const authClient = new _oktaAuthJs.OktaAuth(config);
cy.log("hello" + authClient.token);
return authClient.token.getWithoutPrompt({
sessionToken: body.sessionToken
}).then(({
tokens
}) => {
const userItem = {
token: tokens.accessToken.value,
user: {
sub: user.id,
email: user.profile.login,
given_name: user.profile.firstName,
family_name: user.profile.lastName,
preferred_username: user.profile.login
}
};
window.localStorage.setItem('oktaCypress', JSON.stringify(userItem));
log.snapshot('after');
log.end();
}, error => {
console.log(error);
});
}
Because this error occurred during a before each hook we are skipping the remaining tests in the current suite: OktaLearn more
absolutely no idea why