strapi/test/helpers/auth.js

53 lines
919 B
JavaScript
Raw Normal View History

const { createRequest } = require('./request');
2018-07-19 14:46:03 +02:00
const auth = {
email: 'admin@strapi.io',
firstname: 'admin',
lastname: 'admin',
password: 'Password123',
2018-07-19 14:46:03 +02:00
};
const rq = createRequest();
2019-03-08 17:54:45 +01:00
2019-05-06 15:33:25 +02:00
const register = async () => {
await rq({
url: '/admin/register-admin',
2019-05-06 15:33:25 +02:00
method: 'POST',
body: auth,
}).catch(err => {
console.error(err);
if (err.message === 'You cannot register a new super admin') return;
2019-05-06 15:33:25 +02:00
throw err;
});
};
const login = async () => {
const { body } = await rq({
url: '/admin/login',
2019-05-06 15:33:25 +02:00
method: 'POST',
body: {
email: auth.email,
2019-05-06 15:33:25 +02:00
password: auth.password,
},
});
return body.data;
2019-05-06 15:33:25 +02:00
};
2018-07-19 14:46:03 +02:00
module.exports = {
2019-05-06 15:33:25 +02:00
async registerAndLogin() {
// register
await register();
2019-03-09 01:06:39 +01:00
2019-05-06 15:33:25 +02:00
// login
const user = await login();
return user && user.token;
},
async login() {
const user = await login();
return user && user.token;
2019-03-08 17:54:45 +01:00
},
2018-07-19 14:46:03 +02:00
};