signal-desktop/ts/test-node/jobs/helpers/addReportSpamJob_test.ts

98 lines
2.6 KiB
TypeScript
Raw Normal View History

// Copyright 2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import * as sinon from 'sinon';
import { Job } from '../../../jobs/Job';
import { addReportSpamJob } from '../../../jobs/helpers/addReportSpamJob';
2024-03-12 16:29:31 +00:00
import type { ConversationType } from '../../../state/ducks/conversations';
import { getDefaultConversation } from '../../../test-both/helpers/getDefaultConversation';
describe('addReportSpamJob', () => {
let getMessageServerGuidsForSpam: sinon.SinonStub;
let jobQueue: { add: sinon.SinonStub };
2024-03-12 16:29:31 +00:00
const conversation: ConversationType = getDefaultConversation();
2023-02-08 00:55:12 +00:00
beforeEach(() => {
getMessageServerGuidsForSpam = sinon.stub().resolves(['abc', 'xyz']);
jobQueue = {
add: sinon
.stub()
.callsFake(
async data =>
new Job<unknown>(
'fake-job-id',
Date.now(),
'fake job queue type',
data,
Promise.resolve()
)
),
};
});
2023-08-16 20:54:39 +00:00
it('does nothing if the conversation lacks a serviceId', async () => {
await addReportSpamJob({
2023-02-08 00:55:12 +00:00
conversation: {
...conversation,
2023-08-16 20:54:39 +00:00
serviceId: undefined,
2023-02-08 00:55:12 +00:00
},
getMessageServerGuidsForSpam,
jobQueue,
});
sinon.assert.notCalled(getMessageServerGuidsForSpam);
sinon.assert.notCalled(jobQueue.add);
});
it("doesn't enqueue a job if there are no messages with server GUIDs", async () => {
getMessageServerGuidsForSpam.resolves([]);
await addReportSpamJob({
2023-02-08 00:55:12 +00:00
conversation,
getMessageServerGuidsForSpam,
jobQueue,
});
sinon.assert.notCalled(jobQueue.add);
});
2023-02-08 00:55:12 +00:00
it('enqueues a job without a token', async () => {
await addReportSpamJob({
conversation,
getMessageServerGuidsForSpam,
jobQueue,
});
sinon.assert.calledOnce(getMessageServerGuidsForSpam);
sinon.assert.calledWith(getMessageServerGuidsForSpam, conversation.id);
sinon.assert.calledOnce(jobQueue.add);
sinon.assert.calledWith(jobQueue.add, {
2023-08-16 20:54:39 +00:00
aci: conversation.serviceId,
serverGuids: ['abc', 'xyz'],
2023-02-08 00:55:12 +00:00
token: undefined,
});
});
it('enqueues a job with a token', async () => {
await addReportSpamJob({
conversation: {
...conversation,
reportingToken: 'uvw',
},
getMessageServerGuidsForSpam,
jobQueue,
});
sinon.assert.calledOnce(getMessageServerGuidsForSpam);
sinon.assert.calledWith(getMessageServerGuidsForSpam, conversation.id);
sinon.assert.calledOnce(jobQueue.add);
sinon.assert.calledWith(jobQueue.add, {
2023-08-16 20:54:39 +00:00
aci: conversation.serviceId,
2023-02-08 00:55:12 +00:00
serverGuids: ['abc', 'xyz'],
token: 'uvw',
});
});
});