fix(ui): call logout api to invalidate token (#8243)

This commit is contained in:
Chirag Madlani 2022-10-19 20:55:31 +05:30 committed by GitHub
parent 97cdd22729
commit 38e32af072
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 66 additions and 11 deletions

View File

@ -14,6 +14,10 @@
export const uuid = () => Cypress._.random(0, 1e6); export const uuid = () => Cypress._.random(0, 1e6);
const id = uuid(); const id = uuid();
export const BASE_URL = location.origin;
export const LOGIN_ERROR_MESSAGE = 'You have entered an invalid username or password.';
export const MYDATA_SUMMARY_OPTIONS = { export const MYDATA_SUMMARY_OPTIONS = {
tables: 'tables', tables: 'tables',
topics: 'topics', topics: 'topics',

View File

@ -0,0 +1,36 @@
/*
* Copyright 2021 Collate
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { interceptURL, login, verifyResponseStatusCode } from "../../common/common";
import { BASE_URL, LOGIN } from "../../constants/constants";
describe("Logout User", () => {
beforeEach(() => {
login(LOGIN.username, LOGIN.password);
cy.goToHomePage();
});
it("After login logout the user and invalidate the token", () => {
cy.get('[data-testid="avatar"]').should("be.visible").click()
interceptURL('POST', '/api/v1/users/logout', 'logoutUser');
cy.get('[data-testid="menu-item-Logout"]').should("be.visible").click()
// verify the logout request
verifyResponseStatusCode('@logoutUser', 200);
cy.url().should('eq', `${BASE_URL}/signin`);
})
})

View File

@ -12,6 +12,7 @@
*/ */
import { interceptURL, login, verifyResponseStatusCode } from '../../common/common'; import { interceptURL, login, verifyResponseStatusCode } from '../../common/common';
import { BASE_URL, LOGIN_ERROR_MESSAGE } from '../../constants/constants';
const CREDENTIALS = { const CREDENTIALS = {
firstName: 'Test', firstName: 'Test',
@ -22,10 +23,6 @@ const CREDENTIALS = {
const invalidEmail = 'userTest@openmetadata.org'; const invalidEmail = 'userTest@openmetadata.org';
const invalidPassword = 'testUsers@123'; const invalidPassword = 'testUsers@123';
const baseURL = location.origin;
const ERROR_MESSAGE = 'You have entered an invalid username or password.';
describe('Login flow should work properly', () => { describe('Login flow should work properly', () => {
it('Signup and Login with signed up credentials', () => { it('Signup and Login with signed up credentials', () => {
interceptURL('GET', 'api/v1/config/auth', 'getLoginPage'); interceptURL('GET', 'api/v1/config/auth', 'getLoginPage');
@ -57,13 +54,13 @@ describe('Login flow should work properly', () => {
.type(CREDENTIALS.password); .type(CREDENTIALS.password);
//Click on create account button //Click on create account button
cy.get('.ant-btn').contains('Create Account').should('be.visible').click(); cy.get('.ant-btn').contains('Create Account').should('be.visible').click();
cy.url().should('eq', `${baseURL}/signin`).and('contain', 'signin'); cy.url().should('eq', `${BASE_URL}/signin`).and('contain', 'signin');
//Login with the created user //Login with the created user
login(CREDENTIALS.email, CREDENTIALS.password); login(CREDENTIALS.email, CREDENTIALS.password);
cy.goToHomePage(); cy.goToHomePage();
cy.url().should('eq', `${baseURL}/my-data`); cy.url().should('eq', `${BASE_URL}/my-data`);
//Verify user profile //Verify user profile
cy.get('[data-testid="avatar"]').should('be.visible').click(); cy.get('[data-testid="avatar"]').should('be.visible').click();
@ -89,14 +86,14 @@ describe('Login flow should work properly', () => {
cy.get('[data-testid="login-error-container"]') cy.get('[data-testid="login-error-container"]')
.should('be.visible') .should('be.visible')
.invoke('text') .invoke('text')
.should('eq', ERROR_MESSAGE); .should('eq', LOGIN_ERROR_MESSAGE);
//Login with invalid password //Login with invalid password
login(CREDENTIALS.email, invalidPassword); login(CREDENTIALS.email, invalidPassword);
cy.get('[data-testid="login-error-container"]') cy.get('[data-testid="login-error-container"]')
.should('be.visible') .should('be.visible')
.invoke('text') .invoke('text')
.should('eq', ERROR_MESSAGE); .should('eq', LOGIN_ERROR_MESSAGE);
}); });
it('Forgot password and login with new password', () => { it('Forgot password and login with new password', () => {
@ -110,7 +107,7 @@ describe('Login flow should work properly', () => {
.click(); .click();
cy.url() cy.url()
.should('eq', `${baseURL}/forgot-password`) .should('eq', `${BASE_URL}/forgot-password`)
.and('contain', 'forgot-password'); .and('contain', 'forgot-password');
//Enter email //Enter email
cy.get('[id="email"]').should('be.visible').clear().type(CREDENTIALS.email); cy.get('[id="email"]').should('be.visible').clear().type(CREDENTIALS.email);

View File

@ -20,6 +20,7 @@ import {
basicAuthSignIn, basicAuthSignIn,
checkEmailInUse, checkEmailInUse,
generatePasswordResetLink, generatePasswordResetLink,
logoutUser,
resetPassword, resetPassword,
} from '../../axiosAPIs/auth-API'; } from '../../axiosAPIs/auth-API';
import { import {
@ -192,8 +193,16 @@ const BasicAuthProvider = ({
}; };
const handleLogout = async () => { const handleLogout = async () => {
localState.removeOidcToken(); const token = localState.getOidcToken();
history.push(ROUTES.SIGNIN); if (token) {
try {
await logoutUser(token);
localState.removeOidcToken();
history.push(ROUTES.SIGNIN);
} catch (error) {
showErrorToast(error as AxiosError);
}
}
}; };
const contextValue = { const contextValue = {

View File

@ -101,3 +101,12 @@ export const generateRandomPwd = async () => {
return response.data; return response.data;
}; };
/**
* Logout a User(Only called for saml and basic Auth)
*/
export const logoutUser = async (token: string) => {
const response = await axiosClient.post(`${apiPath}/logout`, { token });
return response.data;
};