Fix Never api key expiration dates (#4965)
closes #4714 We cannot set null expiration dates for api keys. So we will set to 100 years instead of null. If apiKey expires in more that 10 years, it is displayed as "Never expires"
This commit is contained in:
@ -1,3 +1,5 @@
|
||||
import { NEVER_EXPIRE_DELTA_IN_YEARS } from '@/settings/developers/constants/NeverExpireDeltaInYears.ts';
|
||||
|
||||
export const EXPIRATION_DATES: {
|
||||
value: number | null;
|
||||
label: string;
|
||||
@ -7,5 +9,5 @@ export const EXPIRATION_DATES: {
|
||||
{ label: '90 days', value: 90 },
|
||||
{ label: '1 year', value: 365 },
|
||||
{ label: '2 years', value: 2 * 365 },
|
||||
{ label: 'Never', value: null },
|
||||
{ label: 'Never', value: NEVER_EXPIRE_DELTA_IN_YEARS * 365 },
|
||||
];
|
||||
|
||||
@ -0,0 +1 @@
|
||||
export const NEVER_EXPIRE_DELTA_IN_YEARS = 100;
|
||||
@ -3,6 +3,13 @@ import { formatExpiration } from '@/settings/developers/utils/format-expiration'
|
||||
jest.useFakeTimers().setSystemTime(new Date('2024-01-01T00:00:00.000Z'));
|
||||
|
||||
describe('formatExpiration', () => {
|
||||
it('should format properly when expiresAt is great', () => {
|
||||
const expiresAt = '2044-01-10T00:00:00.000Z';
|
||||
const result = formatExpiration(expiresAt);
|
||||
expect(result).toEqual('Never');
|
||||
const resultWithExpiresMention = formatExpiration(expiresAt, true);
|
||||
expect(resultWithExpiresMention).toEqual('Never expires');
|
||||
});
|
||||
it('should format properly when expiresAt is null', () => {
|
||||
const expiresAt = null;
|
||||
const result = formatExpiration(expiresAt);
|
||||
@ -18,17 +25,17 @@ describe('formatExpiration', () => {
|
||||
expect(resultWithExpiresMention).toEqual('Expires in 9 days');
|
||||
});
|
||||
it('should format properly when expiresAt is large', () => {
|
||||
const expiresAt = '2034-01-10T00:00:00.000Z';
|
||||
const expiresAt = '2032-01-10T00:00:00.000Z';
|
||||
const result = formatExpiration(expiresAt);
|
||||
expect(result).toEqual('In 10 years');
|
||||
expect(result).toEqual('In 8 years');
|
||||
const resultWithExpiresMention = formatExpiration(expiresAt, true);
|
||||
expect(resultWithExpiresMention).toEqual('Expires in 10 years');
|
||||
expect(resultWithExpiresMention).toEqual('Expires in 8 years');
|
||||
});
|
||||
it('should format properly when expiresAt is large and long version', () => {
|
||||
const expiresAt = '2034-01-10T00:00:00.000Z';
|
||||
const expiresAt = '2032-01-10T00:00:00.000Z';
|
||||
const result = formatExpiration(expiresAt, undefined, false);
|
||||
expect(result).toEqual('In 10 years and 9 days');
|
||||
expect(result).toEqual('In 8 years and 9 days');
|
||||
const resultWithExpiresMention = formatExpiration(expiresAt, true, false);
|
||||
expect(resultWithExpiresMention).toEqual('Expires in 10 years and 9 days');
|
||||
expect(resultWithExpiresMention).toEqual('Expires in 8 years and 9 days');
|
||||
});
|
||||
});
|
||||
|
||||
@ -1,22 +1,32 @@
|
||||
import { isNonEmptyString } from '@sniptt/guards';
|
||||
import { DateTime } from 'luxon';
|
||||
|
||||
import { NEVER_EXPIRE_DELTA_IN_YEARS } from '@/settings/developers/constants/NeverExpireDeltaInYears.ts';
|
||||
import { ApiFieldItem } from '@/settings/developers/types/api-key/ApiFieldItem';
|
||||
import { ApiKey } from '@/settings/developers/types/api-key/ApiKey';
|
||||
import { beautifyDateDiff } from '~/utils/date-utils';
|
||||
|
||||
export const doesNeverExpire = (expiresAt: string) => {
|
||||
const dateDiff = DateTime.fromISO(expiresAt).diff(DateTime.now(), [
|
||||
'years',
|
||||
'days',
|
||||
]);
|
||||
return dateDiff.years > NEVER_EXPIRE_DELTA_IN_YEARS / 10;
|
||||
};
|
||||
|
||||
export const formatExpiration = (
|
||||
expiresAt: string | null,
|
||||
withExpiresMention = false,
|
||||
short = true,
|
||||
) => {
|
||||
if (isNonEmptyString(expiresAt)) {
|
||||
const dateDiff = beautifyDateDiff(expiresAt, undefined, short);
|
||||
if (dateDiff.includes('-')) {
|
||||
return 'Expired';
|
||||
}
|
||||
return withExpiresMention ? `Expires in ${dateDiff}` : `In ${dateDiff}`;
|
||||
if (!isNonEmptyString(expiresAt) || doesNeverExpire(expiresAt)) {
|
||||
return withExpiresMention ? 'Never expires' : 'Never';
|
||||
}
|
||||
return withExpiresMention ? 'Never expires' : 'Never';
|
||||
const dateDiff = beautifyDateDiff(expiresAt, undefined, short);
|
||||
if (dateDiff.includes('-')) {
|
||||
return 'Expired';
|
||||
}
|
||||
return withExpiresMention ? `Expires in ${dateDiff}` : `In ${dateDiff}`;
|
||||
};
|
||||
|
||||
export const formatExpirations = (
|
||||
|
||||
@ -40,6 +40,7 @@ export const SettingsDevelopersApiKeysNew = () => {
|
||||
const expiresAt = DateTime.now()
|
||||
.plus({ days: formValues.expirationDate ?? 30 })
|
||||
.toString();
|
||||
|
||||
const newApiKey = await createOneApiKey?.({
|
||||
name: formValues.name,
|
||||
expiresAt,
|
||||
|
||||
@ -248,10 +248,10 @@ describe('beautifyDateDiff', () => {
|
||||
expect(result).toEqual('4 days');
|
||||
});
|
||||
it('should return the correct date diff for large diff', () => {
|
||||
const date = '2033-11-05T00:00:00.000Z';
|
||||
const date = '2031-11-05T00:00:00.000Z';
|
||||
const dateToCompareWith = '2023-11-01T00:00:00.000Z';
|
||||
const result = beautifyDateDiff(date, dateToCompareWith);
|
||||
expect(result).toEqual('10 years and 4 days');
|
||||
expect(result).toEqual('8 years and 4 days');
|
||||
});
|
||||
it('should return the correct date for negative diff', () => {
|
||||
const date = '2013-11-05T00:00:00.000Z';
|
||||
@ -260,10 +260,10 @@ describe('beautifyDateDiff', () => {
|
||||
expect(result).toEqual('-9 years and -361 days');
|
||||
});
|
||||
it('should return the correct date diff for large diff', () => {
|
||||
const date = '2033-11-01T00:00:00.000Z';
|
||||
const date = '2031-11-01T00:00:00.000Z';
|
||||
const dateToCompareWith = '2023-11-01T00:00:00.000Z';
|
||||
const result = beautifyDateDiff(date, dateToCompareWith);
|
||||
expect(result).toEqual('10 years');
|
||||
expect(result).toEqual('8 years');
|
||||
});
|
||||
it('should return the proper english date diff', () => {
|
||||
const date = '2024-11-02T00:00:00.000Z';
|
||||
@ -283,10 +283,10 @@ describe('beautifyDateDiff', () => {
|
||||
expect(result).toEqual('3 years and 9 days');
|
||||
});
|
||||
it('should return short version', () => {
|
||||
const date = '2033-11-05T00:00:00.000Z';
|
||||
const date = '2031-11-05T00:00:00.000Z';
|
||||
const dateToCompareWith = '2023-11-01T00:00:00.000Z';
|
||||
const result = beautifyDateDiff(date, dateToCompareWith, true);
|
||||
expect(result).toEqual('10 years');
|
||||
expect(result).toEqual('8 years');
|
||||
});
|
||||
it('should return short version for short differences', () => {
|
||||
const date = '2023-11-05T00:00:00.000Z';
|
||||
|
||||
Reference in New Issue
Block a user