Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BC-6757 - Delete etherpad #4951

Merged
merged 18 commits into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
147 changes: 133 additions & 14 deletions apps/server/src/infra/etherpad-client/etherpad-client.adapter.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { createMock, DeepMocked } from '@golevelup/ts-jest';
import { InternalServerErrorException } from '@nestjs/common';
import { Test, TestingModule } from '@nestjs/testing';
import { AxiosResponse } from 'axios';
import {
Expand All @@ -13,7 +14,7 @@ import {
SessionApi,
} from './etherpad-api-client';
import { EtherpadClientAdapter } from './etherpad-client.adapter';
import { EtherpadErrorType } from './interface';
import { EtherpadErrorType, EtherpadResponseCode } from './interface';
import { EtherpadErrorLoggableException } from './loggable';

describe(EtherpadClientAdapter.name, () => {
Expand Down Expand Up @@ -67,6 +68,7 @@ describe(EtherpadClientAdapter.name, () => {
const username = 'username';
const response = createMock<AxiosResponse<InlineResponse2003>>({
data: {
code: EtherpadResponseCode.OK,
data: { authorID: 'authorId' },
},
});
Expand Down Expand Up @@ -98,6 +100,7 @@ describe(EtherpadClientAdapter.name, () => {
const username = 'username';
const response = createMock<AxiosResponse<InlineResponse2003>>({
data: {
code: EtherpadResponseCode.OK,
data: {},
},
});
Expand Down Expand Up @@ -142,12 +145,14 @@ describe(EtherpadClientAdapter.name, () => {
const sessionCookieExpire = new Date();
const response = createMock<AxiosResponse<InlineResponse2004>>({
data: {
code: EtherpadResponseCode.OK,
data: { sessionID: 'sessionId' },
},
});

const listSessionsResponse = createMock<AxiosResponse<InlineResponse2006>>({
data: {
code: EtherpadResponseCode.OK,
data: {
// @ts-expect-error wrong type mapping
'session-id-1': { groupID: groupId, authorID: authorId },
Expand Down Expand Up @@ -187,12 +192,14 @@ describe(EtherpadClientAdapter.name, () => {
const sessionCookieExpire = new Date();
const response = createMock<AxiosResponse<InlineResponse2004>>({
data: {
code: EtherpadResponseCode.OK,
data: { sessionID: 'sessionId' },
},
});

const listSessionsResponse = createMock<AxiosResponse<InlineResponse2006>>({
data: {
code: EtherpadResponseCode.OK,
data: {},
},
});
Expand Down Expand Up @@ -231,6 +238,7 @@ describe(EtherpadClientAdapter.name, () => {
const sessionCookieExpire = new Date();
const listSessionsResponse = createMock<AxiosResponse<InlineResponse2006>>({
data: {
code: EtherpadResponseCode.OK,
data: {},
},
});
Expand All @@ -239,6 +247,7 @@ describe(EtherpadClientAdapter.name, () => {

const response = createMock<AxiosResponse<InlineResponse2004>>({
data: {
code: EtherpadResponseCode.OK,
data: {},
},
});
Expand All @@ -264,6 +273,7 @@ describe(EtherpadClientAdapter.name, () => {
const sessionCookieExpire = new Date();
const listSessionsResponse = createMock<AxiosResponse<InlineResponse2006>>({
data: {
code: EtherpadResponseCode.OK,
data: {},
},
});
Expand All @@ -290,6 +300,7 @@ describe(EtherpadClientAdapter.name, () => {
const authorId = 'authorId';
const response = createMock<AxiosResponse<InlineResponse2006>>({
data: {
code: EtherpadResponseCode.OK,
// @ts-expect-error wrong type mapping
data: { 'session-id-1': { groupID: 'groupId', authorID: authorId } },
},
Expand All @@ -313,6 +324,7 @@ describe(EtherpadClientAdapter.name, () => {
const authorId = 'authorId';
const response = createMock<AxiosResponse<InlineResponse2006>>({
data: {
code: EtherpadResponseCode.OK,
data: {},
},
});
Expand Down Expand Up @@ -345,6 +357,30 @@ describe(EtherpadClientAdapter.name, () => {
await expect(service.listSessionIdsOfAuthor(authorId)).rejects.toThrowError(EtherpadErrorLoggableException);
});
});

describe('when InlineResponse2006Data is not an object', () => {
const setup = () => {
const authorId = 'authorId';
const response = createMock<AxiosResponse<InlineResponse2006>>({
data: {
code: EtherpadResponseCode.OK,
// @ts-expect-error wrong type mapping
data: [],
},
});

authorApi.listSessionsOfAuthorUsingGET.mockResolvedValue(response);
return authorId;
};

it('should throw an error', async () => {
const authorId = setup();

await expect(service.listSessionIdsOfAuthor(authorId)).rejects.toThrowError(
'Etherpad session ids response is not an object'
);
});
});
});

describe('getOrCreateGroupId', () => {
Expand All @@ -353,6 +389,7 @@ describe(EtherpadClientAdapter.name, () => {
const parentId = 'parentId';
const response = createMock<AxiosResponse<InlineResponse200>>({
data: {
code: EtherpadResponseCode.OK,
data: { groupID: 'groupId' },
},
});
Expand Down Expand Up @@ -382,7 +419,7 @@ describe(EtherpadClientAdapter.name, () => {
const setup = () => {
const parentId = 'parentId';
const response = createMock<AxiosResponse<InlineResponse200>>({
data: {},
data: { code: EtherpadResponseCode.OK },
});

groupApi.createGroupIfNotExistsForUsingGET.mockResolvedValue(response);
Expand Down Expand Up @@ -420,12 +457,14 @@ describe(EtherpadClientAdapter.name, () => {
const parentId = 'parentId';
const response = createMock<AxiosResponse<InlineResponse2001>>({
data: {
code: EtherpadResponseCode.OK,
data: { padID: 'padId' },
},
});

const listPadsResponse = createMock<AxiosResponse<InlineResponse2002>>({
data: {
code: EtherpadResponseCode.OK,
data: { padIDs: [] },
},
});
Expand Down Expand Up @@ -458,6 +497,7 @@ describe(EtherpadClientAdapter.name, () => {
const parentId = 'parentId';
const response = createMock<AxiosResponse<InlineResponse2002>>({
data: {
code: EtherpadResponseCode.OK,
data: { padIDs: ['groupId$parentId'] },
},
});
Expand Down Expand Up @@ -489,11 +529,13 @@ describe(EtherpadClientAdapter.name, () => {
const parentId = 'parentId';
const listPadsResponse = createMock<AxiosResponse<InlineResponse2002>>({
data: {
code: EtherpadResponseCode.OK,
data: { padIDs: [] },
},
});
const response = createMock<AxiosResponse<InlineResponse2001>>({
data: {
code: EtherpadResponseCode.OK,
data: {},
},
});
Expand All @@ -516,6 +558,7 @@ describe(EtherpadClientAdapter.name, () => {
const parentId = 'parentId';
const listPadsResponse = createMock<AxiosResponse<InlineResponse2002>>({
data: {
code: EtherpadResponseCode.OK,
data: { padIDs: [] },
},
});
Expand Down Expand Up @@ -555,8 +598,73 @@ describe(EtherpadClientAdapter.name, () => {
});
});

describe('handleResponse', () => {
const setup = (code = 0) => {
describe('deleteGroup', () => {
describe('when deleteGroupUsingPOST returns successfull', () => {
const setup = () => {
const groupId = 'groupId';
const response = createMock<AxiosResponse<InlineResponse2001>>({
data: {
code: EtherpadResponseCode.OK,
data: {},
},
});

groupApi.deleteGroupUsingPOST.mockResolvedValue(response);

return groupId;
};

it('should call deletePadUsingGET with correct params', async () => {
const groupId = setup();

await service.deleteGroup(groupId);

expect(groupApi.deleteGroupUsingPOST).toBeCalledWith(groupId);
});
});

describe('when deleteGroupUsingPOST returns etherpad error code', () => {
const setup = () => {
const groupId = 'groupId';
const response = createMock<AxiosResponse<InlineResponse2001>>({
data: {
code: EtherpadResponseCode.BAD_REQUEST,
data: {},
},
});

groupApi.deleteGroupUsingPOST.mockResolvedValue(response);

return groupId;
};

it('should throw EtherpadErrorLoggableException', async () => {
const groupId = setup();

const exception = new EtherpadErrorLoggableException(EtherpadErrorType.BAD_REQUEST, { padId: groupId }, {});
await expect(service.deleteGroup(groupId)).rejects.toThrowError(exception);
});
});

describe('when deleteGroupUsingPOST returns error', () => {
const setup = () => {
const groupId = 'padId';

groupApi.deleteGroupUsingPOST.mockRejectedValueOnce(new Error('error'));

return groupId;
};

it('should throw EtherpadErrorLoggableException', async () => {
const groupId = setup();

await expect(service.deleteGroup(groupId)).rejects.toThrowError(EtherpadErrorLoggableException);
});
});
});

describe('handleEtherpadResponse', () => {
const setup = (code: number) => {
const parentId = 'parentId';
const response = createMock<AxiosResponse<InlineResponse200>>({
data: {
Expand All @@ -569,19 +677,19 @@ describe(EtherpadClientAdapter.name, () => {
return parentId;
};

describe('wehn status code is 0', () => {
describe('wehn status code is EtherpadResponseCode.OK', () => {
it('should return data', async () => {
const parentId = setup();
const parentId = setup(EtherpadResponseCode.OK);

const result = await service.getOrCreateGroupId(parentId);

expect(result).toEqual('groupId');
});
});

describe('when status code is 1', () => {
describe('when status code is BAD_REQUEST', () => {
it('should throw an error', async () => {
const parentId = setup(1);
const parentId = setup(EtherpadResponseCode.BAD_REQUEST);

const result = service.getOrCreateGroupId(parentId);

Expand All @@ -590,9 +698,9 @@ describe(EtherpadClientAdapter.name, () => {
});
});

describe('when status code is 2', () => {
describe('when status code is INTERNAL_ERROR', () => {
it('should throw an error', async () => {
const parentId = setup(2);
const parentId = setup(EtherpadResponseCode.INTERNAL_ERROR);

const result = service.getOrCreateGroupId(parentId);

Expand All @@ -601,9 +709,9 @@ describe(EtherpadClientAdapter.name, () => {
});
});

describe('when status code is 3', () => {
describe('when status code is FUNCTION_NOT_FOUND', () => {
it('should throw an error', async () => {
const parentId = setup(3);
const parentId = setup(EtherpadResponseCode.FUNCTION_NOT_FOUND);

const result = service.getOrCreateGroupId(parentId);

Expand All @@ -612,15 +720,26 @@ describe(EtherpadClientAdapter.name, () => {
});
});

describe('when status code is 4', () => {
describe('when status code is WRONG_API_KEY', () => {
it('should throw an error', async () => {
const parentId = setup(4);
const parentId = setup(EtherpadResponseCode.WRONG_API_KEY);

const result = service.getOrCreateGroupId(parentId);

await expect(result).rejects.toThrowError(EtherpadErrorType.WRONG_API_KEY);
await expect(result).rejects.toThrowError(EtherpadErrorLoggableException);
});
});

describe('when status code is other than known code', () => {
it('should throw an error', async () => {
const parentId = setup(5);

const result = service.getOrCreateGroupId(parentId);

const exception = new InternalServerErrorException('Etherpad response code unknown');
await expect(result).rejects.toThrowError(exception);
});
});
});
});
Loading
Loading