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

mendersoftware / gui / 891984097

pending completion
891984097

Pull #3741

gitlab-ci

mzedel
chore: made use of common truth function across codebase to remove code duplication

Signed-off-by: Manuel Zedel <manuel.zedel@northern.tech>
Pull Request #3741: MEN-6487 - fix: added more granular check for device troubleshooting feature

4401 of 6401 branches covered (68.75%)

26 of 27 new or added lines in 8 files covered. (96.3%)

1702 existing lines in 165 files now uncovered.

8060 of 9779 relevant lines covered (82.42%)

123.44 hits per line

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

50.0
/src/js/components/devices/troubleshoot/filetransfer.js
1
// Copyright 2021 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, useState } from 'react';
15

16
import { FileCopy as CopyPasteIcon } from '@mui/icons-material';
17
import { Button, IconButton, Tab, Tabs, TextField, Tooltip } from '@mui/material';
18
import { makeStyles } from 'tss-react/mui';
19

20
import { canAccess } from '../../../constants/appConstants';
21
import FileUpload from '../../common/forms/fileupload';
22
import InfoText from '../../common/infotext';
23

24
const tabs = [
13✔
25
  { key: 'upload', canAccess: ({ userCapabilities: { canTroubleshoot, canWriteDevices } }) => canTroubleshoot && canWriteDevices },
1✔
26
  { key: 'download', canAccess }
27
];
28

29
const maxWidth = 400;
13✔
30

31
const useStyles = makeStyles()(theme => ({
13✔
32
  column: { maxWidth },
33
  inputWrapper: { display: 'grid', gridTemplateColumns: `${maxWidth}px max-content` },
34
  tab: { alignItems: 'flex-start' },
35
  fileDestination: { marginTop: theme.spacing(2) }
36
}));
37

38
export const FileTransfer = ({
13✔
39
  deviceId,
40
  downloadPath,
41
  file,
42
  onDownload,
43
  onUpload,
44
  setFile,
45
  setDownloadPath,
46
  setSnackbar,
47
  setUploadPath,
48
  uploadPath,
49
  userCapabilities
50
}) => {
51
  const { classes } = useStyles();
2✔
52
  const [currentTab, setCurrentTab] = useState(tabs[0].key);
2✔
53
  const [isValidDestination, setIsValidDestination] = useState(true);
2✔
54
  const [availableTabs, setAvailableTabs] = useState(tabs);
2✔
55

56
  useEffect(() => {
2✔
57
    let destination = currentTab === 'download' ? downloadPath : uploadPath;
1!
58
    const isValid = destination.length ? /^(?:\/|[a-z]+:\/\/)/.test(destination) : true;
1!
59
    setIsValidDestination(isValid);
1✔
60
  }, [currentTab, downloadPath, uploadPath]);
61

62
  useEffect(() => {
2✔
63
    const availableTabs = tabs.reduce((accu, item) => {
1✔
64
      if (item.canAccess({ userCapabilities })) {
2!
65
        accu.push(item);
2✔
66
      }
67
      return accu;
2✔
68
    }, []);
69
    setAvailableTabs(availableTabs);
1✔
70
  }, [JSON.stringify(userCapabilities)]);
71

72
  const onPasteDownloadClick = async () => {
2✔
UNCOV
73
    const path = await navigator.clipboard.readText();
×
UNCOV
74
    setDownloadPath(path);
×
75
  };
76

77
  const onPasteUploadClick = async () => {
2✔
UNCOV
78
    const path = await navigator.clipboard.readText();
×
UNCOV
79
    setUploadPath(path);
×
80
  };
81

82
  const onFileSelect = selectedFile => {
2✔
83
    let path;
UNCOV
84
    if (selectedFile) {
×
UNCOV
85
      path = `${uploadPath}/${selectedFile.name}`;
×
86
    } else {
UNCOV
87
      path = file && uploadPath.includes(file.name) ? uploadPath.substring(0, uploadPath.lastIndexOf('/')) : uploadPath;
×
88
    }
UNCOV
89
    setUploadPath(path);
×
UNCOV
90
    setFile(selectedFile);
×
91
  };
92

93
  return (
2✔
94
    <div className="tab-container with-sub-panels" style={{ minHeight: '95%' }}>
UNCOV
95
      <Tabs orientation="vertical" className="leftFixed" onChange={(e, item) => setCurrentTab(item)} value={currentTab}>
×
96
        {availableTabs.map(({ key }) => (
97
          <Tab className={`${classes.tab} capitalized`} key={key} label={key} value={key} />
4✔
98
        ))}
99
      </Tabs>
100
      <div className="rightFluid padding-right">
101
        {currentTab === 'upload' ? (
2!
102
          <>
103
            <InfoText className={classes.column}>Upload a file to the device</InfoText>
104
            <FileUpload
105
              enableContentReading={false}
106
              fileNameSelection={file?.name}
UNCOV
107
              onFileChange={() => undefined}
×
108
              onFileSelect={onFileSelect}
109
              placeholder={
110
                <>
111
                  Drag here or <a>browse</a> to upload a file
112
                </>
113
              }
114
              setSnackbar={setSnackbar}
115
              style={{ maxWidth }}
116
            />
117
            <div className={classes.inputWrapper}>
118
              <TextField
119
                autoFocus={true}
120
                error={!isValidDestination}
121
                fullWidth
122
                helperText={!isValidDestination && <div className="warning">Destination has to be an absolute path</div>}
2!
123
                inputProps={{ style: { marginTop: 16 } }}
124
                InputLabelProps={{ shrink: true }}
125
                label="Destination directory on the device where the file will be transferred"
UNCOV
126
                onChange={e => setUploadPath(e.target.value)}
×
127
                placeholder="Example: /opt/installed-by-single-file"
128
                value={uploadPath}
129
              />
130
              <Tooltip title="Paste" placement="top">
131
                <IconButton style={{ alignSelf: 'flex-end' }} onClick={onPasteUploadClick} size="large">
132
                  <CopyPasteIcon />
133
                </IconButton>
134
              </Tooltip>
135
            </div>
136
            <div className={`flexbox margin-top ${classes.column}`} style={{ justifyContent: 'flex-end' }}>
137
              <Button
138
                variant="contained"
139
                color="primary"
140
                disabled={!(file && uploadPath && isValidDestination)}
2!
UNCOV
141
                onClick={() => onUpload(deviceId, uploadPath, file)}
×
142
              >
143
                Upload
144
              </Button>
145
            </div>
146
          </>
147
        ) : (
148
          <>
149
            <InfoText>Download a file from the device</InfoText>
150
            <div className={classes.inputWrapper}>
151
              <TextField
152
                autoFocus={true}
153
                className={classes.column}
154
                error={!isValidDestination}
155
                fullWidth
156
                helperText={!isValidDestination && <div className="warning">Destination has to be an absolute path</div>}
×
157
                inputProps={{ className: classes.fileDestination }}
158
                InputLabelProps={{ shrink: true }}
159
                label="Path to the file on the device"
UNCOV
160
                onChange={e => setDownloadPath(e.target.value)}
×
161
                placeholder="Example: /home/mender/"
162
                value={downloadPath}
163
              />
164
              <Tooltip title="Paste" placement="top">
165
                <IconButton style={{ alignSelf: 'flex-end' }} onClick={onPasteDownloadClick} size="large">
166
                  <CopyPasteIcon />
167
                </IconButton>
168
              </Tooltip>
169
            </div>
170
            <div className={`flexbox margin-top ${classes.column}`} style={{ justifyContent: 'flex-end' }}>
171
              <Button
172
                variant="contained"
173
                color="primary"
174
                disabled={!(downloadPath && isValidDestination)}
×
UNCOV
175
                onClick={() => onDownload(downloadPath)}
×
176
                style={{ alignSelf: 'flex-end' }}
177
              >
178
                Download
179
              </Button>
180
            </div>
181
          </>
182
        )}
183
      </div>
184
    </div>
185
  );
186
};
187

188
export default FileTransfer;
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