From 75fcbf1f0ac7e918580e3532de390ce4d8b4d6f4 Mon Sep 17 00:00:00 2001 From: Raju Udava Date: Wed, 22 Sep 2021 19:16:19 +0530 Subject: [PATCH] test: [cypress] Swagger API verification Signed-off-by: Raju Udava --- .../common/6b_auth_token_verification.js | 13 ++-- cypress/integration/common/6c_swagger_api.js | 68 +++++++++++++++++++ cypress/support/page_objects/mainPage.js | 5 ++ 3 files changed, 81 insertions(+), 5 deletions(-) create mode 100644 cypress/integration/common/6c_swagger_api.js diff --git a/cypress/integration/common/6b_auth_token_verification.js b/cypress/integration/common/6b_auth_token_verification.js index 9a7b14e3dc..1c033c912c 100644 --- a/cypress/integration/common/6b_auth_token_verification.js +++ b/cypress/integration/common/6b_auth_token_verification.js @@ -1,12 +1,16 @@ import { loginPage, projectsPage } from "../../support/page_objects/navigation" import { staticProjects, roles } from "../../support/page_objects/projectConstants" +import { mainPage } from "../../support/page_objects/mainPage" +// https://stackoverflow.com/questions/38552003/how-to-decode-jwt-token-in-javascript-without-using-a-library +// sample code to parse Jwt +// function parseJwt (token) { var base64Url = token.split('.')[1]; var base64 = base64Url.replace(/-/g, '+').replace(/_/g, '/'); var jsonPayload = decodeURIComponent(atob(base64).split('').map(function(c) { return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2); - }).join('')); + }).join('')) return JSON.parse(jsonPayload); }; @@ -22,10 +26,9 @@ const genTest = (type, xcdb) => { }) it('auth info', ()=> { - let obj = JSON.parse(localStorage['vuex']) - var decoded = parseJwt(obj["users"]["token"]) - let email = decoded["email"] - let role = decoded["roles"] + var decodedToken = parseJwt(mainPage.getAuthToken()) + let email = decodedToken["email"] + let role = decodedToken["roles"] assert.equal(email, roles.owner.credentials.username) assert.equal(role, "user") diff --git a/cypress/integration/common/6c_swagger_api.js b/cypress/integration/common/6c_swagger_api.js new file mode 100644 index 0000000000..ce316343d8 --- /dev/null +++ b/cypress/integration/common/6c_swagger_api.js @@ -0,0 +1,68 @@ + +import { loginPage } from "../../support/page_objects/navigation" +import { mainPage } from "../../support/page_objects/mainPage" + +describe(`Swagger API - Test preparation`, () => { + before(()=> { + loginPage.loginAndOpenProject('rest', false) + }) + + it("[REST] open project & record swagger URL, AuthToken", () => { + let authToken = mainPage.getAuthToken() + cy.url().then( (url) => { + // retrieve project name from URL & use it to construct Swagger URL + // URL on homepage: http://localhost:3000/#/nc/externalrest_weUO?type=roles&dbalias=&name=Team%20%26%20Auth%20 + // Swagger URL: http://localhost:8080/nc/externalrest_weUO/db/swagger + // + const projectName = url.split("/")[5].split("?")[0]; + let swaggerURL = `http://localhost:8080/nc/${projectName}/db/swagger` + + // exchange information between two tests using a file + // https://stackoverflow.com/questions/52050657/what-is-the-best-practice-of-pass-states-between-tests-in-cypress + // + cy.writeFile("shared.json", {SWAGGER_URL: swaggerURL, AUTH_TOKEN: authToken}) + }) + }) +}) + +describe(`Swagger page, base verification`, () => { + + // returns swagger button intended for + // + const getSwaggerButton = (tag, idx, desc) => { + return cy.get(`#operations-tag-${tag}`).next().find('.opblock').eq(idx).find(`button:contains(${desc})`) + } + + // basic authentication tag verification + // + it("Swagger URL access & basic validation", () => { + // retrieve information stored in previous IT block + // + cy.readFile("shared.json").then((jsonPayload) => { + let URL = jsonPayload.SWAGGER_URL + let Token = jsonPayload.AUTH_TOKEN + + cy.visit(URL) + cy.log(Token) + + // validate + cy.get('#operations-tag-Authentication').next().find('.opblock').should('has.length', 9) + getSwaggerButton("Authentication", 0, "User login").should('exist') + getSwaggerButton("Authentication", 1, "User signup").should('exist') + getSwaggerButton("Authentication", 2, "Password Forgot").should('exist') + getSwaggerButton("Authentication", 3, "Email validate link").should('exist') + getSwaggerButton("Authentication", 4, "Validate password reset token").should('exist') + getSwaggerButton("Authentication", 5, "Password reset").should('exist') + getSwaggerButton("Authentication", 6, "User details").should('exist') + getSwaggerButton("Authentication", 7, "Update user details").should('exist') + getSwaggerButton("Authentication", 8, "Update user details").should('exist') + }) + }) + + // clean-up created file + after(() => { + cy.exec("del shared.json").then(()=> { + cy.log("file cleaned up!") + }) + }) +}) diff --git a/cypress/support/page_objects/mainPage.js b/cypress/support/page_objects/mainPage.js index fd2a080b77..44b9c23a59 100644 --- a/cypress/support/page_objects/mainPage.js +++ b/cypress/support/page_objects/mainPage.js @@ -120,6 +120,11 @@ export class _mainPage { cy.get('.nc-column-name-input input', {timeout: 3000}).clear().type(colName) cy.get('.nc-col-create-or-edit-card').contains('Save').click() } + + getAuthToken = () => { + let obj = JSON.parse(localStorage['vuex']) + return obj["users"]["token"] + } }