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:
@ -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 = (
|
||||
|
||||
Reference in New Issue
Block a user