2020-10-27 11:27:17 +01:00
|
|
|
'use strict';
|
|
|
|
|
2019-10-16 00:17:54 +09:00
|
|
|
// Test a simple default API with no relations
|
|
|
|
|
2020-11-17 15:38:41 +01:00
|
|
|
const { createStrapiInstance } = require('../../../test/helpers/strapi');
|
2019-10-16 00:17:54 +09:00
|
|
|
const { createAuthRequest } = require('../../../test/helpers/request');
|
|
|
|
|
2020-11-17 15:38:41 +01:00
|
|
|
let strapi;
|
2019-10-16 00:17:54 +09:00
|
|
|
let rq;
|
|
|
|
let graphqlQuery;
|
|
|
|
let data = {};
|
|
|
|
|
|
|
|
describe('Test Graphql Users API End to End', () => {
|
|
|
|
beforeAll(async () => {
|
2020-11-30 20:20:36 +01:00
|
|
|
strapi = await createStrapiInstance();
|
2020-11-17 15:38:41 +01:00
|
|
|
rq = await createAuthRequest({ strapi });
|
2019-10-16 00:17:54 +09:00
|
|
|
|
|
|
|
graphqlQuery = body => {
|
|
|
|
return rq({
|
|
|
|
url: '/graphql',
|
|
|
|
method: 'POST',
|
|
|
|
body,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}, 60000);
|
|
|
|
|
2020-11-17 15:38:41 +01:00
|
|
|
afterAll(async () => {
|
|
|
|
await strapi.destroy();
|
|
|
|
});
|
|
|
|
|
2019-10-16 00:17:54 +09:00
|
|
|
describe('Test register and login', () => {
|
|
|
|
const user = {
|
|
|
|
username: 'User 1',
|
|
|
|
email: 'user1@strapi.io',
|
|
|
|
password: 'test1234',
|
|
|
|
};
|
|
|
|
|
|
|
|
test('Register a user', async () => {
|
|
|
|
const res = await graphqlQuery({
|
|
|
|
query: /* GraphQL */ `
|
2020-05-04 13:32:47 -03:00
|
|
|
mutation register($input: UsersPermissionsRegisterInput!) {
|
2019-10-16 00:17:54 +09:00
|
|
|
register(input: $input) {
|
|
|
|
jwt
|
|
|
|
user {
|
|
|
|
id
|
|
|
|
email
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`,
|
|
|
|
variables: {
|
|
|
|
input: user,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const { body } = res;
|
|
|
|
|
|
|
|
expect(res.statusCode).toBe(200);
|
|
|
|
expect(body).toMatchObject({
|
|
|
|
data: {
|
|
|
|
register: {
|
|
|
|
jwt: expect.any(String),
|
|
|
|
user: {
|
|
|
|
id: expect.any(String),
|
|
|
|
email: user.email,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
data.user = res.body.data.register.user;
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Log in a user', async () => {
|
|
|
|
const res = await graphqlQuery({
|
|
|
|
query: /* GraphQL */ `
|
|
|
|
mutation login($input: UsersPermissionsLoginInput!) {
|
|
|
|
login(input: $input) {
|
|
|
|
jwt
|
|
|
|
user {
|
|
|
|
id
|
|
|
|
email
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`,
|
|
|
|
variables: {
|
|
|
|
input: {
|
|
|
|
identifier: user.username,
|
|
|
|
password: user.password,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const { body } = res;
|
|
|
|
|
|
|
|
expect(res.statusCode).toBe(200);
|
|
|
|
expect(body).toMatchObject({
|
|
|
|
data: {
|
|
|
|
login: {
|
|
|
|
jwt: expect.any(String),
|
|
|
|
user: {
|
|
|
|
id: expect.any(String),
|
|
|
|
email: user.email,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
data.user = res.body.data.login.user;
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Delete a user', async () => {
|
|
|
|
const res = await graphqlQuery({
|
|
|
|
query: /* GraphQL */ `
|
|
|
|
mutation deleteUser($input: deleteUserInput) {
|
|
|
|
deleteUser(input: $input) {
|
|
|
|
user {
|
|
|
|
email
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`,
|
|
|
|
variables: {
|
|
|
|
input: {
|
|
|
|
where: {
|
|
|
|
id: data.user.id,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const { body } = res;
|
|
|
|
|
|
|
|
expect(res.statusCode).toBe(200);
|
|
|
|
expect(body).toMatchObject({
|
|
|
|
data: {
|
|
|
|
deleteUser: {
|
|
|
|
user: {
|
|
|
|
email: data.user.email,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|