import userEvent from '@testing-library/user-event';
import { UserEvent } from '@testing-library/user-event/dist/types/setup';
import selectEvent from 'react-select-event';
-import { getMyProjects } from '../../../api/components';
+import { getMyProjects, getScannableProjects } from '../../../api/components';
import NotificationsMock from '../../../api/mocks/NotificationsMock';
import UserTokensMock from '../../../api/mocks/UserTokensMock';
import getHistory from '../../../helpers/getHistory';
}
);
+ it("should not suggest creating a Project token if the user doesn't have at least one scannable Projects", async () => {
+ (getScannableProjects as jest.Mock).mockResolvedValueOnce({
+ projects: []
+ });
+ renderAccountApp(
+ mockLoggedInUser({ permissions: { global: [Permissions.Scan] } }),
+ securityPagePath
+ );
+
+ await selectEvent.openMenu(screen.getAllByRole('textbox')[1]);
+ expect(screen.queryByText(`users.tokens.${TokenType.Project}`)).not.toBeInTheDocument();
+ });
+
it('should allow local users to change password', async () => {
const user = userEvent.setup();
renderAccountApp(mockLoggedInUser({ local: true }), securityPagePath);
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
+import { isEmpty } from 'lodash';
import * as React from 'react';
import { getScannableProjects } from '../../../api/components';
import { generateToken, getTokens } from '../../../api/user-tokens';
componentDidMount() {
this.mounted = true;
this.fetchTokens();
+ if (this.props.displayTokenTypeInput) {
+ this.fetchProjects();
+ }
}
componentWillUnmount() {
};
handleNewTokenTypeChange = ({ value }: { value: TokenType }) => {
- if (value === TokenType.Project && this.state.projects.length === 0) {
- this.fetchProjects();
- }
this.setState({ newTokenType: value });
};
const { displayTokenTypeInput, currentUser } = this.props;
const tokenTypeOptions = [
- { label: translate('users.tokens', TokenType.Project), value: TokenType.Project },
{ label: translate('users.tokens', TokenType.User), value: TokenType.User }
];
if (hasGlobalPermission(currentUser, Permissions.Scan)) {
- tokenTypeOptions.push({
+ tokenTypeOptions.unshift({
label: translate('users.tokens', TokenType.Global),
value: TokenType.Global
});
}
+ if (!isEmpty(projects)) {
+ tokenTypeOptions.unshift({
+ label: translate('users.tokens', TokenType.Project),
+ value: TokenType.Project
+ });
+ }
return (
<form autoComplete="off" className="display-flex-center" onSubmit={this.handleGenerateToken}>