Skip to content

Commit

Permalink
ATHENS-7451 - fix red role review icon conditions (#2684)
Browse files Browse the repository at this point in the history
Signed-off-by: aporss <art.porss@yahooinc.com>
  • Loading branch information
ArtjomsPorss authored Aug 14, 2024
1 parent a5178cd commit fea1fe6
Show file tree
Hide file tree
Showing 5 changed files with 159 additions and 18 deletions.
118 changes: 112 additions & 6 deletions ui/src/__tests__/components/role/RoleRow.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,19 @@
* limitations under the License.
*/
import React from 'react';
import RoleRow from '../../../components/role/RoleRow';
import RoleRow, { getSmallestExpiryOrReview, isReviewRequired } from '../../../components/role/RoleRow';
import { _ } from 'lodash';
import { colors } from '../../../components/denali/styles';
import { renderWithRedux } from '../../../tests_utils/ComponentsTestUtils';
import { fireEvent, screen, waitFor } from '@testing-library/react';
import { configure } from '@testing-library/dom';
import { act } from 'react-dom/test-utils';
import { USER_DOMAIN } from '../../../components/constants/constants';
import { fireEvent, screen } from '@testing-library/react';
import moment from 'moment';

describe('RoleRow', (object, method) => {

afterAll(() => {
jest.clearAllMocks();
});

describe('RoleRow', () => {
it('should render', () => {
const details = {
name: 'athens:role.ztssia_cert_rotate',
Expand Down Expand Up @@ -79,4 +83,106 @@ describe('RoleRow', () => {
fireEvent.mouseEnter(descriptionIcon);
await screen.findByText('test description');
});

it('getSmallestExpiryOrReview check memberExpiryDays value is picked up', async () => {
const role = {memberExpiryDays: 5, lastReviewedDate: '2024-07-24T10:58:07.533Z'};
let actualDays = getSmallestExpiryOrReview(role);

expect(_.isEqual(role.memberExpiryDays, actualDays)).toBeTruthy();
});

it('getSmallestExpiryOrReview check serviceExpiryDays value is picked up', async () => {
const role = {serviceExpiryDays: 9, lastReviewedDate: '2024-07-24T10:58:07.533Z'};
let actualDays = getSmallestExpiryOrReview(role);

expect(_.isEqual(role.serviceExpiryDays, actualDays)).toBeTruthy();
});

it('getSmallestExpiryOrReview check memberReviewDays value is picked up', async () => {
const role = {memberReviewDays: 6, lastReviewedDate: '2024-07-24T10:58:07.533Z'};
let actualDays = getSmallestExpiryOrReview(role);

expect(_.isEqual(role.memberReviewDays, actualDays)).toBeTruthy();
});

it('getSmallestExpiryOrReview check serviceReviewDays value is picked up', async () => {
const role = {serviceReviewDays: 10, lastReviewedDate: '2024-07-24T10:58:07.533Z'};
let actualDays = getSmallestExpiryOrReview(role);

expect(_.isEqual(role.serviceReviewDays, actualDays)).toBeTruthy();
});

it('getSmallestExpiryOrReview check groupReviewDays value is picked up', async () => {
const role = {groupReviewDays: 8, lastReviewedDate: '2024-07-24T10:58:07.533Z'};
let actualDays = getSmallestExpiryOrReview(role);

expect(_.isEqual(role.groupReviewDays, actualDays)).toBeTruthy();
});

it('getSmallestExpiryOrReview check groupExpiryDays value is picked up', async () => {
const role = {groupExpiryDays: 7, lastReviewedDate: '2024-07-24T10:58:07.533Z'};
let actualDays = getSmallestExpiryOrReview(role);

expect(_.isEqual(role.groupExpiryDays, actualDays)).toBeTruthy();
});

it("getSmallestExpiryOrReview check smallest value picked, non 0, null doesn't produce error", async () => {
const role = {
memberExpiryDays: 0,
serviceExpiryDays: null,
memberReviewDays: 6,
serviceReviewDays: 10,
groupReviewDays: 8,
groupExpiryDays: 7,
lastReviewedDate: '2024-07-24T10:58:07.533Z'
};
let actualDays = getSmallestExpiryOrReview(role);

expect(_.isEqual(role.memberReviewDays, actualDays)).toBeTruthy();
});

it("getSmallestExpiryOrReview when no expiry or review days assigned to the role, return 0", async () => {
const role = {
lastReviewedDate: '2024-07-24T10:58:07.533Z'
};
let actualDays = getSmallestExpiryOrReview(role);

expect(_.isEqual(0, actualDays)).toBeTruthy();
});

it("isReviewRequired when no expiry or review days assigned to the role, return false", async () => {
const role = {
};
let reviewRequired = isReviewRequired(role);

expect(_.isEqual(false, reviewRequired)).toBeTruthy();
});

it("isReviewRequired when lastReviewDate is more than 80% of memberExpiryDate ago - return true", async () => {
const role = {
lastReviewedDate: '2024-07-01T11:59:59.000Z',
memberExpiryDays: '10'
};
// mock current date
jest.spyOn(moment.prototype, 'utc')
.mockReturnValue(moment('2024-07-09T12:00:00.000Z').utc());

let reviewRequired = isReviewRequired(role);

expect(_.isEqual(true, reviewRequired)).toBeTruthy();
});

it("isReviewRequired when lastReviewDate is less than 80% of memberExpiryDate ago - return false", async () => {
const role = {
lastReviewedDate: '2024-07-01T12:00:01.000Z',
memberExpiryDays: '10'
};
// mock current date
// jest.spyOn(moment.prototype, 'utc')
// .mockReturnValue(moment('2024-07-09T12:00:00.000Z').utc());

let reviewRequired = isReviewRequired(role);

expect(_.isEqual(false, reviewRequired)).toBeTruthy();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,6 @@ exports[`RoleRow should render 1`] = `
viewBox="0 0 1024 1024"
width="1.25em"
>
<title>
assignment-priority
</title>
<path
d="M512 640c23.564 0 42.667-19.103 42.667-42.667v0-256c0-23.564-19.103-42.667-42.667-42.667s-42.667 19.103-42.667 42.667v0 256c0 23.564 19.103 42.667 42.667 42.667v0z"
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,9 +200,6 @@ exports[`RoleTable should render 1`] = `
viewBox="0 0 1024 1024"
width="1.25em"
>
<title>
assignment-priority
</title>
<path
d="M512 640c23.564 0 42.667-19.103 42.667-42.667v0-256c0-23.564-19.103-42.667-42.667-42.667s-42.667 19.103-42.667 42.667v0 256c0 23.564 19.103 42.667 42.667 42.667v0z"
/>
Expand Down Expand Up @@ -396,9 +393,6 @@ exports[`RoleTable should render 1`] = `
viewBox="0 0 1024 1024"
width="1.25em"
>
<title>
assignment-priority
</title>
<path
d="M512 640c23.564 0 42.667-19.103 42.667-42.667v0-256c0-23.564-19.103-42.667-42.667-42.667s-42.667 19.103-42.667 42.667v0 256c0 23.564 19.103 42.667 42.667 42.667v0z"
/>
Expand Down
2 changes: 2 additions & 0 deletions ui/src/components/constants/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -277,3 +277,5 @@ export const ENVIRONMENT_DROPDOWN_OPTIONS = [
export const MEMBER_AUTHORITY_FILTER_DISABLED = 1;
export const MEMBER_AUTHORITY_SYSTEM_SUSPENDED = 2;
export const MEMBER_ATHENZ_SYSTEM_DISABLED = 4;

export const ROLE_PERCENTAGE_OF_DAYS_TILL_NEXT_REVIEW = 0.2;
48 changes: 45 additions & 3 deletions ui/src/components/role/RoleRow.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import { css, keyframes } from '@emotion/react';
import { deleteRole } from '../../redux/thunks/roles';
import { connect } from 'react-redux';
import { selectDomainAuditEnabled } from '../../redux/selectors/domainData';
import moment from 'moment-timezone';
import { ROLE_PERCENTAGE_OF_DAYS_TILL_NEXT_REVIEW } from '../constants/constants';

const TDStyledName = styled.div`
background-color: ${(props) => props.color};
Expand Down Expand Up @@ -93,6 +95,46 @@ const LeftSpan = styled.span`
padding-left: 20px;
`;

export function isReviewRequired(role) {
// determine last review or last modified date
const reviewData = role.lastReviewedDate ? {lastReviewedDate: role.lastReviewedDate}
: {lastReviewedDate: role.modified};
// get smallest expiry or review days value for the role
const smallestExpiryOrReview = getSmallestExpiryOrReview(role);

if (smallestExpiryOrReview === 0) {
// review or expiry days were not set in settings - no review required
return false;
}

// get 20% of the smallest review period
reviewData.pct20 = Math.ceil(smallestExpiryOrReview * ROLE_PERCENTAGE_OF_DAYS_TILL_NEXT_REVIEW);

const lastReviewedDate = moment(reviewData.lastReviewedDate, 'YYYY-MM-DDTHH:mm:ss.SSSZ');
const now = moment().utc();

// check if expiry/review is coming up within 20% of the smallest review/expiry period
return now.subtract(smallestExpiryOrReview, 'days').add(reviewData.pct20, 'days').isAfter(lastReviewedDate);
}

export function getSmallestExpiryOrReview(role){
const values = [
role.memberExpiryDays,
role.memberReviewDays,
role.groupExpiryDays,
role.groupReviewDays,
role.serviceExpiryDays,
role.serviceReviewDays
].filter(obj => obj > 0); // pick only those that have days set and days > 0

if (values.length > 0) {
// pick the one with the smallest days value
return values.reduce((obj1, obj2) => obj1 < obj2 ?
obj1 : obj2);
}
return 0;
}

class RoleRow extends React.Component {
constructor(props) {
super(props);
Expand Down Expand Up @@ -277,8 +319,7 @@ class RoleRow extends React.Component {
</Menu>
);

let reviewRequired =
role.reviewEnabled && (role.memberExpiryDays || role.serviceExpiry);
let reviewRequired = isReviewRequired(role);

let roleTypeIcon = role.trust ? iconDelegated : '';
let roleDescriptionIcon = role.description ? iconDescription : '';
Expand Down Expand Up @@ -354,11 +395,12 @@ class RoleRow extends React.Component {
isLink
size={'1.25em'}
verticalAlign={'text-bottom'}
enableTitle={false}
/>
</span>
}
>
<MenuDiv>Review Members</MenuDiv>
<MenuDiv>{reviewRequired ? 'Role Review is required' : 'Review Members'}</MenuDiv>
</Menu>
</TDStyledIcon>
<TDStyledIcon color={color} align={center}>
Expand Down

0 comments on commit fea1fe6

Please sign in to comment.