UNPKG

@umbraco/playwright-testhelpers

Version:

Test helpers for making playwright tests for Umbraco solutions

169 lines 7.16 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.WebhookApiHelper = void 0; const test_1 = require("@playwright/test"); const json_models_builders_1 = require("@umbraco/json-models-builders"); const ConstantHelper_1 = require("./ConstantHelper"); class WebhookApiHelper { api; page; webhookSiteUrl = 'https://webhook.site/'; constructor(api, page) { this.api = api; this.page = page; } async get(id) { const response = await this.api.get(this.api.baseUrl + '/umbraco/management/api/v1/webhook/' + id); const json = await response.json(); if (json !== null) { return json; } return null; } async doesExist(id) { const response = await this.get(this.api.baseUrl + '/umbraco/management/api/v1/webhook/' + id); return response.status() === 200; } async create(webhookData) { if (webhookData == null) { return; } const response = await this.api.post(this.api.baseUrl + '/umbraco/management/api/v1/webhook', webhookData); // Returns the id of the created webhook return response.headers().location.split("/").pop(); } async delete(id) { return await this.api.delete(this.api.baseUrl + '/umbraco/management/api/v1/webhook/' + id); } async update(webhook) { return await this.api.put(this.api.baseUrl + '/umbraco/management/api/v1/webhook/', webhook); } async getAll() { return await this.api.get(this.api.baseUrl + '/umbraco/management/api/v1/webhook?pageSize=50&skip=0&take=10000'); } async doesNameExist(name) { return await this.getByName(name); } async getByName(name) { const allWebhooks = await this.getAll(); const jsonWebhooks = await allWebhooks.json(); for (const webhook of jsonWebhooks.items) { if (webhook.name === name) { return await this.get(webhook.id); } } return null; } async ensureNameNotExists(name) { const allWebhooks = await this.getAll(); const jsonWebhooks = await allWebhooks.json(); for (const webhook of jsonWebhooks.items) { if (webhook.name === name) { return await this.delete(webhook.id); } } return null; } async generateWebhookSiteToken() { const createWebhookResponse = await this.page.request.post(this.webhookSiteUrl + 'token', { headers: { 'Accept': 'application/json' } }); const webhookData = await createWebhookResponse.json(); const webhookToken = webhookData.uuid; return webhookToken; } async getWebhookSiteRequestResponse(webhookSiteToken, timeoutMs = 15000, pollInterval = 1000) { const requestUrl = this.webhookSiteUrl + 'token/' + webhookSiteToken + '/requests'; const start = Date.now(); while (Date.now() - start < timeoutMs) { const requestResponse = await this.page.request.get(requestUrl, { headers: { 'Accept': 'application/json' } }); const requestJson = await requestResponse.json(); if (requestJson.total > 0) { return requestJson.data; } // Polling again if there is no webhook received yet await new Promise((resolve) => setTimeout(resolve, pollInterval)); } return null; } async doesWebhookHaveEvent(webhookName, eventName) { const webhookData = await this.getByName(webhookName); return webhookData.events.find(event => event.eventName === eventName); } async doesWebhookHaveContentTypeId(webhookName, contentTypeId) { const webhookData = await this.getByName(webhookName); return webhookData.contentTypeKeys.includes(contentTypeId); } async doesWebhookHaveHeader(webhookName, headerName, headerValue) { const webhookData = await this.getByName(webhookName); return webhookData.headers[headerName] === headerValue; } async doesWebhookHaveUrl(webhookName, url) { const webhookData = await this.getByName(webhookName); return webhookData.url === url; } async isWebhookEnabled(webhookName, isEnabled = true) { const webhookData = await this.getByName(webhookName); return (0, test_1.expect)(webhookData.enabled).toBe(isEnabled); } async getEventTypeValue(eventName) { const eventData = ConstantHelper_1.ConstantHelper.webhookEvents.find(event => event.eventName === eventName); return eventData?.eventType || ''; } async getEventAliasValue(eventName) { const eventData = ConstantHelper_1.ConstantHelper.webhookEvents.find(event => event.eventName === eventName); return eventData?.alias || ''; } async createDefaultWebhook(webhookName, webhookSiteToken, eventName = 'Content Published', isEnabled = true) { await this.ensureNameNotExists(webhookName); const webhookUrl = this.webhookSiteUrl + webhookSiteToken; const eventAlias = await this.getEventAliasValue(eventName); const webhook = new json_models_builders_1.WebhookBuilder() .withName(webhookName) .withUrl(webhookUrl) .withEventAlias(eventAlias) .withEnabled(isEnabled) .build(); return await this.create(webhook); } async createWebhookForSpecificContentType(webhookName, webhookSiteToken, eventName, contentTypeName) { await this.ensureNameNotExists(webhookName); const webhookUrl = this.webhookSiteUrl + webhookSiteToken; const eventAlias = await this.getEventAliasValue(eventName); const eventType = await this.getEventTypeValue(eventName); let contentTypeData; if (eventType == 'Content') { contentTypeData = await this.api.documentType.getByName(contentTypeName); } else { contentTypeData = await this.api.mediaType.getByName(contentTypeName); } const webhook = new json_models_builders_1.WebhookBuilder() .withName(webhookName) .withUrl(webhookUrl) .withEventAlias(eventAlias) .withContentTypeKey(contentTypeData.id) .build(); return await this.create(webhook); } async createWebhookWithHeader(webhookName, webhookSiteToken, eventName, headerName, headerValue) { await this.ensureNameNotExists(webhookName); const webhookUrl = this.webhookSiteUrl + webhookSiteToken; const eventAlias = await this.getEventAliasValue(eventName); const webhook = new json_models_builders_1.WebhookBuilder() .withName(webhookName) .withUrl(webhookUrl) .withEventAlias(eventAlias) .withHeader(headerName, headerValue) .build(); return await this.create(webhook); } } exports.WebhookApiHelper = WebhookApiHelper; //# sourceMappingURL=WebhookApiHelper.js.map