cozy-keys-lib 6.1.0 → 6.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cozy-keys-lib",
3
- "version": "6.1.0",
3
+ "version": "6.2.0",
4
4
  "description": "Interactions between cozy-keys and a bitwarden server",
5
5
  "browser": "transpiled/index.js",
6
6
  "main": "transpiled/index.node.js",
@@ -38,28 +38,26 @@
38
38
  "@babel/plugin-transform-runtime": "7.17.0",
39
39
  "@babel/preset-env": "7.16.11",
40
40
  "@babel/runtime": "7.17.8",
41
- "@material-ui/core": "3",
42
- "@semantic-release/changelog": "^3.0.4",
43
- "@semantic-release/git": "^7.0.16",
44
- "@semantic-release/npm": "^5.1.15",
45
- "@testing-library/react": "^11.2.2",
46
- "@testing-library/react-hooks": "^3.2.1",
41
+ "@material-ui/core": "3.9.4",
42
+ "@semantic-release/changelog": "3.0.6",
43
+ "@semantic-release/git": "7.0.18",
44
+ "@semantic-release/npm": "5.3.5",
45
+ "@testing-library/react": "11.2.7",
46
+ "@testing-library/react-hooks": "8.0.1",
47
47
  "babel-loader": "8.2.4",
48
- "babel-plugin-inline-react-svg": "^1.1.0",
49
- "babel-plugin-mock-imports": "^1.0.2",
48
+ "babel-plugin-inline-react-svg": "1.1.2",
49
+ "babel-plugin-mock-imports": "1.2.0",
50
50
  "babel-plugin-rewire": "1.2.0",
51
- "babel-preset-cozy-app": "^1.6.0",
51
+ "babel-preset-cozy-app": "1.12.0",
52
52
  "cozy-client": "^40.5.0",
53
53
  "cozy-flags": "2.8.6",
54
- "cozy-intent": "^2.0.2",
55
- "cozy-ui": "^93.1.1",
56
- "enzyme": "^3.10.0",
57
- "enzyme-adapter-react-16": "^1.14.0",
58
- "enzyme-to-json": "3.4.0",
59
- "eslint-config-cozy-app": "^1.2.2",
54
+ "cozy-intent": "2.13.1",
55
+ "cozy-ui": "93.1.1",
56
+ "eslint-config-cozy-app": "1.6.0",
60
57
  "jest": "26.2.2",
61
- "react": "^16.9.0",
62
- "react-dom": "^16.9.0",
58
+ "react": "16.14.0",
59
+ "react-dom": "16.14.0",
60
+ "react-test-renderer": "16.14.0",
63
61
  "semantic-release": "^15.13.24"
64
62
  },
65
63
  "files": [
@@ -1,30 +1,12 @@
1
- import React, { useState, useCallback, useMemo, useEffect } from 'react'
2
- import cx from 'classnames'
1
+ import React, { useState, useCallback } from 'react'
3
2
  import get from 'lodash/get'
4
3
  import PropTypes from 'prop-types'
5
4
 
6
- import { useClient } from 'cozy-client'
7
5
  import flag from 'cozy-flags'
8
- import Typography from 'cozy-ui/transpiled/react/Typography'
9
- import useBreakpoints from 'cozy-ui/transpiled/react/providers/Breakpoints'
10
- import { useI18n } from 'cozy-ui/transpiled/react/providers/I18n'
11
- import Button from 'cozy-ui/transpiled/react/deprecated/Button'
12
- import Icon from 'cozy-ui/transpiled/react/Icon'
13
- import CozyTheme from 'cozy-ui/transpiled/react/providers/CozyTheme'
14
- import { IllustrationDialog } from 'cozy-ui/transpiled/react/CozyDialogs'
15
- import KeychainIcon from 'cozy-ui/transpiled/react/Icons/Keychain'
16
- import { useWebviewIntent } from 'cozy-intent'
17
-
18
- import CloudIcon from './IconCozySecurity'
19
- import PasswordField from './PasswordField'
6
+ import { AuthentificationDialog } from 'cozy-ui/transpiled/react/CozyDialogs'
20
7
 
21
8
  import { useVaultClient } from './VaultContext'
22
-
23
- const getPassphraseResetUrl = client => {
24
- const url = new URL('/auth/passphrase_reset', client.getStackClient().uri)
25
-
26
- return url.href
27
- }
9
+ import { useClient } from 'cozy-client'
28
10
 
29
11
  const canClientAuthWithOIDC = client => {
30
12
  return (
@@ -36,137 +18,43 @@ const canClientAuthWithOIDC = client => {
36
18
  const UnlockForm = props => {
37
19
  const client = useClient()
38
20
  const vaultClient = useVaultClient()
39
- const { isMobile } = useBreakpoints()
40
- const { t } = useI18n()
41
21
 
42
22
  const { onUnlock, onDismiss, closable } = props
43
23
 
44
- const [unlocking, setUnlocking] = useState(false)
24
+ const [isUnlocking, setUnlocking] = useState(false)
45
25
  const [error, setError] = useState(null)
46
- const [password, setPassword] = useState('')
47
-
48
- const handlePasswordChange = useCallback(ev => {
49
- setPassword(ev.currentTarget.value)
50
- })
26
+ const canAuthWithOIDC = canClientAuthWithOIDC(client)
51
27
 
52
- const unlockVault = useCallback(async () => {
53
- setUnlocking(true)
54
- setError(null)
55
- try {
56
- await vaultClient.unlock(password)
57
- if (onUnlock) {
58
- onUnlock()
28
+ const handleUnlock = useCallback(
29
+ async password => {
30
+ setUnlocking(true)
31
+ setError(null)
32
+ try {
33
+ await vaultClient.unlock(password)
34
+ if (onUnlock) {
35
+ onUnlock()
36
+ }
37
+ } catch (e) {
38
+ setError(
39
+ e.response.error === 'invalid password'
40
+ ? 'invalid_password'
41
+ : 'server_error'
42
+ )
43
+ } finally {
44
+ setUnlocking(false)
59
45
  }
60
- } catch (error) {
61
- setError(error)
62
- } finally {
63
- setUnlocking(false)
64
- }
65
- }, [vaultClient, onUnlock, password])
66
-
67
- const handleVaultUnlock = useCallback(
68
- ev => {
69
- ev.preventDefault()
70
- unlockVault()
71
46
  },
72
- [unlockVault]
47
+ [vaultClient, onUnlock]
73
48
  )
74
49
 
75
- const getPassphraseResetUrlCb = useMemo(() => getPassphraseResetUrl(client), [
76
- client
77
- ])
78
-
79
- const canAuthWithOIDC = canClientAuthWithOIDC(client)
80
-
81
- const webviewIntent = useWebviewIntent()
82
-
83
- useEffect(() => {
84
- webviewIntent &&
85
- webviewIntent.call(
86
- 'setFlagshipUI',
87
- {
88
- bottomTheme: 'light',
89
- topTheme: 'light'
90
- },
91
- 'cozy-keys-lib/UnlockForm'
92
- )
93
-
94
- return () =>
95
- webviewIntent &&
96
- webviewIntent.call(
97
- 'setFlagshipUI',
98
- {
99
- bottomTheme: 'light',
100
- topTheme: 'light'
101
- },
102
- 'cozy-keys-lib/UnlockForm'
103
- )
104
- }, [webviewIntent])
105
-
106
50
  return (
107
- <CozyTheme variant="inverted">
108
- <IllustrationDialog
109
- size="small"
110
- open={true}
111
- onClose={closable && onDismiss}
112
- content={
113
- <form
114
- onSubmit={handleVaultUnlock}
115
- className="u-stack-m u-flex u-flex-column u-flex-items-center"
116
- >
117
- {canAuthWithOIDC ? (
118
- <Icon icon={KeychainIcon} size={64} className="u-mb-half" />
119
- ) : (
120
- <CloudIcon className="u-mb-half" size={104} />
121
- )}
122
- <Typography variant="h3" gutterBottom>
123
- {canAuthWithOIDC ? t('unlock.title-oidc') : t('unlock.title')}
124
- </Typography>
125
- {error ? (
126
- <Typography variant="body1" className="u-error">
127
- {t('unlock.error')}
128
- </Typography>
129
- ) : (
130
- <Typography variant="body1">{t('unlock.subtitle')}</Typography>
131
- )}
132
-
133
- <PasswordField
134
- id="idField"
135
- label={
136
- canAuthWithOIDC ? t('unlock.label-oidc') : t('unlock.label')
137
- }
138
- value={password}
139
- onChange={handlePasswordChange}
140
- fullWidth
141
- className="u-mt-2"
142
- />
143
-
144
- <Typography
145
- variant="body1"
146
- component="a"
147
- color="primary"
148
- href={getPassphraseResetUrlCb}
149
- className="u-link u-mt-1 u-flex-self-start"
150
- >
151
- {t('unlock.forgotten-password')}
152
- </Typography>
153
- </form>
154
- }
155
- actions={
156
- <Button
157
- onClick={handleVaultUnlock}
158
- label={t('unlock.unlock')}
159
- theme="primary"
160
- className={cx({
161
- 'u-mt-1': !isMobile,
162
- 'u-mt-auto': isMobile
163
- })}
164
- busy={unlocking}
165
- extension="full"
166
- />
167
- }
168
- />
169
- </CozyTheme>
51
+ <AuthentificationDialog
52
+ onClose={closable ? onDismiss : null}
53
+ onSubmit={handleUnlock}
54
+ isLoading={isUnlocking}
55
+ isOIDC={canAuthWithOIDC}
56
+ error={error}
57
+ />
170
58
  )
171
59
  }
172
60
 
@@ -1,11 +1,12 @@
1
1
  import React from 'react'
2
- import UnlockForm from './UnlockForm'
3
- import { render, fireEvent, act, waitFor } from '@testing-library/react'
2
+ import { render, fireEvent, act, waitFor, screen } from '@testing-library/react'
3
+
4
4
  import CozyClient, { CozyProvider } from 'cozy-client'
5
- import { BreakpointsProvider } from 'cozy-ui/transpiled/react/providers/Breakpoints'
6
5
  import I18n from 'cozy-ui/transpiled/react/providers/I18n'
6
+ import { BreakpointsProvider } from 'cozy-ui/transpiled/react/providers/Breakpoints'
7
+
7
8
  import { VaultProvider, useVaultClient } from './VaultContext'
8
- import en from '../locales/en.json'
9
+ import UnlockForm from './UnlockForm'
9
10
 
10
11
  jest.mock('./VaultContext', () => {
11
12
  const { VaultProvider } = jest.requireActual('./VaultContext')
@@ -27,20 +28,22 @@ describe('unlock form', () => {
27
28
  const root = render(
28
29
  <CozyProvider client={client}>
29
30
  <BreakpointsProvider>
30
- <I18n dictRequire={() => en} lang="en">
31
- <VaultProvider instance="cozy.tools:8080">
31
+ <VaultProvider instance="cozy.tools:8080">
32
+ <I18n dictRequire={() => ({})} lang="en">
32
33
  <UnlockForm
33
34
  closable={true}
34
35
  onDismiss={jest.fn()}
35
36
  onUnlock={onUnlock}
36
37
  />
37
- </VaultProvider>
38
- </I18n>
38
+ </I18n>
39
+ </VaultProvider>
39
40
  </BreakpointsProvider>
40
41
  </CozyProvider>
41
42
  )
42
43
 
43
- const password = await waitFor(() => root.getByTestId('password'))
44
+ const password = await waitFor(() =>
45
+ screen.getByLabelText(/password/i, { selector: 'input' })
46
+ )
44
47
  await act(async () => {
45
48
  fireEvent.change(password, { target: { value: 'my-password' } })
46
49
  })
@@ -1,5 +1,5 @@
1
1
  import React from 'react'
2
- import { mount } from 'enzyme'
2
+ import { EventEmitter } from 'events'
3
3
  import {
4
4
  VaultContext,
5
5
  VaultProvider,
@@ -7,65 +7,78 @@ import {
7
7
  useVaultClient
8
8
  } from './VaultContext'
9
9
  import WebVaultClient from '../WebVaultClient'
10
+ import { render, waitFor } from '@testing-library/react'
10
11
  import { renderHook } from '@testing-library/react-hooks'
11
12
 
12
13
  jest.mock('../WebVaultClient')
13
14
 
15
+ const createFakeVaultClient = () => {
16
+ const vaultClient = new EventEmitter()
17
+
18
+ vaultClient.on('lock', () => {
19
+ vaultClient.locked = true
20
+ })
21
+ vaultClient.on('unlock', () => {
22
+ vaultClient.locked = false
23
+ })
24
+ vaultClient.isLocked = () => vaultClient.locked
25
+
26
+ return vaultClient
27
+ }
28
+
14
29
  describe('VaultProvider', () => {
15
30
  it('should rerender when the locked state changes', async () => {
16
- const ChildComponent = jest.fn()
31
+ const ChildComponent = jest.fn(() => null)
32
+ const vaultClient = createFakeVaultClient()
17
33
 
18
- const component = mount(
19
- <VaultProvider instance={'test@example.com'}>
34
+ render(
35
+ <VaultProvider vaultClient={vaultClient} instance={'test@example.com'}>
20
36
  <VaultContext.Consumer>{ChildComponent}</VaultContext.Consumer>
21
37
  </VaultProvider>
22
38
  )
23
39
 
24
- const vaultClient = component.state('vaultClient')
40
+ expect(ChildComponent).toHaveBeenCalledWith({
41
+ vaultClient,
42
+ locked: true
43
+ })
25
44
 
26
- expect(ChildComponent).toHaveBeenCalledWith({ vaultClient, locked: true })
45
+ vaultClient.emit('unlock')
27
46
 
28
- vaultClient.unlock()
29
-
30
- await new Promise(resolve =>
31
- setTimeout(() => {
32
- expect(ChildComponent).toHaveBeenCalledTimes(3)
33
- expect(ChildComponent).toHaveBeenCalledWith({
34
- vaultClient,
35
- locked: false
36
- })
37
- resolve()
47
+ await waitFor(() => {
48
+ expect(ChildComponent).toHaveBeenCalledWith({
49
+ vaultClient,
50
+ locked: false
38
51
  })
39
- )
52
+ })
40
53
 
41
- vaultClient.lock()
54
+ vaultClient.emit('lock')
42
55
 
43
- await new Promise(resolve =>
44
- setTimeout(() => {
45
- expect(ChildComponent).toHaveBeenCalledTimes(4)
46
- expect(ChildComponent).toHaveBeenCalledWith({
47
- vaultClient,
48
- locked: true
49
- })
50
- resolve()
56
+ await waitFor(() => {
57
+ expect(ChildComponent).toHaveBeenCalledWith({
58
+ vaultClient,
59
+ locked: true
51
60
  })
52
- )
61
+ })
53
62
  })
54
63
  })
55
64
 
56
65
  describe('withVaultClient', () => {
57
- it('should inject vaultClient as a prop', () => {
58
- const ChildComponent = () => <div />
66
+ it('should inject vaultClient as a prop', async () => {
67
+ const ChildComponent = jest.fn(() => null)
68
+
59
69
  const ChildWithClient = withVaultClient(ChildComponent)
60
70
 
61
- const component = mount(
71
+ render(
62
72
  <VaultProvider instance={'test@example.com'}>
63
73
  <ChildWithClient />
64
74
  </VaultProvider>
65
75
  )
66
76
 
67
- const child = component.find(ChildComponent)
68
- expect(child.prop('vaultClient')).toBeDefined()
77
+ // Check if the vault is correctly injected as a prop
78
+ const propsPassedToChildComponent = Object.keys(
79
+ ChildComponent.mock.calls[0][0]
80
+ )
81
+ expect(propsPassedToChildComponent).toContain('vaultClient')
69
82
  })
70
83
  })
71
84
 
@@ -3,20 +3,11 @@ import PropTypes from 'prop-types'
3
3
 
4
4
  import Spinner from 'cozy-ui/transpiled/react/Spinner'
5
5
  import Overlay from 'cozy-ui/transpiled/react/deprecated/Overlay'
6
- import withLocales from 'cozy-ui/transpiled/react/providers/I18n/withLocales'
7
-
8
- import localesEn from '../locales/en.json'
9
- import localesFr from '../locales/fr.json'
10
6
 
11
7
  import { VaultContext } from './VaultContext'
12
8
  import UnlockForm from './UnlockForm'
13
9
  import { checkShouldUnlock } from './defaults'
14
10
 
15
- const locales = {
16
- en: localesEn,
17
- fr: localesFr
18
- }
19
-
20
11
  const VaultUnlocker = ({
21
12
  children,
22
13
  onDismiss,
@@ -84,4 +75,4 @@ VaultUnlocker.defaultProps = {
84
75
  UnlockForm
85
76
  }
86
77
 
87
- export default withLocales(locales)(VaultUnlocker)
78
+ export default VaultUnlocker
@@ -1,15 +1 @@
1
- {
2
- "unlock": {
3
- "title": "Login",
4
- "title-oidc": "Cozy Pass",
5
- "subtitle": "For security, please confirm your identity",
6
- "error": "Incorrect password",
7
- "label": "Cozy password",
8
- "label-oidc": "Cozy Pass password",
9
- "show": "show",
10
- "hide": "hide",
11
- "abort": "Leave",
12
- "unlock": "Unlock",
13
- "forgotten-password": "I forgot my password"
14
- }
15
- }
1
+ {}
@@ -1,15 +1 @@
1
- {
2
- "unlock": {
3
- "title": "Authentification",
4
- "title-oidc": "Cozy Pass",
5
- "subtitle": "Par sécurité, merci de confirmer votre identité",
6
- "error": "Mot de passe incorrect, essayer à nouveau",
7
- "label": "Mot de passe Cozy",
8
- "label-oidc": "Mot de passe Cozy Pass",
9
- "show": "afficher",
10
- "hide": "masquer",
11
- "abort": "Quitter",
12
- "unlock": "Déverrouiller",
13
- "forgotten-password": "J'ai oublié mon mot de passe"
14
- }
15
- }
1
+ {}
@@ -1,156 +1,72 @@
1
1
  import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
2
2
  import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
3
3
  import _regeneratorRuntime from "@babel/runtime/regenerator";
4
- import React, { useState, useCallback, useMemo, useEffect } from 'react';
5
- import cx from 'classnames';
4
+ import React, { useState, useCallback } from 'react';
6
5
  import get from 'lodash/get';
7
6
  import PropTypes from 'prop-types';
8
- import { useClient } from 'cozy-client';
9
7
  import flag from 'cozy-flags';
10
- import Typography from 'cozy-ui/transpiled/react/Typography';
11
- import useBreakpoints from 'cozy-ui/transpiled/react/providers/Breakpoints';
12
- import { useI18n } from 'cozy-ui/transpiled/react/providers/I18n';
13
- import Button from 'cozy-ui/transpiled/react/deprecated/Button';
14
- import Icon from 'cozy-ui/transpiled/react/Icon';
15
- import CozyTheme from 'cozy-ui/transpiled/react/providers/CozyTheme';
16
- import { IllustrationDialog } from 'cozy-ui/transpiled/react/CozyDialogs';
17
- import KeychainIcon from 'cozy-ui/transpiled/react/Icons/Keychain';
18
- import { useWebviewIntent } from 'cozy-intent';
19
- import CloudIcon from './IconCozySecurity';
20
- import PasswordField from './PasswordField';
8
+ import { AuthentificationDialog } from 'cozy-ui/transpiled/react/CozyDialogs';
21
9
  import { useVaultClient } from './VaultContext';
22
- var getPassphraseResetUrl = function getPassphraseResetUrl(client) {
23
- var url = new URL('/auth/passphrase_reset', client.getStackClient().uri);
24
- return url.href;
25
- };
10
+ import { useClient } from 'cozy-client';
26
11
  var canClientAuthWithOIDC = function canClientAuthWithOIDC(client) {
27
12
  return get(client, 'capabilities.can_auth_with_oidc') || flag('vault.force-oidc-display');
28
13
  };
29
14
  var UnlockForm = function UnlockForm(props) {
30
15
  var client = useClient();
31
16
  var vaultClient = useVaultClient();
32
- var _useBreakpoints = useBreakpoints(),
33
- isMobile = _useBreakpoints.isMobile;
34
- var _useI18n = useI18n(),
35
- t = _useI18n.t;
36
17
  var onUnlock = props.onUnlock,
37
18
  onDismiss = props.onDismiss,
38
19
  closable = props.closable;
39
20
  var _useState = useState(false),
40
21
  _useState2 = _slicedToArray(_useState, 2),
41
- unlocking = _useState2[0],
22
+ isUnlocking = _useState2[0],
42
23
  setUnlocking = _useState2[1];
43
24
  var _useState3 = useState(null),
44
25
  _useState4 = _slicedToArray(_useState3, 2),
45
26
  error = _useState4[0],
46
27
  setError = _useState4[1];
47
- var _useState5 = useState(''),
48
- _useState6 = _slicedToArray(_useState5, 2),
49
- password = _useState6[0],
50
- setPassword = _useState6[1];
51
- var handlePasswordChange = useCallback(function (ev) {
52
- setPassword(ev.currentTarget.value);
53
- });
54
- var unlockVault = useCallback( /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
55
- return _regeneratorRuntime.wrap(function _callee$(_context) {
56
- while (1) switch (_context.prev = _context.next) {
57
- case 0:
58
- setUnlocking(true);
59
- setError(null);
60
- _context.prev = 2;
61
- _context.next = 5;
62
- return vaultClient.unlock(password);
63
- case 5:
64
- if (onUnlock) {
65
- onUnlock();
66
- }
67
- _context.next = 11;
68
- break;
69
- case 8:
70
- _context.prev = 8;
71
- _context.t0 = _context["catch"](2);
72
- setError(_context.t0);
73
- case 11:
74
- _context.prev = 11;
75
- setUnlocking(false);
76
- return _context.finish(11);
77
- case 14:
78
- case "end":
79
- return _context.stop();
80
- }
81
- }, _callee, null, [[2, 8, 11, 14]]);
82
- })), [vaultClient, onUnlock, password]);
83
- var handleVaultUnlock = useCallback(function (ev) {
84
- ev.preventDefault();
85
- unlockVault();
86
- }, [unlockVault]);
87
- var getPassphraseResetUrlCb = useMemo(function () {
88
- return getPassphraseResetUrl(client);
89
- }, [client]);
90
28
  var canAuthWithOIDC = canClientAuthWithOIDC(client);
91
- var webviewIntent = useWebviewIntent();
92
- useEffect(function () {
93
- webviewIntent && webviewIntent.call('setFlagshipUI', {
94
- bottomTheme: 'light',
95
- topTheme: 'light'
96
- }, 'cozy-keys-lib/UnlockForm');
97
- return function () {
98
- return webviewIntent && webviewIntent.call('setFlagshipUI', {
99
- bottomTheme: 'light',
100
- topTheme: 'light'
101
- }, 'cozy-keys-lib/UnlockForm');
29
+ var handleUnlock = useCallback( /*#__PURE__*/function () {
30
+ var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(password) {
31
+ return _regeneratorRuntime.wrap(function _callee$(_context) {
32
+ while (1) switch (_context.prev = _context.next) {
33
+ case 0:
34
+ setUnlocking(true);
35
+ setError(null);
36
+ _context.prev = 2;
37
+ _context.next = 5;
38
+ return vaultClient.unlock(password);
39
+ case 5:
40
+ if (onUnlock) {
41
+ onUnlock();
42
+ }
43
+ _context.next = 11;
44
+ break;
45
+ case 8:
46
+ _context.prev = 8;
47
+ _context.t0 = _context["catch"](2);
48
+ setError(_context.t0.response.error === 'invalid password' ? 'invalid_password' : 'server_error');
49
+ case 11:
50
+ _context.prev = 11;
51
+ setUnlocking(false);
52
+ return _context.finish(11);
53
+ case 14:
54
+ case "end":
55
+ return _context.stop();
56
+ }
57
+ }, _callee, null, [[2, 8, 11, 14]]);
58
+ }));
59
+ return function (_x) {
60
+ return _ref.apply(this, arguments);
102
61
  };
103
- }, [webviewIntent]);
104
- return /*#__PURE__*/React.createElement(CozyTheme, {
105
- variant: "inverted"
106
- }, /*#__PURE__*/React.createElement(IllustrationDialog, {
107
- size: "small",
108
- open: true,
109
- onClose: closable && onDismiss,
110
- content: /*#__PURE__*/React.createElement("form", {
111
- onSubmit: handleVaultUnlock,
112
- className: "u-stack-m u-flex u-flex-column u-flex-items-center"
113
- }, canAuthWithOIDC ? /*#__PURE__*/React.createElement(Icon, {
114
- icon: KeychainIcon,
115
- size: 64,
116
- className: "u-mb-half"
117
- }) : /*#__PURE__*/React.createElement(CloudIcon, {
118
- className: "u-mb-half",
119
- size: 104
120
- }), /*#__PURE__*/React.createElement(Typography, {
121
- variant: "h3",
122
- gutterBottom: true
123
- }, canAuthWithOIDC ? t('unlock.title-oidc') : t('unlock.title')), error ? /*#__PURE__*/React.createElement(Typography, {
124
- variant: "body1",
125
- className: "u-error"
126
- }, t('unlock.error')) : /*#__PURE__*/React.createElement(Typography, {
127
- variant: "body1"
128
- }, t('unlock.subtitle')), /*#__PURE__*/React.createElement(PasswordField, {
129
- id: "idField",
130
- label: canAuthWithOIDC ? t('unlock.label-oidc') : t('unlock.label'),
131
- value: password,
132
- onChange: handlePasswordChange,
133
- fullWidth: true,
134
- className: "u-mt-2"
135
- }), /*#__PURE__*/React.createElement(Typography, {
136
- variant: "body1",
137
- component: "a",
138
- color: "primary",
139
- href: getPassphraseResetUrlCb,
140
- className: "u-link u-mt-1 u-flex-self-start"
141
- }, t('unlock.forgotten-password'))),
142
- actions: /*#__PURE__*/React.createElement(Button, {
143
- onClick: handleVaultUnlock,
144
- label: t('unlock.unlock'),
145
- theme: "primary",
146
- className: cx({
147
- 'u-mt-1': !isMobile,
148
- 'u-mt-auto': isMobile
149
- }),
150
- busy: unlocking,
151
- extension: "full"
152
- })
153
- }));
62
+ }(), [vaultClient, onUnlock]);
63
+ return /*#__PURE__*/React.createElement(AuthentificationDialog, {
64
+ onClose: closable ? onDismiss : null,
65
+ onSubmit: handleUnlock,
66
+ isLoading: isUnlocking,
67
+ isOIDC: canAuthWithOIDC,
68
+ error: error
69
+ });
154
70
  };
155
71
  UnlockForm.propTypes = {
156
72
  onDismiss: PropTypes.func.isRequired,
@@ -1,13 +1,12 @@
1
1
  import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
2
2
  import _regeneratorRuntime from "@babel/runtime/regenerator";
3
3
  import React from 'react';
4
- import UnlockForm from './UnlockForm';
5
- import { render, fireEvent, act, waitFor } from '@testing-library/react';
4
+ import { render, fireEvent, act, waitFor, screen } from '@testing-library/react';
6
5
  import CozyClient, { CozyProvider } from 'cozy-client';
7
- import { BreakpointsProvider } from 'cozy-ui/transpiled/react/providers/Breakpoints';
8
6
  import I18n from 'cozy-ui/transpiled/react/providers/I18n';
7
+ import { BreakpointsProvider } from 'cozy-ui/transpiled/react/providers/Breakpoints';
9
8
  import { VaultProvider, useVaultClient } from './VaultContext';
10
- import en from '../locales/en.json';
9
+ import UnlockForm from './UnlockForm';
11
10
  jest.mock('./VaultContext', function () {
12
11
  var _jest$requireActual = jest.requireActual('./VaultContext'),
13
12
  VaultProvider = _jest$requireActual.VaultProvider;
@@ -38,13 +37,13 @@ describe('unlock form', function () {
38
37
  onUnlock = jest.fn();
39
38
  root = render( /*#__PURE__*/React.createElement(CozyProvider, {
40
39
  client: client
41
- }, /*#__PURE__*/React.createElement(BreakpointsProvider, null, /*#__PURE__*/React.createElement(I18n, {
40
+ }, /*#__PURE__*/React.createElement(BreakpointsProvider, null, /*#__PURE__*/React.createElement(VaultProvider, {
41
+ instance: "cozy.tools:8080"
42
+ }, /*#__PURE__*/React.createElement(I18n, {
42
43
  dictRequire: function dictRequire() {
43
- return en;
44
+ return {};
44
45
  },
45
46
  lang: "en"
46
- }, /*#__PURE__*/React.createElement(VaultProvider, {
47
- instance: "cozy.tools:8080"
48
47
  }, /*#__PURE__*/React.createElement(UnlockForm, {
49
48
  closable: true,
50
49
  onDismiss: jest.fn(),
@@ -52,7 +51,9 @@ describe('unlock form', function () {
52
51
  }))))));
53
52
  _context3.next = 7;
54
53
  return waitFor(function () {
55
- return root.getByTestId('password');
54
+ return screen.getByLabelText(/password/i, {
55
+ selector: 'input'
56
+ });
56
57
  });
57
58
  case 7:
58
59
  password = _context3.sent;
@@ -1,49 +1,58 @@
1
1
  import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
2
2
  import _regeneratorRuntime from "@babel/runtime/regenerator";
3
3
  import React from 'react';
4
- import { mount } from 'enzyme';
4
+ import { EventEmitter } from 'events';
5
5
  import { VaultContext, VaultProvider, withVaultClient, useVaultClient } from './VaultContext';
6
6
  import WebVaultClient from '../WebVaultClient';
7
+ import { render, waitFor } from '@testing-library/react';
7
8
  import { renderHook } from '@testing-library/react-hooks';
8
9
  jest.mock('../WebVaultClient');
10
+ var createFakeVaultClient = function createFakeVaultClient() {
11
+ var vaultClient = new EventEmitter();
12
+ vaultClient.on('lock', function () {
13
+ vaultClient.locked = true;
14
+ });
15
+ vaultClient.on('unlock', function () {
16
+ vaultClient.locked = false;
17
+ });
18
+ vaultClient.isLocked = function () {
19
+ return vaultClient.locked;
20
+ };
21
+ return vaultClient;
22
+ };
9
23
  describe('VaultProvider', function () {
10
24
  it('should rerender when the locked state changes', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
11
- var ChildComponent, component, vaultClient;
25
+ var ChildComponent, vaultClient;
12
26
  return _regeneratorRuntime.wrap(function _callee$(_context) {
13
27
  while (1) switch (_context.prev = _context.next) {
14
28
  case 0:
15
- ChildComponent = jest.fn();
16
- component = mount( /*#__PURE__*/React.createElement(VaultProvider, {
29
+ ChildComponent = jest.fn(function () {
30
+ return null;
31
+ });
32
+ vaultClient = createFakeVaultClient();
33
+ render( /*#__PURE__*/React.createElement(VaultProvider, {
34
+ vaultClient: vaultClient,
17
35
  instance: 'test@example.com'
18
36
  }, /*#__PURE__*/React.createElement(VaultContext.Consumer, null, ChildComponent)));
19
- vaultClient = component.state('vaultClient');
20
37
  expect(ChildComponent).toHaveBeenCalledWith({
21
38
  vaultClient: vaultClient,
22
39
  locked: true
23
40
  });
24
- vaultClient.unlock();
41
+ vaultClient.emit('unlock');
25
42
  _context.next = 7;
26
- return new Promise(function (resolve) {
27
- return setTimeout(function () {
28
- expect(ChildComponent).toHaveBeenCalledTimes(3);
29
- expect(ChildComponent).toHaveBeenCalledWith({
30
- vaultClient: vaultClient,
31
- locked: false
32
- });
33
- resolve();
43
+ return waitFor(function () {
44
+ expect(ChildComponent).toHaveBeenCalledWith({
45
+ vaultClient: vaultClient,
46
+ locked: false
34
47
  });
35
48
  });
36
49
  case 7:
37
- vaultClient.lock();
50
+ vaultClient.emit('lock');
38
51
  _context.next = 10;
39
- return new Promise(function (resolve) {
40
- return setTimeout(function () {
41
- expect(ChildComponent).toHaveBeenCalledTimes(4);
42
- expect(ChildComponent).toHaveBeenCalledWith({
43
- vaultClient: vaultClient,
44
- locked: true
45
- });
46
- resolve();
52
+ return waitFor(function () {
53
+ expect(ChildComponent).toHaveBeenCalledWith({
54
+ vaultClient: vaultClient,
55
+ locked: true
47
56
  });
48
57
  });
49
58
  case 10:
@@ -54,22 +63,33 @@ describe('VaultProvider', function () {
54
63
  })));
55
64
  });
56
65
  describe('withVaultClient', function () {
57
- it('should inject vaultClient as a prop', function () {
58
- var ChildComponent = function ChildComponent() {
59
- return /*#__PURE__*/React.createElement("div", null);
60
- };
61
- var ChildWithClient = withVaultClient(ChildComponent);
62
- var component = mount( /*#__PURE__*/React.createElement(VaultProvider, {
63
- instance: 'test@example.com'
64
- }, /*#__PURE__*/React.createElement(ChildWithClient, null)));
65
- var child = component.find(ChildComponent);
66
- expect(child.prop('vaultClient')).toBeDefined();
67
- });
66
+ it('should inject vaultClient as a prop', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2() {
67
+ var ChildComponent, ChildWithClient, propsPassedToChildComponent;
68
+ return _regeneratorRuntime.wrap(function _callee2$(_context2) {
69
+ while (1) switch (_context2.prev = _context2.next) {
70
+ case 0:
71
+ ChildComponent = jest.fn(function () {
72
+ return null;
73
+ });
74
+ ChildWithClient = withVaultClient(ChildComponent);
75
+ render( /*#__PURE__*/React.createElement(VaultProvider, {
76
+ instance: 'test@example.com'
77
+ }, /*#__PURE__*/React.createElement(ChildWithClient, null)));
78
+
79
+ // Check if the vault is correctly injected as a prop
80
+ propsPassedToChildComponent = Object.keys(ChildComponent.mock.calls[0][0]);
81
+ expect(propsPassedToChildComponent).toContain('vaultClient');
82
+ case 5:
83
+ case "end":
84
+ return _context2.stop();
85
+ }
86
+ }, _callee2);
87
+ })));
68
88
  });
69
89
  describe('useVaultClient', function () {
70
90
  it('should return the vault client from the context', function () {
71
- var wrapper = function wrapper(_ref2) {
72
- var children = _ref2.children;
91
+ var wrapper = function wrapper(_ref3) {
92
+ var children = _ref3.children;
73
93
  return /*#__PURE__*/React.createElement(VaultProvider, {
74
94
  instance: "test@example.com"
75
95
  }, children);
@@ -5,16 +5,9 @@ import React, { useContext, useState, useEffect } from 'react';
5
5
  import PropTypes from 'prop-types';
6
6
  import Spinner from 'cozy-ui/transpiled/react/Spinner';
7
7
  import Overlay from 'cozy-ui/transpiled/react/deprecated/Overlay';
8
- import withLocales from 'cozy-ui/transpiled/react/providers/I18n/withLocales';
9
- import localesEn from '../locales/en.json';
10
- import localesFr from '../locales/fr.json';
11
8
  import { VaultContext } from './VaultContext';
12
9
  import UnlockForm from './UnlockForm';
13
10
  import { checkShouldUnlock } from './defaults';
14
- var locales = {
15
- en: localesEn,
16
- fr: localesFr
17
- };
18
11
  var VaultUnlocker = function VaultUnlocker(_ref) {
19
12
  var children = _ref.children,
20
13
  onDismiss = _ref.onDismiss,
@@ -98,4 +91,4 @@ VaultUnlocker.propTypes = {
98
91
  VaultUnlocker.defaultProps = {
99
92
  UnlockForm: UnlockForm
100
93
  };
101
- export default withLocales(locales)(VaultUnlocker);
94
+ export default VaultUnlocker;
@@ -1,15 +1 @@
1
- {
2
- "unlock": {
3
- "title": "Login",
4
- "title-oidc": "Cozy Pass",
5
- "subtitle": "For security, please confirm your identity",
6
- "error": "Incorrect password",
7
- "label": "Cozy password",
8
- "label-oidc": "Cozy Pass password",
9
- "show": "show",
10
- "hide": "hide",
11
- "abort": "Leave",
12
- "unlock": "Unlock",
13
- "forgotten-password": "I forgot my password"
14
- }
15
- }
1
+ {}
@@ -1,15 +1 @@
1
- {
2
- "unlock": {
3
- "title": "Authentification",
4
- "title-oidc": "Cozy Pass",
5
- "subtitle": "Par sécurité, merci de confirmer votre identité",
6
- "error": "Mot de passe incorrect, essayer à nouveau",
7
- "label": "Mot de passe Cozy",
8
- "label-oidc": "Mot de passe Cozy Pass",
9
- "show": "afficher",
10
- "hide": "masquer",
11
- "abort": "Quitter",
12
- "unlock": "Déverrouiller",
13
- "forgotten-password": "J'ai oublié mon mot de passe"
14
- }
15
- }
1
+ {}