Fix Search webhooks toggling (#961)
* Fix Search webhooks toggling * fix test
This commit is contained in:
parent
30140eea06
commit
55315b89f3
3 changed files with 40 additions and 32 deletions
|
@ -10,6 +10,7 @@ import { SettingsManager } from "@saleor/app-sdk/settings-manager";
|
||||||
import { createGraphQLClient, getAppBaseUrl } from "@saleor/apps-shared";
|
import { createGraphQLClient, getAppBaseUrl } from "@saleor/apps-shared";
|
||||||
import { Client } from "urql";
|
import { Client } from "urql";
|
||||||
import { isConfigured } from "../../lib/algolia/is-configured";
|
import { isConfigured } from "../../lib/algolia/is-configured";
|
||||||
|
import { AppConfigMetadataManager } from "../../modules/configuration/app-config-metadata-manager";
|
||||||
|
|
||||||
const logger = createLogger({
|
const logger = createLogger({
|
||||||
service: "recreateWebhooksHandler",
|
service: "recreateWebhooksHandler",
|
||||||
|
@ -41,22 +42,17 @@ export const recreateWebhooksHandlerFactory =
|
||||||
const webhooksToggler = webhookActivityTogglerFactory(authData.appId, client);
|
const webhooksToggler = webhookActivityTogglerFactory(authData.appId, client);
|
||||||
const settingsManager = settingsManagerFactory(client, authData.appId);
|
const settingsManager = settingsManagerFactory(client, authData.appId);
|
||||||
|
|
||||||
const domain = new URL(authData.saleorApiUrl).host;
|
const configManager = new AppConfigMetadataManager(settingsManager);
|
||||||
|
|
||||||
const [secretKey, appId] = await Promise.all([
|
const config = (await configManager.get(authData.saleorApiUrl)).getConfig();
|
||||||
settingsManager.get("secretKey", domain),
|
|
||||||
settingsManager.get("appId", domain),
|
|
||||||
]);
|
|
||||||
|
|
||||||
const settings = { secretKey, appId };
|
logger.debug("fetched settings");
|
||||||
|
|
||||||
logger.debug(settings, "fetched settings");
|
|
||||||
|
|
||||||
const baseUrl = getAppBaseUrl(req.headers);
|
const baseUrl = getAppBaseUrl(req.headers);
|
||||||
const enableWebhooks = isConfigured({
|
const enableWebhooks = isConfigured({
|
||||||
configuration: {
|
configuration: {
|
||||||
appId: appId,
|
appId: config.appConfig?.appId,
|
||||||
secretKey: secretKey,
|
secretKey: config.appConfig?.secretKey,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -13,6 +13,7 @@ import { SearchProvider } from "../../lib/searchProvider";
|
||||||
import { createGraphQLClient } from "@saleor/apps-shared";
|
import { createGraphQLClient } from "@saleor/apps-shared";
|
||||||
import { Client } from "urql";
|
import { Client } from "urql";
|
||||||
import { isWebhookUpdateNeeded } from "../../lib/algolia/is-webhook-update-needed";
|
import { isWebhookUpdateNeeded } from "../../lib/algolia/is-webhook-update-needed";
|
||||||
|
import { AppConfigMetadataManager } from "../../modules/configuration/app-config-metadata-manager";
|
||||||
|
|
||||||
const logger = createLogger({
|
const logger = createLogger({
|
||||||
service: "webhooksStatusHandler",
|
service: "webhooksStatusHandler",
|
||||||
|
@ -54,23 +55,18 @@ export const webhooksStatusHandlerFactory =
|
||||||
const webhooksToggler = webhookActivityTogglerFactory(authData.appId, client);
|
const webhooksToggler = webhookActivityTogglerFactory(authData.appId, client);
|
||||||
const settingsManager = settingsManagerFactory(client, authData.appId);
|
const settingsManager = settingsManagerFactory(client, authData.appId);
|
||||||
|
|
||||||
const domain = new URL(authData.saleorApiUrl).host;
|
const configManager = new AppConfigMetadataManager(settingsManager);
|
||||||
|
|
||||||
const [secretKey, appId] = await Promise.all([
|
const config = (await configManager.get(authData.saleorApiUrl)).getConfig();
|
||||||
settingsManager.get("secretKey", domain),
|
|
||||||
settingsManager.get("appId", domain),
|
|
||||||
]);
|
|
||||||
|
|
||||||
const settings = { secretKey, appId };
|
logger.debug("fetched settings");
|
||||||
|
|
||||||
logger.debug(settings, "fetched settings");
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* If settings are incomplete, disable webhooks
|
* If settings are incomplete, disable webhooks
|
||||||
*
|
*
|
||||||
* TODO Extract config operations to domain/
|
* TODO Extract config operations to domain/
|
||||||
*/
|
*/
|
||||||
if (!settings.appId || !settings.secretKey) {
|
if (!config.appConfig) {
|
||||||
logger.debug("Settings not set, will disable webhooks");
|
logger.debug("Settings not set, will disable webhooks");
|
||||||
|
|
||||||
await webhooksToggler.disableOwnWebhooks();
|
await webhooksToggler.disableOwnWebhooks();
|
||||||
|
@ -78,7 +74,10 @@ export const webhooksStatusHandlerFactory =
|
||||||
/**
|
/**
|
||||||
* Otherwise, if settings are set, check in Algolia if tokens are valid
|
* Otherwise, if settings are set, check in Algolia if tokens are valid
|
||||||
*/
|
*/
|
||||||
const algoliaService = algoliaSearchProviderFactory(settings.appId, settings.secretKey);
|
const algoliaService = algoliaSearchProviderFactory(
|
||||||
|
config.appConfig.appId,
|
||||||
|
config.appConfig.secretKey,
|
||||||
|
);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
logger.debug("Settings set, will ping Algolia");
|
logger.debug("Settings set, will ping Algolia");
|
||||||
|
|
|
@ -1,16 +1,13 @@
|
||||||
import { beforeEach, describe, expect, it, Mock, vi } from "vitest";
|
import { NextProtectedApiHandler } from "@saleor/app-sdk/handlers/next";
|
||||||
|
import { SettingsManager } from "@saleor/app-sdk/settings-manager";
|
||||||
import { createMocks } from "node-mocks-http";
|
import { createMocks } from "node-mocks-http";
|
||||||
import { webhooksStatusHandlerFactory } from "../../pages/api/webhooks-status";
|
|
||||||
import { Client, OperationResult } from "urql";
|
import { Client, OperationResult } from "urql";
|
||||||
|
import { beforeEach, describe, expect, it, Mock, vi } from "vitest";
|
||||||
|
import { FetchOwnWebhooksQuery, WebhookEventTypeAsyncEnum } from "../../../generated/graphql";
|
||||||
import { IWebhookActivityTogglerService } from "../../domain/WebhookActivityToggler.service";
|
import { IWebhookActivityTogglerService } from "../../domain/WebhookActivityToggler.service";
|
||||||
import { SearchProvider } from "../../lib/searchProvider";
|
import { SearchProvider } from "../../lib/searchProvider";
|
||||||
import { SettingsManager } from "@saleor/app-sdk/settings-manager";
|
import { webhooksStatusHandlerFactory } from "../../pages/api/webhooks-status";
|
||||||
import { NextProtectedApiHandler } from "@saleor/app-sdk/handlers/next";
|
import { AppConfig } from "../../modules/configuration/configuration";
|
||||||
import {
|
|
||||||
FetchOwnWebhooksQuery,
|
|
||||||
WebhookEventTypeAsyncEnum,
|
|
||||||
WebhookEventTypeEnum,
|
|
||||||
} from "../../../generated/graphql";
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Context provided from ProtectedApiHandler to handler body
|
* Context provided from ProtectedApiHandler to handler body
|
||||||
|
@ -100,8 +97,16 @@ describe("webhooksStatusHandler", () => {
|
||||||
expect(res._getStatusCode()).toBe(200);
|
expect(res._getStatusCode()).toBe(200);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("Disables webhooks if Algolia credentials are invalid", async function () {
|
it("Disables webhooks if Algolia credentials are set, but invalid", async function () {
|
||||||
(settingsManagerMock.get as Mock).mockReturnValue("metadata-value");
|
const invalidConfig = new AppConfig();
|
||||||
|
|
||||||
|
invalidConfig.setAlgoliaSettings({
|
||||||
|
appId: "asd",
|
||||||
|
secretKey: "wrong",
|
||||||
|
indexNamePrefix: "test",
|
||||||
|
});
|
||||||
|
|
||||||
|
(settingsManagerMock.get as Mock).mockReturnValueOnce(invalidConfig.serialize());
|
||||||
(algoliaSearchProviderMock.ping as Mock).mockImplementationOnce(async () => {
|
(algoliaSearchProviderMock.ping as Mock).mockImplementationOnce(async () => {
|
||||||
throw new Error();
|
throw new Error();
|
||||||
});
|
});
|
||||||
|
@ -116,7 +121,15 @@ describe("webhooksStatusHandler", () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
it("Returns webhooks if Algolia credentials are valid", async function () {
|
it("Returns webhooks if Algolia credentials are valid", async function () {
|
||||||
(settingsManagerMock.get as Mock).mockReturnValue("metadata-value");
|
const validConfig = new AppConfig();
|
||||||
|
|
||||||
|
validConfig.setAlgoliaSettings({
|
||||||
|
appId: "asd",
|
||||||
|
secretKey: "asddsada",
|
||||||
|
indexNamePrefix: "test",
|
||||||
|
});
|
||||||
|
|
||||||
|
(settingsManagerMock.get as Mock).mockReturnValueOnce(validConfig.serialize());
|
||||||
(algoliaSearchProviderMock.ping as Mock).mockImplementationOnce(async () => Promise.resolve());
|
(algoliaSearchProviderMock.ping as Mock).mockImplementationOnce(async () => Promise.resolve());
|
||||||
|
|
||||||
const { req, res } = createMocks({});
|
const { req, res } = createMocks({});
|
||||||
|
|
Loading…
Reference in a new issue