• 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

74.07
/src/js/components/settings/user-management/twofactorauthsetup.js
1
// Copyright 2019 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, { useCallback, useEffect, useState } from 'react';
15
import { useDispatch, useSelector } from 'react-redux';
16

17
import { Collapse, Switch } from '@mui/material';
18

19
import { setSnackbar } from '../../../actions/appActions';
20
import { disableUser2fa, enableUser2fa, get2FAQRCode, verify2FA, verifyEmailComplete, verifyEmailStart } from '../../../actions/userActions';
21
import { twoFAStates } from '../../../constants/userConstants';
22
import { getCurrentUser, getHas2FA } from '../../../selectors';
23
import InfoText from '../../common/infotext';
24
import AuthSetup from './twofactorauth-steps/authsetup';
25
import EmailVerification from './twofactorauth-steps/emailverification';
26

27
export const TwoFactorAuthSetup = () => {
6✔
28
  const activationCode = useSelector(state => state.users.activationCode);
22✔
29
  const currentUser = useSelector(getCurrentUser);
14✔
30
  const has2FA = useSelector(getHas2FA);
14✔
31
  const qrImage = useSelector(state => state.users.qrCode);
22✔
32
  const [qrExpanded, setQrExpanded] = useState(false);
14✔
33
  const [is2FAEnabled, setIs2FAEnabled] = useState(has2FA);
14✔
34
  const [showEmailVerification, setShowEmailVerification] = useState(false);
14✔
35
  const dispatch = useDispatch();
14✔
36

37
  useEffect(() => {
14✔
38
    if ((currentUser.verified || currentUser.email?.endsWith('@example.com')) && is2FAEnabled && !has2FA) {
4✔
39
      setShowEmailVerification(false);
1✔
40
      setQrExpanded(true);
1✔
41
    }
42
  }, [currentUser.email, currentUser.verified, is2FAEnabled, has2FA]);
43

44
  useEffect(() => {
14✔
45
    if (activationCode) {
2!
46
      setIs2FAEnabled(true);
×
47
      dispatch(verifyEmailComplete(activationCode))
×
48
        .catch(() => {
49
          setShowEmailVerification(true);
×
50
          setQrExpanded(false);
×
51
        })
52
        // we have to explicitly call this, to not send the returned promise as user to activate 2fa for
53
        .then(() => dispatch(enableUser2fa()))
×
54
        .then(() => dispatch(get2FAQRCode()));
×
55
    }
56
  }, [activationCode]);
57

58
  useEffect(() => {
14✔
59
    if (has2FA) {
3✔
60
      setIs2FAEnabled(has2FA);
1✔
61
    }
62
  }, [has2FA]);
63

64
  const handle2FAState = state => {
14✔
65
    setIs2FAEnabled(state !== twoFAStates.disabled);
2✔
66
    setQrExpanded(state === twoFAStates.unverified);
2✔
67
    let request;
68
    if (state === twoFAStates.disabled) {
2!
69
      request = dispatch(disableUser2fa());
×
70
    } else if (state === twoFAStates.enabled && has2FA) {
2✔
71
      request = Promise.resolve(setQrExpanded(false));
1✔
72
    } else {
73
      request = dispatch(enableUser2fa());
1✔
74
    }
75
    request.then(() => {
2✔
76
      if (state === twoFAStates.unverified) {
2✔
77
        dispatch(get2FAQRCode());
1✔
78
      } else if (state === twoFAStates.enabled) {
1!
79
        setSnackbar('Two Factor authentication set up successfully.');
1✔
80
      }
81
    });
82
  };
83

84
  const onToggle2FAClick = useCallback(() => {
14✔
85
    if (!(currentUser.verified || currentUser.email?.endsWith('@example.com'))) {
1!
86
      setShowEmailVerification(!showEmailVerification);
×
87
      setIs2FAEnabled(!showEmailVerification);
×
88
      return;
×
89
    }
90
    if (has2FA) {
1!
91
      handle2FAState(twoFAStates.disabled);
×
92
    } else {
93
      is2FAEnabled ? dispatch(disableUser2fa()) : handle2FAState(twoFAStates.unverified);
1!
94
      setQrExpanded(!is2FAEnabled);
1✔
95
      setIs2FAEnabled(!is2FAEnabled);
1✔
96
    }
97
  }, [currentUser.email, currentUser.verified, has2FA, is2FAEnabled, showEmailVerification]);
98

99
  return (
14✔
100
    <div className="margin-top">
101
      <div className="clickable flexbox space-between" onClick={onToggle2FAClick}>
102
        <p className="help-content">Enable Two Factor authentication</p>
103
        <Switch checked={is2FAEnabled} />
104
      </div>
105
      <InfoText style={{ width: '75%', margin: 0 }}>
106
        Two Factor Authentication adds a second layer of protection to your account by asking for an additional verification code each time you log in.
107
      </InfoText>
108
      {showEmailVerification && (
14!
109
        <EmailVerification
110
          activationCode={activationCode}
111
          verifyEmailComplete={data => dispatch(verifyEmailComplete(data))}
×
112
          verifyEmailStart={() => dispatch(verifyEmailStart())}
×
113
        />
114
      )}
115
      <Collapse in={qrExpanded} timeout="auto" unmountOnExit>
116
        <AuthSetup currentUser={currentUser} handle2FAState={handle2FAState} has2FA={has2FA} qrImage={qrImage} verify2FA={data => dispatch(verify2FA(data))} />
1✔
117
      </Collapse>
118
    </div>
119
  );
120
};
121

122
export default TwoFactorAuthSetup;
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