cozy-keys-lib 3.10.0 → 4.1.1

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.10.0",
3
+ "version": "4.1.1",
4
4
  "description": "Interactions between cozy-keys and a bitwarden server",
5
5
  "browser": "transpiled/index.js",
6
6
  "main": "transpiled/index.node.js",
@@ -14,8 +14,16 @@ class VaultProvider extends React.Component {
14
14
  constructor(props) {
15
15
  super(props)
16
16
 
17
+ let vaultClient = this.props.vaultClient
18
+ if (this.props.client) {
19
+ vaultClient = this.props.client
20
+ // eslint-disable-next-line no-console
21
+ console.warn(
22
+ 'Passing client in props is deprecated, please use vaultClient instead.'
23
+ )
24
+ }
17
25
  this.state = {
18
- client: null,
26
+ vaultClient: vaultClient,
19
27
  locked: true
20
28
  }
21
29
 
@@ -24,77 +32,88 @@ class VaultProvider extends React.Component {
24
32
  }
25
33
 
26
34
  componentDidMount() {
27
- if (this.props.instance) {
28
- 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)
29
46
  }
30
47
  }
31
48
 
32
49
  componentWillUnmount() {
33
- const { client } = this.state
34
- if (!client) {
50
+ const { vaultClient } = this.state
51
+ if (!vaultClient) {
35
52
  return
36
53
  }
37
- client.removeListener('unlock', this.updateLockedState)
38
- client.removeListener('lock', this.updateLockedState)
39
- client.removeListener('login', this.updateLockedState)
54
+ vaultClient.removeListener('unlock', this.updateLockedState)
55
+ vaultClient.removeListener('lock', this.updateLockedState)
56
+ vaultClient.removeListener('login', this.updateLockedState)
40
57
  }
41
58
 
42
59
  async updateLockedState() {
43
- const { client } = this.state
44
- const locked = await client.isLocked()
60
+ const { vaultClient } = this.state
61
+ const locked = await vaultClient.isLocked()
45
62
  if (locked != this.state.locked) {
46
63
  this.setState({ locked })
47
64
  }
48
65
  }
49
66
 
50
- setupClient() {
67
+ setupClient(instance) {
51
68
  const unsafeStorage = this.props.unsafeStorage
52
-
53
- const client = getVaultClient(
54
- this.props.instance,
55
- unsafeStorage,
56
- this.props.vaultData
57
- )
69
+ const vaultClient =
70
+ this.props.vaultClient ||
71
+ this.props.client ||
72
+ getVaultClient(instance, unsafeStorage, this.props.vaultData)
58
73
 
59
74
  this.setState(
60
75
  {
61
- client,
76
+ vaultClient,
62
77
  locked: true
63
78
  },
64
79
  () => {
65
- client.on('unlock', this.updateLockedState)
66
- client.on('lock', this.updateLockedState)
67
- client.on('login', this.updateLockedState)
80
+ vaultClient.on('unlock', this.updateLockedState)
81
+ vaultClient.on('lock', this.updateLockedState)
82
+ vaultClient.on('login', this.updateLockedState)
68
83
 
69
84
  this.updateLockedState()
70
85
  }
71
86
  )
72
87
 
73
88
  if (this.props.setClient) {
74
- this.props.setClient(client)
89
+ this.props.setClient(vaultClient)
75
90
  }
76
91
  }
77
92
 
78
93
  render() {
79
- const { client } = this.state
80
- return client ? (
94
+ return (
81
95
  <VaultContext.Provider value={this.state}>
82
96
  {this.props.children}
83
97
  </VaultContext.Provider>
84
- ) : null
98
+ )
85
99
  }
86
100
  }
87
101
 
88
102
  VaultProvider.propTypes = {
89
- instance: PropTypes.string.isRequired,
103
+ instance: PropTypes.string,
104
+ vaultClient: PropTypes.object,
105
+ client: PropTypes.object, // deprecated
90
106
  unsafeStorage: PropTypes.bool,
91
- setClient: PropTypes.func
107
+ setClient: PropTypes.func,
108
+ cozyClient: PropTypes.object
92
109
  }
93
110
 
94
111
  const withVaultClient = BaseComponent => {
95
112
  const Component = props => (
96
113
  <VaultContext.Consumer>
97
- {({ client }) => <BaseComponent vaultClient={client} {...props} />}
114
+ {({ vaultClient }) => (
115
+ <BaseComponent vaultClient={vaultClient} {...props} />
116
+ )}
98
117
  </VaultContext.Consumer>
99
118
  )
100
119
 
@@ -106,7 +125,7 @@ const withVaultClient = BaseComponent => {
106
125
 
107
126
  const useVaultClient = () => {
108
127
  const ctx = useContext(VaultContext)
109
- return ctx.client
128
+ return ctx.vaultClient
110
129
  }
111
130
 
112
131
  export { VaultContext, VaultProvider, withVaultClient, useVaultClient }
@@ -21,26 +21,32 @@ describe('VaultProvider', () => {
21
21
  </VaultProvider>
22
22
  )
23
23
 
24
- const client = component.state('client')
24
+ const vaultClient = component.state('vaultClient')
25
25
 
26
- expect(ChildComponent).toHaveBeenCalledWith({ client, locked: true })
26
+ expect(ChildComponent).toHaveBeenCalledWith({ vaultClient, locked: true })
27
27
 
28
- client.unlock()
28
+ vaultClient.unlock()
29
29
 
30
30
  await new Promise(resolve =>
31
31
  setTimeout(() => {
32
- expect(ChildComponent).toHaveBeenCalledTimes(2)
33
- expect(ChildComponent).toHaveBeenCalledWith({ client, locked: false })
32
+ expect(ChildComponent).toHaveBeenCalledTimes(3)
33
+ expect(ChildComponent).toHaveBeenCalledWith({
34
+ vaultClient,
35
+ locked: false
36
+ })
34
37
  resolve()
35
38
  })
36
39
  )
37
40
 
38
- client.lock()
41
+ vaultClient.lock()
39
42
 
40
43
  await new Promise(resolve =>
41
44
  setTimeout(() => {
42
- expect(ChildComponent).toHaveBeenCalledTimes(3)
43
- expect(ChildComponent).toHaveBeenCalledWith({ client, locked: true })
45
+ expect(ChildComponent).toHaveBeenCalledTimes(4)
46
+ expect(ChildComponent).toHaveBeenCalledWith({
47
+ vaultClient,
48
+ locked: true
49
+ })
44
50
  resolve()
45
51
  })
46
52
  )
@@ -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,10 +23,9 @@ const VaultUnlocker = ({
24
23
  closable,
25
24
  onUnlock,
26
25
  UnlockForm,
27
- checkShouldUnlock
26
+ addCheckShouldUnlock
28
27
  }) => {
29
- const cozyClient = useClient()
30
- const { locked, client: vaultClient } = useContext(VaultContext)
28
+ const { locked, vaultClient } = useContext(VaultContext)
31
29
 
32
30
  const [showSpinner, setShowSpinner] = useState(false)
33
31
  const [isChecking, setIsChecking] = useState(true)
@@ -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()
@@ -29,8 +29,16 @@ function (_React$Component) {
29
29
  _classCallCheck(this, VaultProvider);
30
30
 
31
31
  _this = _possibleConstructorReturn(this, _getPrototypeOf(VaultProvider).call(this, props));
32
+ var vaultClient = _this.props.vaultClient;
33
+
34
+ if (_this.props.client) {
35
+ vaultClient = _this.props.client; // eslint-disable-next-line no-console
36
+
37
+ console.warn('Passing client in props is deprecated, please use vaultClient instead.');
38
+ }
39
+
32
40
  _this.state = {
33
- client: null,
41
+ vaultClient: vaultClient,
34
42
  locked: true
35
43
  };
36
44
  _this.updateLockedState = _this.updateLockedState.bind(_assertThisInitialized(_this));
@@ -41,22 +49,38 @@ function (_React$Component) {
41
49
  _createClass(VaultProvider, [{
42
50
  key: "componentDidMount",
43
51
  value: function componentDidMount() {
44
- if (this.props.instance) {
45
- 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);
46
70
  }
47
71
  }
48
72
  }, {
49
73
  key: "componentWillUnmount",
50
74
  value: function componentWillUnmount() {
51
- var client = this.state.client;
75
+ var vaultClient = this.state.vaultClient;
52
76
 
53
- if (!client) {
77
+ if (!vaultClient) {
54
78
  return;
55
79
  }
56
80
 
57
- client.removeListener('unlock', this.updateLockedState);
58
- client.removeListener('lock', this.updateLockedState);
59
- client.removeListener('login', this.updateLockedState);
81
+ vaultClient.removeListener('unlock', this.updateLockedState);
82
+ vaultClient.removeListener('lock', this.updateLockedState);
83
+ vaultClient.removeListener('login', this.updateLockedState);
60
84
  }
61
85
  }, {
62
86
  key: "updateLockedState",
@@ -64,14 +88,14 @@ function (_React$Component) {
64
88
  var _updateLockedState = _asyncToGenerator(
65
89
  /*#__PURE__*/
66
90
  _regeneratorRuntime.mark(function _callee() {
67
- var client, locked;
91
+ var vaultClient, locked;
68
92
  return _regeneratorRuntime.wrap(function _callee$(_context) {
69
93
  while (1) {
70
94
  switch (_context.prev = _context.next) {
71
95
  case 0:
72
- client = this.state.client;
96
+ vaultClient = this.state.vaultClient;
73
97
  _context.next = 3;
74
- return client.isLocked();
98
+ return vaultClient.isLocked();
75
99
 
76
100
  case 3:
77
101
  locked = _context.sent;
@@ -98,33 +122,32 @@ function (_React$Component) {
98
122
  }()
99
123
  }, {
100
124
  key: "setupClient",
101
- value: function setupClient() {
102
- var _this2 = this;
125
+ value: function setupClient(instance) {
126
+ var _this3 = this;
103
127
 
104
128
  var unsafeStorage = this.props.unsafeStorage;
105
- var client = getVaultClient(this.props.instance, unsafeStorage, this.props.vaultData);
129
+ var vaultClient = this.props.vaultClient || this.props.client || getVaultClient(instance, unsafeStorage, this.props.vaultData);
106
130
  this.setState({
107
- client: client,
131
+ vaultClient: vaultClient,
108
132
  locked: true
109
133
  }, function () {
110
- client.on('unlock', _this2.updateLockedState);
111
- client.on('lock', _this2.updateLockedState);
112
- client.on('login', _this2.updateLockedState);
134
+ vaultClient.on('unlock', _this3.updateLockedState);
135
+ vaultClient.on('lock', _this3.updateLockedState);
136
+ vaultClient.on('login', _this3.updateLockedState);
113
137
 
114
- _this2.updateLockedState();
138
+ _this3.updateLockedState();
115
139
  });
116
140
 
117
141
  if (this.props.setClient) {
118
- this.props.setClient(client);
142
+ this.props.setClient(vaultClient);
119
143
  }
120
144
  }
121
145
  }, {
122
146
  key: "render",
123
147
  value: function render() {
124
- var client = this.state.client;
125
- return client ? React.createElement(VaultContext.Provider, {
148
+ return React.createElement(VaultContext.Provider, {
126
149
  value: this.state
127
- }, this.props.children) : null;
150
+ }, this.props.children);
128
151
  }
129
152
  }]);
130
153
 
@@ -132,17 +155,21 @@ function (_React$Component) {
132
155
  }(React.Component);
133
156
 
134
157
  VaultProvider.propTypes = {
135
- instance: PropTypes.string.isRequired,
158
+ instance: PropTypes.string,
159
+ vaultClient: PropTypes.object,
160
+ client: PropTypes.object,
161
+ // deprecated
136
162
  unsafeStorage: PropTypes.bool,
137
- setClient: PropTypes.func
163
+ setClient: PropTypes.func,
164
+ cozyClient: PropTypes.object
138
165
  };
139
166
 
140
167
  var withVaultClient = function withVaultClient(BaseComponent) {
141
168
  var Component = function Component(props) {
142
169
  return React.createElement(VaultContext.Consumer, null, function (_ref) {
143
- var client = _ref.client;
170
+ var vaultClient = _ref.vaultClient;
144
171
  return React.createElement(BaseComponent, _extends({
145
- vaultClient: client
172
+ vaultClient: vaultClient
146
173
  }, props));
147
174
  });
148
175
  };
@@ -153,7 +180,7 @@ var withVaultClient = function withVaultClient(BaseComponent) {
153
180
 
154
181
  var useVaultClient = function useVaultClient() {
155
182
  var ctx = useContext(VaultContext);
156
- return ctx.client;
183
+ return ctx.vaultClient;
157
184
  };
158
185
 
159
186
  export { VaultContext, VaultProvider, withVaultClient, useVaultClient };
@@ -12,7 +12,7 @@ describe('VaultProvider', function () {
12
12
  _asyncToGenerator(
13
13
  /*#__PURE__*/
14
14
  _regeneratorRuntime.mark(function _callee() {
15
- var ChildComponent, component, client;
15
+ var ChildComponent, component, vaultClient;
16
16
  return _regeneratorRuntime.wrap(function _callee$(_context) {
17
17
  while (1) {
18
18
  switch (_context.prev = _context.next) {
@@ -21,18 +21,18 @@ describe('VaultProvider', function () {
21
21
  component = mount(React.createElement(VaultProvider, {
22
22
  instance: 'test@example.com'
23
23
  }, React.createElement(VaultContext.Consumer, null, ChildComponent)));
24
- client = component.state('client');
24
+ vaultClient = component.state('vaultClient');
25
25
  expect(ChildComponent).toHaveBeenCalledWith({
26
- client: client,
26
+ vaultClient: vaultClient,
27
27
  locked: true
28
28
  });
29
- client.unlock();
29
+ vaultClient.unlock();
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
- client: client,
35
+ vaultClient: vaultClient,
36
36
  locked: false
37
37
  });
38
38
  resolve();
@@ -40,13 +40,13 @@ describe('VaultProvider', function () {
40
40
  });
41
41
 
42
42
  case 7:
43
- client.lock();
43
+ vaultClient.lock();
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
- client: client,
49
+ vaultClient: vaultClient,
50
50
  locked: true
51
51
  });
52
52
  resolve();
@@ -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,12 +22,11 @@ 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,
31
- vaultClient = _useContext.client;
29
+ vaultClient = _useContext.vaultClient;
32
30
 
33
31
  var _useState = useState(false),
34
32
  _useState2 = _slicedToArray(_useState, 2),
@@ -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
  });