cozy-keys-lib 3.11.0 → 4.1.2

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": "3.11.0",
3
+ "version": "4.1.2",
4
4
  "description": "Interactions between cozy-keys and a bitwarden server",
5
5
  "browser": "transpiled/index.js",
6
6
  "main": "transpiled/index.node.js",
@@ -32,7 +32,18 @@ class VaultProvider extends React.Component {
32
32
  }
33
33
 
34
34
  componentDidMount() {
35
- this.setupClient()
35
+ const { cozyClient, instance } = this.props
36
+ if (cozyClient) {
37
+ const uri = cozyClient.getStackClient().uri
38
+ if (!uri) {
39
+ cozyClient.on('login', () => {
40
+ this.setupClient(cozyClient.getStackClient().uri)
41
+ })
42
+ }
43
+ }
44
+ if (instance) {
45
+ this.setupClient(this.props.instance)
46
+ }
36
47
  }
37
48
 
38
49
  componentWillUnmount() {
@@ -53,11 +64,12 @@ class VaultProvider extends React.Component {
53
64
  }
54
65
  }
55
66
 
56
- setupClient() {
67
+ setupClient(instance) {
57
68
  const unsafeStorage = this.props.unsafeStorage
58
69
  const vaultClient =
59
70
  this.props.vaultClient ||
60
- getVaultClient(this.props.instance, unsafeStorage, this.props.vaultData)
71
+ this.props.client ||
72
+ getVaultClient(instance, unsafeStorage, this.props.vaultData)
61
73
 
62
74
  this.setState(
63
75
  {
@@ -79,12 +91,11 @@ class VaultProvider extends React.Component {
79
91
  }
80
92
 
81
93
  render() {
82
- const { vaultClient } = this.state
83
- return vaultClient ? (
94
+ return (
84
95
  <VaultContext.Provider value={this.state}>
85
96
  {this.props.children}
86
97
  </VaultContext.Provider>
87
- ) : null
98
+ )
88
99
  }
89
100
  }
90
101
 
@@ -93,7 +104,8 @@ VaultProvider.propTypes = {
93
104
  vaultClient: PropTypes.object,
94
105
  client: PropTypes.object, // deprecated
95
106
  unsafeStorage: PropTypes.bool,
96
- setClient: PropTypes.func
107
+ setClient: PropTypes.func,
108
+ cozyClient: PropTypes.object
97
109
  }
98
110
 
99
111
  const withVaultClient = BaseComponent => {
@@ -29,7 +29,7 @@ describe('VaultProvider', () => {
29
29
 
30
30
  await new Promise(resolve =>
31
31
  setTimeout(() => {
32
- expect(ChildComponent).toHaveBeenCalledTimes(2)
32
+ expect(ChildComponent).toHaveBeenCalledTimes(3)
33
33
  expect(ChildComponent).toHaveBeenCalledWith({
34
34
  vaultClient,
35
35
  locked: false
@@ -42,7 +42,7 @@ describe('VaultProvider', () => {
42
42
 
43
43
  await new Promise(resolve =>
44
44
  setTimeout(() => {
45
- expect(ChildComponent).toHaveBeenCalledTimes(3)
45
+ expect(ChildComponent).toHaveBeenCalledTimes(4)
46
46
  expect(ChildComponent).toHaveBeenCalledWith({
47
47
  vaultClient,
48
48
  locked: true
@@ -4,7 +4,6 @@ import PropTypes from 'prop-types'
4
4
  import Spinner from 'cozy-ui/transpiled/react/Spinner'
5
5
  import Overlay from 'cozy-ui/transpiled/react/Overlay'
6
6
  import withLocales from 'cozy-ui/transpiled/react/I18n/withLocales'
7
- import { useClient } from 'cozy-client'
8
7
 
9
8
  import localesEn from '../locales/en.json'
10
9
  import localesFr from '../locales/fr.json'
@@ -24,9 +23,8 @@ const VaultUnlocker = ({
24
23
  closable,
25
24
  onUnlock,
26
25
  UnlockForm,
27
- checkShouldUnlock
26
+ addCheckShouldUnlock
28
27
  }) => {
29
- const cozyClient = useClient()
30
28
  const { locked, vaultClient } = useContext(VaultContext)
31
29
 
32
30
  const [showSpinner, setShowSpinner] = useState(false)
@@ -42,7 +40,10 @@ const VaultUnlocker = ({
42
40
 
43
41
  useEffect(() => {
44
42
  const doCheckShouldUnlock = async () => {
45
- const shouldUnlock = await checkShouldUnlock(vaultClient, cozyClient)
43
+ const shouldUnlock = await checkShouldUnlock(
44
+ vaultClient,
45
+ addCheckShouldUnlock
46
+ )
46
47
 
47
48
  setShouldUnlock(shouldUnlock)
48
49
  setIsChecking(false)
@@ -75,12 +76,12 @@ const VaultUnlocker = ({
75
76
  VaultUnlocker.propTypes = {
76
77
  onDismiss: PropTypes.func.isRequired,
77
78
  closable: PropTypes.bool,
78
- onUnlock: PropTypes.func.isRequired
79
+ onUnlock: PropTypes.func.isRequired,
80
+ addCheckShouldUnlock: PropTypes.func
79
81
  }
80
82
 
81
83
  VaultUnlocker.defaultProps = {
82
- UnlockForm,
83
- checkShouldUnlock: checkShouldUnlock
84
+ UnlockForm
84
85
  }
85
86
 
86
87
  export default withLocales(locales)(VaultUnlocker)
@@ -1,7 +1,13 @@
1
- import { checkHasInstalledExtension } from '../CozyUtils'
2
-
3
- export const checkShouldUnlock = async (vaultClient, client) => {
4
- return (
5
- (await checkHasInstalledExtension(client)) && (await vaultClient.isLocked())
6
- )
1
+ /**
2
+ * Check whether or not the vault should be unlocked
3
+ *
4
+ * @param {object} vaultClient - The vault client
5
+ * @param {function} addCheckShouldUnlock - An additional check method to unlock
6
+ * @returns {boolean} True if the vault should be unlocked
7
+ */
8
+ export const checkShouldUnlock = async (
9
+ vaultClient,
10
+ addCheckShouldUnlock = () => true
11
+ ) => {
12
+ return (await addCheckShouldUnlock()) && (await vaultClient.isLocked())
7
13
  }
@@ -0,0 +1,34 @@
1
+ import { checkShouldUnlock } from './defaults'
2
+ import WebVaultClient from '../WebVaultClient'
3
+
4
+ jest.mock('../CozyUtils', () => ({
5
+ checkHasInstalledExtension: jest.fn(),
6
+ getEmail: jest.fn().mockReturnValue('me@test.fr'),
7
+ isInstance: jest.fn().mockReturnValue(true)
8
+ }))
9
+
10
+ describe('checkShouldUnlock', () => {
11
+ const vaultClient = new WebVaultClient('https://me.cozy.wtf')
12
+
13
+ it('should return true when the vault is locked', async () => {
14
+ jest.spyOn(vaultClient, 'isLocked').mockResolvedValue(true)
15
+ const shouldUnlock = await checkShouldUnlock(vaultClient)
16
+ expect(shouldUnlock).toBe(true)
17
+ })
18
+
19
+ it('should return false when the vault is already unlocked', async () => {
20
+ jest.spyOn(vaultClient, 'isLocked').mockResolvedValue(false)
21
+ const shouldUnlock = await checkShouldUnlock(vaultClient)
22
+ expect(shouldUnlock).toBe(false)
23
+ })
24
+
25
+ it('should return false when additional check function return false', async () => {
26
+ jest.spyOn(vaultClient, 'isLocked').mockResolvedValue(true)
27
+ const additionalUnlockCheck = () => false
28
+ const shouldUnlock = await checkShouldUnlock(
29
+ vaultClient,
30
+ additionalUnlockCheck
31
+ )
32
+ expect(shouldUnlock).toBe(false)
33
+ })
34
+ })
@@ -1,6 +1,4 @@
1
1
  import React, { useMemo, useState, useContext } from 'react'
2
- import { useClient } from 'cozy-client'
3
-
4
2
  import { useVaultClient } from './VaultContext'
5
3
  import { checkShouldUnlock } from './defaults'
6
4
 
@@ -42,8 +40,7 @@ export const useVaultUnlockContext = () => {
42
40
  * </VaultProvider>
43
41
  * ```
44
42
  */
45
- export const VaultUnlockProvider = ({ children, checkShouldUnlock }) => {
46
- const client = useClient()
43
+ export const VaultUnlockProvider = ({ children, addCheckShouldUnlock }) => {
47
44
  const vaultClient = useVaultClient()
48
45
  const [showingUnlockForm, setShowingUnlockForm] = useState(false)
49
46
  const [unlockFormProps, setUnlockFormProps] = useState(null)
@@ -64,7 +61,10 @@ export const VaultUnlockProvider = ({ children, checkShouldUnlock }) => {
64
61
  unlockFormProps.onDismiss && unlockFormProps.onDismiss()
65
62
  }
66
63
 
67
- const shouldUnlock = await checkShouldUnlock(vaultClient, client)
64
+ const shouldUnlock = await checkShouldUnlock(
65
+ vaultClient,
66
+ addCheckShouldUnlock
67
+ )
68
68
 
69
69
  if (shouldUnlock) {
70
70
  setUnlockFormProps({
@@ -84,13 +84,7 @@ export const VaultUnlockProvider = ({ children, checkShouldUnlock }) => {
84
84
  unlockFormProps,
85
85
  vaultClient
86
86
  }
87
- }, [
88
- showingUnlockForm,
89
- unlockFormProps,
90
- vaultClient,
91
- checkShouldUnlock,
92
- client
93
- ])
87
+ }, [showingUnlockForm, unlockFormProps, vaultClient, addCheckShouldUnlock])
94
88
 
95
89
  return (
96
90
  <vaultUnlockContext.Provider value={value}>
@@ -99,10 +93,6 @@ export const VaultUnlockProvider = ({ children, checkShouldUnlock }) => {
99
93
  )
100
94
  }
101
95
 
102
- VaultUnlockProvider.defaultProps = {
103
- checkShouldUnlock: checkShouldUnlock
104
- }
105
-
106
96
  export const withVaultUnlockContext = Component => {
107
97
  const Wrapper = props => {
108
98
  const vaultUnlockContextValue = useVaultUnlockContext()
@@ -4,17 +4,12 @@ import {
4
4
  VaultUnlockProvider,
5
5
  useVaultUnlockContext
6
6
  } from './vaultUnlockContext'
7
- import { checkHasInstalledExtension } from '../CozyUtils'
8
7
  import { useVaultClient } from './VaultContext'
9
8
 
10
9
  jest.mock('./VaultContext', () => ({
11
10
  useVaultClient: jest.fn()
12
11
  }))
13
12
 
14
- jest.mock('../CozyUtils', () => ({
15
- checkHasInstalledExtension: jest.fn()
16
- }))
17
-
18
13
  describe('vault unlock context', () => {
19
14
  const Component = ({ onUnlock }) => {
20
15
  const { showingUnlockForm, showUnlockForm } = useVaultUnlockContext()
@@ -51,27 +46,15 @@ describe('vault unlock context', () => {
51
46
  useVaultClient.mockReturnValue({
52
47
  isLocked: jest.fn().mockResolvedValue(true)
53
48
  })
54
- checkHasInstalledExtension.mockResolvedValue(true)
55
49
  const { root, onUnlock } = await setup()
56
50
  root.getByText('showingUnlockForm: true')
57
51
  expect(onUnlock).not.toHaveBeenCalled()
58
52
  })
59
53
 
60
- it('should not show unlock form and call onUnlock if extension is not installed', async () => {
61
- useVaultClient.mockReturnValue({
62
- isLocked: jest.fn().mockResolvedValue(true)
63
- })
64
- checkHasInstalledExtension.mockResolvedValue(false)
65
- const { root, onUnlock } = await setup()
66
- expect(root.getByText('showingUnlockForm: false')).toBeTruthy()
67
- expect(onUnlock).toHaveBeenCalled()
68
- })
69
-
70
54
  it('should not show unlock form and call onUnlock if vault is unlocked', async () => {
71
55
  useVaultClient.mockReturnValue({
72
56
  isLocked: jest.fn().mockResolvedValue(false)
73
57
  })
74
- checkHasInstalledExtension.mockResolvedValue(true)
75
58
  const { root, onUnlock } = await setup()
76
59
  expect(root.getByText('showingUnlockForm: false')).toBeTruthy()
77
60
  expect(onUnlock).toHaveBeenCalled()
package/src/index.js CHANGED
@@ -18,4 +18,3 @@ export {
18
18
  VaultUnlockProvider,
19
19
  withVaultUnlockContext
20
20
  } from './components/vaultUnlockContext'
21
- export { UnlockForm } from './components/UnlockForm'
@@ -49,7 +49,25 @@ function (_React$Component) {
49
49
  _createClass(VaultProvider, [{
50
50
  key: "componentDidMount",
51
51
  value: function componentDidMount() {
52
- this.setupClient();
52
+ var _this2 = this;
53
+
54
+ var _this$props = this.props,
55
+ cozyClient = _this$props.cozyClient,
56
+ instance = _this$props.instance;
57
+
58
+ if (cozyClient) {
59
+ var uri = cozyClient.getStackClient().uri;
60
+
61
+ if (!uri) {
62
+ cozyClient.on('login', function () {
63
+ _this2.setupClient(cozyClient.getStackClient().uri);
64
+ });
65
+ }
66
+ }
67
+
68
+ if (instance) {
69
+ this.setupClient(this.props.instance);
70
+ }
53
71
  }
54
72
  }, {
55
73
  key: "componentWillUnmount",
@@ -104,20 +122,20 @@ function (_React$Component) {
104
122
  }()
105
123
  }, {
106
124
  key: "setupClient",
107
- value: function setupClient() {
108
- var _this2 = this;
125
+ value: function setupClient(instance) {
126
+ var _this3 = this;
109
127
 
110
128
  var unsafeStorage = this.props.unsafeStorage;
111
- var vaultClient = this.props.vaultClient || getVaultClient(this.props.instance, unsafeStorage, this.props.vaultData);
129
+ var vaultClient = this.props.vaultClient || this.props.client || getVaultClient(instance, unsafeStorage, this.props.vaultData);
112
130
  this.setState({
113
131
  vaultClient: vaultClient,
114
132
  locked: true
115
133
  }, function () {
116
- vaultClient.on('unlock', _this2.updateLockedState);
117
- vaultClient.on('lock', _this2.updateLockedState);
118
- vaultClient.on('login', _this2.updateLockedState);
134
+ vaultClient.on('unlock', _this3.updateLockedState);
135
+ vaultClient.on('lock', _this3.updateLockedState);
136
+ vaultClient.on('login', _this3.updateLockedState);
119
137
 
120
- _this2.updateLockedState();
138
+ _this3.updateLockedState();
121
139
  });
122
140
 
123
141
  if (this.props.setClient) {
@@ -127,10 +145,9 @@ function (_React$Component) {
127
145
  }, {
128
146
  key: "render",
129
147
  value: function render() {
130
- var vaultClient = this.state.vaultClient;
131
- return vaultClient ? React.createElement(VaultContext.Provider, {
148
+ return React.createElement(VaultContext.Provider, {
132
149
  value: this.state
133
- }, this.props.children) : null;
150
+ }, this.props.children);
134
151
  }
135
152
  }]);
136
153
 
@@ -143,7 +160,8 @@ VaultProvider.propTypes = {
143
160
  client: PropTypes.object,
144
161
  // deprecated
145
162
  unsafeStorage: PropTypes.bool,
146
- setClient: PropTypes.func
163
+ setClient: PropTypes.func,
164
+ cozyClient: PropTypes.object
147
165
  };
148
166
 
149
167
  var withVaultClient = function withVaultClient(BaseComponent) {
@@ -30,7 +30,7 @@ describe('VaultProvider', function () {
30
30
  _context.next = 7;
31
31
  return new Promise(function (resolve) {
32
32
  return setTimeout(function () {
33
- expect(ChildComponent).toHaveBeenCalledTimes(2);
33
+ expect(ChildComponent).toHaveBeenCalledTimes(3);
34
34
  expect(ChildComponent).toHaveBeenCalledWith({
35
35
  vaultClient: vaultClient,
36
36
  locked: false
@@ -44,7 +44,7 @@ describe('VaultProvider', function () {
44
44
  _context.next = 10;
45
45
  return new Promise(function (resolve) {
46
46
  return setTimeout(function () {
47
- expect(ChildComponent).toHaveBeenCalledTimes(3);
47
+ expect(ChildComponent).toHaveBeenCalledTimes(4);
48
48
  expect(ChildComponent).toHaveBeenCalledWith({
49
49
  vaultClient: vaultClient,
50
50
  locked: true
@@ -6,7 +6,6 @@ import PropTypes from 'prop-types';
6
6
  import Spinner from 'cozy-ui/transpiled/react/Spinner';
7
7
  import Overlay from 'cozy-ui/transpiled/react/Overlay';
8
8
  import withLocales from 'cozy-ui/transpiled/react/I18n/withLocales';
9
- import { useClient } from 'cozy-client';
10
9
  import localesEn from '../locales/en.json';
11
10
  import localesFr from '../locales/fr.json';
12
11
  import { VaultContext } from './VaultContext';
@@ -23,8 +22,7 @@ var VaultUnlocker = function VaultUnlocker(_ref) {
23
22
  closable = _ref.closable,
24
23
  onUnlock = _ref.onUnlock,
25
24
  UnlockForm = _ref.UnlockForm,
26
- checkShouldUnlock = _ref.checkShouldUnlock;
27
- var cozyClient = useClient();
25
+ addCheckShouldUnlock = _ref.addCheckShouldUnlock;
28
26
 
29
27
  var _useContext = useContext(VaultContext),
30
28
  locked = _useContext.locked,
@@ -67,7 +65,7 @@ var VaultUnlocker = function VaultUnlocker(_ref) {
67
65
  switch (_context.prev = _context.next) {
68
66
  case 0:
69
67
  _context.next = 2;
70
- return checkShouldUnlock(vaultClient, cozyClient);
68
+ return checkShouldUnlock(vaultClient, addCheckShouldUnlock);
71
69
 
72
70
  case 2:
73
71
  shouldUnlock = _context.sent;
@@ -113,10 +111,10 @@ var VaultUnlocker = function VaultUnlocker(_ref) {
113
111
  VaultUnlocker.propTypes = {
114
112
  onDismiss: PropTypes.func.isRequired,
115
113
  closable: PropTypes.bool,
116
- onUnlock: PropTypes.func.isRequired
114
+ onUnlock: PropTypes.func.isRequired,
115
+ addCheckShouldUnlock: PropTypes.func
117
116
  };
118
117
  VaultUnlocker.defaultProps = {
119
- UnlockForm: UnlockForm,
120
- checkShouldUnlock: checkShouldUnlock
118
+ UnlockForm: UnlockForm
121
119
  };
122
120
  export default withLocales(locales)(VaultUnlocker);
@@ -1,37 +1,49 @@
1
1
  import _regeneratorRuntime from "@babel/runtime/regenerator";
2
2
  import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
3
- import { checkHasInstalledExtension } from '../CozyUtils';
3
+
4
+ /**
5
+ * Check whether or not the vault should be unlocked
6
+ *
7
+ * @param {object} vaultClient - The vault client
8
+ * @param {function} addCheckShouldUnlock - An additional check method to unlock
9
+ * @returns {boolean} True if the vault should be unlocked
10
+ */
4
11
  export var checkShouldUnlock =
5
12
  /*#__PURE__*/
6
13
  function () {
7
14
  var _ref = _asyncToGenerator(
8
15
  /*#__PURE__*/
9
- _regeneratorRuntime.mark(function _callee(vaultClient, client) {
16
+ _regeneratorRuntime.mark(function _callee(vaultClient) {
17
+ var addCheckShouldUnlock,
18
+ _args = arguments;
10
19
  return _regeneratorRuntime.wrap(function _callee$(_context) {
11
20
  while (1) {
12
21
  switch (_context.prev = _context.next) {
13
22
  case 0:
14
- _context.next = 2;
15
- return checkHasInstalledExtension(client);
23
+ addCheckShouldUnlock = _args.length > 1 && _args[1] !== undefined ? _args[1] : function () {
24
+ return true;
25
+ };
26
+ _context.next = 3;
27
+ return addCheckShouldUnlock();
16
28
 
17
- case 2:
29
+ case 3:
18
30
  _context.t0 = _context.sent;
19
31
 
20
32
  if (!_context.t0) {
21
- _context.next = 7;
33
+ _context.next = 8;
22
34
  break;
23
35
  }
24
36
 
25
- _context.next = 6;
37
+ _context.next = 7;
26
38
  return vaultClient.isLocked();
27
39
 
28
- case 6:
40
+ case 7:
29
41
  _context.t0 = _context.sent;
30
42
 
31
- case 7:
43
+ case 8:
32
44
  return _context.abrupt("return", _context.t0);
33
45
 
34
- case 8:
46
+ case 9:
35
47
  case "end":
36
48
  return _context.stop();
37
49
  }
@@ -39,7 +51,7 @@ function () {
39
51
  }, _callee);
40
52
  }));
41
53
 
42
- return function checkShouldUnlock(_x, _x2) {
54
+ return function checkShouldUnlock(_x) {
43
55
  return _ref.apply(this, arguments);
44
56
  };
45
57
  }();
@@ -0,0 +1,94 @@
1
+ import _regeneratorRuntime from "@babel/runtime/regenerator";
2
+ import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
3
+ import { checkShouldUnlock } from './defaults';
4
+ import WebVaultClient from '../WebVaultClient';
5
+ jest.mock('../CozyUtils', function () {
6
+ return {
7
+ checkHasInstalledExtension: jest.fn(),
8
+ getEmail: jest.fn().mockReturnValue('me@test.fr'),
9
+ isInstance: jest.fn().mockReturnValue(true)
10
+ };
11
+ });
12
+ describe('checkShouldUnlock', function () {
13
+ var vaultClient = new WebVaultClient('https://me.cozy.wtf');
14
+ it('should return true when the vault is locked',
15
+ /*#__PURE__*/
16
+ _asyncToGenerator(
17
+ /*#__PURE__*/
18
+ _regeneratorRuntime.mark(function _callee() {
19
+ var shouldUnlock;
20
+ return _regeneratorRuntime.wrap(function _callee$(_context) {
21
+ while (1) {
22
+ switch (_context.prev = _context.next) {
23
+ case 0:
24
+ jest.spyOn(vaultClient, 'isLocked').mockResolvedValue(true);
25
+ _context.next = 3;
26
+ return checkShouldUnlock(vaultClient);
27
+
28
+ case 3:
29
+ shouldUnlock = _context.sent;
30
+ expect(shouldUnlock).toBe(true);
31
+
32
+ case 5:
33
+ case "end":
34
+ return _context.stop();
35
+ }
36
+ }
37
+ }, _callee);
38
+ })));
39
+ it('should return false when the vault is already unlocked',
40
+ /*#__PURE__*/
41
+ _asyncToGenerator(
42
+ /*#__PURE__*/
43
+ _regeneratorRuntime.mark(function _callee2() {
44
+ var shouldUnlock;
45
+ return _regeneratorRuntime.wrap(function _callee2$(_context2) {
46
+ while (1) {
47
+ switch (_context2.prev = _context2.next) {
48
+ case 0:
49
+ jest.spyOn(vaultClient, 'isLocked').mockResolvedValue(false);
50
+ _context2.next = 3;
51
+ return checkShouldUnlock(vaultClient);
52
+
53
+ case 3:
54
+ shouldUnlock = _context2.sent;
55
+ expect(shouldUnlock).toBe(false);
56
+
57
+ case 5:
58
+ case "end":
59
+ return _context2.stop();
60
+ }
61
+ }
62
+ }, _callee2);
63
+ })));
64
+ it('should return false when additional check function return false',
65
+ /*#__PURE__*/
66
+ _asyncToGenerator(
67
+ /*#__PURE__*/
68
+ _regeneratorRuntime.mark(function _callee3() {
69
+ var additionalUnlockCheck, shouldUnlock;
70
+ return _regeneratorRuntime.wrap(function _callee3$(_context3) {
71
+ while (1) {
72
+ switch (_context3.prev = _context3.next) {
73
+ case 0:
74
+ jest.spyOn(vaultClient, 'isLocked').mockResolvedValue(true);
75
+
76
+ additionalUnlockCheck = function additionalUnlockCheck() {
77
+ return false;
78
+ };
79
+
80
+ _context3.next = 4;
81
+ return checkShouldUnlock(vaultClient, additionalUnlockCheck);
82
+
83
+ case 4:
84
+ shouldUnlock = _context3.sent;
85
+ expect(shouldUnlock).toBe(false);
86
+
87
+ case 6:
88
+ case "end":
89
+ return _context3.stop();
90
+ }
91
+ }
92
+ }, _callee3);
93
+ })));
94
+ });
@@ -4,7 +4,6 @@ import _objectSpread from "@babel/runtime/helpers/objectSpread";
4
4
  import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
5
5
  import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
6
6
  import React, { useMemo, useState, useContext } from 'react';
7
- import { useClient } from 'cozy-client';
8
7
  import { useVaultClient } from './VaultContext';
9
8
  import { checkShouldUnlock } from './defaults';
10
9
  var vaultUnlockContext = React.createContext();
@@ -46,8 +45,7 @@ export var useVaultUnlockContext = function useVaultUnlockContext() {
46
45
 
47
46
  export var VaultUnlockProvider = function VaultUnlockProvider(_ref) {
48
47
  var children = _ref.children,
49
- checkShouldUnlock = _ref.checkShouldUnlock;
50
- var client = useClient();
48
+ addCheckShouldUnlock = _ref.addCheckShouldUnlock;
51
49
  var vaultClient = useVaultClient();
52
50
 
53
51
  var _useState = useState(false),
@@ -91,7 +89,7 @@ export var VaultUnlockProvider = function VaultUnlockProvider(_ref) {
91
89
  };
92
90
 
93
91
  _context.next = 6;
94
- return checkShouldUnlock(vaultClient, client);
92
+ return checkShouldUnlock(vaultClient, addCheckShouldUnlock);
95
93
 
96
94
  case 6:
97
95
  shouldUnlock = _context.sent;
@@ -125,14 +123,11 @@ export var VaultUnlockProvider = function VaultUnlockProvider(_ref) {
125
123
  unlockFormProps: unlockFormProps,
126
124
  vaultClient: vaultClient
127
125
  };
128
- }, [showingUnlockForm, unlockFormProps, vaultClient, checkShouldUnlock, client]);
126
+ }, [showingUnlockForm, unlockFormProps, vaultClient, addCheckShouldUnlock]);
129
127
  return React.createElement(vaultUnlockContext.Provider, {
130
128
  value: value
131
129
  }, children);
132
130
  };
133
- VaultUnlockProvider.defaultProps = {
134
- checkShouldUnlock: checkShouldUnlock
135
- };
136
131
  export var withVaultUnlockContext = function withVaultUnlockContext(Component) {
137
132
  var Wrapper = function Wrapper(props) {
138
133
  var vaultUnlockContextValue = useVaultUnlockContext();
@@ -3,18 +3,12 @@ import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
3
3
  import React from 'react';
4
4
  import { render, act, fireEvent } from '@testing-library/react';
5
5
  import { VaultUnlockProvider, useVaultUnlockContext } from './vaultUnlockContext';
6
- import { checkHasInstalledExtension } from '../CozyUtils';
7
6
  import { useVaultClient } from './VaultContext';
8
7
  jest.mock('./VaultContext', function () {
9
8
  return {
10
9
  useVaultClient: jest.fn()
11
10
  };
12
11
  });
13
- jest.mock('../CozyUtils', function () {
14
- return {
15
- checkHasInstalledExtension: jest.fn()
16
- };
17
- });
18
12
  describe('vault unlock context', function () {
19
13
  var Component = function Component(_ref) {
20
14
  var onUnlock = _ref.onUnlock;
@@ -105,25 +99,24 @@ describe('vault unlock context', function () {
105
99
  useVaultClient.mockReturnValue({
106
100
  isLocked: jest.fn().mockResolvedValue(true)
107
101
  });
108
- checkHasInstalledExtension.mockResolvedValue(true);
109
- _context3.next = 4;
102
+ _context3.next = 3;
110
103
  return setup();
111
104
 
112
- case 4:
105
+ case 3:
113
106
  _ref5 = _context3.sent;
114
107
  root = _ref5.root;
115
108
  onUnlock = _ref5.onUnlock;
116
109
  root.getByText('showingUnlockForm: true');
117
110
  expect(onUnlock).not.toHaveBeenCalled();
118
111
 
119
- case 9:
112
+ case 8:
120
113
  case "end":
121
114
  return _context3.stop();
122
115
  }
123
116
  }
124
117
  }, _callee3);
125
118
  })));
126
- it('should not show unlock form and call onUnlock if extension is not installed',
119
+ it('should not show unlock form and call onUnlock if vault is unlocked',
127
120
  /*#__PURE__*/
128
121
  _asyncToGenerator(
129
122
  /*#__PURE__*/
@@ -135,56 +128,23 @@ describe('vault unlock context', function () {
135
128
  switch (_context4.prev = _context4.next) {
136
129
  case 0:
137
130
  useVaultClient.mockReturnValue({
138
- isLocked: jest.fn().mockResolvedValue(true)
131
+ isLocked: jest.fn().mockResolvedValue(false)
139
132
  });
140
- checkHasInstalledExtension.mockResolvedValue(false);
141
- _context4.next = 4;
133
+ _context4.next = 3;
142
134
  return setup();
143
135
 
144
- case 4:
136
+ case 3:
145
137
  _ref7 = _context4.sent;
146
138
  root = _ref7.root;
147
139
  onUnlock = _ref7.onUnlock;
148
140
  expect(root.getByText('showingUnlockForm: false')).toBeTruthy();
149
141
  expect(onUnlock).toHaveBeenCalled();
150
142
 
151
- case 9:
143
+ case 8:
152
144
  case "end":
153
145
  return _context4.stop();
154
146
  }
155
147
  }
156
148
  }, _callee4);
157
149
  })));
158
- it('should not show unlock form and call onUnlock if vault is unlocked',
159
- /*#__PURE__*/
160
- _asyncToGenerator(
161
- /*#__PURE__*/
162
- _regeneratorRuntime.mark(function _callee5() {
163
- var _ref9, root, onUnlock;
164
-
165
- return _regeneratorRuntime.wrap(function _callee5$(_context5) {
166
- while (1) {
167
- switch (_context5.prev = _context5.next) {
168
- case 0:
169
- useVaultClient.mockReturnValue({
170
- isLocked: jest.fn().mockResolvedValue(false)
171
- });
172
- checkHasInstalledExtension.mockResolvedValue(true);
173
- _context5.next = 4;
174
- return setup();
175
-
176
- case 4:
177
- _ref9 = _context5.sent;
178
- root = _ref9.root;
179
- onUnlock = _ref9.onUnlock;
180
- expect(root.getByText('showingUnlockForm: false')).toBeTruthy();
181
- expect(onUnlock).toHaveBeenCalled();
182
-
183
- case 9:
184
- case "end":
185
- return _context5.stop();
186
- }
187
- }
188
- }, _callee5);
189
- })));
190
150
  });
@@ -9,5 +9,4 @@ import _VaultUnlocker from './components/VaultUnlocker';
9
9
  export { _VaultUnlocker as VaultUnlocker };
10
10
  import _VaultUnlockPlaceholder from './components/VaultUnlockPlaceholder';
11
11
  export { _VaultUnlockPlaceholder as VaultUnlockPlaceholder };
12
- export { useVaultUnlockContext, VaultUnlockProvider, withVaultUnlockContext } from './components/vaultUnlockContext';
13
- export { UnlockForm } from './components/UnlockForm';
12
+ export { useVaultUnlockContext, VaultUnlockProvider, withVaultUnlockContext } from './components/vaultUnlockContext';