Skip to content

Commit

Permalink
Giving webhook tests (#47)
Browse files Browse the repository at this point in the history
* Add webhook tests

* Update expected title
  • Loading branch information
gcatanese authored Sep 19, 2023
1 parent f1e10b0 commit e8181bc
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 2 deletions.
2 changes: 1 addition & 1 deletion tests/giving/card-and-donation-cancel.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const utilities = require('../utilities');
test('Card and Cancel', async ({ page }) => {
await page.goto('/');

await expect(page).toHaveTitle(/Adyen Giving Demo - Select type/);
await expect(page).toHaveTitle(/Adyen Giving Demo/);

// Select "Card"
await page.getByRole('link', { name: 'Card', exact: true }).click();
Expand Down
2 changes: 1 addition & 1 deletion tests/giving/card-and-donation.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const utilities = require('../utilities');
test('Card and donate', async ({ page }) => {
await page.goto('/');

await expect(page).toHaveTitle(/Adyen Giving Demo - Select type/);
await expect(page).toHaveTitle(/Adyen Giving Demo/);

// Select "Card"
await page.getByRole('link', { name: 'Card', exact: true }).click();
Expand Down
42 changes: 42 additions & 0 deletions tests/giving/webhook-failure.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// @ts-check
const { test, expect } = require('@playwright/test');

// test webhook is rejected (invalid HMAC signature)
test('Webhook Notification', async ({ request }) => {
const notifications = await request.post(`/api/webhooks/notifications`, {
data: {
"live": "false",
"notificationItems":[
{
"NotificationRequestItem":{
"additionalData":{
"hmacSignature":"INVALID_HMAC_SIGNATURE"
},
"eventCode":"AUTHORISATION",
"success":"true",
"eventDate":"2019-06-28T18:03:50+01:00",
"merchantAccountCode":"YOUR_MERCHANT_ACCOUNT",
"pspReference": "7914073381342284",
"merchantReference": "YOUR_REFERENCE",
"amount": {
"value":24999,
"currency":"EUR"
}
}
}
]
}
});

/// Verify notification is not accepted (invalid HMAC)

// Status code not 404 (verify webhook is found)
expect(notifications.status()).not.toEqual(404);

// Status code not 200 (verify webhook does not accept the notification ie HMAC invalid)
expect(notifications.status()).not.toEqual(200);

// Body response does not contain [accepted]
notifications.text()
.then(value => {expect(value).not.toEqual("[accepted]");} );
});
45 changes: 45 additions & 0 deletions tests/giving/webhook.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// @ts-check
const { test, expect } = require('@playwright/test');
const utilities = require('../utilities');

// test webhook is successfully delivered
test('Webhook Notification', async ({ request }) => {

var notificationRequestItem = {
"eventCode":"AUTHORISATION",
"success":"true",
"eventDate":"2019-06-28T18:03:50+01:00",
"merchantAccountCode":"YOUR_MERCHANT_ACCOUNT",
"pspReference": "7914073381342284",
"merchantReference": "YOUR_REFERENCE",
"amount": {
"value":1130,
"currency":"EUR"
}
};

// calculate signature from payload
const hmacSignature = await utilities.calculateHmacSignature(notificationRequestItem);
// add hmacSignature to 'additionalData'
notificationRequestItem["additionalData"] = {"hmacSignature" : ""+hmacSignature+""}

// POST webhook
const notifications = await request.post(`/api/webhooks/notifications`, {
data: {
"live": "false",
"notificationItems":[
{
"NotificationRequestItem": notificationRequestItem
}
]
}
});

// Verify status code
expect(notifications.status()).toEqual(200);

// Verify body response
notifications.text()
.then(value => {expect(value).toEqual("[accepted]");} );
});

0 comments on commit e8181bc

Please sign in to comment.