• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mendersoftware / gui / 951400782

pending completion
951400782

Pull #3900

gitlab-ci

web-flow
chore: bump @testing-library/jest-dom from 5.16.5 to 5.17.0

Bumps [@testing-library/jest-dom](https://github.com/testing-library/jest-dom) from 5.16.5 to 5.17.0.
- [Release notes](https://github.com/testing-library/jest-dom/releases)
- [Changelog](https://github.com/testing-library/jest-dom/blob/main/CHANGELOG.md)
- [Commits](https://github.com/testing-library/jest-dom/compare/v5.16.5...v5.17.0)

---
updated-dependencies:
- dependency-name: "@testing-library/jest-dom"
  dependency-type: direct:development
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
Pull Request #3900: chore: bump @testing-library/jest-dom from 5.16.5 to 5.17.0

4446 of 6414 branches covered (69.32%)

8342 of 10084 relevant lines covered (82.73%)

186.0 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

87.32
/src/js/components/settings/user-management/userform.js
1
// Copyright 2017 Northern.tech AS
2
//
3
//    Licensed under the Apache License, Version 2.0 (the "License");
4
//    you may not use this file except in compliance with the License.
5
//    You may obtain a copy of the License at
6
//
7
//        http://www.apache.org/licenses/LICENSE-2.0
8
//
9
//    Unless required by applicable law or agreed to in writing, software
10
//    distributed under the License is distributed on an "AS IS" BASIS,
11
//    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
//    See the License for the specific language governing permissions and
13
//    limitations under the License.
14
import React, { useMemo, useState } from 'react';
15

16
import { InfoOutlined } from '@mui/icons-material';
17
import {
18
  Checkbox,
19
  Dialog,
20
  DialogActions,
21
  DialogContent,
22
  DialogTitle,
23
  FormControl,
24
  FormControlLabel,
25
  FormHelperText,
26
  InputLabel,
27
  ListItemText,
28
  MenuItem,
29
  Select,
30
  Tooltip
31
} from '@mui/material';
32

33
import pluralize from 'pluralize';
34

35
import { rolesById, rolesByName, uiPermissionsById } from '../../../constants/userConstants';
36
import { toggle } from '../../../helpers';
37
import Form from '../../common/forms/form';
38
import PasswordInput from '../../common/forms/passwordinput';
39
import TextInput from '../../common/forms/textinput';
40

41
export const UserRolesSelect = ({ currentUser, onSelect, roles, user }) => {
7✔
42
  const [selectedRoleIds, setSelectedRoleIds] = useState(
31✔
43
    (user.roles || [rolesByName.admin]).reduce((accu, roleId) => {
62✔
44
      const foundRole = roles[roleId];
31✔
45
      if (foundRole) {
31!
46
        accu.push(roleId);
31✔
47
      }
48
      return accu;
31✔
49
    }, [])
50
  );
51

52
  const onInputChange = ({ target: { value } }) => {
31✔
53
    const { roles = [] } = user;
3✔
54
    let newlySelectedRoles = value;
3✔
55
    if (value.includes('')) {
3!
56
      newlySelectedRoles = [];
×
57
    }
58
    const hadRoleChanges =
59
      roles.length !== newlySelectedRoles.length || roles.some(currentRoleId => !newlySelectedRoles.some(roleId => currentRoleId === roleId));
3✔
60
    setSelectedRoleIds(newlySelectedRoles);
3✔
61
    onSelect(newlySelectedRoles, hadRoleChanges);
3✔
62
  };
63

64
  const editableRoles = useMemo(
31✔
65
    () =>
66
      Object.entries(roles).map(([id, role]) => {
6✔
67
        const enabled = selectedRoleIds.some(roleId => id === roleId);
36✔
68
        return { enabled, id, ...role };
36✔
69
      }),
70
    [roles, selectedRoleIds]
71
  );
72

73
  const showRoleUsageNotification = useMemo(
31✔
74
    () =>
75
      selectedRoleIds.reduce((accu, roleId) => {
6✔
76
        const { permissions, uiPermissions } = roles[roleId];
6✔
77
        const hasUiApiAccess = [rolesByName.ci].includes(roleId)
6✔
78
          ? false
79
          : roleId === rolesByName.admin ||
7✔
80
            permissions.some(permission => ![rolesByName.deploymentCreation.action].includes(permission.action)) ||
×
81
            uiPermissions.userManagement.includes(uiPermissionsById.read.value);
82
        if (hasUiApiAccess) {
6✔
83
          return false;
5✔
84
        }
85
        return typeof accu !== 'undefined' ? accu : true;
1!
86
      }, undefined),
87
    [selectedRoleIds]
88
  );
89

90
  return (
31✔
91
    <FormControl id="roles-form" style={{ maxWidth: 400 }}>
92
      <InputLabel id="roles-selection-label">Roles</InputLabel>
93
      <Select
94
        labelId="roles-selection-label"
95
        id={`roles-selector-${selectedRoleIds.length}`}
96
        multiple
97
        value={selectedRoleIds}
98
        required
99
        onChange={onInputChange}
100
        renderValue={selected => selected.map(role => roles[role].name).join(', ')}
42✔
101
      >
102
        {editableRoles.map(role => (
103
          <MenuItem id={role.id} key={role.id} value={role.id}>
186✔
104
            <Checkbox id={`${role.id}-checkbox`} checked={role.enabled} />
105
            <ListItemText id={`${role.id}-text`} primary={role.name} />
106
          </MenuItem>
107
        ))}
108
      </Select>
109
      {showRoleUsageNotification && (
32✔
110
        <FormHelperText className="info">
111
          The selected {pluralize('role', selectedRoleIds.length)} may prevent {currentUser.email === user.email ? 'you' : <i>{user.email}</i>} from using the
1!
112
          Mender UI.
113
          <br />
114
          Consider adding the <i>{rolesById[rolesByName.readOnly].name}</i> role as well.
115
        </FormHelperText>
116
      )}
117
    </FormControl>
118
  );
119
};
120

121
const PasswordLabel = () => (
7✔
122
  <div className="flexbox center-aligned">
6✔
123
    Optional
124
    <Tooltip
125
      title={
126
        <>
127
          <p>You can skip setting a password for now - you can opt to send the new user an email containing a password reset link by checking the box below.</p>
128
          <p>Organizations using Single Sign-On or other means of authorization may want to create users with no password.</p>
129
        </>
130
      }
131
    >
132
      <InfoOutlined fontSize="small" className="margin-left-small" />
133
    </Tooltip>
134
  </div>
135
);
136

137
export const UserForm = ({ closeDialog, currentUser, canManageUsers, isEnterprise, roles, submit }) => {
7✔
138
  const [hadRoleChanges, setHadRoleChanges] = useState(false);
6✔
139
  const [selectedRoles, setSelectedRoles] = useState();
6✔
140
  const [shouldResetPassword, setShouldResetPassword] = useState(false);
6✔
141

142
  const onSelect = (newlySelectedRoles, hadRoleChanges) => {
6✔
143
    setSelectedRoles(newlySelectedRoles);
×
144
    setHadRoleChanges(hadRoleChanges);
×
145
  };
146

147
  const onSubmit = data => {
6✔
148
    const { password, ...remainder } = data;
1✔
149
    const roleData = hadRoleChanges ? { roles: selectedRoles } : {};
1!
150
    return submit({ ...remainder, ...roleData, password }, 'create');
1✔
151
  };
152

153
  const togglePasswordReset = () => setShouldResetPassword(toggle);
6✔
154

155
  return (
6✔
156
    <Dialog open={true} fullWidth={true} maxWidth="sm">
157
      <DialogTitle>Create new user</DialogTitle>
158
      <DialogContent style={{ overflowY: 'initial' }}>
159
        <Form onSubmit={onSubmit} handleCancel={closeDialog} submitLabel="Create user" submitButtonId="submit_button" showButtons={true} autocomplete="off">
160
          <TextInput hint="Email" label="Email" id="email" validations="isLength:1,isEmail" required autocomplete="off" />
161
          <PasswordInput
162
            id="password"
163
            className="edit-pass"
164
            autocomplete="off"
165
            create
166
            edit={false}
167
            generate
168
            InputLabelProps={{ shrink: true }}
169
            label={<PasswordLabel />}
170
            placeholder="Password"
171
            validations="isLength:8"
172
          />
173
          <FormControlLabel
174
            control={<Checkbox checked={shouldResetPassword} onChange={togglePasswordReset} />}
175
            label="Send an email to the user containing a link to reset the password"
176
          />
177
          {canManageUsers && isEnterprise && <UserRolesSelect currentUser={currentUser} onSelect={onSelect} roles={roles} user={{}} />}
16✔
178
        </Form>
179
      </DialogContent>
180
      <DialogActions />
181
    </Dialog>
182
  );
183
};
184

185
export default UserForm;
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc