Skip to content

Commit

Permalink
Subscription webhook tests (#54)
Browse files Browse the repository at this point in the history
* Add test failing webhook

* Add test successful webhook

* Add uuid

* Add test to receive and display the token

* Rename file

* Correct expectation
  • Loading branch information
gcatanese authored Oct 10, 2023
1 parent 679bbbd commit 1b691f6
Show file tree
Hide file tree
Showing 5 changed files with 309 additions and 29 deletions.
165 changes: 164 additions & 1 deletion package-lock.json

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

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@
"author": "",
"license": "ISC",
"devDependencies": {
"@playwright/test": "^1.37.1",
"@adyen/api-library": "^14.0.0",
"@playwright/test": "^1.37.1",
"dotenv": "^16.3.1"
},
"dependencies": {
"uuid": "^9.0.1"
}
}
65 changes: 65 additions & 0 deletions tests/subscription/display-token.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// @ts-check
const { test, expect } = require('@playwright/test');
const utilities = require('../utilities');

import { v4 as uuidv4 } from 'uuid';


// test RecurringDetailReference is displayed in the Admin panel
test('Display RecurringDetailReference', async ({ request, page }) => {

var shopperReference = uuidv4();
var token = uuidv4();

// send webhook with RECURRING_CONTRACT event
var notificationRequestItem = {
"eventCode": "RECURRING_CONTRACT",
"eventDate": "2023-06-20T16:09:48+02:00",
"merchantAccountCode": "YOUR_MERCHANT_ACCOUNT",
"merchantReference": "YOUR_PAYMENT_REFERENCE",
"originalReference": "INITIAL_PAYMENT_PSP_REFERENCE",
"originalPsp": "PSP_REFERENCE",
"paymentMethod": "mc",
"amount": {
"value": 0,
"currency": "EUR"
},
"success": "true",
};

// calculate signature from payload
const hmacSignature = await utilities.calculateHmacSignature(notificationRequestItem);
// add 'additionalData' with hmacSignature
notificationRequestItem["additionalData"] = {
"hmacSignature": "" + hmacSignature + "",
"recurring.recurringDetailReference": token,
"recurring.shopperReference": shopperReference,
"shopperReference": "YOUR_SHOPPER_REFERENCE"
}

// 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 token is visible in the Admin panel
await page.goto('/admin');

await expect(page).toHaveTitle("Adyen Subscription Admin View");
await expect(page.locator('text="ADMIN PANEL"')).toBeVisible();

await expect(page.locator('text="ShopperReference: ' + shopperReference + '"')).toBeVisible();
await expect(page.locator('text="RecurringDetailReference: ' + token + '"')).toBeVisible();

});

43 changes: 43 additions & 0 deletions tests/subscription/webhook-failure.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// @ts-check
const { test, expect } = require('@playwright/test');

test('Webhook failure', async ({ request }) => {
const notifications = await request.post(`/api/webhooks/notifications`, {
data: {
"live": "false",
"notificationItems": [
{
"NotificationRequestItem": {
"additionalData": {
"hmacSignature": "INVALID_HMAC_SIGNATURE",
"recurring.recurringDetailReference": "XXXXXXXXX",
"recurring.shopperReference": "UniqueShopperReference"
},
"eventCode": "AUTHORISATION",
"success": "true",
"eventDate": "2019-06-28T18:03:50+01:00",
"merchantAccountCode": "YOUR_MERCHANT_ACCOUNT",
"pspReference": "7914073381342284",
"merchantReference": "YOUR_REFERENCE",
"amount": {
"value": 0,
"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]"); });
});
Loading

0 comments on commit 1b691f6

Please sign in to comment.