Skip to content

Commit

Permalink
bug: fix race condition in hasValidStartExpirationDates
Browse files Browse the repository at this point in the history
  • Loading branch information
taliaedX committed Mar 3, 2021
1 parent a3f46d7 commit 770d069
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 2 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
"build": "fedx-scripts webpack",
"i18n_extract": "BABEL_ENV=i18n fedx-scripts babel src --quiet > /dev/null",
"lint": "fedx-scripts eslint --ext .js --ext .jsx .",
"lint:fix": "fedx-scripts eslint --fix --ext .js --ext .jsx .",
"is-es5": "es-check es5 ./dist/*.js",
"precommit": "npm run lint",
"snapshot": "fedx-scripts jest --updateSnapshot",
Expand Down
6 changes: 4 additions & 2 deletions src/utils/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ export const hasTruthyValue = (value) => {
return values.every(item => !!item);
};

export const hasValidStartExpirationDates = ({ startDate, expirationDate }) => {
export const hasValidStartExpirationDates = ({ startDate, expirationDate, endDate }) => {
const now = moment();
return now.isBetween(startDate, expirationDate);
// Subscriptions use "expirationDate" while Codes use "endDate"
const realEndDate = expirationDate || endDate;
return now.isBetween(startDate, realEndDate);
};
57 changes: 57 additions & 0 deletions src/utils/tests/common.test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import moment from 'moment';
import {
isDefinedAndNotNull,
createArrayFromValue,
isDefinedAndNull,
hasTruthyValue,
hasValidStartExpirationDates,
} from '../common';

function assertTestCaseEquals(testCase, expectedValue) {
Expand Down Expand Up @@ -96,3 +98,58 @@ describe('hasTruthyValue', () => {
expect(result).toBeFalsy();
});
});

const now = moment();
const validStartDate = moment(now).subtract(5, 'days');
const validEndDate = moment(now).add(5, 'days');
const validExpirationDate = moment(now).add(6, 'days');
const invalidStartDate = moment(now).add(1, 'days');
const invalidEndDate = moment(now).subtract(1, 'days');
const invalidExpirationDate = moment(now).subtract(2, 'days');

describe('hasValidStartExpirationDates', () => {
it('returns true when now is between startDate and endDate', () => {
const validStartEnd = {
startDate: validStartDate,
endDate: validEndDate,
};
const result = hasValidStartExpirationDates(validStartEnd);
expect(result).toBeTruthy();
});

it('returns true when now is between startDate and expirationDate', () => {
const validStartExp = {
startDate: validStartDate,
expirationDate: validExpirationDate,
};
const result = hasValidStartExpirationDates(validStartExp);
expect(result).toBeTruthy();
});

it('returns false when startDate is invalid', () => {
const invalidStart = {
startDate: invalidStartDate,
endDate: validEndDate,
};
const result = hasValidStartExpirationDates(invalidStart);
expect(result).toBeFalsy();
});

it('returns false when endDate is invalid', () => {
const invalidEnd = {
startDate: validStartDate,
endDate: invalidEndDate,
};
const result = hasValidStartExpirationDates(invalidEnd);
expect(result).toBeFalsy();
});

it('returns false when expirationDate is invalid', () => {
const invalidExp = {
startDate: validStartDate,
expirationDate: invalidExpirationDate,
};
const result = hasValidStartExpirationDates(invalidExp);
expect(result).toBeFalsy();
});
});

0 comments on commit 770d069

Please sign in to comment.