import { mockCurrentUser, mockLoggedInUser } from '../../../helpers/testMocks';
import { renderApp } from '../../../helpers/testReactTestingUtils';
import { Permissions } from '../../../types/permissions';
+import { SettingsKey } from '../../../types/settings';
import { TokenExpiration, TokenType } from '../../../types/token';
import { CurrentUser } from '../../../types/users';
import routes from '../routes';
});
jest.mock('../../../api/settings', () => {
+ const { SettingsKey } = jest.requireActual('../../../types/settings');
return {
...jest.requireActual('../../../api/settings'),
getValues: jest.fn().mockResolvedValue([
{
+ key: SettingsKey.TokenMaxAllowedLifetime,
value: 'No expiration'
}
])
])(
'should display expiration date inferior or equal to the settings limit %s',
async (settingMaxLifetime, expectedTime, notExpectedTime) => {
- (getValues as jest.Mock).mockImplementation(() =>
- Promise.resolve([{ value: settingMaxLifetime }])
+ (getValues as jest.Mock).mockImplementationOnce(() =>
+ Promise.resolve([{ key: SettingsKey.TokenMaxAllowedLifetime, value: settingMaxLifetime }])
);
renderAccountApp(
}
);
+ it('should handle absent setting', async () => {
+ (getValues as jest.Mock).mockImplementationOnce(() => Promise.resolve([]));
+
+ renderAccountApp(
+ mockLoggedInUser({ permissions: { global: [Permissions.Scan] } }),
+ securityPagePath
+ );
+
+ await selectEvent.openMenu(screen.getAllByRole('textbox')[2]);
+
+ [
+ TokenExpiration.OneMonth,
+ TokenExpiration.ThreeMonths,
+ TokenExpiration.OneYear,
+ TokenExpiration.NoExpiration
+ ].forEach(time => {
+ // TokenExpiration.OneMonth is expected twice has it is the default value.
+ expect(screen.getAllByText(`users.tokens.expiration.${time}`).length).toBe(
+ time === TokenExpiration.OneMonth ? 2 : 1
+ );
+ });
+ });
+
it.each([
[TokenExpiration.OneMonth, '2022-07-01'],
[TokenExpiration.ThreeMonths, '2022-08-30'],
import { translate } from '../../../helpers/l10n';
import { hasGlobalPermission } from '../../../helpers/users';
import { Permissions } from '../../../types/permissions';
+import { SettingsKey } from '../../../types/settings';
import { TokenExpiration, TokenType, UserToken } from '../../../types/token';
import { CurrentUser } from '../../../types/users';
import TokensFormItem, { TokenDeleteConfirmation } from './TokensFormItem';
};
fetchTokenSettings = async () => {
- const setting = await getValues({ keys: 'sonar.auth.token.max.allowed.lifetime' });
- if (setting === undefined || setting[0].value === undefined) {
+ /*
+ * We intentionally fetch all settings, because fetching a specific setting will
+ * return it from the DB as a fallback, even if the setting is not defined at startup.
+ */
+ const settings = await getValues({ keys: '' });
+ const maxTokenLifetime = settings.find(
+ ({ key }) => key === SettingsKey.TokenMaxAllowedLifetime
+ );
+
+ if (maxTokenLifetime === undefined || maxTokenLifetime.value === undefined) {
return;
}
- const maxTokenLifetime = setting[0].value;
- if (SETTINGS_EXPIRATION_MAP[maxTokenLifetime] !== TokenExpiration.NoExpiration) {
+
+ const maxTokenExpirationOption = SETTINGS_EXPIRATION_MAP[maxTokenLifetime.value];
+
+ if (maxTokenExpirationOption !== TokenExpiration.NoExpiration) {
const tokenExpirationOptions = EXPIRATION_OPTIONS.filter(
option =>
- option.value <= SETTINGS_EXPIRATION_MAP[maxTokenLifetime] &&
- option.value !== TokenExpiration.NoExpiration
+ option.value <= maxTokenExpirationOption && option.value !== TokenExpiration.NoExpiration
);
if (this.mounted) {
this.setState({ tokenExpirationOptions });
DefaultProjectVisibility = 'projects.default.visibility',
ServerBaseUrl = 'sonar.core.serverBaseURL',
PluginRiskConsent = 'sonar.plugins.risk.consent',
- LicenceRemainingLocNotificationThreshold = 'sonar.license.notifications.remainingLocThreshold'
+ LicenceRemainingLocNotificationThreshold = 'sonar.license.notifications.remainingLocThreshold',
+ TokenMaxAllowedLifetime = 'sonar.auth.token.max.allowed.lifetime'
}
export enum GlobalSettingKeys {