From b1e9f499db2dcc4d3a75af369b3703483083cd01 Mon Sep 17 00:00:00 2001 From: Karolina Rakoczy Date: Thu, 15 Jul 2021 13:27:07 +0300 Subject: [PATCH] add tests for apps (#1227) --- cypress/apiRequests/Apps.js | 63 +++++++++++ cypress/elements/apps/appDetails.js | 12 ++ cypress/elements/apps/appsList.js | 3 + cypress/elements/apps/webhookDetails.js | 4 + cypress/integration/allEnv/apps.js | 104 ++++++++++++++++++ cypress/url/urlList.js | 3 + cypress/utils/appUtils.js | 5 + .../CustomAppTokens/CustomAppTokens.tsx | 2 +- src/apps/components/CustomApps/CustomApps.tsx | 6 +- .../TokenCreateDialog/TokenCreateDialog.tsx | 12 +- .../AccountPermissions/AccountPermissions.tsx | 1 + .../__snapshots__/Stories.test.ts.snap | 9 ++ .../components/WebhooksList/WebhooksList.tsx | 6 +- 13 files changed, 225 insertions(+), 5 deletions(-) create mode 100644 cypress/apiRequests/Apps.js create mode 100644 cypress/elements/apps/appDetails.js create mode 100644 cypress/elements/apps/appsList.js create mode 100644 cypress/elements/apps/webhookDetails.js create mode 100644 cypress/integration/allEnv/apps.js create mode 100644 cypress/utils/appUtils.js diff --git a/cypress/apiRequests/Apps.js b/cypress/apiRequests/Apps.js new file mode 100644 index 000000000..9f5b1e7a6 --- /dev/null +++ b/cypress/apiRequests/Apps.js @@ -0,0 +1,63 @@ +export function createApp(name, permission) { + const mutation = `mutation{ + appCreate(input:{name:"${name}" permissions:${permission}}){ + authToken + errors{ + field + message + } + app{ + id + } + } + }`; + return cy.sendRequestWithQuery(mutation).its("body.data.appCreate.app"); +} + +export function getApps(first, search) { + const mutation = `query{ + apps(filter:{ search: "${search}"} first: ${first}){ + edges{ + node{ + name + id + } + } + } + }`; + return cy + .sendRequestWithQuery(mutation) + .then(resp => resp.body.data.apps.edges); +} + +export function deleteApp(appId) { + const mutation = `mutation{ + appDelete(id:"${appId}"){ + errors{ + field + message + } + } + }`; + return cy.sendRequestWithQuery(mutation); +} + +export function getApp(appId) { + const query = `query{ + app(id:"${appId}"){ + name + permissions{ + code + } + tokens{ + name + authToken + } + webhooks{ + name + targetUrl + } + } + }`; + return cy.sendRequestWithQuery(query).its("body.data.app"); +} diff --git a/cypress/elements/apps/appDetails.js b/cypress/elements/apps/appDetails.js new file mode 100644 index 000000000..d54642cd9 --- /dev/null +++ b/cypress/elements/apps/appDetails.js @@ -0,0 +1,12 @@ +export const APP_DETAILS = { + nameInput: '[name="name"]', + manageAppsPermissionCheckbox: '[id="MANAGE_APPS"]', + createWebhookButton: '[data-test-id = "createWebhook"]', + createTokenButton: '[data-test-id="createToken"]', + createTokenForm: { + tokenDialog: '[role="dialog"]', + nameInput: '[name="name"]', + tokenToCopy: '[data-test-id="generatedToken"]', + doneButton: '[data-test-id="done"]' + } +}; diff --git a/cypress/elements/apps/appsList.js b/cypress/elements/apps/appsList.js new file mode 100644 index 000000000..96b3e7b28 --- /dev/null +++ b/cypress/elements/apps/appsList.js @@ -0,0 +1,3 @@ +export const APPS_LIST = { + createLocalAppButton: '[data-test-id="createApp"]' +}; diff --git a/cypress/elements/apps/webhookDetails.js b/cypress/elements/apps/webhookDetails.js new file mode 100644 index 000000000..f31022c3b --- /dev/null +++ b/cypress/elements/apps/webhookDetails.js @@ -0,0 +1,4 @@ +export const WEBHOOK_DETAILS = { + nameInput: '[name="name"]', + targetUrlInput: '[name="targetUrl"]' +}; diff --git a/cypress/integration/allEnv/apps.js b/cypress/integration/allEnv/apps.js new file mode 100644 index 000000000..491e725f1 --- /dev/null +++ b/cypress/integration/allEnv/apps.js @@ -0,0 +1,104 @@ +import faker from "faker"; + +import { createApp, getApp } from "../../apiRequests/Apps"; +import { ONE_PERMISSION_USERS } from "../../Data/users"; +import { APP_DETAILS } from "../../elements/apps/appDetails"; +import { APPS_LIST } from "../../elements/apps/appsList"; +import { WEBHOOK_DETAILS } from "../../elements/apps/webhookDetails"; +import { BUTTON_SELECTORS } from "../../elements/shared/button-selectors"; +import { confirmationMessageShouldDisappear } from "../../steps/shared/confirmationMessage"; +import { appDetailsUrl, urlList } from "../../url/urlList"; +import { deleteAppsStartsWith } from "../../utils/appUtils"; + +describe("Tests for apps", () => { + const startsWith = "Apps"; + const name = `${startsWith}${faker.datatype.number()}`; + + let createdApp; + + before(() => { + cy.clearSessionData().loginUserViaRequest(); + deleteAppsStartsWith(startsWith); + createApp(name, "MANAGE_APPS").then(app => { + createdApp = app; + }); + }); + + beforeEach(() => { + cy.clearSessionData().loginUserViaRequest("auth", ONE_PERMISSION_USERS.app); + }); + + it("should create app", () => { + const randomName = `${startsWith}${faker.datatype.number()}`; + + cy.visit(urlList.apps) + .get(APPS_LIST.createLocalAppButton) + .click() + .get(APP_DETAILS.nameInput) + .type(randomName) + .get(APP_DETAILS.manageAppsPermissionCheckbox) + .click() + .addAliasToGraphRequest("AppCreate") + .get(BUTTON_SELECTORS.confirm) + .click(); + confirmationMessageShouldDisappear(); + cy.wait("@AppCreate") + .its("response.body.data.appCreate.app") + .then(app => { + getApp(app.id); + }) + .then(app => { + expect(app.name).to.eq(randomName); + const token = app.tokens.find(element => element.name === "Default"); + expect(token).to.be.ok; + }); + }); + + it("should create webhook", () => { + const randomName = `${startsWith}${faker.datatype.number()}`; + const targetUrl = `http://example.${randomName}`; + + cy.visit(appDetailsUrl(createdApp.id)) + .get(APP_DETAILS.createWebhookButton) + .click() + .get(WEBHOOK_DETAILS.nameInput) + .type(randomName) + .get(WEBHOOK_DETAILS.targetUrlInput) + .type(targetUrl) + .get(BUTTON_SELECTORS.confirm) + .click(); + confirmationMessageShouldDisappear(); + getApp(createdApp.id).then(({ webhooks }) => { + expect(webhooks[0].name).to.eq(randomName); + expect(webhooks[0].targetUrl).to.eq(targetUrl); + }); + }); + + it("should create token", () => { + const randomName = `${startsWith}${faker.datatype.number()}`; + let expectedToken; + + cy.visit(appDetailsUrl(createdApp.id)) + .get(APP_DETAILS.createTokenButton) + .click() + .get(APP_DETAILS.createTokenForm.tokenDialog) + .find(APP_DETAILS.createTokenForm.nameInput) + .type(randomName) + .get(BUTTON_SELECTORS.submit) + .click() + .get(APP_DETAILS.createTokenForm.tokenToCopy) + .invoke("text") + .then(text => { + expectedToken = text; + cy.get(APP_DETAILS.createTokenForm.doneButton).click(); + getApp(createdApp.id); + }) + .then(app => { + const token = app.tokens.find(element => element.name === randomName); + const tokenLastFourDigits = expectedToken.slice( + expectedToken.length - 4 + ); + expect(token.authToken).to.eq(tokenLastFourDigits); + }); + }); +}); diff --git a/cypress/url/urlList.js b/cypress/url/urlList.js index 8c6b5075b..f74c3eb23 100644 --- a/cypress/url/urlList.js +++ b/cypress/url/urlList.js @@ -17,6 +17,7 @@ export const urlList = { weightRete: "weight/", attributes: "attributes/", productTypes: "product-types/", + apps: "apps/", customers: "customers/" }; @@ -41,3 +42,5 @@ export const warehouseDetailsUrl = warehouseId => export const productTypeDetailsUrl = productTypeId => `${urlList.productTypes}${productTypeId}`; + +export const appDetailsUrl = appId => `${urlList.apps}custom/${appId}`; diff --git a/cypress/utils/appUtils.js b/cypress/utils/appUtils.js new file mode 100644 index 000000000..610fdddca --- /dev/null +++ b/cypress/utils/appUtils.js @@ -0,0 +1,5 @@ +import { deleteApp, getApps } from "../apiRequests/Apps"; + +export function deleteAppsStartsWith(startsWith) { + cy.deleteElementsStartsWith(deleteApp, getApps, startsWith); +} diff --git a/src/apps/components/CustomAppTokens/CustomAppTokens.tsx b/src/apps/components/CustomAppTokens/CustomAppTokens.tsx index 782612018..ee50a1cf3 100644 --- a/src/apps/components/CustomAppTokens/CustomAppTokens.tsx +++ b/src/apps/components/CustomAppTokens/CustomAppTokens.tsx @@ -39,7 +39,7 @@ const CustomAppTokens: React.FC = props => { description: "header" })} toolbar={ - @@ -125,7 +128,12 @@ const TokenCreateDialog: React.FC = props => { ) : ( - )} diff --git a/src/components/AccountPermissions/AccountPermissions.tsx b/src/components/AccountPermissions/AccountPermissions.tsx index 348f49772..33a2eaf30 100644 --- a/src/components/AccountPermissions/AccountPermissions.tsx +++ b/src/components/AccountPermissions/AccountPermissions.tsx @@ -136,6 +136,7 @@ const AccountPermissions: React.FC = props => { >