).not.toBeInTheDocument();
});
- it("should not suggest creating a Project token if the user doesn't have at least one scannable Projects", async () => {
+ it("should not suggest creating a Project token if the user doesn't have at least one scannable Projects", () => {
(getScannableProjects as jest.Mock).mockResolvedValueOnce({
projects: []
});
securityPagePath
);
- await selectEvent.openMenu(screen.getAllByRole('textbox')[1]);
+ selectEvent.openMenu(screen.getAllByRole('textbox')[1]);
expect(screen.queryByText(`users.tokens.${TokenType.Project}`)).not.toBeInTheDocument();
});
+ it('should preselect the user token type if the user has no scan rights', async () => {
+ (getScannableProjects as jest.Mock).mockResolvedValueOnce({
+ projects: []
+ });
+ renderAccountApp(mockLoggedInUser(), securityPagePath);
+
+ const globalToken = await screen.findByText(`users.tokens.${TokenType.User}`);
+ expect(globalToken).toBeInTheDocument();
+ });
+
+ it('should preselect the only project the user has access to if they select project token', async () => {
+ (getScannableProjects as jest.Mock).mockResolvedValueOnce({
+ projects: [
+ {
+ key: 'project-key-1',
+ name: 'Project Name 1'
+ }
+ ]
+ });
+ renderAccountApp(
+ mockLoggedInUser({ permissions: { global: [Permissions.Scan] } }),
+ securityPagePath
+ );
+
+ await selectEvent.select(screen.getAllByRole('textbox')[1], [
+ `users.tokens.${TokenType.Project}`
+ ]);
+
+ expect(screen.getByText('Project Name 1')).toBeInTheDocument();
+ });
+
it('should allow local users to change password', async () => {
const user = userEvent.setup();
renderAccountApp(mockLoggedInUser({ local: true }), securityPagePath);
newTokenType?: TokenType;
tokens: UserToken[];
projects: BasicSelectOption[];
- selectedProject: { key: string; name: string };
+ selectedProject?: BasicSelectOption;
newTokenExpiration: TokenExpiration;
tokenExpirationOptions: { value: TokenExpiration; label: string }[];
}
loading: true,
newTokenName: '',
newTokenType: this.props.displayTokenTypeInput ? undefined : TokenType.User,
- selectedProject: { key: '', name: '' },
tokens: [],
projects: [],
newTokenExpiration: TokenExpiration.OneMonth,
const { projects: projectArray } = await getScannableProjects();
const projects = projectArray.map(project => ({ label: project.name, value: project.key }));
this.setState({
- projects
+ projects,
+ selectedProject: projects.length === 1 ? projects[0] : undefined
});
};
name: newTokenName,
login,
type: newTokenType,
- ...(newTokenType === TokenType.Project && { projectKey: selectedProject.key }),
+ ...(newTokenType === TokenType.Project &&
+ selectedProject !== undefined && { projectKey: selectedProject.value }),
...(newTokenExpiration !== TokenExpiration.NoExpiration && {
expirationDate: computeTokenExpirationDate(newTokenExpiration)
})
isExpired: false,
expirationDate: newToken.expirationDate,
type: newTokenType,
- ...(newTokenType === TokenType.Project && {
- project: { key: selectedProject.key, name: selectedProject.name }
- })
+ ...(newTokenType === TokenType.Project &&
+ selectedProject !== undefined && {
+ project: { key: selectedProject.value, name: selectedProject.label }
+ })
}
];
return {
generating: false,
newToken,
newTokenName: '',
- selectedProject: { key: '', name: '' },
+ selectedProject: undefined,
newTokenType: undefined,
+ newTokenExpiration: TokenExpiration.OneMonth,
tokens
};
}, this.updateTokensCount);
return true;
}
if (newTokenType === TokenType.Project) {
- return !selectedProject.key;
+ return !selectedProject?.value;
}
return !newTokenType;
this.setState({ newTokenType: value });
};
- handleProjectChange = ({ value, label }: { value: string; label: string }) => {
- this.setState({ selectedProject: { key: value, name: label } });
+ handleProjectChange = (selectedProject: BasicSelectOption) => {
+ this.setState({ selectedProject });
};
handleNewTokenExpirationChange = ({ value }: { value: TokenExpiration }) => {
onChange={this.handleNewTokenTypeChange}
options={tokenTypeOptions}
placeholder={translate('users.tokens.select_type')}
- value={tokenTypeOptions.find(option => option.value === newTokenType) || null}
+ value={
+ tokenTypeOptions.length === 1
+ ? tokenTypeOptions[0]
+ : tokenTypeOptions.find(option => option.value === newTokenType) || null
+ }
/>
</div>
{newTokenType === TokenType.Project && (
onChange={this.handleProjectChange}
options={projects}
placeholder={translate('users.tokens.select_project')}
- value={projects.find(project => project.value === selectedProject.key)}
+ value={selectedProject}
/>
</div>
)}