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

mendersoftware / gui / 1350829378

27 Jun 2024 01:46PM UTC coverage: 83.494% (-16.5%) from 99.965%
1350829378

Pull #4465

gitlab-ci

mzedel
chore: test fixes

Signed-off-by: Manuel Zedel <manuel.zedel@northern.tech>
Pull Request #4465: MEN-7169 - feat: added multi sorting capabilities to devices view

4506 of 6430 branches covered (70.08%)

81 of 100 new or added lines in 14 files covered. (81.0%)

1661 existing lines in 163 files now uncovered.

8574 of 10269 relevant lines covered (83.49%)

160.6 hits per line

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

88.24
/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, { useEffect, useMemo, useState } from 'react';
15
import { useWatch } from 'react-hook-form';
16

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

34
import pluralize from 'pluralize';
35
import { isUUID } from 'validator';
36

37
import { BENEFITS } from '../../../constants/appConstants';
38
import { rolesById, rolesByName, uiPermissionsById } from '../../../constants/userConstants';
39
import EnterpriseNotification from '../../common/enterpriseNotification';
40
import Form from '../../common/forms/form';
41
import FormCheckbox from '../../common/forms/formcheckbox';
42
import PasswordInput from '../../common/forms/passwordinput';
43
import TextInput from '../../common/forms/textinput';
44

45
export const UserRolesSelect = ({ currentUser, disabled, onSelect, roles, user }) => {
6✔
46
  const [selectedRoleIds, setSelectedRoleIds] = useState(
36✔
47
    (user.roles || [rolesByName.admin]).reduce((accu, roleId) => {
43✔
48
      const foundRole = roles[roleId];
36✔
49
      if (foundRole) {
36✔
50
        accu.push(roleId);
35✔
51
      }
52
      return accu;
36✔
53
    }, [])
54
  );
55

56
  const onInputChange = ({ target: { value } }) => {
36✔
57
    const { roles = [] } = user;
4!
58
    let newlySelectedRoles = value;
4✔
59
    if (value.includes('')) {
4!
UNCOV
60
      newlySelectedRoles = [];
×
61
    }
62
    const hadRoleChanges =
63
      roles.length !== newlySelectedRoles.length || roles.some(currentRoleId => !newlySelectedRoles.some(roleId => currentRoleId === roleId));
4✔
64
    setSelectedRoleIds(newlySelectedRoles);
4✔
65
    onSelect(newlySelectedRoles, hadRoleChanges);
4✔
66
  };
67

68
  const { editableRoles, showRoleUsageNotification } = useMemo(() => {
36✔
69
    const editableRoles = Object.entries(roles).map(([id, role]) => {
8✔
70
      const enabled = selectedRoleIds.some(roleId => id === roleId);
42✔
71
      return { enabled, id, ...role };
42✔
72
    });
73
    const showRoleUsageNotification = selectedRoleIds.reduce((accu, roleId) => {
8✔
74
      const { permissions, uiPermissions } = roles[roleId];
5✔
75
      const hasUiApiAccess = [rolesByName.ci].includes(roleId)
5✔
76
        ? false
77
        : roleId === rolesByName.admin ||
6✔
UNCOV
78
          permissions.some(permission => ![rolesByName.deploymentCreation.action].includes(permission.action)) ||
×
79
          uiPermissions.userManagement.includes(uiPermissionsById.read.value);
80
      if (hasUiApiAccess) {
5✔
81
        return false;
4✔
82
      }
83
      return typeof accu !== 'undefined' ? accu : true;
1!
84
    }, undefined);
85
    return { editableRoles, showRoleUsageNotification };
8✔
86
    // eslint-disable-next-line react-hooks/exhaustive-deps
87
  }, [JSON.stringify(roles), selectedRoleIds]);
88

89
  return (
36✔
90
    <div className="flexbox" style={{ alignItems: 'flex-end' }}>
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
          disabled={disabled}
97
          multiple
98
          value={selectedRoleIds}
99
          required
100
          onChange={onInputChange}
101
          renderValue={selected => selected.map(role => roles[role].name).join(', ')}
42✔
102
        >
103
          {editableRoles.map(role => (
104
            <MenuItem id={role.id} key={role.id} value={role.id}>
210✔
105
              <Checkbox id={`${role.id}-checkbox`} checked={role.enabled} />
106
              <ListItemText id={`${role.id}-text`} primary={role.name} />
107
            </MenuItem>
108
          ))}
109
        </Select>
110
        {showRoleUsageNotification && (
37✔
111
          <FormHelperText className="info">
112
            The selected {pluralize('role', selectedRoleIds.length)} may prevent {currentUser.email === user.email ? 'you' : <i>{user.email}</i>} from using the
1!
113
            Mender UI.
114
            <br />
115
            Consider adding the <i>{rolesById[rolesByName.readOnly].name}</i> role as well.
116
          </FormHelperText>
117
        )}
118
      </FormControl>
119
      <EnterpriseNotification className="margin-left-small" id={BENEFITS.rbac.id} />
120
    </div>
121
  );
122
};
123

124
const PasswordLabel = () => (
6✔
125
  <div className="flexbox center-aligned">
3✔
126
    Optional
127
    <Tooltip
128
      title={
129
        <>
130
          <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>
131
          <p>Organizations using Single Sign-On or other means of authorization may want to create users with no password.</p>
132
        </>
133
      }
134
    >
135
      <InfoOutlined fontSize="small" className="margin-left-small" />
136
    </Tooltip>
137
  </div>
138
);
139

140
const UserIdentifier = ({ onHasUserId }) => {
6✔
141
  const value = useWatch({ name: 'email', defaultValue: '' });
25✔
142

143
  useEffect(() => {
25✔
144
    onHasUserId(isUUID(value));
15✔
145
  }, [value, onHasUserId]);
146

147
  return <TextInput hint="Email" label="Email or User ID" id="email" validations="isLength:1,isUUID||isEmail,trim" required autocomplete="off" />;
25✔
148
};
149

150
export const UserForm = ({ closeDialog, currentUser, canManageUsers, isEnterprise, roles, submit }) => {
6✔
151
  const [hadRoleChanges, setHadRoleChanges] = useState(false);
3✔
152
  const [selectedRoles, setSelectedRoles] = useState();
3✔
153
  const [isAddingExistingUser, setIsAddingExistingUser] = useState(false);
3✔
154

155
  const onSelect = (newlySelectedRoles, hadRoleChanges) => {
3✔
UNCOV
156
    setSelectedRoles(newlySelectedRoles);
×
UNCOV
157
    setHadRoleChanges(hadRoleChanges);
×
158
  };
159

160
  const onSubmit = data => {
3✔
161
    const { password, ...remainder } = data;
1✔
162
    const roleData = hadRoleChanges ? { roles: selectedRoles } : {};
1!
163
    if (isAddingExistingUser) {
1!
UNCOV
164
      const { email: userId } = data;
×
UNCOV
165
      return submit(userId, 'add');
×
166
    }
167
    return submit({ ...remainder, ...roleData, password }, 'create');
1✔
168
  };
169

170
  return (
3✔
171
    <Dialog open={true} fullWidth={true} maxWidth="sm">
172
      <DialogTitle>Add new user</DialogTitle>
173
      <DialogContent style={{ overflowY: 'initial' }}>
174
        <Form
175
          onSubmit={onSubmit}
176
          handleCancel={closeDialog}
177
          submitLabel={`${isAddingExistingUser ? 'Add' : 'Create'} user`}
3!
178
          showButtons={true}
179
          autocomplete="off"
180
        >
181
          <UserIdentifier onHasUserId={setIsAddingExistingUser} />
182
          <Collapse in={!isAddingExistingUser}>
183
            <PasswordInput
184
              id="password"
185
              className="edit-pass"
186
              autocomplete="off"
187
              create
188
              edit={false}
189
              generate
190
              InputLabelProps={{ shrink: true }}
191
              label={<PasswordLabel />}
192
              placeholder="Password"
193
              validations="isLength:8"
194
            />
195
            <FormCheckbox id="shouldResetPassword" label="Send an email to the user containing a link to reset the password" />
196
            <UserRolesSelect currentUser={currentUser} disabled={!(canManageUsers && isEnterprise)} onSelect={onSelect} roles={roles} user={{}} />
5✔
197
          </Collapse>
198
        </Form>
199
      </DialogContent>
200
      <DialogActions />
201
    </Dialog>
202
  );
203
};
204

205
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