diff --git a/tests/functional/cypress/e2e/cla-group.spec.ts b/tests/functional/cypress/e2e/cla-group.spec.ts index 1e8b0f853..4eff0f264 100644 --- a/tests/functional/cypress/e2e/cla-group.spec.ts +++ b/tests/functional/cypress/e2e/cla-group.spec.ts @@ -1,4 +1,4 @@ -// import {getBearerToken,bearerToken } from '../../support/commands.js'; +import {validateApiResponse} from '../support/commands' describe("To Validate 'GET, CREATE, UPDATE and DELETE' CLA groups API call on child project", function () { //Reference api doc: https://api-gw.dev.platform.linuxfoundation.org/cla-service/v4/api-docs#tag/cla-group @@ -28,10 +28,6 @@ describe("To Validate 'GET, CREATE, UPDATE and DELETE' CLA groups API call on ch const EnrollProjectsSFID='a09P000000DsNHCIA3' //project name: easyAutomChild1-GrandChild1 const child_Project_name='easyAutomChild1-GrandChild1' - //Headers - let optionalHeaders: Headers = { - "X-LFX-CACHE": false, - } before(() => { @@ -59,7 +55,7 @@ describe("To Validate 'GET, CREATE, UPDATE and DELETE' CLA groups API call on ch cy.request({ method: 'POST', url: `${claEndpoint}/cla-group`, - headers: optionalHeaders, + auth: { 'bearer': bearerToken, }, @@ -107,7 +103,7 @@ describe("To Validate 'GET, CREATE, UPDATE and DELETE' CLA groups API call on ch claGroupId = response.body.cla_group_id; //To validate schema of response - schemaValidate("claGroup/create_claGroup2.json",response.body); + validateApiResponse("claGroup/create_claGroup2.json",response.body); }); }); @@ -115,7 +111,7 @@ describe("To Validate 'GET, CREATE, UPDATE and DELETE' CLA groups API call on ch cy.request({ method: 'GET', url: `${claEndpoint}/foundation/${projectSfid}/cla-groups`, - headers: optionalHeaders, + auth: { 'bearer': bearerToken, } @@ -131,7 +127,7 @@ describe("To Validate 'GET, CREATE, UPDATE and DELETE' CLA groups API call on ch expect(list[0].cla_group_name).to.eql(cla_group_name) //To validate schema of response - schemaValidate("claGroup/list_claGroup.json",response.body); + validateApiResponse("claGroup/list_claGroup.json",response.body); }); }); @@ -139,7 +135,7 @@ describe("To Validate 'GET, CREATE, UPDATE and DELETE' CLA groups API call on ch cy.request({ method: 'PUT', url: `${claEndpoint}/cla-group/${claGroupId}`, - headers: optionalHeaders, + auth: { 'bearer': bearerToken, }, @@ -156,7 +152,7 @@ describe("To Validate 'GET, CREATE, UPDATE and DELETE' CLA groups API call on ch expect(response.body).to.have.property('cla_group_description', update_cla_group_description); //To validate schema of response - schemaValidate("claGroup/update_claGroup2.json",response.body); + validateApiResponse("claGroup/update_claGroup2.json",response.body); }); }); @@ -164,7 +160,7 @@ describe("To Validate 'GET, CREATE, UPDATE and DELETE' CLA groups API call on ch cy.request({ method: 'PUT', url: `${claEndpoint}/cla-group/${claGroupId}/enroll-projects`, - headers: optionalHeaders, + auth: { 'bearer': bearerToken, }, @@ -178,7 +174,7 @@ describe("To Validate 'GET, CREATE, UPDATE and DELETE' CLA groups API call on ch cy.request({ method: 'GET', url: `${claEndpoint}/foundation/${projectSfid}/cla-groups`, - headers: optionalHeaders, + auth: { 'bearer': bearerToken, }, @@ -200,7 +196,7 @@ describe("To Validate 'GET, CREATE, UPDATE and DELETE' CLA groups API call on ch cy.request({ method: 'PUT', url: `${claEndpoint}/cla-group/${claGroupId}/unenroll-projects`, - headers: optionalHeaders, + auth: { 'bearer': bearerToken, }, @@ -215,7 +211,7 @@ describe("To Validate 'GET, CREATE, UPDATE and DELETE' CLA groups API call on ch cy.request({ method: 'GET', url: `${claEndpoint}/project/${projectSfidOrg}/github/organizations`, - headers: optionalHeaders, + auth: { 'bearer': bearerToken, } @@ -234,7 +230,7 @@ describe("To Validate 'GET, CREATE, UPDATE and DELETE' CLA groups API call on ch cy.request({ method: 'PUT', url: `${claEndpoint}/project/${projectSfidOrg}/github/organizations/${gitHubOrgName}/config`, - headers: optionalHeaders, + auth: { 'bearer': bearerToken, }, @@ -253,7 +249,7 @@ describe("To Validate 'GET, CREATE, UPDATE and DELETE' CLA groups API call on ch cy.request({ method: 'DELETE', url: `${claEndpoint}/cla-group/${claGroupId}`, - headers: optionalHeaders, + auth: { 'bearer': bearerToken, }, @@ -265,7 +261,7 @@ describe("To Validate 'GET, CREATE, UPDATE and DELETE' CLA groups API call on ch cy.request({ method: 'GET', url: `${claEndpoint}/foundation/${projectSfid}/cla-groups`, - headers: optionalHeaders, + auth: { 'bearer': bearerToken, }, @@ -284,17 +280,4 @@ describe("To Validate 'GET, CREATE, UPDATE and DELETE' CLA groups API call on ch } }); - function schemaValidate(schemaPath,body){ - //To validate schema of response - const ajv = new Ajv(); - // Load the JSON schema - cy.fixture(schemaPath).then( - (schema) => { - const validate = ajv.compile(schema); - const isValid = validate(body); - // Assert that the response matches the schema - expect(isValid, 'API response schema is valid').to.be.true; - }); -} - }) \ No newline at end of file diff --git a/tests/functional/cypress/e2e/events.spec.ts b/tests/functional/cypress/e2e/events.spec.ts index b433999f4..90e6469c3 100644 --- a/tests/functional/cypress/e2e/events.spec.ts +++ b/tests/functional/cypress/e2e/events.spec.ts @@ -1,3 +1,4 @@ +import {validateApiResponse} from '../support/commands' describe("To Validate events are properly capture via API call", function () { //Reference api doc: https://api-gw.dev.platform.linuxfoundation.org/cla-service/v4/api-docs#tag/events const claEndpoint = `${Cypress.env("APP_URL")}cla-service/v4/events`; @@ -54,8 +55,7 @@ it("Get recent events of company and project - Record should Returns 200 Respons expect(Events).to.be.an('array'); // Assert that the array has at least one item expect(Events.length).to.be.greaterThan(0); - // schemaValidate("events/getFoundationEvents.json",list); - schemaValidate("events/getCompanyProjectEvents.json",list); + validateApiResponse("events/getCompanyProjectEvents.json",response); fetchNextRecords(claEndpointForNextKey,NextKey); }); }); @@ -82,8 +82,7 @@ it("Get events of foundation project - Record should Returns 200 Response", func expect(Events).to.be.an('array'); // Assert that the array has at least one item expect(Events.length).to.be.greaterThan(0); - // schemaValidate("events/getFoundationEvents.json",list); - schemaValidate("events/getFoundationEvents.json",list); + validateApiResponse("events/getFoundationEvents.json",list); fetchNextRecords(claEndpointForNextKey,NextKey); }); }); @@ -110,7 +109,7 @@ it("Get events of foundation project - Record should Returns 200 Response", func // Assert that the array has at least one item expect(Events.length).to.be.greaterThan(0); //To validate schema of response - schemaValidate("events/getProjectEvents",list) + validateApiResponse("events/getProjectEvents",list) fetchNextRecords(claEndpointForNextKey,NextKey); }); }); @@ -136,7 +135,7 @@ it("Get events of foundation project - Record should Returns 200 Response", func // Assert that the array has at least one item expect(Events.length).to.be.greaterThan(0); //To validate schema of response - schemaValidate("events/getProjectEvents.json",list) + validateApiResponse("events/getProjectEvents.json",list) }); }); @@ -182,18 +181,4 @@ it("Get events of foundation project - Record should Returns 200 Response", func } }); } - - function schemaValidate(schemaPath,body){ - //To validate schema of response - const ajv = new Ajv(); - // Load the JSON schema - cy.fixture(schemaPath).then( - (schema) => { - const validate = ajv.compile(schema); - const isValid = validate(body); - // Assert that the response matches the schema - expect(isValid, 'API response schema is valid').to.be.true; - }); - } - }) \ No newline at end of file diff --git a/tests/functional/cypress/e2e/foundation.spec.ts b/tests/functional/cypress/e2e/foundation.spec.ts index 39c3c9683..6de6e9c83 100644 --- a/tests/functional/cypress/e2e/foundation.spec.ts +++ b/tests/functional/cypress/e2e/foundation.spec.ts @@ -1,9 +1,9 @@ +import {validateApiResponse} from '../support/commands' describe("To Validate & get list of Foundation ClaGroups via API call", function () { //Reference api doc: https://api-gw.dev.platform.linuxfoundation.org/cla-service/v4/api-docs#tag/foundation const claEndpoint = `${Cypress.env("APP_URL")}cla-service/v4/foundation-mapping`; let bearerToken: string = ""; const foundationSFID='a09P000000DsNGsIAN'; //project name: easyAutom foundation - const Ajv = require('ajv'); before(() => { @@ -46,15 +46,7 @@ it("Get CLA Groups under a foundation- Record should Returns 200 Response", func // Assert that the array has at least one item expect(list[0].cla_groups.length).to.be.greaterThan(0); //To validate schema of response - const ajv = new Ajv(); - // Load the JSON schema - cy.fixture("foundation/listFoundationClaGroups.json").then( - (schema) => { - const validate = ajv.compile(schema); - const isValid = validate(response.body); - // Assert that the response matches the schema - expect(isValid, 'API response schema is valid').to.be.true; - }); + validateApiResponse("foundation/listFoundationClaGroups.json",response) }); }); diff --git a/tests/functional/cypress/e2e/github-organizations.spec.ts b/tests/functional/cypress/e2e/github-organizations.spec.ts index 2dcaa50cb..03368bd33 100644 --- a/tests/functional/cypress/e2e/github-organizations.spec.ts +++ b/tests/functional/cypress/e2e/github-organizations.spec.ts @@ -1,4 +1,4 @@ -// import {schemaValidate} from '../support/commands.js'; +import {validateApiResponse} from '../support/commands' describe("To Validate github-organizations API call", function () { //Reference api doc: https://api-gw.dev.platform.linuxfoundation.org/cla-service/v4/api-docs#tag/github-organizations @@ -13,10 +13,6 @@ const claEndpoint = `${Cypress.env("APP_URL")}cla-service/v4/project/${projectSf let bearerToken: string = ""; const claGroupId: string ="1baf67ab-d894-4edf-b6fc-c5f939db59f7"; -//Headers -let optionalHeaders: Headers = { - "X-LFX-CACHE": false, - } before(() => { @@ -44,7 +40,7 @@ it("Get list of Github organization associated with project - Record should Retu cy.request({ method: 'GET', url: `${claEndpoint}`, - headers: optionalHeaders, + auth: { 'bearer': bearerToken, } @@ -57,7 +53,7 @@ it("Get list of Github organization associated with project - Record should Retu expect(list[0].github_organization_name).to.eql('ApiAutomStandaloneOrg') expect(list[0].connection_status).to.eql('connected') //To validate schema of response - schemaValidate("github-organizations/getProjectGithubOrganizations.json",response.body); + validateApiResponse("github-organizations/getProjectGithubOrganizations.json",response.body); }); }); @@ -65,7 +61,7 @@ it("Get list of Github organization associated with project - Record should Retu cy.request({ method: 'PUT', url: `${claEndpoint}/${gitHubOrgName}/config`, - headers: optionalHeaders, + auth: { 'bearer': bearerToken, }, @@ -83,7 +79,7 @@ it("Get list of Github organization associated with project - Record should Retu cy.request({ method: 'POST', url: `${claEndpoint}`, - headers: optionalHeaders, + auth: { 'bearer': bearerToken, }, @@ -102,7 +98,7 @@ it("Get list of Github organization associated with project - Record should Retu expect(list[1].github_organization_name).to.eql(gitHubOrg) expect(list[1].connection_status).to.eql('connected') //To validate schema of response - schemaValidate("github-organizations/addProjectGithubOrganization.json",response.body); + validateApiResponse("github-organizations/addProjectGithubOrganization.json",response.body); }); }); @@ -110,7 +106,7 @@ it("Get list of Github organization associated with project - Record should Retu cy.request({ method: 'DELETE', url: `${claEndpoint}/${gitHubOrg}`, - headers: optionalHeaders, + auth: { 'bearer': bearerToken, }, @@ -120,16 +116,4 @@ it("Get list of Github organization associated with project - Record should Retu }); }); - function schemaValidate(schemaPath,body){ - //To validate schema of response - const ajv = new Ajv(); - // Load the JSON schema - cy.fixture(schemaPath).then( - (schema) => { - const validate = ajv.compile(schema); - const isValid = validate(body); - // Assert that the response matches the schema - expect(isValid, 'API response schema is valid').to.be.true; - }); -} }) \ No newline at end of file diff --git a/tests/functional/cypress/e2e/github-repositories.spec.ts b/tests/functional/cypress/e2e/github-repositories.spec.ts index 49d932684..fb6c8bf09 100644 --- a/tests/functional/cypress/e2e/github-repositories.spec.ts +++ b/tests/functional/cypress/e2e/github-repositories.spec.ts @@ -1,3 +1,4 @@ +import {validateApiResponse} from '../support/commands' describe("To Validate github-organizations API call", function () { //Reference api doc: https://api-gw.dev.platform.linuxfoundation.org/cla-service/v4/api-docs#tag/github-repositories @@ -14,10 +15,6 @@ describe("To Validate github-organizations API call", function () { let gitHubOrgName: string=""; let branch_name: string=""; - //Headers - let optionalHeaders: Headers = { - "X-LFX-CACHE": false, - } before(() => { @@ -45,7 +42,7 @@ describe("To Validate github-organizations API call", function () { cy.request({ method: 'GET', url: `${claEndpoint}`, - headers: optionalHeaders, + auth: { 'bearer': bearerToken, } @@ -62,7 +59,7 @@ describe("To Validate github-organizations API call", function () { repository_external_id2=list[1].repository_external_id; expect(list[0].repository_name).to.eql('ApiAutomStandaloneOrg/repo01') //To validate schema of response - schemaValidate("github-repositories/getRepositories.json",response.body); + validateApiResponse("github-repositories/getRepositories.json",response.body); }); }); @@ -70,7 +67,7 @@ describe("To Validate github-organizations API call", function () { cy.request({ method: 'DELETE', url: `${claEndpoint}/${repository_id}`, - headers: optionalHeaders, + auth: { 'bearer': bearerToken, } @@ -84,7 +81,7 @@ describe("To Validate github-organizations API call", function () { cy.request({ method: 'POST', url: `${claEndpoint}`, - headers: optionalHeaders, + auth: { 'bearer': bearerToken, }, @@ -110,7 +107,7 @@ describe("To Validate github-organizations API call", function () { expect(list[0].repository_name).to.eql('ApiAutomStandaloneOrg/repo01') //To validate schema of response - schemaValidate("github-repositories/getRepositories.json",response.body); + validateApiResponse("github-repositories/getRepositories.json",response.body); }); }); @@ -118,7 +115,7 @@ describe("To Validate github-organizations API call", function () { cy.request({ method: 'GET', url: `${claEndpoint}/${repository_id}/branch-protection`, - headers: optionalHeaders, + auth: { 'bearer': bearerToken, } @@ -129,7 +126,7 @@ describe("To Validate github-organizations API call", function () { if(list.protection_enabled){ //To validate schema of response - schemaValidate("github-repositories/getBranchProtection.json",response.body); + validateApiResponse("github-repositories/getBranchProtection.json",response.body); } else{ console.log('branch protection is false') @@ -141,7 +138,7 @@ it("Update github branch protection for given repository - Record should Returns cy.request({ method: 'POST', url: `${claEndpoint}/${repository_id}/branch-protection`, - headers: optionalHeaders, + auth: { 'bearer': bearerToken, }, @@ -158,22 +155,8 @@ it("Update github branch protection for given repository - Record should Returns }).then((response) => { expect(response.status).to.eq(200); //To validate schema of response - schemaValidate("github-repositories/getBranchProtection.json",response.body); - + validateApiResponse("github-repositories/getBranchProtection.json",response.body); }); }); - function schemaValidate(schemaPath,body){ - //To validate schema of response - const ajv = new Ajv(); - // Load the JSON schema - cy.fixture(schemaPath).then( - (schema) => { - const validate = ajv.compile(schema); - const isValid = validate(body); - // Assert that the response matches the schema - expect(isValid, 'API response schema is valid').to.be.true; - }); -} - }) \ No newline at end of file diff --git a/tests/functional/cypress/support/commands.js b/tests/functional/cypress/support/commands.js index 4075d384a..cf18537fb 100644 --- a/tests/functional/cypress/support/commands.js +++ b/tests/functional/cypress/support/commands.js @@ -1,18 +1,21 @@ -/* -// const Ajv = require('ajv'); -export function schemaValidate(schemaPath,body,ajv){ - - cy.fixture(schemaPath).then( - (schema) => { - console.log(schema) - const validate = ajv.compile(schema); - const isValid = validate(body); - // Assert that the response matches the schema - expect(isValid, 'API response schema is valid').to.be.true; - -}) -}; -*/ +import Ajv from 'ajv'; + +const ajv = new Ajv(); + //To validate API response using schema +export function validateApiResponse (schemaPath,response) { + cy.fixture(schemaPath).then( + (schema) => { + const validate = ajv.compile(schema); + const isValid = validate(response.body); + + // Assert that the response matches the schema + if (isValid) { +} else { + console.log('Data is not valid.', validate.errors); +} +expect(isValid, 'API response schema is valid').to.be.true; +}); +} // let bearerToken={}; // Cypress.Commands.add('setBearerToken', (value) => {