Javascript Azure OAuth with Cypress:无限循环
正在尝试设置Cypress以测试针对Azure AD使用OAuth的应用程序。我的Javascript Azure OAuth with Cypress:无限循环,javascript,oauth,azure-active-directory,cypress,ui-testing,Javascript,Oauth,Azure Active Directory,Cypress,Ui Testing,正在尝试设置Cypress以测试针对Azure AD使用OAuth的应用程序。我的login命令定义如下: Cypress.Commands.add('login', () => { return cy.request('POST', Cypress.env('AccessTokenUrl') + '?grant_type=' + Cypress.env('GrantType') + '&client_id=' + Cypress.env('
login
命令定义如下:
Cypress.Commands.add('login', () => {
return cy.request('POST', Cypress.env('AccessTokenUrl') +
'?grant_type=' + Cypress.env('GrantType') +
'&client_id=' + Cypress.env('ClientId') +
'&client_secret=' + Cypress.env('ClientSecret'))
})
这就是我在测试中所说的:
cy.login().then(response => {
expect(response.status).to.eq(200)
expect(response.body).to.have.property('access_token')
expect(response.body).to.have.property('token_type', 'Bearer')
const {access_token, expires_in, id_token} = response.body
cy.setCookie('access_token', access_token)
})
cy.visit('my-url')
验证通过了。登录响应包含有效的令牌。但是,ct.visit
调用会以无限递归的方式失败,因为像&iframe request id=[some uuid]
这样的参数会反复添加到login.microsoftonline.com
URL,直到最终返回HTTP错误414。请求URL太长。
以下是URL的外观,其中包含一些经过编辑的信息,并带有一些清晰的格式:
https://login.microsoftonline.com/
[tenant-id]/oauth2/v2.0/authorize
?response_type=code
&client_id=[client-id]
&redirect_uri=[my-url]
&scope=openid+profile+email+https%3A%2F%2Fgraph.microsoft.com%2Fuser.read
&iframe-request-id=1a9fdcbd-6b9e-46c8-93e3-ce0edf62b600
&iframe-request-id=b5b5cf2b-e0a6-4d92-9e55-cf32208ab900
&iframe-request-id=8471e17f-1d36-48f7-8419-f54e14b3b100
&iframe-request-id=56113dad-6029-4a37-9758-5828f93f0300
&iframe-request-id=51c06224-98f1-4b83-a8f2-84f8dfe9aa00
&iframe-request-id=09775645-505c-42e0-ac56-1335b5a7ba00
&iframe-request-id=5c98158b-b202-41fe-9d65-8fbfe4e46500
&[and-so-on]
我在网上找到了各种关于使用木偶演员作为Azure AD SSO任务的建议,但没有一个适合我的目的。首先,他们试图解决实际获取代币的问题,我已经解决了这个问题。其次,它们依赖于显示HTML表单的登录URL,而login.microsoftonline.com
的情况并非如此
你有什么建议
更新:尝试不同的解决方案时,我收到一个有趣的错误。loginMS
命令:
import * as MSAL from '@azure/msal-browser'
Cypress.Commands.add('loginMS', () => {
cy.request({
method: 'POST',
url: `https://login.microsoftonline.com/${Cypress.env('TenantId')}/oauth2/token`,
form: true,
body: {
scope: Cypress.env('LoginScope'),
client_id: Cypress.env('ClientId'),
client_secret: Cypress.env('ClientSecret'),
redirect_uri: Cypress.env('LoginRedirect'),
grant_type: Cypress.env('GrantType'),
username: Cypress.env('Username'),
password: Cypress.env('Password'),
response_type: 'code'
}
}).then(response => {
console.log(response)
window.localStorage.setItem(`msal.idtoken`, response.body.access_token);
window.localStorage.setItem(`msal.client.info`, MSAL.clientInfo);
})
})
错误是:
Failed to find a valid digest in the 'integrity' attribute for resource
'https://aadcdn.msauth.net/shared/1.0/content/js/OldConvergedLogin_PCore_Up8WrFIk8-TG_eqBz8MSlw2.js'
with computed SHA-256 integrity 'NxfOkHjbTYDy/EOknsK0PMOfym7iLRGY+yBShyznzx4='.
The resource has been blocked.
这实际上取决于被测试的应用程序如何处理请求。但我猜你用的是阿达尔图书馆 在它的帮助下,我在使用adal v1的vuejs应用程序中工作 重要的是
localStorage.setItem("adal.token.keys", `${Cypress.config("clientId")}|`);
localStorage.setItem(`adal.access.token.key${Cypress.config("clientId")}`, ADALToken);
localStorage.setItem(`adal.expiration.key${Cypress.config("clientId")}`, expiresOn);
localStorage.setItem("adal.idtoken", ADALToken);
实际上,我没有从azure请求令牌,只是在使用测试中的应用程序时将我看到的F12工具复制为我的令牌。如果
我的url
将您重定向到登录。microsoftonline.com
,是否仅设置访问令牌
cookie是不够的?如果我理解正确,您将希望在访问应用程序之前设置登录所需的所有Cookie和/或localStorage,这样您就不会被重定向。另外,您的baseUrl
是否设置为与我的url
相同的主机?或者更一般地说:当执行cy.setCookie
时,Cypress runner是否与my url
位于同一个超域上?否则,cookie可能会被设置在错误的域上!是的,同一个域名。但你可能是对的,我没有设置正确的cookie。调查这件事。。。。