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

Fixes and audits #351

Merged
merged 4 commits into from
Aug 1, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
38 changes: 26 additions & 12 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

65 changes: 34 additions & 31 deletions test/index.test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -1060,34 +1060,34 @@ describe("WhatsAppAPI", function () {
});

describe("Check truthy (default)", function () {
it("should fail if the form param is not a FormData instance", function () {
rejects(Whatsapp.uploadMedia(bot, {}));
it("should fail if the form param is not a FormData instance", async function () {
await rejects(Whatsapp.uploadMedia(bot, {}));

rejects(Whatsapp.uploadMedia(bot, []));
await rejects(Whatsapp.uploadMedia(bot, []));

rejects(Whatsapp.uploadMedia(bot, "Hello World"));
await rejects(Whatsapp.uploadMedia(bot, "Hello World"));
});

it("should fail if the form param does not contain a file", function () {
rejects(Whatsapp.uploadMedia(bot, new FormData()));
it("should fail if the form param does not contain a file", async function () {
await rejects(Whatsapp.uploadMedia(bot, new FormData()));
});

it("should fail if the form param contains a file with no type", function () {
it("should fail if the form param contains a file with no type", async function () {
form.append("file", new Blob(["Hello World"]));

rejects(Whatsapp.uploadMedia(bot, form));
await rejects(Whatsapp.uploadMedia(bot, form));
});

it("should fail if the file type is invalid", function () {
it("should fail if the file type is invalid", async function () {
form.append(
"file",
new Blob(["Not a real file"], { type: "random/type" })
);

rejects(Whatsapp.uploadMedia(bot, form));
await rejects(Whatsapp.uploadMedia(bot, form));
});

it("should fail if the file size is too big for the format", function () {
it("should fail if the file size is too big for the format", async function () {
const str = "I only need 500.000 chars";
form.append(
"file",
Expand All @@ -1097,7 +1097,7 @@ describe("WhatsAppAPI", function () {
)
);

rejects(Whatsapp.uploadMedia(bot, form));
await rejects(Whatsapp.uploadMedia(bot, form));
});
});

Expand Down Expand Up @@ -1596,38 +1596,41 @@ describe("WhatsAppAPI", function () {

describe("Validation", function () {
describe("Secure truthy (default)", function () {
it("should throw 400 if rawBody is missing", function () {
rejects(Whatsapp.post(valid_message_mock), threw(400));
it("should throw 400 if rawBody is missing", async function () {
await rejects(
Whatsapp.post(valid_message_mock),
threw(400)
);

rejects(
await rejects(
Whatsapp.post(valid_message_mock, undefined),
threw(400)
);
});

it("should throw 401 if signature is missing", function () {
rejects(
it("should throw 401 if signature is missing", async function () {
await rejects(
Whatsapp.post(valid_message_mock, body),
threw(401)
);

rejects(
await rejects(
Whatsapp.post(valid_message_mock, body, undefined),
threw(401)
);
});

it("should throw 500 if appSecret is not specified", function () {
it("should throw 500 if appSecret is not specified", async function () {
delete Whatsapp.appSecret;

rejects(
await rejects(
Whatsapp.post(valid_message_mock, body, signature),
threw(500)
);
});

it("should throw 401 if the signature doesn't match the hash", function () {
rejects(
it("should throw 401 if the signature doesn't match the hash", async function () {
await rejects(
Whatsapp.post(valid_message_mock, body, "wrong"),
threw(401)
);
Expand All @@ -1652,10 +1655,10 @@ describe("WhatsAppAPI", function () {
});
});

it("should throw 400 if the request isn't a valid WhatsApp Cloud API request (data.object)", function () {
it("should throw 400 if the request isn't a valid WhatsApp Cloud API request (data.object)", async function () {
Whatsapp.secure = false;

rejects(Whatsapp.post({}), threw(400));
await rejects(Whatsapp.post({}), threw(400));
});
});

Expand Down Expand Up @@ -1770,17 +1773,17 @@ describe("WhatsAppAPI", function () {
sinon_assert.calledOnce(spy_on_message);
});

it("should throw TypeError if the request is missing any data", function () {
it("should throw TypeError if the request is missing any data", async function () {
let moddedMock;

moddedMock = new MessageWebhookMock();
rejects(Whatsapp.post(moddedMock), TypeError);
await rejects(Whatsapp.post(moddedMock), TypeError);

moddedMock = new MessageWebhookMock(phoneID);
rejects(Whatsapp.post(moddedMock), TypeError);
await rejects(Whatsapp.post(moddedMock), TypeError);

moddedMock = new MessageWebhookMock(phoneID, user);
rejects(Whatsapp.post(moddedMock), TypeError);
await rejects(Whatsapp.post(moddedMock), TypeError);

// Missing name doesn't throw error
});
Expand Down Expand Up @@ -1858,14 +1861,14 @@ describe("WhatsAppAPI", function () {
sinon_assert.calledOnce(spy_on_status);
});

it("should throw TypeError if the request is missing any data", function () {
it("should throw TypeError if the request is missing any data", async function () {
let moddedMock;

moddedMock = new StatusWebhookMock();
rejects(Whatsapp.post(moddedMock), TypeError);
await rejects(Whatsapp.post(moddedMock), TypeError);

moddedMock = new StatusWebhookMock(phoneID);
rejects(Whatsapp.post(moddedMock), TypeError);
await rejects(Whatsapp.post(moddedMock), TypeError);

// In conclution, it's pointless. As soon as any of the other parameters are defined,
// the code will return undefined for the missing ones, without any error.
Expand Down
4 changes: 3 additions & 1 deletion test/webhooks.mocks.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ class MessageWebhookMock {
field: "messages",
value: {
messaging_product: "whatsapp",
messages: [{}]
// eslint-disable-next-line tsdoc/syntax
/** @type {Array<any>} */
messages: []
}
}
]
Expand Down