This repository has been archived on 2024-02-08. You can view files and clone it, but cannot push or open issues or pull requests.
2020-12-10 13:52:35 +00:00
|
|
|
|
|
|
|
// This authenticates a user and puts the token in local storage which allows us to perform authenticated requests.
|
|
|
|
// Built after https://github.com/cypress-io/cypress-example-recipes/tree/bd2d6ffb33214884cab343d38e7f9e6ebffb323f/examples/logging-in__jwt
|
|
|
|
|
|
|
|
import {UserFactory} from '../factories/user'
|
|
|
|
|
2022-12-15 21:35:17 +00:00
|
|
|
export function login(user, cacheAcrossSpecs = false) {
|
|
|
|
if (!user) {
|
|
|
|
throw new Error('Needs user')
|
|
|
|
}
|
|
|
|
// Caching session when logging in via page visit
|
|
|
|
cy.session(`user__${user.username}`, () => {
|
|
|
|
cy.request('POST', `${Cypress.env('API_URL')}/login`, {
|
|
|
|
username: user.username,
|
|
|
|
password: '1234',
|
|
|
|
}).then(({ body }) => {
|
|
|
|
window.localStorage.setItem('token', body.token)
|
|
|
|
})
|
|
|
|
}, {
|
|
|
|
cacheAcrossSpecs,
|
|
|
|
})
|
|
|
|
}
|
2020-12-10 13:52:35 +00:00
|
|
|
|
2022-12-15 21:35:17 +00:00
|
|
|
export function createFakeUserAndLogin() {
|
|
|
|
let user
|
|
|
|
before(() => {
|
|
|
|
user = UserFactory.create(1)[0]
|
2020-12-10 13:52:35 +00:00
|
|
|
})
|
|
|
|
|
2022-12-15 21:35:17 +00:00
|
|
|
beforeEach(() => {
|
|
|
|
login(user, true)
|
2020-12-10 13:52:35 +00:00
|
|
|
})
|
2022-12-15 21:35:17 +00:00
|
|
|
|
|
|
|
return user
|
|
|
|
}
|