[英]Autentication with Cypress and bearer token
I'm pretty new with Cypress, I started using it just two days ago;我对赛普拉斯还很陌生,两天前我才开始使用它; I'm tryng to realize some tests for a website, after logging on it first.
在先登录后,我正在尝试对网站进行一些测试。
Following this answer , I added the package cypress-localstorage-commands and I created this code in cypress/support/command.js
, which is empty except for the following:在这个答案之后,我添加了 package cypress-localstorage-commands并在
cypress/support/command.js
中创建了这个代码,除了以下内容之外它是空的:
import 'cypress-localstorage-commands'
let user
Cypress.Commands.add('postToken', () => {
cy.request({
method: 'POST',
url: 'http://localhost:1337/auth/local',
auth: {
identifier: 'myUsername',
password: 'myPassword',
},
})
.its('body')
.then(res => {
console.log(res)
user = res.user
cy.setLocalStorage('identity_token', res.jwt)
})
})
I have in integration/backoffice/backoffice.test.js
( backoffice
is the only folder for now) this simple code:我在
integration/backoffice/backoffice.test.js
( backoffice
是目前唯一的文件夹)中有这个简单的代码:
/// <reference types="cypress" />
const siteUrl = 'http://localhost:3000'
describe('Backoffice with Cypress', () => {
before(() => {
cy.postToken()
cy.saveLocalStorage()
})
beforeEach(() => {
cy.viewport(2048, 1080)
cy.restoreLocalStorage()
})
after(() => {
// quit and close browser
})
describe('Authentication', () => {
it('should login', () => {
cy.getLocalStorage('identity_token').should('exist')
cy.getLocalStorage('identity_token').then(token => {
console.log('Identity token', token)
})
cy.visit(siteUrl)
})
})
})
A correct payload for http://localhost:1337/auth/local
is: http://localhost:1337/auth/local
的正确有效载荷是:
{identifier: "myUsername", password: "myPassword"}
And authentication is made through bearer token.并且通过不记名令牌进行身份验证。
When I launch the cypress test, the first time says:当我启动 cypress 测试时,第一次说:
cy.request() failed trying to load:
http://localhost:1337/auth/local
We attempted to make an http request to this URL but the request failed without a response.
We received this error at the network level:
> Error: no auth mechanism defined
-----------------------------------------------------------
The request we sent was:
Method: POST
URL: http://localhost:1337/auth/local
And the server on localhost:1337 says: localhost:1337 上的服务器说:
Error during sync with gitlab tag list - TypeError [ERR_HTTP_INVALID_HEADER_VALUE]: Invalid value "undefined" for header "PRIVATE-TOKEN".
POST /auth/local (101 ms) 400
If I try tu run it again through the refresh button, cypress brutally crashes and stops.如果我尝试通过刷新按钮再次运行它,赛普拉斯会残酷地崩溃并停止。
If I try to use "username" instead of "identifier" in command.js
, the servers responds with 401
.如果我尝试在
command.js
中使用“用户名”而不是“标识符”,服务器会以401
响应。
All I want to do is to test for authentication, and keep this authentication for the following tests i will realize.我要做的就是测试身份验证,并为我将实现的以下测试保留此身份验证。 What I'm doing wrong?
我做错了什么?
Solved.解决了。 Problem was that i used
问题是我用过
cy.request({
method: 'POST',
url: 'http://localhost:1337/auth/local',
auth: {
identifier: 'myUsername',
password: 'myPassword',
},
})
but that's wrong, because I'm not putting user and password in the body of the request.但这是错误的,因为我没有将用户名和密码放在请求的正文中。 I need to use this:
我需要使用这个:
cy.request({
method: 'POST',
url: 'http://localhost:1337/auth/local',
body: {
identifier: 'myUsername',
password: 'myPassword',
},
})
So the full function in commands.js
is所以
commands.js
中的完整 function 是
import 'cypress-localstorage-commands'
let user
Cypress.Commands.add('postToken', () => {
cy.request({
method: 'POST',
url: 'http://localhost:1337/auth/local',
body: {
identifier: 'myUsername',
password: 'myPassword',
},
})
.its('body')
.then(res => {
console.log(res)
user = res.user
cy.setLocalStorage('identity_token', res.jwt)
})
})
and its use is:它的用途是:
before(() => {
cy.postToken()
cy.saveLocalStorage()
})
beforeEach(() => {
cy.restoreLocalStorage()
})
after(() => {
// quit and close browser
})
describe('Authentication', () => {
it.only('should login', () => {
cy.getLocalStorage('identity_token').should('exist')
cy.getLocalStorage('identity_token').then(token => {
console.log('Identity token', token)
})
cy.visit(siteUrl)
})
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.