[2/n]: Rest API -> TwentyORM migration POST rest/* (#9986)
# This PR - Addressing #3644 - Migrates the `POST rest/*` endpoint to use TwentyORM directly - Adds integration tests - Refactors common login in the v2 service file - Refactors test utility files
This commit is contained in:
@ -0,0 +1,95 @@
|
||||
import {
|
||||
FAKE_PERSON_ID,
|
||||
PERSON_2_ID,
|
||||
} from 'test/integration/constants/mock-person-ids.constants';
|
||||
import { makeRestAPIRequest } from 'test/integration/rest/utils/make-rest-api-request.util';
|
||||
import { generateRecordName } from 'test/integration/utils/generate-record-name';
|
||||
|
||||
describe('Core REST API Create One endpoint', () => {
|
||||
afterAll(async () => {
|
||||
await makeRestAPIRequest({
|
||||
method: 'delete',
|
||||
path: `/people/${PERSON_2_ID}`,
|
||||
}).expect(200);
|
||||
});
|
||||
|
||||
it('2.a. should create a new person', async () => {
|
||||
const personCity = generateRecordName(PERSON_2_ID);
|
||||
const requestBody = {
|
||||
id: PERSON_2_ID,
|
||||
city: personCity,
|
||||
};
|
||||
|
||||
const response = await makeRestAPIRequest({
|
||||
method: 'post',
|
||||
path: `/people`,
|
||||
body: requestBody,
|
||||
});
|
||||
|
||||
const createdPerson = response.body.data.createPerson;
|
||||
|
||||
expect(createdPerson.id).toBe(PERSON_2_ID);
|
||||
expect(createdPerson.city).toBe(personCity);
|
||||
});
|
||||
|
||||
it('2.b. should return a BadRequestException when trying to create a person with an existing ID', async () => {
|
||||
const personCity = generateRecordName(PERSON_2_ID);
|
||||
const requestBody = {
|
||||
id: PERSON_2_ID,
|
||||
city: personCity,
|
||||
};
|
||||
|
||||
await makeRestAPIRequest({
|
||||
method: 'post',
|
||||
path: `/people`,
|
||||
body: requestBody,
|
||||
})
|
||||
.expect(400)
|
||||
.expect((res) => {
|
||||
expect(res.body.messages[0]).toContain(
|
||||
`duplicate key value violates unique constraint`,
|
||||
);
|
||||
expect(res.body.error).toBe('QueryFailedError');
|
||||
});
|
||||
});
|
||||
|
||||
it('2.c. should return an UnauthorizedException when no token is provided', async () => {
|
||||
await makeRestAPIRequest({
|
||||
method: 'post',
|
||||
path: `/people`,
|
||||
headers: { authorization: '' },
|
||||
body: { id: FAKE_PERSON_ID, city: 'FakeCity' },
|
||||
})
|
||||
.expect(401)
|
||||
.expect((res) => {
|
||||
expect(res.body.error).toBe('UNAUTHENTICATED');
|
||||
});
|
||||
});
|
||||
|
||||
it('2.d. should return an UnauthorizedException when an invalid token is provided', async () => {
|
||||
await makeRestAPIRequest({
|
||||
method: 'post',
|
||||
path: `/people`,
|
||||
body: { id: FAKE_PERSON_ID, city: 'FakeCity' },
|
||||
headers: { authorization: 'Bearer invalid-token' },
|
||||
})
|
||||
.expect(401)
|
||||
.expect((res) => {
|
||||
expect(res.body.error).toBe('UNAUTHENTICATED');
|
||||
});
|
||||
});
|
||||
|
||||
it('2.e. should return an UnauthorizedException when an expired token is provided', async () => {
|
||||
await makeRestAPIRequest({
|
||||
method: 'post',
|
||||
path: `/people`,
|
||||
body: { id: FAKE_PERSON_ID, city: 'FakeCity' },
|
||||
headers: { authorization: `Bearer ${EXPIRED_ACCESS_TOKEN}` },
|
||||
})
|
||||
.expect(401)
|
||||
.expect((res) => {
|
||||
expect(res.body.error).toBe('UNAUTHENTICATED');
|
||||
expect(res.body.messages[0]).toBe('Token has expired.');
|
||||
});
|
||||
});
|
||||
});
|
||||
@ -3,7 +3,6 @@ import {
|
||||
PERSON_1_ID,
|
||||
} from 'test/integration/constants/mock-person-ids.constants';
|
||||
import { PERSON_GQL_FIELDS } from 'test/integration/constants/person-gql-fields.constants';
|
||||
import { createManyOperationFactory } from 'test/integration/graphql/utils/create-many-operation-factory.util';
|
||||
import { findOneOperationFactory } from 'test/integration/graphql/utils/find-one-operation-factory.util';
|
||||
import { makeGraphqlAPIRequest } from 'test/integration/graphql/utils/make-graphql-api-request.util';
|
||||
import { makeRestAPIRequest } from 'test/integration/rest/utils/make-rest-api-request.util';
|
||||
@ -15,21 +14,15 @@ describe('Core REST API Delete One endpoint', () => {
|
||||
beforeAll(async () => {
|
||||
const personCity1 = generateRecordName(PERSON_1_ID);
|
||||
|
||||
// TODO: move this creation to REST API when the POST method is migrated
|
||||
const graphqlOperation = createManyOperationFactory({
|
||||
objectMetadataSingularName: 'person',
|
||||
objectMetadataPluralName: 'people',
|
||||
gqlFields: PERSON_GQL_FIELDS,
|
||||
data: [
|
||||
{
|
||||
id: PERSON_1_ID,
|
||||
city: personCity1,
|
||||
},
|
||||
],
|
||||
const response = await makeRestAPIRequest({
|
||||
method: 'post',
|
||||
path: '/people',
|
||||
body: {
|
||||
id: PERSON_1_ID,
|
||||
city: personCity1,
|
||||
},
|
||||
});
|
||||
|
||||
const response = await makeGraphqlAPIRequest(graphqlOperation);
|
||||
|
||||
people = response.body.data.createPeople;
|
||||
expect(people.length).toBe(1);
|
||||
expect(people[0].id).toBe(PERSON_1_ID);
|
||||
@ -55,16 +48,19 @@ describe('Core REST API Delete One endpoint', () => {
|
||||
});
|
||||
|
||||
it('1a. should delete one person', async () => {
|
||||
const response = await makeRestAPIRequest(
|
||||
'delete',
|
||||
`/people/${PERSON_1_ID}`,
|
||||
);
|
||||
const response = await makeRestAPIRequest({
|
||||
method: 'delete',
|
||||
path: `/people/${PERSON_1_ID}`,
|
||||
});
|
||||
|
||||
expect(response.body.data.deletePerson.id).toBe(PERSON_1_ID);
|
||||
});
|
||||
|
||||
it('1.b. should return a BadRequestException when trying to delete a non-existing person', async () => {
|
||||
await makeRestAPIRequest('delete', `/people/${FAKE_PERSON_ID}`)
|
||||
await makeRestAPIRequest({
|
||||
method: 'delete',
|
||||
path: `/people/${FAKE_PERSON_ID}`,
|
||||
})
|
||||
.expect(400)
|
||||
.expect((res) => {
|
||||
expect(res.body.messages[0]).toContain(
|
||||
@ -75,8 +71,12 @@ describe('Core REST API Delete One endpoint', () => {
|
||||
});
|
||||
|
||||
it('1.c. should return an UnauthorizedException when no token is provided', async () => {
|
||||
await makeRestAPIRequest('delete', `/people/${PERSON_1_ID}`, {
|
||||
authorization: '',
|
||||
await makeRestAPIRequest({
|
||||
method: 'delete',
|
||||
path: `/people/${PERSON_1_ID}`,
|
||||
headers: {
|
||||
authorization: '',
|
||||
},
|
||||
})
|
||||
.expect(401)
|
||||
.expect((res) => {
|
||||
@ -85,8 +85,12 @@ describe('Core REST API Delete One endpoint', () => {
|
||||
});
|
||||
|
||||
it('1.d. should return an UnauthorizedException when an invalid token is provided', async () => {
|
||||
await makeRestAPIRequest('delete', `/people/${PERSON_1_ID}`, {
|
||||
authorization: 'Bearer invalid-token',
|
||||
await makeRestAPIRequest({
|
||||
method: 'delete',
|
||||
path: `/people/${PERSON_1_ID}`,
|
||||
headers: {
|
||||
authorization: 'Bearer invalid-token',
|
||||
},
|
||||
})
|
||||
.expect(401)
|
||||
.expect((res) => {
|
||||
@ -95,8 +99,12 @@ describe('Core REST API Delete One endpoint', () => {
|
||||
});
|
||||
|
||||
it('1.e. should return an UnauthorizedException when an expired token is provided', async () => {
|
||||
await makeRestAPIRequest('delete', `/people/${PERSON_1_ID}`, {
|
||||
authorization: `Bearer ${EXPIRED_ACCESS_TOKEN}`,
|
||||
await makeRestAPIRequest({
|
||||
method: 'delete',
|
||||
path: `/people/${PERSON_1_ID}`,
|
||||
headers: {
|
||||
authorization: `Bearer ${EXPIRED_ACCESS_TOKEN}`,
|
||||
},
|
||||
})
|
||||
.expect(401)
|
||||
.expect((res) => {
|
||||
|
||||
@ -4,15 +4,23 @@ import request from 'supertest';
|
||||
|
||||
export type RestAPIRequestMethod = 'get' | 'post' | 'put' | 'patch' | 'delete';
|
||||
|
||||
export const makeRestAPIRequest = (
|
||||
method: RestAPIRequestMethod,
|
||||
path: string,
|
||||
headers: IncomingHttpHeaders = {},
|
||||
) => {
|
||||
interface RestAPIRequestParams {
|
||||
method: RestAPIRequestMethod;
|
||||
path: string;
|
||||
headers?: IncomingHttpHeaders;
|
||||
body?: any;
|
||||
}
|
||||
|
||||
export const makeRestAPIRequest = ({
|
||||
method,
|
||||
path,
|
||||
headers = {},
|
||||
body,
|
||||
}: RestAPIRequestParams) => {
|
||||
const client = request(`http://localhost:${APP_PORT}`);
|
||||
|
||||
return client[method]('/rest' + path)
|
||||
return client[method](`/rest${path}`)
|
||||
.set('Authorization', `Bearer ${ACCESS_TOKEN}`)
|
||||
.set({ ...headers })
|
||||
.send();
|
||||
.set(headers)
|
||||
.send(body ? JSON.stringify(body) : undefined);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user