Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Functional Tests : API : GET /api/hooks/{id} #34553

Merged
merged 2 commits into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ describe('API : GET /api/hook-status/{id}', async () => {
let page: Page;
let apiContext: APIRequestContext;
let accessToken: string;
let jsonResponse: any;
let idHook: number;
let statusHook: boolean;

Expand Down Expand Up @@ -95,13 +96,20 @@ describe('API : GET /api/hook-status/{id}', async () => {
expect(api.hasResponseHeader(apiResponse, 'Content-Type')).to.eq(true);
expect(api.getResponseHeader(apiResponse, 'Content-Type')).to.contains('application/json');

const jsonResponse = await apiResponse.json();
// id
jsonResponse = await apiResponse.json();
});

it('should check the JSON Response : `id`', async function () {
await testContext.addContextItem(this, 'testIdentifier', 'checkResponseId', baseContext);

expect(jsonResponse).to.have.property('id');
expect(jsonResponse.id).to.be.a('number');
expect(jsonResponse.id).to.be.equal(idHook);
});

it('should check the JSON Response : `active`', async function () {
await testContext.addContextItem(this, 'testIdentifier', 'checkResponseActive', baseContext);

// active
expect(jsonResponse).to.have.property('active');
expect(jsonResponse.active).to.be.a('boolean');
expect(jsonResponse.active).to.be.equal(statusHook);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
// Import utils
import api from '@utils/api';
import helper from '@utils/helpers';
import testContext from '@utils/testContext';

// Import commonTests
import loginCommon from '@commonTests/BO/loginBO';

// Import pages
import dashboardPage from '@pages/BO/dashboard';
import positionsPage from '@pages/BO/design/positions';

import {expect} from 'chai';
import type {APIRequestContext, BrowserContext, Page} from 'playwright';

const baseContext: string = 'functional_API_endpoints_hook_getAPIHooksId';

describe('API : GET /api/hooks/{id}', async () => {
let browserContext: BrowserContext;
let page: Page;
let apiContext: APIRequestContext;
let accessToken: string;
let jsonResponse: any;
let idHook: number;
let statusHook: boolean;
let nameHook: string;
//let titleHook: string;
let descriptionHook: string;

before(async function () {
browserContext = await helper.createBrowserContext(this.browser);
page = await helper.newTab(browserContext);

apiContext = await helper.createAPIContext(global.BO.URL);

if (!global.GENERATE_FAILED_STEPS) {
// @todo : https://github.com/PrestaShop/PrestaShop/issues/34297
const apiResponse = await apiContext.post('api/oauth2/token', {
form: {
client_id: 'my_client_id',
client_secret: 'prestashop',
grant_type: 'client_credentials',
},
});
expect(apiResponse.status()).to.eq(200);

const jsonResponse = await apiResponse.json();
expect(jsonResponse).to.have.property('access_token');
expect(jsonResponse.access_token).to.be.a('string');

accessToken = jsonResponse.access_token;
}
});

after(async () => {
await helper.closeBrowserContext(browserContext);
});

describe('BackOffice : Expected data', async () => {
it('should login in BO', async function () {
await loginCommon.loginBO(this, page);
});

it('should go to \'Design > Positions\' page', async function () {
await testContext.addContextItem(this, 'testIdentifier', 'goToPositionsPage', baseContext);

await dashboardPage.goToSubMenu(
page,
dashboardPage.designParentLink,
dashboardPage.positionsLink,
);
await positionsPage.closeSfToolBar(page);

const pageTitle = await positionsPage.getPageTitle(page);
expect(pageTitle).to.contains(positionsPage.pageTitle);
});

it('should get the hook informations', async function () {
await testContext.addContextItem(this, 'testIdentifier', 'getHookInformations', baseContext);

idHook = await positionsPage.getHookId(page, 0);
expect(idHook).to.be.gt(0);

statusHook = await positionsPage.getHookStatus(page, 0);
expect(statusHook).to.be.equal(true);

nameHook = await positionsPage.getHookName(page, 0);
expect(nameHook.length).to.be.gt(0);

// @todo : https://github.com/PrestaShop/PrestaShop/issues/34552
//titleHook = await positionsPage.getHookStatus(page, 0);
//expect(titleHook.length).to.be.gt(0);

descriptionHook = await positionsPage.getHookDescription(page, 0);
expect(descriptionHook.length).to.be.gt(0);
});
});

describe('API : Check Data', async () => {
it('should request the endpoint /admin-dev/api/hooks/{id}', async function () {
await testContext.addContextItem(this, 'testIdentifier', 'requestEndpoint', baseContext);

const apiResponse = await apiContext.get(`api/hooks/${idHook}`, {
headers: {
Authorization: `Bearer ${accessToken}`,
},
});
expect(apiResponse.status()).to.eq(200);
expect(api.hasResponseHeader(apiResponse, 'Content-Type')).to.eq(true);
expect(api.getResponseHeader(apiResponse, 'Content-Type')).to.contains('application/json');

jsonResponse = await apiResponse.json();
});

it('should check the JSON Response : `id`', async function () {
await testContext.addContextItem(this, 'testIdentifier', 'checkResponseId', baseContext);

expect(jsonResponse).to.have.property('id');
expect(jsonResponse.id).to.be.a('number');
expect(jsonResponse.id).to.be.equal(idHook);
});

it('should check the JSON Response : `active`', async function () {
await testContext.addContextItem(this, 'testIdentifier', 'checkResponseActive', baseContext);

expect(jsonResponse).to.have.property('active');
expect(jsonResponse.active).to.be.a('boolean');
expect(jsonResponse.active).to.be.equal(statusHook);
});

it('should check the JSON Response : `name`', async function () {
await testContext.addContextItem(this, 'testIdentifier', 'checkResponseName', baseContext);

expect(jsonResponse).to.have.property('name');
expect(jsonResponse.name).to.be.a('string');
expect(jsonResponse.name).to.be.equal(nameHook);
});

// @todo : https://github.com/PrestaShop/PrestaShop/issues/34552
it('should check the JSON Response : `title`', async function () {
await testContext.addContextItem(this, 'testIdentifier', 'checkResponseTitle', baseContext);

this.skip();

//expect(jsonResponse).to.have.property('title');
//expect(jsonResponse.title).to.be.a('string');
//expect(jsonResponse.title).to.be.equal(titleHook);
});

it('should check the JSON Response : `description`', async function () {
await testContext.addContextItem(this, 'testIdentifier', 'checkResponseDescription', baseContext);

expect(jsonResponse).to.have.property('description');
expect(jsonResponse.description).to.be.a('string');
expect(jsonResponse.description).to.be.equal(descriptionHook);
});
});
});
37 changes: 33 additions & 4 deletions tests/UI/pages/BO/design/positions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,13 @@ class Positions extends BOBasePage {

private readonly hookRowNth: (hookRow: number) => string;

private readonly hookStatusInput: (hookRow: number) => string;
private readonly hookHeader: (hookRow: number) => string;

private readonly hookHeaderStatusInput: (hookRow: number) => string;

private readonly hookHeaderNameSpan: (hookRow: number) => string;

private readonly hookHeaderDescriptionDiv: (hookRow: number) => string;

private readonly hookRowName: (hookName: string) => string;

Expand Down Expand Up @@ -56,7 +62,10 @@ class Positions extends BOBasePage {
this.modulePositionFormHookSectionVisible = `${this.modulePositionFormHookSection}.hook-panel.hook-visible`;
this.hookRowNth = (hookRow: number) => `${this.modulePositionFormHookSection}:nth-child(${hookRow + 1
} of .hook-panel.hook-visible)`;
this.hookStatusInput = (hookRow: number) => `${this.hookRowNth(hookRow)} header span.hook-status input.hook-switch-action`;
this.hookHeader = (hookRow: number) => `${this.hookRowNth(hookRow)} header`;
this.hookHeaderStatusInput = (hookRow: number) => `${this.hookHeader(hookRow)} span.hook-status input.hook-switch-action`;
this.hookHeaderNameSpan = (hookRow: number) => `${this.hookHeader(hookRow)} span.hook-name`;
this.hookHeaderDescriptionDiv = (hookRow: number) => `${this.hookHeader(hookRow)} div.hook_description`;
this.hookRowName = (hookName: string) => `${this.modulePositionFormHookSection} a[name=${hookName}]`;
this.hookRowModulesList = (hookName: string) => `${this.hookRowName(hookName)} ~ section.module-list ul`;
this.hookNameSpan = (hookName: string) => `${this.hookRowName(hookName)} + header span.hook-name`;
Expand Down Expand Up @@ -124,19 +133,39 @@ class Positions extends BOBasePage {
* @returns {Promise<boolean>}
*/
async getHookId(page: Page, hookRow: number): Promise<number> {
const attribute = await this.getAttributeContent(page, this.hookStatusInput(hookRow), 'data-hook-id');
const attribute = await this.getAttributeContent(page, this.hookHeaderStatusInput(hookRow), 'data-hook-id');

return parseInt(attribute, 10);
}

/**
* Return the hook name
* @param page {Page} Browser tab
* @param hookRow {number} Hook Row
* @returns {Promise<string>}
*/
async getHookName(page: Page, hookRow: number): Promise<string> {
return this.getTextContent(page, this.hookHeaderNameSpan(hookRow));
}

/**
* Return the hook description
* @param page {Page} Browser tab
* @param hookRow {number} Hook Row
* @returns {Promise<string>}
*/
async getHookDescription(page: Page, hookRow: number): Promise<string> {
return this.getTextContent(page, this.hookHeaderDescriptionDiv(hookRow));
}

/**
* Return the hook status
* @param page {Page} Browser tab
* @param hookRow {number} Hook Row
* @returns {Promise<boolean>}
*/
async getHookStatus(page: Page, hookRow: number): Promise<boolean> {
const inputValue = await this.getAttributeContent(page, `${this.hookStatusInput(hookRow)}:checked`, 'value');
const inputValue = await this.getAttributeContent(page, `${this.hookHeaderStatusInput(hookRow)}:checked`, 'value');

// Return status=false if value='0' and true otherwise
return (inputValue !== '0');
Expand Down