forked from PluralFlux/PluralFlux
* refactored async/await for import helper to not also use then/catch * added enum * refactor webhookHelper and tests to not use then/catch * changed docstring * refactoring bot and tests to not use then/catch * refactoring commands.js and tests to not use then/catch * refactoring memberHelper.js and tests to not use then/catch * removing then/catch from messageHelper.test.js * fixed set up for commands tests * edited bot to have top level main function * one more test in commands.js, and removed console.error * fixed typo in webhookHelper * forgot to switch over some tests in bot.test and commands.test * removed console.log from import helper * put console.error in commands * converted utils.js to not use then/catch * tested utils checkImageFormatValidity * removed jest-fetch-mock since it turns out I was just manually mocking it anyway * refactored database to not use then/catch * added dash to commands.js and test to pass * added the remaining webhook tests * changed utils to check for 10MB size not 1MB * removed unnecessary try/catch from utils * Simplify getWebhook to use .find() instead of foreach logic * make memberCommand exit when error occurs with parseMemberCommand * changed commands.js to not have user interaction within the catch * updated console.error message in database.js * made importHelper mock throw error instead of "resolve" error * replaced "pk;" with "pf;" in test * Got rid of unnecessary check for empty message from user (Fluxer doesn't allow this to happen) Removed export of token * getAllMembersInfo checks for fields.length * added default case to memberCommandHandler to throw error if command is not recognized * reversed check for valid proxy (was returning valid if the proxy existed and invalid if it didn't) * pushes e.message instead of full error object to errors array in importHelper * adjusted tests to properly use mockRejectedValue for async rejections * changed getAllMembersInfo map to say `index` not `name` as it actually gets the index of a member and then the member object * adjusted importHelper to properly test throwing of aggregate error * revamped setting of expiration warning (moved to utils and changed logic, wrote tests) --------- Co-authored-by: Aster Fialla <asterfialla@gmail.com>
103 lines
4.0 KiB
JavaScript
103 lines
4.0 KiB
JavaScript
const {enums} = require('../../src/enums.js');
|
|
|
|
jest.mock('../../src/helpers/memberHelper.js', () => {
|
|
return {
|
|
memberHelper: {
|
|
addFullMember: jest.fn()
|
|
}
|
|
}
|
|
})
|
|
|
|
const {memberHelper} = require("../../src/helpers/memberHelper.js");
|
|
const {importHelper} = require('../../src/helpers/importHelper.js');
|
|
|
|
describe('importHelper', () => {
|
|
const authorId = '123';
|
|
const attachmentUrl = 'system.json';
|
|
const mockImportedMember = {
|
|
proxy_tags: [{
|
|
prefix: "SP{",
|
|
suffix: "}"
|
|
}],
|
|
display_name: "SomePerson",
|
|
avatar_url: 'oya.png',
|
|
name: 'somePerson'
|
|
}
|
|
const mockData = {
|
|
members: [mockImportedMember]
|
|
};
|
|
const mockAddReturnMember = {
|
|
proxy: "SP{text}",
|
|
displayname: "SomePerson",
|
|
propic: 'oya.png',
|
|
name: 'somePerson'
|
|
}
|
|
const mockAddReturn = {
|
|
member: mockAddReturnMember,
|
|
errors: []
|
|
}
|
|
|
|
beforeEach(() => {
|
|
jest.resetModules();
|
|
jest.clearAllMocks();
|
|
global.fetch = jest.fn().mockResolvedValue({
|
|
ok: true,
|
|
json: () => Promise.resolve(mockData)
|
|
})
|
|
})
|
|
|
|
describe('pluralKitImport', () => {
|
|
|
|
test('if no attachment URL, throws error', async () => {
|
|
await expect(importHelper.pluralKitImport(authorId)).rejects.toThrow(enums.err.NOT_JSON_FILE);
|
|
})
|
|
|
|
test('if attachment URL, calls fetch and addFullMember and returns value', async () => {
|
|
memberHelper.addFullMember.mockResolvedValue(mockAddReturn);
|
|
const result = await importHelper.pluralKitImport(authorId, attachmentUrl);
|
|
|
|
expect(fetch).toHaveBeenCalledTimes(1);
|
|
expect(fetch).toHaveBeenCalledWith(attachmentUrl);
|
|
expect(memberHelper.addFullMember).toHaveBeenCalledWith(authorId, mockImportedMember.name, mockImportedMember.display_name, 'SP{text}', mockImportedMember.avatar_url);
|
|
expect(result).toEqual(`Successfully added members: ${mockAddReturnMember.name}`)
|
|
})
|
|
|
|
|
|
test('if fetch fails, throws error', async () => {
|
|
global.fetch = jest.fn().mockRejectedValue("can't get");
|
|
await expect(importHelper.pluralKitImport(authorId, attachmentUrl)).rejects.toThrow(enums.err.CANNOT_FETCH_RESOURCE, "can't get file");
|
|
})
|
|
|
|
test('if json conversion fails, throws error', async () => {
|
|
global.fetch = jest.fn().mockResolvedValue({
|
|
ok: true,
|
|
json: () => Promise.reject("not json")
|
|
})
|
|
await expect(importHelper.pluralKitImport(authorId, attachmentUrl)).rejects.toThrow(enums.err.NOT_JSON_FILE, "not json");
|
|
})
|
|
|
|
test('if addFullMember returns nothing, return correct enum', async () => {
|
|
memberHelper.addFullMember.mockResolvedValue();
|
|
const promise = importHelper.pluralKitImport(authorId, attachmentUrl);
|
|
await expect(promise).rejects.toBeInstanceOf(AggregateError);
|
|
await expect(promise).rejects.toMatchObject(AggregateError([], enums.err.NO_MEMBERS_IMPORTED));
|
|
})
|
|
|
|
test('if addFullMember throws error, catch and return error', async () => {
|
|
memberHelper.addFullMember.mockRejectedValue(new Error('error'));
|
|
await expect(importHelper.pluralKitImport(authorId, attachmentUrl)).rejects.toMatchObject(new AggregateError(['error'], enums.err.NO_MEMBERS_IMPORTED));
|
|
});
|
|
|
|
test('if addFullMember returns member but also contains error, return member and error', async () => {
|
|
// Arrange
|
|
const memberObj = {errors: ['error'], member: mockAddReturnMember};
|
|
memberHelper.addFullMember.mockResolvedValue(memberObj);
|
|
await expect(importHelper.pluralKitImport(authorId, attachmentUrl)).rejects.toMatchObject(new AggregateError(['error'], `Successfully added members: ${mockAddReturnMember.name}`));
|
|
});
|
|
});
|
|
|
|
afterEach(() => {
|
|
// restore the spy created with spyOn
|
|
jest.clearAllMocks();
|
|
});
|
|
}) |