From b0cddcffb70559cee1cde0501800cc97fdcadfe3 Mon Sep 17 00:00:00 2001 From: Gary Roybal Date: Tue, 1 Aug 2023 04:02:15 -0700 Subject: [PATCH] UPSE-386: Update Playwright search tests for portlet 'favorite' flag --- tests/api/search-v5_0.spec.ts | 43 ++++++++++++++++++++++++ tests/api/utils/api-portlets-utils.ts | 32 ++++++++++++++++++ tests/api/utils/api-preferences-utils.ts | 37 ++++++++++++++++++++ 3 files changed, 112 insertions(+) create mode 100644 tests/api/utils/api-portlets-utils.ts create mode 100644 tests/api/utils/api-preferences-utils.ts diff --git a/tests/api/search-v5_0.spec.ts b/tests/api/search-v5_0.spec.ts index 15d213191b..2af85c00df 100644 --- a/tests/api/search-v5_0.spec.ts +++ b/tests/api/search-v5_0.spec.ts @@ -1,6 +1,28 @@ import { test, expect } from "@playwright/test"; import { config } from "../general-config"; import { loginViaApi } from "../ux/utils/ux-general-utils"; +import { + PortletDefBasicInfo, + getPortletDetails, +} from "./utils/api-portlets-utils"; +import { + favoritePortlet, + unfavoritePortlet, +} from "./utils/api-preferences-utils"; + +interface PortletSearchResult { + description: string; + fname: string; + name: string; + score: string; + title: string; + url: string; + favorite: boolean; +} + +interface PortletSearchResults { + portlets: PortletSearchResult[]; +} test("search all", async ({ request }) => { await loginViaApi(request, config.users.admin); @@ -18,11 +40,32 @@ test("search all", async ({ request }) => { score: "4.0", title: "Daily Business Cartoon", url: "/uPortal/p/daily-business-cartoon.ctf3/max/render.uP", + favorite: false, }, ], }); }); +test("search favorited portlet", async({ request }) => { + await loginViaApi(request, config.users.admin); + const portletFname = 'daily-business-cartoon'; + const portletDetails: PortletDefBasicInfo | null = await getPortletDetails(request, portletFname); + if (!portletDetails) { + console.error('could not retrieve portlet details in order to get portlet ID'); + test.fail(); + } else { + const portletId = portletDetails.id; + expect(await favoritePortlet(request, portletId)).not.toBeNull(); + const response = await request.get(`${config.url}api/v5-0/portal/search?q=cartoon&type=portlets`); + expect(await unfavoritePortlet(request, portletId)).toBe(true); + expect(response.status()).toEqual(200); + const portletSearchResults: PortletSearchResults = JSON.parse(await response.text()) as PortletSearchResults; + const portletFound: PortletSearchResult = portletSearchResults.portlets[0]; + expect(portletFound.favorite).toBe(true); + expect(portletFound.fname).toBe(portletFname); + } +}); + test("search type people", async ({ request }) => { await loginViaApi(request, config.users.admin); const response = await request.get( diff --git a/tests/api/utils/api-portlets-utils.ts b/tests/api/utils/api-portlets-utils.ts new file mode 100644 index 0000000000..fb0fda2d1b --- /dev/null +++ b/tests/api/utils/api-portlets-utils.ts @@ -0,0 +1,32 @@ +import { expect, APIRequestContext } from "@playwright/test"; +import { config } from "../../general-config"; + +export interface PortletDefBasicInfo { + id: string; + name: string; + fname: string; +} + +interface PortletsResponse { + portlets: PortletDefBasicInfo[]; +} + +/* + * Retrieve the details for a portlet identified by it's fname. + */ +export async function getPortletDetails( + request: APIRequestContext, + portletFname: string +): Promise { + const response = await request.get(`${config.url}api/portlets.json`); + expect(response.status()).toEqual(200); + const portletsJson: PortletsResponse = await response.json() as PortletsResponse; + const portlets: PortletDefBasicInfo[] = portletsJson.portlets; + for (const val of portlets) { + if (val != null && val.fname == portletFname) { + return val; + } + } + return null; +} + diff --git a/tests/api/utils/api-preferences-utils.ts b/tests/api/utils/api-preferences-utils.ts new file mode 100644 index 0000000000..073d51dc0a --- /dev/null +++ b/tests/api/utils/api-preferences-utils.ts @@ -0,0 +1,37 @@ +import { expect, APIRequestContext } from "@playwright/test"; +import { config } from "../../general-config"; + +interface PreferenceFavoriteResponse { + newNodeId: string; + response: string; +} + +/* + * Favorite a portlet, indentified by portlet ID. Return new node ID. + */ +export async function favoritePortlet( + request: APIRequestContext, + portletId: string +): Promise { + expect(portletId).not.toBeNull(); + const response = await request.post( + `${config.url}api/layout?action=addFavorite&channelId=${portletId}` + ); + expect(response.status()).toEqual(200); + const responseBody: PreferenceFavoriteResponse = JSON.parse(await response.text()) as PreferenceFavoriteResponse; + return responseBody.newNodeId; +} + +/* + * Unfavorite a portlet, indentified by portlet ID. Returns boolean indicating whether or not operation was successful. + */ +export async function unfavoritePortlet( + request: APIRequestContext, + portletId: string +): Promise { + expect(portletId).not.toBeNull(); + const response = await request.post(`${config.url}api/layout?action=removeFavorite&channelId=${portletId}`); + expect(response.status()).toEqual(200); + const responseBody: PreferenceFavoriteResponse = JSON.parse(await response.text()) as PreferenceFavoriteResponse; + return responseBody.response === "Removed from Favorites successfully"; +}