From b4f57492516e98b2dae96420e602bef9d9de2370 Mon Sep 17 00:00:00 2001 From: veerendra thakur Date: Mon, 11 Sep 2023 14:39:22 +0530 Subject: [PATCH] Added cypress test case for gitlab-organizations API Signed-off-by: veerendra thakur --- .../cypress/appConfig/config.dev.ts | 5 + .../cypress/e2e/gitlab-organizations.cy.ts | 134 ++++++++++++++++++ .../addProjectGitlabOrganization.json | 126 ++++++++++++++++ .../getGitLabGroupMembers.json | 61 ++++++++ .../getProjectGitlabOrganizations.json | 126 ++++++++++++++++ .../updateProjectGitlabGroupConfig.json | 126 ++++++++++++++++ tests/functional/cypress/support/commands.js | 10 +- 7 files changed, 586 insertions(+), 2 deletions(-) create mode 100644 tests/functional/cypress/e2e/gitlab-organizations.cy.ts create mode 100644 tests/functional/cypress/fixtures/gitlab-organizations/addProjectGitlabOrganization.json create mode 100644 tests/functional/cypress/fixtures/gitlab-organizations/getGitLabGroupMembers.json create mode 100644 tests/functional/cypress/fixtures/gitlab-organizations/getProjectGitlabOrganizations.json create mode 100644 tests/functional/cypress/fixtures/gitlab-organizations/updateProjectGitlabGroupConfig.json diff --git a/tests/functional/cypress/appConfig/config.dev.ts b/tests/functional/cypress/appConfig/config.dev.ts index e83f63beb..354652aa9 100644 --- a/tests/functional/cypress/appConfig/config.dev.ts +++ b/tests/functional/cypress/appConfig/config.dev.ts @@ -34,4 +34,9 @@ export const appConfig = { /* project */ projectName:'easyAutom-child2', + + /* GitLab-Organization*/ + gitLabOrganizationName:'sunGitlabGroup', + gitLabOrganizationFullPath:'https://gitlab.com/cla_dev_automationgroup', + groupId:'72539854', }; \ No newline at end of file diff --git a/tests/functional/cypress/e2e/gitlab-organizations.cy.ts b/tests/functional/cypress/e2e/gitlab-organizations.cy.ts new file mode 100644 index 000000000..a088a82e2 --- /dev/null +++ b/tests/functional/cypress/e2e/gitlab-organizations.cy.ts @@ -0,0 +1,134 @@ +import {validateApiResponse,validate_200_Status,getTokenKey} from '../support/commands'; + +describe("To Validate & get list of gitlab-organizations via API call", function () { + +// Define a variable for the environment +const environment = Cypress.env("CYPRESS_ENV"); + +// Import the appropriate configuration based on the environment +let appConfig; +if (environment === 'dev') { + appConfig = require('../appConfig/config.dev.ts').appConfig; +} else if (environment === 'production') { + appConfig = require('../appConfig/config.production.ts').appConfig; +} + + //Reference api doc: https://api-gw.dev.platform.linuxfoundation.org/cla-service/v4/api-docs#tag/gitlab-organizations + const claEndpoint = `${Cypress.env("APP_URL")}cla-service/v4`; + const projectSFID=appConfig.projectSFID; //project name: sun + let gitLabOrgName=appConfig.gitLabOrganizationName; + const gitLabGroupID=appConfig.groupId; + let gitLabOrganizationFullPath=appConfig.gitLabOrganizationFullPath;// it will update on POST request + let claGroupId=""; + let organizationExternalId=""; + + let bearerToken: string = null; +before(() => { + if(bearerToken==null){ + getTokenKey(bearerToken); + cy.window().then((win) => { + bearerToken = win.localStorage.getItem('bearerToken'); + }); + } + }); + +it("Get the Gitlab organizations of the project", function () { + getGitLabGroupMembers(); + }); + +it("List members of a given GitLab group", function () { + cy.request({ + method: 'GET', + url: `${claEndpoint}/gitlab/group/${organizationExternalId}/members`, + auth: { + 'bearer': bearerToken, + } + }).then((response) => { + validate_200_Status(response); + //To validate schema of response + validateApiResponse("gitlab-organizations/getGitLabGroupMembers.json",response.body); + }); + }); + +it("Update Gitlab Group/Organization Configuration", function () { + cy.request({ + method: 'PUT', + url: `${claEndpoint}/project/${projectSFID}/gitlab/group/${gitLabGroupID}/config`, + auth: { + 'bearer': bearerToken, + }, + body:{ + "auto_enabled": true, + "auto_enabled_cla_group_id": claGroupId, + "branch_protection_enabled": true + } + }).then((response) => { + validate_200_Status(response); + //To validate schema of response + validateApiResponse("gitlab-organizations/updateProjectGitlabGroupConfig.json",response.body); + }); + }); + +it("Add new Gitlab Organization in the project", function () { + console.log("gitLabOrganizationFullPath: "+gitLabOrganizationFullPath) + cy.request({ + method: 'POST', + url: `${claEndpoint}/project/${projectSFID}/gitlab/organizations`, + auth: { + 'bearer': bearerToken, + },body:{ + "auto_enabled": false, + "auto_enabled_cla_group_id": claGroupId, + "branch_protection_enabled": false, + "group_id": parseInt(gitLabGroupID, 10), + "organization_full_path": gitLabOrganizationFullPath + },failOnStatusCode: false + }).then((response) => { + validate_200_Status(response); + //To validate schema of response + validateApiResponse("gitlab-organizations/addProjectGitlabOrganization.json",response.body); + }); + }); + +it("Delete Gitlab Group/Organization Configuration", function () { + // Define the URL + const url = gitLabOrganizationFullPath; + // Use JavaScript string methods to extract the desired substring + gitLabOrganizationFullPath = url.split('/').pop(); + // Log or use the extracted substring as needed + cy.log(gitLabOrganizationFullPath); + cy.request({ + method: 'DELETE', + url: `${claEndpoint}/project/${projectSFID}/gitlab/organization?organization_full_path=${gitLabOrganizationFullPath}`, + auth: { + 'bearer': bearerToken, + },failOnStatusCode: false + }).then((response) => { + expect(response.status).to.eq(204); + }); + }); + +function getGitLabGroupMembers(){ + cy.request({ + method: 'GET', + url: `${claEndpoint}/project/${projectSFID}/gitlab/organizations`, + auth: { + 'bearer': bearerToken, + } + }).then((response) => { + validate_200_Status(response); + let list=response.body.list; + for(let i=0;i<=list.length-1;i++){ + if(list[i].organization_name===gitLabOrgName){ + organizationExternalId=list[i].organization_external_id; + // gitLabOrganizationFullPath=list[i].organization_full_path; + claGroupId=list[i].repositories[0].cla_group_id + break; + } + } + //To validate schema of response + validateApiResponse("gitlab-organizations/getProjectGitlabOrganizations.json",response.body); + }); + } + +}) \ No newline at end of file diff --git a/tests/functional/cypress/fixtures/gitlab-organizations/addProjectGitlabOrganization.json b/tests/functional/cypress/fixtures/gitlab-organizations/addProjectGitlabOrganization.json new file mode 100644 index 000000000..3edba1899 --- /dev/null +++ b/tests/functional/cypress/fixtures/gitlab-organizations/addProjectGitlabOrganization.json @@ -0,0 +1,126 @@ +{ + "$schema": "http://json-schema.org/draft-07/schema#", + "type": "object", + "properties": { + "list": { + "type": "array", + "items": [ + { + "type": "object", + "properties": { + "auto_enable_cla_group_id": { + "type": "string" + }, + "auto_enabled": { + "type": "boolean" + }, + "auto_enabled_cla_group_name": { + "type": "string" + }, + "branch_protection_enabled": { + "type": "boolean" + }, + "connection_status": { + "type": "string" + }, + "connection_status_message": { + "type": "string" + }, + "installation_url": { + "type": "string" + }, + "organization_external_id": { + "type": "integer" + }, + "organization_full_path": { + "type": "string" + }, + "organization_name": { + "type": "string" + }, + "organization_url": { + "type": "string" + }, + "parent_project_sfid": { + "type": "string" + }, + "project_sfid": { + "type": "string" + }, + "repositories": { + "type": "array", + "items": [ + { + "type": "object", + "properties": { + "cla_group_id": { + "type": "string" + }, + "connection_status": { + "type": "string" + }, + "enabled": { + "type": "boolean" + }, + "parent_project_id": { + "type": "string" + }, + "project_id": { + "type": "string" + }, + "repository_full_path": { + "type": "string" + }, + "repository_gitlab_id": { + "type": "integer" + }, + "repository_id": { + "type": "string" + }, + "repository_name": { + "type": "string" + }, + "repository_url": { + "type": "string" + } + }, + "required": [ + "cla_group_id", + "connection_status", + "enabled", + "parent_project_id", + "project_id", + "repository_full_path", + "repository_gitlab_id", + "repository_id", + "repository_name", + "repository_url" + ] + } + ] + } + }, + "required": [ + "auto_enable_cla_group_id", + "auto_enabled", + "auto_enabled_cla_group_name", + "branch_protection_enabled", + "connection_status", + "connection_status_message", + "installation_url", + "organization_external_id", + "organization_full_path", + "organization_name", + "organization_url", + "parent_project_sfid", + "project_sfid", + "repositories" + ] + } + ] + } + }, + "required": [ + "list" + ] + } \ No newline at end of file diff --git a/tests/functional/cypress/fixtures/gitlab-organizations/getGitLabGroupMembers.json b/tests/functional/cypress/fixtures/gitlab-organizations/getGitLabGroupMembers.json new file mode 100644 index 000000000..edc60bfa2 --- /dev/null +++ b/tests/functional/cypress/fixtures/gitlab-organizations/getGitLabGroupMembers.json @@ -0,0 +1,61 @@ +{ + "$schema": "http://json-schema.org/draft-07/schema#", + "type": "object", + "properties": { + "list": { + "type": "array", + "items": [ + { + "type": "object", + "properties": { + "access_level": { + "type": "string" + }, + "avatar_url": { + "type": "string" + }, + "created_at": { + "type": "string" + }, + "expired_at": { + "type": "string" + }, + "group_saml_identity": { + "type": "string" + }, + "id": { + "type": "string" + }, + "name": { + "type": "string" + }, + "state": { + "type": "string" + }, + "username": { + "type": "string" + }, + "web_url": { + "type": "string" + } + }, + "required": [ + "access_level", + "avatar_url", + "created_at", + "expired_at", + "group_saml_identity", + "id", + "name", + "state", + "username", + "web_url" + ] + } + ] + } + }, + "required": [ + "list" + ] + } \ No newline at end of file diff --git a/tests/functional/cypress/fixtures/gitlab-organizations/getProjectGitlabOrganizations.json b/tests/functional/cypress/fixtures/gitlab-organizations/getProjectGitlabOrganizations.json new file mode 100644 index 000000000..3edba1899 --- /dev/null +++ b/tests/functional/cypress/fixtures/gitlab-organizations/getProjectGitlabOrganizations.json @@ -0,0 +1,126 @@ +{ + "$schema": "http://json-schema.org/draft-07/schema#", + "type": "object", + "properties": { + "list": { + "type": "array", + "items": [ + { + "type": "object", + "properties": { + "auto_enable_cla_group_id": { + "type": "string" + }, + "auto_enabled": { + "type": "boolean" + }, + "auto_enabled_cla_group_name": { + "type": "string" + }, + "branch_protection_enabled": { + "type": "boolean" + }, + "connection_status": { + "type": "string" + }, + "connection_status_message": { + "type": "string" + }, + "installation_url": { + "type": "string" + }, + "organization_external_id": { + "type": "integer" + }, + "organization_full_path": { + "type": "string" + }, + "organization_name": { + "type": "string" + }, + "organization_url": { + "type": "string" + }, + "parent_project_sfid": { + "type": "string" + }, + "project_sfid": { + "type": "string" + }, + "repositories": { + "type": "array", + "items": [ + { + "type": "object", + "properties": { + "cla_group_id": { + "type": "string" + }, + "connection_status": { + "type": "string" + }, + "enabled": { + "type": "boolean" + }, + "parent_project_id": { + "type": "string" + }, + "project_id": { + "type": "string" + }, + "repository_full_path": { + "type": "string" + }, + "repository_gitlab_id": { + "type": "integer" + }, + "repository_id": { + "type": "string" + }, + "repository_name": { + "type": "string" + }, + "repository_url": { + "type": "string" + } + }, + "required": [ + "cla_group_id", + "connection_status", + "enabled", + "parent_project_id", + "project_id", + "repository_full_path", + "repository_gitlab_id", + "repository_id", + "repository_name", + "repository_url" + ] + } + ] + } + }, + "required": [ + "auto_enable_cla_group_id", + "auto_enabled", + "auto_enabled_cla_group_name", + "branch_protection_enabled", + "connection_status", + "connection_status_message", + "installation_url", + "organization_external_id", + "organization_full_path", + "organization_name", + "organization_url", + "parent_project_sfid", + "project_sfid", + "repositories" + ] + } + ] + } + }, + "required": [ + "list" + ] + } \ No newline at end of file diff --git a/tests/functional/cypress/fixtures/gitlab-organizations/updateProjectGitlabGroupConfig.json b/tests/functional/cypress/fixtures/gitlab-organizations/updateProjectGitlabGroupConfig.json new file mode 100644 index 000000000..3edba1899 --- /dev/null +++ b/tests/functional/cypress/fixtures/gitlab-organizations/updateProjectGitlabGroupConfig.json @@ -0,0 +1,126 @@ +{ + "$schema": "http://json-schema.org/draft-07/schema#", + "type": "object", + "properties": { + "list": { + "type": "array", + "items": [ + { + "type": "object", + "properties": { + "auto_enable_cla_group_id": { + "type": "string" + }, + "auto_enabled": { + "type": "boolean" + }, + "auto_enabled_cla_group_name": { + "type": "string" + }, + "branch_protection_enabled": { + "type": "boolean" + }, + "connection_status": { + "type": "string" + }, + "connection_status_message": { + "type": "string" + }, + "installation_url": { + "type": "string" + }, + "organization_external_id": { + "type": "integer" + }, + "organization_full_path": { + "type": "string" + }, + "organization_name": { + "type": "string" + }, + "organization_url": { + "type": "string" + }, + "parent_project_sfid": { + "type": "string" + }, + "project_sfid": { + "type": "string" + }, + "repositories": { + "type": "array", + "items": [ + { + "type": "object", + "properties": { + "cla_group_id": { + "type": "string" + }, + "connection_status": { + "type": "string" + }, + "enabled": { + "type": "boolean" + }, + "parent_project_id": { + "type": "string" + }, + "project_id": { + "type": "string" + }, + "repository_full_path": { + "type": "string" + }, + "repository_gitlab_id": { + "type": "integer" + }, + "repository_id": { + "type": "string" + }, + "repository_name": { + "type": "string" + }, + "repository_url": { + "type": "string" + } + }, + "required": [ + "cla_group_id", + "connection_status", + "enabled", + "parent_project_id", + "project_id", + "repository_full_path", + "repository_gitlab_id", + "repository_id", + "repository_name", + "repository_url" + ] + } + ] + } + }, + "required": [ + "auto_enable_cla_group_id", + "auto_enabled", + "auto_enabled_cla_group_name", + "branch_protection_enabled", + "connection_status", + "connection_status_message", + "installation_url", + "organization_external_id", + "organization_full_path", + "organization_name", + "organization_url", + "parent_project_sfid", + "project_sfid", + "repositories" + ] + } + ] + } + }, + "required": [ + "list" + ] + } \ No newline at end of file diff --git a/tests/functional/cypress/support/commands.js b/tests/functional/cypress/support/commands.js index ef0059eda..ea453a879 100644 --- a/tests/functional/cypress/support/commands.js +++ b/tests/functional/cypress/support/commands.js @@ -13,8 +13,14 @@ export function validateApiResponse (schemaPath,response) { if (isValid) { cy.log('API response schema is valid'); expect(isValid, 'API response schema is valid').to.be.true; - } else { - cy.log('API response schema is not valid, but the test will continue.', validate.errors); + } else { + Cypress.on('test:after:run', (test, runnable) => { + const testName = `${runnable.parent.title} - ${test.title}` + cy.log(`API response schema is not valid for Test Case : ${testName}`) + console.log(`API response schema is not valid for Test Case : ${testName}`) + cy.log('Schema Error : ', validate.errors); + console.error('Schema Error : ', validate.errors); + }) } });