Skip to content

Commit

Permalink
#3044-fixed tests
Browse files Browse the repository at this point in the history
  • Loading branch information
caiodasilva2005 committed Dec 24, 2024
1 parent 1bd863a commit c94172f
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 40 deletions.
2 changes: 0 additions & 2 deletions src/backend/src/services/slack.services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,6 @@ export default class slackServices {
return;
}

console.log('processed event');

if (event.subtype === 'message_changed') {
//try to edit the announcement, if no announcement with that id exists create a new announcement
try {
Expand Down
65 changes: 32 additions & 33 deletions src/backend/tests/integration/slackMessages.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ describe('Slack message tests', () => {
let wonderwoman: User;

beforeEach(async () => {
await resetUsers();
organization = await createTestOrganization();
orgId = organization.organizationId;
batman = await createTestUser(batmanAppAdmin, orgId, batmanSettings);
Expand All @@ -49,7 +50,7 @@ describe('Slack message tests', () => {
const spy = vi.spyOn(AnnouncementService, 'createAnnouncement');

const announcement = await slackServices.processMessageSent(
createSlackMessageEvent('channel id', '1000000000000', 'user name', 'id_1', [
createSlackMessageEvent('channel id', '1', 'user name', 'id_1', [
{ type: 'text', text: 'test with ' },
{ type: 'broadcast', range: 'everyone' },
{ type: 'text', text: ' broadcast (@everyone)' }
Expand All @@ -63,15 +64,15 @@ describe('Slack message tests', () => {
expect(spy).toBeCalledWith(
'test with @everyone broadcast (@everyone)',
[organization.userCreatedId, batman.userId, superman.userId, wonderwoman.userId],
new Date(1000000000000),
new Date(1000),
'Slack User Name',
'id_1',
'Slack Channel Name',
orgId
);

expect(announcement?.text).toBe('test with @everyone broadcast (@everyone)');
expect(announcement?.dateCreated.toDateString()).toBe(new Date(1000000000000).toDateString());
expect(announcement?.dateMessageSent.toDateString()).toBe(new Date(1000).toDateString());
expect(announcement?.senderName).toBe('Slack User Name');
expect(announcement?.slackChannelName).toBe('Slack Channel Name');
expect(announcement?.slackEventId).toBe('id_1');
Expand All @@ -86,7 +87,7 @@ describe('Slack message tests', () => {
const spy = vi.spyOn(AnnouncementService, 'createAnnouncement');

const announcement = await slackServices.processMessageSent(
createSlackMessageEvent('channel id', '1000000000000', 'user name', 'id_1', [
createSlackMessageEvent('channel id', '1', 'user name', 'id_1', [
{ type: 'text', text: 'test with ' },
{ type: 'broadcast', range: 'channel' },
{ type: 'text', text: ' broadcast (@channel)' },
Expand All @@ -100,15 +101,15 @@ describe('Slack message tests', () => {
expect(spy).toBeCalledWith(
'test with @channel broadcast (@channel)@Slack User Name@Slack User Name',
[batman.userId, wonderwoman.userId, superman.userId],
new Date(1000000000000),
new Date(1000),
'Slack User Name',
'id_1',
'Slack Channel Name',
orgId
);

expect(announcement?.text).toBe('test with @channel broadcast (@channel)@Slack User Name@Slack User Name');
expect(announcement?.dateCreated.toDateString()).toBe(new Date(1000000000000).toDateString());
expect(announcement?.dateMessageSent.toDateString()).toBe(new Date(1000).toDateString());
expect(announcement?.senderName).toBe('Slack User Name');
expect(announcement?.slackChannelName).toBe('Slack Channel Name');
expect(announcement?.slackEventId).toBe('id_1');
Expand All @@ -122,7 +123,7 @@ describe('Slack message tests', () => {
const spy = vi.spyOn(AnnouncementService, 'createAnnouncement');

const announcement = await slackServices.processMessageSent(
createSlackMessageEvent('channel id', '1000000000000', 'user name', 'id_1', [
createSlackMessageEvent('channel id', '1', 'user name', 'id_1', [
{ type: 'text', text: 'test with ' },
{ type: 'user', user_id: 'slackWW' },
{ type: 'text', text: ' broadcast (@wonderwoman)' }
Expand All @@ -134,15 +135,15 @@ describe('Slack message tests', () => {
expect(spy).toBeCalledWith(
'test with @Slack User Name broadcast (@wonderwoman)',
[wonderwoman.userId],
new Date(1000000000000),
new Date(1000),
'Slack User Name',
'id_1',
'Slack Channel Name',
orgId
);

expect(announcement?.text).toBe('test with @Slack User Name broadcast (@wonderwoman)');
expect(announcement?.dateCreated.toDateString()).toBe(new Date(1000000000000).toDateString());
expect(announcement?.dateMessageSent.toDateString()).toBe(new Date(1000).toDateString());
expect(announcement?.senderName).toBe('Slack User Name');
expect(announcement?.slackChannelName).toBe('Slack Channel Name');
expect(announcement?.slackEventId).toBe('id_1');
Expand All @@ -156,7 +157,7 @@ describe('Slack message tests', () => {
const spy = vi.spyOn(AnnouncementService, 'createAnnouncement');

const announcement = await slackServices.processMessageSent(
createSlackMessageEvent('channel id', '1000000000000', 'user name', 'id_1', [
createSlackMessageEvent('channel id', '1', 'user name', 'id_1', [
{ type: 'text', text: 'test with: ' },
{ type: 'link', url: 'http://www.example.com', text: 'link' },
{ type: 'text', text: 'Italics', style: { italic: true } },
Expand All @@ -180,7 +181,7 @@ describe('Slack message tests', () => {
expect(spy).toBeCalledWith(
'test with: link:(http://www.example.com)Italics and a unicode emoji: 😝 and a slack emoji: emoji:birthday-parrot@Slack User Name',
[wonderwoman.userId],
new Date(1000000000000),
new Date(1000),
'Slack User Name',
'id_1',
'Slack Channel Name',
Expand All @@ -190,7 +191,7 @@ describe('Slack message tests', () => {
expect(announcement?.text).toBe(
'test with: link:(http://www.example.com)Italics and a unicode emoji: 😝 and a slack emoji: emoji:birthday-parrot@Slack User Name'
);
expect(announcement?.dateCreated.toDateString()).toBe(new Date(1000000000000).toDateString());
expect(announcement?.dateMessageSent.toDateString()).toBe(new Date(1000).toDateString());
expect(announcement?.senderName).toBe('Slack User Name');
expect(announcement?.slackChannelName).toBe('Slack Channel Name');
expect(announcement?.slackEventId).toBe('id_1');
Expand All @@ -204,9 +205,7 @@ describe('Slack message tests', () => {
const spy = vi.spyOn(AnnouncementService, 'createAnnouncement');

const announcement = await slackServices.processMessageSent(
createSlackMessageEvent('channel id', '1000000000000', 'user name', 'id_1', [
{ type: 'text', text: 'just a text message' }
]),
createSlackMessageEvent('channel id', '1', 'user name', 'id_1', [{ type: 'text', text: 'just a text message' }]),
orgId
);

Expand All @@ -223,13 +222,13 @@ describe('Slack message tests', () => {
const updateSpy = vi.spyOn(AnnouncementService, 'updateAnnouncement');

await slackServices.processMessageSent(
createSlackMessageEvent('channel id', '1000000000000', 'user name', 'id_1', [{ type: 'user', user_id: 'slackWW' }]),
createSlackMessageEvent('channel id', '1', 'user name', 'id_1', [{ type: 'user', user_id: 'slackWW' }]),
orgId
);
expect(createSpy).toBeCalledWith(
'@Slack User Name',
[wonderwoman.userId],
new Date(1000000000000),
new Date(1000),
'Slack User Name',
'id_1',
'Slack Channel Name',
Expand All @@ -241,13 +240,13 @@ describe('Slack message tests', () => {
type: 'message',
subtype: 'message_changed',
channel: 'channel id',
event_ts: '1000000000000',
event_ts: '1',
channel_type: 'channel',
message: createSlackMessageEvent('channel id', '1000000000000', 'user name', 'id_1', [
message: createSlackMessageEvent('channel id', '1', 'user name', 'id_1', [
{ type: 'user', user_id: 'slackWW' },
{ type: 'text', text: ' added text' }
]),
previous_message: createSlackMessageEvent('channel id', '1000000000000', 'user name', 'id_1', [
previous_message: createSlackMessageEvent('channel id', '1', 'user name', 'id_1', [
{ type: 'user', user_id: 'slackWW' }
])
},
Expand All @@ -257,15 +256,15 @@ describe('Slack message tests', () => {
expect(updateSpy).toBeCalledWith(
'@Slack User Name added text',
[wonderwoman.userId],
new Date(1000000000000),
new Date(1000),
'Slack User Name',
'id_1',
'Slack Channel Name',
orgId
);

expect(announcement2?.text).toBe('@Slack User Name added text');
expect(announcement2?.dateCreated.toDateString()).toBe(new Date(1000000000000).toDateString());
expect(announcement2?.dateMessageSent.toDateString()).toBe(new Date(1000).toDateString());
expect(announcement2?.senderName).toBe('Slack User Name');
expect(announcement2?.slackChannelName).toBe('Slack Channel Name');
expect(announcement2?.slackEventId).toBe('id_1');
Expand All @@ -287,13 +286,13 @@ describe('Slack message tests', () => {
type: 'message',
subtype: 'message_changed',
channel: 'channel id',
event_ts: '1000000000000',
event_ts: '1',
channel_type: 'channel',
message: createSlackMessageEvent('channel id', '1000000000000', 'user name', 'id_1', [
message: createSlackMessageEvent('channel id', '1', 'user name', 'id_1', [
{ type: 'user', user_id: 'slackWW' },
{ type: 'text', text: ' added text' }
]),
previous_message: createSlackMessageEvent('channel id', '1000000000000', 'user name', 'id_1', [
previous_message: createSlackMessageEvent('channel id', '1', 'user name', 'id_1', [
{ type: 'user', user_id: 'slackWW' }
])
},
Expand All @@ -303,7 +302,7 @@ describe('Slack message tests', () => {
expect(updateSpy).toBeCalledWith(
'@Slack User Name added text',
[wonderwoman.userId],
new Date(1000000000000),
new Date(1000),
'Slack User Name',
'id_1',
'Slack Channel Name',
Expand All @@ -313,15 +312,15 @@ describe('Slack message tests', () => {
expect(createSpy).toBeCalledWith(
'@Slack User Name added text',
[wonderwoman.userId],
new Date(1000000000000),
new Date(1000),
'Slack User Name',
'id_1',
'Slack Channel Name',
orgId
);

expect(announcement2?.text).toBe('@Slack User Name added text');
expect(announcement2?.dateCreated.toDateString()).toBe(new Date(1000000000000).toDateString());
expect(announcement2?.dateMessageSent.toDateString()).toBe(new Date(1000).toDateString());
expect(announcement2?.senderName).toBe('Slack User Name');
expect(announcement2?.slackChannelName).toBe('Slack Channel Name');
expect(announcement2?.slackEventId).toBe('id_1');
Expand All @@ -339,13 +338,13 @@ describe('Slack message tests', () => {
const deleteSpy = vi.spyOn(AnnouncementService, 'deleteAnnouncement');

await slackServices.processMessageSent(
createSlackMessageEvent('channel id', '1000000000000', 'user name', 'id_1', [{ type: 'user', user_id: 'slackWW' }]),
createSlackMessageEvent('channel id', '1', 'user name', 'id_1', [{ type: 'user', user_id: 'slackWW' }]),
orgId
);
expect(createSpy).toBeCalledWith(
'@Slack User Name',
[wonderwoman.userId],
new Date(1000000000000),
new Date(1000),
'Slack User Name',
'id_1',
'Slack Channel Name',
Expand All @@ -357,9 +356,9 @@ describe('Slack message tests', () => {
type: 'message',
subtype: 'message_deleted',
channel: 'channel id',
event_ts: '1000000000000',
event_ts: '1',
channel_type: 'channel',
previous_message: createSlackMessageEvent('channel id', '1000000000000', 'user name', 'id_1', [
previous_message: createSlackMessageEvent('channel id', '1', 'user name', 'id_1', [
{ type: 'user', user_id: 'slackWW' }
])
},
Expand All @@ -381,7 +380,7 @@ describe('Slack message tests', () => {
type: 'message',
subtype: 'other-nonprocessed-subtype',
channel: 'channel id',
event_ts: '1000000000000',
event_ts: '1',
channel_type: 'channel',
bogus_data: 'other data'
},
Expand Down
45 changes: 40 additions & 5 deletions src/backend/tests/unit/announcements.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ describe('announcement tests', () => {
let wonderwoman: User;

beforeEach(async () => {
await resetUsers();
organization = await createTestOrganization();
orgId = organization.organizationId;
batman = await createTestUser(batmanAppAdmin, orgId, batmanSettings);
Expand Down Expand Up @@ -157,10 +158,9 @@ describe('announcement tests', () => {

describe('Get Announcements', () => {
it('Succeeds and gets user announcements', async () => {
const testBatman = await createTestUser(batmanAppAdmin, orgId);
await AnnouncementService.createAnnouncement(
'test1',
[testBatman.userId],
[batman.userId],
new Date(),
'Thomas Emrax',
'1',
Expand All @@ -169,22 +169,57 @@ describe('announcement tests', () => {
);
await AnnouncementService.createAnnouncement(
'test2',
[testBatman.userId],
[batman.userId],
new Date(),
'Superman',
'50',
'mechanical',
organization.organizationId
);

const announcements = await AnnouncementService.getUserUnreadAnnouncements(
testBatman.userId,
const announcements = await AnnouncementService.getUserUnreadAnnouncements(batman.userId, organization.organizationId);

expect(announcements).toHaveLength(2);
expect(announcements[0].text).toBe('test1');
expect(announcements[1].text).toBe('test2');
});
});

describe('Remove Announcement', () => {
it('Succeeds and removes user announcement', async () => {
await AnnouncementService.createAnnouncement(
'test1',
[batman.userId],
new Date(),
'Thomas Emrax',
'1',
'software',
organization.organizationId
);
await AnnouncementService.createAnnouncement(
'test2',
[batman.userId],
new Date(),
'Superman',
'50',
'mechanical',
organization.organizationId
);

const announcements = await AnnouncementService.getUserUnreadAnnouncements(batman.userId, organization.organizationId);

expect(announcements).toHaveLength(2);
expect(announcements[0].text).toBe('test1');
expect(announcements[1].text).toBe('test2');

const updatedAnnouncements = await AnnouncementService.removeUserAnnouncement(
batman.userId,
announcements[0].announcementId,
organization.organizationId
);

expect(updatedAnnouncements).toHaveLength(1);
expect(updatedAnnouncements[0].text).toBe('test2');
});
});
});

0 comments on commit c94172f

Please sign in to comment.