fix: `Accounts_LoginExpiration` being used differently on codebase (#32527)
parent
eff91b44a5
commit
8fc6ca8b4e
@ -0,0 +1,8 @@ |
||||
--- |
||||
"@rocket.chat/meteor": patch |
||||
"@rocket.chat/tools": patch |
||||
"@rocket.chat/account-service": patch |
||||
--- |
||||
|
||||
Fixed an inconsistent evaluation of the `Accounts_LoginExpiration` setting over the codebase. In some places, it was being used as milliseconds while in others as days. Invalid values produced different results. A helper function was created to centralize the setting validation and the proper value being returned to avoid edge cases. |
||||
Negative values may be saved on the settings UI panel but the code will interpret any negative, NaN or 0 value to the default expiration which is 90 days. |
||||
@ -0,0 +1,3 @@ |
||||
export default { |
||||
preset: 'ts-jest', |
||||
}; |
||||
@ -0,0 +1,15 @@ |
||||
import { convertFromDaysToMilliseconds } from './converter'; |
||||
|
||||
describe('convertFromDaysToMilliseconds', () => { |
||||
it('should throw an error when a non number is passed', () => { |
||||
// @ts-expect-error - Testing
|
||||
expect(() => convertFromDaysToMilliseconds('90')).toThrow(); |
||||
}); |
||||
it('should return the value passed when its valid', () => { |
||||
expect(convertFromDaysToMilliseconds(85)).toBe(85 * 24 * 60 * 60 * 1000); |
||||
}); |
||||
it('should fail if anything but an integer is passed', () => { |
||||
expect(() => convertFromDaysToMilliseconds(85.5)).toThrow(); |
||||
expect(() => convertFromDaysToMilliseconds(-2.3)).toThrow(); |
||||
}); |
||||
}); |
||||
@ -0,0 +1,7 @@ |
||||
export const convertFromDaysToMilliseconds = (days: number) => { |
||||
if (typeof days !== 'number' || !Number.isInteger(days)) { |
||||
throw new Error('days must be a number'); |
||||
} |
||||
|
||||
return days * 24 * 60 * 60 * 1000; |
||||
}; |
||||
@ -0,0 +1,35 @@ |
||||
import { getLoginExpirationInDays, getLoginExpirationInMs } from './getLoginExpiration'; |
||||
|
||||
describe('getLoginExpirationInDays', () => { |
||||
it('should return 90 by default', () => { |
||||
expect(getLoginExpirationInDays()).toBe(90); |
||||
}); |
||||
it('should return 90 when value is 0', () => { |
||||
expect(getLoginExpirationInDays(0)).toBe(90); |
||||
}); |
||||
it('should return 90 when value is NaN', () => { |
||||
expect(getLoginExpirationInDays(NaN)).toBe(90); |
||||
}); |
||||
it('should return 90 when value is negative', () => { |
||||
expect(getLoginExpirationInDays(-1)).toBe(90); |
||||
}); |
||||
it('should return 90 when value is undefined', () => { |
||||
expect(getLoginExpirationInDays(undefined)).toBe(90); |
||||
}); |
||||
it('should return 90 when value is not a number', () => { |
||||
// @ts-expect-error - Testing
|
||||
expect(getLoginExpirationInDays('90')).toBe(90); |
||||
}); |
||||
it('should return the value passed when its valid', () => { |
||||
expect(getLoginExpirationInDays(85)).toBe(85); |
||||
}); |
||||
}); |
||||
|
||||
describe('getLoginExpirationInMs', () => { |
||||
it('should return 90 days in milliseconds when no value is passed', () => { |
||||
expect(getLoginExpirationInMs()).toBe(90 * 24 * 60 * 60 * 1000); |
||||
}); |
||||
it('should return the value passed when its valid', () => { |
||||
expect(getLoginExpirationInMs(85)).toBe(85 * 24 * 60 * 60 * 1000); |
||||
}); |
||||
}); |
||||
@ -0,0 +1,16 @@ |
||||
import { convertFromDaysToMilliseconds } from './converter'; |
||||
|
||||
const ACCOUNTS_DEFAULT_LOGIN_EXPIRATION_DAYS = 90; |
||||
|
||||
// Given a value, validates if it mets the conditions to be a valid login expiration.
|
||||
// Else, returns the default login expiration (which for Meteor is 90 days)
|
||||
export const getLoginExpirationInDays = (expiry?: number) => { |
||||
if (expiry && typeof expiry === 'number' && !Number.isNaN(expiry) && expiry > 0) { |
||||
return expiry; |
||||
} |
||||
return ACCOUNTS_DEFAULT_LOGIN_EXPIRATION_DAYS; |
||||
}; |
||||
|
||||
export const getLoginExpirationInMs = (expiry?: number) => { |
||||
return convertFromDaysToMilliseconds(getLoginExpirationInDays(expiry)); |
||||
}; |
||||
Loading…
Reference in new issue