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

test-case-save-db-verify #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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: 35 additions & 3 deletions tests/browser-tests/testWebhookWhatsapp.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
const { expect } = require('chai');
const testBase = require('./testBase.js');

// Function to generate a unique ProfileName
function generateUniqueProfileName() {
return `ProfileName-${Date.now()}-${Math.floor(Math.random() * 10000)}`;
}

it("If account ssid different then message should get saved to a file but shouldn't get stored in database", async function() {
console.log('Testing ' + __filename);
try {
console.log("send message to /webhook/whatsapp");
// Generate a unique ProfileName
const uniqueProfileName = generateUniqueProfileName();

const response = await fetch('http://node:8080/webhook/whatsapp', {
method: 'POST',
headers: {
Expand All @@ -14,7 +22,7 @@ it("If account ssid different then message should get saved to a file but should
AccountSid: 'testUser',
SmsMessageSid: 'SMXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
NumMedia: '0',
ProfileName: 'Profile Name',
ProfileName: uniqueProfileName,
MessageType: 'text',
SmsSid: 'SMXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
WaId: '1234567890',
Expand All @@ -39,6 +47,12 @@ it("If account ssid different then message should get saved to a file but should
console.log("Confirm that Message saved to file");
expect(content).to.include('Test message');

console.log("Confirm that Message NOT saved to database");
// Query for the document by the non-existing ProfileName
const result = await app.c("webhookWhatsApp").collection().findOne({ ProfileName: uniqueProfileName });
// Check that the document does not exist
expect(result).to.be.null;

console.log("Ensuring that account ssid id is different then we should get 403");
// Assert status and message
expect(response.status).to.equal(403);
Expand All @@ -55,6 +69,9 @@ it("If account ssid same as message then message should get saved to a file and

console.log('Testing ' + __filename);
try {
// Generate a unique ProfileName
const uniqueProfileName = generateUniqueProfileName();

console.log("send message to /webhook/whatsapp");
const response = await fetch('http://node:8080/webhook/whatsapp', {
method: 'POST',
Expand All @@ -65,7 +82,7 @@ it("If account ssid same as message then message should get saved to a file and
AccountSid: twilioUser,
SmsMessageSid: 'SMXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
NumMedia: '0',
ProfileName: 'Profile Name',
ProfileName: uniqueProfileName,
MessageType: 'text',
SmsSid: 'SMXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
WaId: '1234567890',
Expand All @@ -92,6 +109,12 @@ it("If account ssid same as message then message should get saved to a file and
console.log("Ensuring that account ssid id is same then we should get 200");
// Assert status and message
expect(response.status).to.equal(200);

console.log("Confirm that Message saved to database");
const result = await app.c("webhookWhatsApp").collection().findOne({ ProfileName: uniqueProfileName });
expect(result).to.include(uniqueProfileName);
// Clean up by removing the document after the test
await app.c("webhookWhatsApp").collection().deleteOne({ ProfileName: uniqueProfileName });
}
catch (error) {
console.log(error);
Expand All @@ -104,6 +127,9 @@ it("If account ssid undefined then message shouldn't stored in db", async functi
console.log('Testing ' + __filename);
try {
console.log("send message to /webhook/whatsapp");
// Generate a unique ProfileName
const uniqueProfileName = generateUniqueProfileName();

const response = await fetch('http://node:8080/webhook/whatsapp', {
method: 'POST',
headers: {
Expand All @@ -112,7 +138,7 @@ it("If account ssid undefined then message shouldn't stored in db", async functi
body: JSON.stringify({
SmsMessageSid: 'SMXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
NumMedia: '0',
ProfileName: 'Profile Name',
ProfileName: uniqueProfileName,
MessageType: 'text',
SmsSid: 'SMXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
WaId: '1234567890',
Expand All @@ -127,6 +153,12 @@ it("If account ssid undefined then message shouldn't stored in db", async functi
})
});

console.log("Confirm that Message NOT saved to database");
// Query for the document by the non-existing ProfileName
const result = await app.c("webhookWhatsApp").collection().findOne({ ProfileName: uniqueProfileName });
// Check that the document does not exist
expect(result).to.be.null;

console.log("Ensuring account ssid is undefined then we should get 403");
// Assert status and message
expect(response.status).to.equal(403);
Expand Down