2020-10-27 11:27:17 +01:00
|
|
|
'use strict';
|
|
|
|
|
2019-03-25 16:37:46 +01:00
|
|
|
// Helpers.
|
2021-04-29 11:11:46 +02:00
|
|
|
const { createTestBuilder } = require('../../../../test/helpers/builder');
|
|
|
|
const { createStrapiInstance } = require('../../../../test/helpers/strapi');
|
|
|
|
const { createAuthRequest } = require('../../../../test/helpers/request');
|
2019-03-25 16:37:46 +01:00
|
|
|
|
2020-11-17 15:38:41 +01:00
|
|
|
const builder = createTestBuilder();
|
|
|
|
let strapi;
|
2019-03-25 16:37:46 +01:00
|
|
|
let rq;
|
|
|
|
let graphqlQuery;
|
|
|
|
|
|
|
|
const postModel = {
|
2019-11-14 16:37:57 +01:00
|
|
|
attributes: {
|
|
|
|
name: {
|
|
|
|
type: 'richtext',
|
2019-03-25 16:37:46 +01:00
|
|
|
},
|
2019-11-14 16:37:57 +01:00
|
|
|
bigint: {
|
|
|
|
type: 'biginteger',
|
2019-04-05 07:45:56 -07:00
|
|
|
},
|
2019-11-14 16:37:57 +01:00
|
|
|
nullable: {
|
|
|
|
type: 'string',
|
2019-07-04 19:10:17 -03:00
|
|
|
},
|
2019-11-14 16:37:57 +01:00
|
|
|
},
|
2019-03-25 16:37:46 +01:00
|
|
|
name: 'post',
|
|
|
|
description: '',
|
|
|
|
collectionName: '',
|
|
|
|
};
|
|
|
|
|
|
|
|
describe('Test Graphql API End to End', () => {
|
|
|
|
beforeAll(async () => {
|
2020-11-17 15:38:41 +01:00
|
|
|
await builder.addContentType(postModel).build();
|
|
|
|
|
2020-11-30 20:20:36 +01:00
|
|
|
strapi = await createStrapiInstance();
|
2020-11-17 15:38:41 +01:00
|
|
|
rq = await createAuthRequest({ strapi });
|
2019-03-25 16:37:46 +01:00
|
|
|
|
|
|
|
graphqlQuery = body => {
|
|
|
|
return rq({
|
|
|
|
url: '/graphql',
|
|
|
|
method: 'POST',
|
|
|
|
body,
|
|
|
|
});
|
|
|
|
};
|
2021-03-26 20:15:38 +01:00
|
|
|
});
|
2019-03-25 16:37:46 +01:00
|
|
|
|
2020-11-17 15:38:41 +01:00
|
|
|
afterAll(async () => {
|
|
|
|
await strapi.destroy();
|
|
|
|
await builder.cleanup();
|
2021-03-26 20:15:38 +01:00
|
|
|
});
|
2019-03-25 16:37:46 +01:00
|
|
|
|
|
|
|
describe('Test CRUD', () => {
|
2019-05-06 15:33:25 +02:00
|
|
|
const postsPayload = [
|
2019-07-04 19:10:17 -03:00
|
|
|
{ name: 'post 1', bigint: 1316130638171, nullable: 'value' },
|
|
|
|
{ name: 'post 2', bigint: 1416130639261, nullable: null },
|
2019-05-06 15:33:25 +02:00
|
|
|
];
|
2019-03-25 16:37:46 +01:00
|
|
|
let data = {
|
|
|
|
posts: [],
|
|
|
|
};
|
|
|
|
|
|
|
|
test.each(postsPayload)('Create Post %o', async post => {
|
|
|
|
const res = await graphqlQuery({
|
|
|
|
query: /* GraphQL */ `
|
|
|
|
mutation createPost($input: createPostInput) {
|
|
|
|
createPost(input: $input) {
|
|
|
|
post {
|
|
|
|
name
|
2019-04-05 07:45:56 -07:00
|
|
|
bigint
|
2019-07-04 19:10:17 -03:00
|
|
|
nullable
|
2019-03-25 16:37:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`,
|
|
|
|
variables: {
|
|
|
|
input: {
|
|
|
|
data: post,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const { body } = res;
|
|
|
|
|
|
|
|
expect(res.statusCode).toBe(200);
|
|
|
|
expect(body).toEqual({
|
|
|
|
data: {
|
|
|
|
createPost: {
|
|
|
|
post,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
test('List posts', async () => {
|
|
|
|
const res = await graphqlQuery({
|
|
|
|
query: /* GraphQL */ `
|
|
|
|
{
|
|
|
|
posts {
|
|
|
|
id
|
|
|
|
name
|
2019-04-05 07:45:56 -07:00
|
|
|
bigint
|
2019-07-04 19:10:17 -03:00
|
|
|
nullable
|
2019-03-25 16:37:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
`,
|
|
|
|
});
|
|
|
|
|
|
|
|
const { body } = res;
|
|
|
|
|
|
|
|
expect(res.statusCode).toBe(200);
|
|
|
|
expect(body).toMatchObject({
|
|
|
|
data: {
|
|
|
|
posts: postsPayload,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
// assign for later use
|
|
|
|
data.posts = res.body.data.posts;
|
|
|
|
});
|
|
|
|
|
|
|
|
test('List posts with limit', async () => {
|
|
|
|
const res = await graphqlQuery({
|
|
|
|
query: /* GraphQL */ `
|
|
|
|
{
|
|
|
|
posts(limit: 1) {
|
|
|
|
id
|
|
|
|
name
|
2019-04-05 07:45:56 -07:00
|
|
|
bigint
|
2019-07-04 19:10:17 -03:00
|
|
|
nullable
|
2019-03-25 16:37:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
`,
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(res.statusCode).toBe(200);
|
|
|
|
expect(res.body).toEqual({
|
|
|
|
data: {
|
|
|
|
posts: [data.posts[0]],
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
test('List posts with sort', async () => {
|
|
|
|
const res = await graphqlQuery({
|
|
|
|
query: /* GraphQL */ `
|
|
|
|
{
|
2019-03-25 18:04:48 +01:00
|
|
|
posts(sort: "name:desc") {
|
2019-03-25 16:37:46 +01:00
|
|
|
id
|
|
|
|
name
|
2019-04-05 07:45:56 -07:00
|
|
|
bigint
|
2019-07-04 19:10:17 -03:00
|
|
|
nullable
|
2019-03-25 16:37:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
`,
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(res.statusCode).toBe(200);
|
|
|
|
expect(res.body).toEqual({
|
|
|
|
data: {
|
|
|
|
posts: [...data.posts].reverse(),
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
test('List posts with start', async () => {
|
|
|
|
const res = await graphqlQuery({
|
|
|
|
query: /* GraphQL */ `
|
|
|
|
{
|
|
|
|
posts(start: 1) {
|
|
|
|
id
|
|
|
|
name
|
2019-04-05 07:45:56 -07:00
|
|
|
bigint
|
2019-07-04 19:10:17 -03:00
|
|
|
nullable
|
2019-03-25 16:37:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
`,
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(res.statusCode).toBe(200);
|
|
|
|
expect(res.body).toEqual({
|
|
|
|
data: {
|
|
|
|
posts: [data.posts[1]],
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-10-01 17:47:08 +02:00
|
|
|
test.skip('List posts with `created_by` and `updated_by`', async () => {
|
2020-08-07 15:50:07 +02:00
|
|
|
const res = await graphqlQuery({
|
2020-08-26 10:38:54 +02:00
|
|
|
query: /* GraphQL */ `
|
|
|
|
{
|
|
|
|
posts(start: 1) {
|
|
|
|
id
|
|
|
|
name
|
|
|
|
bigint
|
|
|
|
nullable
|
|
|
|
created_by {
|
|
|
|
username
|
|
|
|
}
|
|
|
|
updated_by {
|
|
|
|
username
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`,
|
2020-08-07 15:50:07 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
expect(res.statusCode).toBe(200);
|
|
|
|
|
|
|
|
// no errors should be present in the response
|
|
|
|
expect(res.body.error).toBeUndefined();
|
|
|
|
|
|
|
|
// since the posts are created without AdminUser, it should return null
|
|
|
|
expect(res.body.data.posts[0].created_by).toBeNull();
|
|
|
|
});
|
|
|
|
|
2019-03-25 16:37:46 +01:00
|
|
|
test.each([
|
|
|
|
[
|
|
|
|
{
|
|
|
|
name: 'post 1',
|
2019-04-05 07:45:56 -07:00
|
|
|
bigint: 1316130638171,
|
2019-03-25 16:37:46 +01:00
|
|
|
},
|
|
|
|
[postsPayload[0]],
|
|
|
|
],
|
|
|
|
[
|
|
|
|
{
|
|
|
|
name_eq: 'post 1',
|
2019-04-05 07:45:56 -07:00
|
|
|
bigint_eq: 1316130638171,
|
2019-03-25 16:37:46 +01:00
|
|
|
},
|
|
|
|
[postsPayload[0]],
|
|
|
|
],
|
|
|
|
[
|
|
|
|
{
|
|
|
|
name_ne: 'post 1',
|
2019-04-05 07:45:56 -07:00
|
|
|
bigint_ne: 1316130638171,
|
2019-03-25 16:37:46 +01:00
|
|
|
},
|
|
|
|
[postsPayload[1]],
|
|
|
|
],
|
|
|
|
[
|
|
|
|
{
|
|
|
|
name_contains: 'Post',
|
|
|
|
},
|
|
|
|
postsPayload,
|
|
|
|
],
|
|
|
|
[
|
|
|
|
{
|
|
|
|
name_contains: 'Post 1',
|
|
|
|
},
|
|
|
|
[postsPayload[0]],
|
|
|
|
],
|
|
|
|
[
|
|
|
|
{
|
|
|
|
name_containss: 'post',
|
|
|
|
},
|
|
|
|
postsPayload,
|
|
|
|
],
|
|
|
|
[
|
|
|
|
{
|
|
|
|
name_ncontainss: 'post 1',
|
|
|
|
},
|
|
|
|
[postsPayload[1]],
|
|
|
|
],
|
|
|
|
[
|
|
|
|
{
|
2019-07-04 19:10:17 -03:00
|
|
|
name_in: ['post 1', 'post 2', 'post 3'],
|
2019-03-25 16:37:46 +01:00
|
|
|
},
|
|
|
|
postsPayload,
|
|
|
|
],
|
|
|
|
[
|
|
|
|
{
|
|
|
|
name_nin: ['post 2'],
|
|
|
|
},
|
|
|
|
[postsPayload[0]],
|
|
|
|
],
|
2019-07-04 19:10:17 -03:00
|
|
|
[
|
|
|
|
{
|
|
|
|
nullable_null: true,
|
|
|
|
},
|
|
|
|
[postsPayload[1]],
|
|
|
|
],
|
|
|
|
[
|
|
|
|
{
|
|
|
|
nullable_null: false,
|
|
|
|
},
|
|
|
|
[postsPayload[0]],
|
|
|
|
],
|
2020-10-15 13:22:40 +02:00
|
|
|
[
|
|
|
|
{
|
|
|
|
_or: [{ name_in: ['post 2'] }, { bigint_eq: 1316130638171 }],
|
|
|
|
},
|
|
|
|
[postsPayload[0], postsPayload[1]],
|
|
|
|
],
|
|
|
|
[
|
|
|
|
{
|
|
|
|
_where: { nullable_null: false },
|
|
|
|
},
|
|
|
|
[postsPayload[0]],
|
|
|
|
],
|
|
|
|
[
|
|
|
|
{
|
|
|
|
_where: { _or: { nullable_null: false } },
|
|
|
|
},
|
|
|
|
[postsPayload[0]],
|
|
|
|
],
|
|
|
|
[
|
|
|
|
{
|
|
|
|
_where: [{ nullable_null: false }],
|
|
|
|
},
|
|
|
|
[postsPayload[0]],
|
|
|
|
],
|
|
|
|
[
|
|
|
|
{
|
|
|
|
_where: [{ _or: [{ name_in: ['post 2'] }, { bigint_eq: 1316130638171 }] }],
|
|
|
|
},
|
|
|
|
[postsPayload[0], postsPayload[1]],
|
|
|
|
],
|
|
|
|
[
|
|
|
|
{
|
|
|
|
_where: [
|
|
|
|
{
|
|
|
|
_or: [
|
|
|
|
{ name_in: ['post 2'] },
|
|
|
|
{ _or: [{ bigint_eq: 1316130638171 }, { nullable_null: false }] },
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
[postsPayload[0], postsPayload[1]],
|
|
|
|
],
|
2019-03-25 16:37:46 +01:00
|
|
|
])('List posts with where clause %o', async (where, expected) => {
|
|
|
|
const res = await graphqlQuery({
|
|
|
|
query: /* GraphQL */ `
|
|
|
|
query findPosts($where: JSON) {
|
|
|
|
posts(where: $where) {
|
|
|
|
name
|
2019-04-05 07:45:56 -07:00
|
|
|
bigint
|
2019-07-04 19:10:17 -03:00
|
|
|
nullable
|
2019-03-25 16:37:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
`,
|
|
|
|
variables: {
|
|
|
|
where,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(res.statusCode).toBe(200);
|
2019-05-13 13:59:41 +02:00
|
|
|
|
|
|
|
// same length
|
|
|
|
expect(res.body.data.posts.length).toBe(expected.length);
|
|
|
|
|
|
|
|
// all the posts returned are in the expected array
|
|
|
|
res.body.data.posts.forEach(post => {
|
|
|
|
expect(expected).toEqual(expect.arrayContaining([post]));
|
|
|
|
});
|
|
|
|
|
|
|
|
// all expected values are in the result
|
|
|
|
expected.forEach(expectedPost => {
|
2020-08-26 10:38:54 +02:00
|
|
|
expect(res.body.data.posts).toEqual(expect.arrayContaining([expectedPost]));
|
2019-03-25 16:37:46 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Get One Post', async () => {
|
|
|
|
const res = await graphqlQuery({
|
|
|
|
query: /* GraphQL */ `
|
|
|
|
query getPost($id: ID!) {
|
|
|
|
post(id: $id) {
|
|
|
|
id
|
|
|
|
name
|
2019-04-05 07:45:56 -07:00
|
|
|
bigint
|
2019-07-04 19:10:17 -03:00
|
|
|
nullable
|
2019-03-25 16:37:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
`,
|
|
|
|
variables: {
|
|
|
|
id: data.posts[0].id,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(res.statusCode).toBe(200);
|
|
|
|
expect(res.body).toEqual({
|
|
|
|
data: {
|
|
|
|
post: data.posts[0],
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Update Post', async () => {
|
|
|
|
const newName = 'new post name';
|
|
|
|
const res = await graphqlQuery({
|
|
|
|
query: /* GraphQL */ `
|
|
|
|
mutation updatePost($input: updatePostInput) {
|
|
|
|
updatePost(input: $input) {
|
|
|
|
post {
|
|
|
|
id
|
|
|
|
name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`,
|
|
|
|
variables: {
|
|
|
|
input: {
|
|
|
|
where: {
|
|
|
|
id: data.posts[0].id,
|
|
|
|
},
|
|
|
|
data: {
|
|
|
|
name: newName,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(res.statusCode).toBe(200);
|
|
|
|
expect(res.body).toEqual({
|
|
|
|
data: {
|
|
|
|
updatePost: {
|
|
|
|
post: {
|
|
|
|
id: data.posts[0].id,
|
|
|
|
name: newName,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
data.posts[0] = res.body.data.updatePost.post;
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Delete Posts', async () => {
|
|
|
|
for (let post of data.posts) {
|
|
|
|
const res = await graphqlQuery({
|
|
|
|
query: /* GraphQL */ `
|
|
|
|
mutation deletePost($input: deletePostInput) {
|
|
|
|
deletePost(input: $input) {
|
|
|
|
post {
|
2019-08-23 14:15:24 +02:00
|
|
|
id
|
2019-03-25 16:37:46 +01:00
|
|
|
name
|
2019-04-05 07:45:56 -07:00
|
|
|
bigint
|
2019-03-25 16:37:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`,
|
|
|
|
variables: {
|
|
|
|
input: {
|
|
|
|
where: {
|
|
|
|
id: post.id,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(res.statusCode).toBe(200);
|
2019-08-23 14:15:24 +02:00
|
|
|
expect(res.body).toMatchObject({
|
|
|
|
data: {
|
|
|
|
deletePost: {
|
|
|
|
post: {
|
|
|
|
id: post.id,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
2019-03-25 16:37:46 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|