-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Check if extension has been installed before ask for unlock
We figured than only checking if some ciphers exist is not a strong enough condition to know if we should ask the user to unlock the vault or not. To resolve our problems, the stack now adds a `extension_installed` attribute on the `io.cozy.settings.bitwarden` document when an extension has been connected for the first time to the instance. See cozy/cozy-stack#2315. So we now check this attribute. The flow is now: * Check if there is some cipher, is yes => the user should unlock * If there is no cipher, check if the extension has been installed, if yes => the user should unlock * Otherwise, skip the unlock step
- Loading branch information
Showing
7 changed files
with
189 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
module.exports = { | ||
moduleFileExtensions: ['js', 'jsx', 'json'], | ||
setupFilesAfterEnv: ['<rootDir>enzyme.setup.js'], | ||
setupFilesAfterEnv: ['<rootDir>enzyme.setup.js', '<rootDir>jest.setup.js'], | ||
testPathIgnorePatterns: ['@bitwarden/jslib', 'transpiled/'] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* eslint-disable no-console */ | ||
|
||
const ignoreOnConditions = (originalWarn, ignoreConditions) => { | ||
return function(...args) { | ||
const msg = args[0] | ||
if (ignoreConditions.some(condition => condition(msg))) { | ||
return | ||
} | ||
originalWarn.apply(this, args) | ||
} | ||
} | ||
|
||
const callAndThrow = (fn, errorMessage) => { | ||
return function() { | ||
fn.apply(this, arguments) | ||
throw new Error(errorMessage) | ||
} | ||
} | ||
|
||
const ignoredErrors = { | ||
'mocked error': { | ||
reason: 'Mocked error', | ||
matcher: message => message.includes('mock error') | ||
} | ||
} | ||
|
||
console.error = ignoreOnConditions( | ||
callAndThrow( | ||
console.error, | ||
'console.error should not be called during tests' | ||
), | ||
Object.values(ignoredErrors).map(x => x.matcher) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
import { checkHasCiphers, checkHasInstalledExtension } from './utils' | ||
import { createMockClient } from 'cozy-client/dist/mock' | ||
|
||
describe('checkHasCiphers', () => { | ||
describe('when there are ciphers', () => { | ||
let client | ||
|
||
beforeEach(() => { | ||
client = createMockClient({ | ||
remote: { | ||
'com.bitwarden.ciphers': [{ _id: 'cipher1' }, { _id: 'cipher2' }] | ||
} | ||
}) | ||
}) | ||
|
||
it('should return true', async () => { | ||
const hasCiphers = await checkHasCiphers(client) | ||
|
||
expect(hasCiphers).toBe(true) | ||
}) | ||
}) | ||
|
||
describe('when there is no cipher', () => { | ||
let client | ||
|
||
beforeEach(() => { | ||
client = createMockClient({ | ||
remote: { | ||
'com.bitwarden.ciphers': [] | ||
} | ||
}) | ||
}) | ||
|
||
it('should return false', async () => { | ||
const hasCiphers = await checkHasCiphers(client) | ||
|
||
expect(hasCiphers).toBe(false) | ||
}) | ||
}) | ||
|
||
describe('when there is an error while fetching ciphers', () => { | ||
let client | ||
|
||
beforeEach(() => { | ||
client = createMockClient({}) | ||
client.query = jest.fn().mockRejectedValue({ message: 'mock error' }) | ||
}) | ||
|
||
it('should return false', async () => { | ||
const hasCiphers = await checkHasCiphers(client) | ||
|
||
expect(hasCiphers).toBe(false) | ||
}) | ||
}) | ||
}) | ||
|
||
describe('checkHasInstalledExtension', () => { | ||
describe('when the extension has been installed', () => { | ||
let client | ||
|
||
beforeEach(() => { | ||
client = createMockClient({}) | ||
client.stackClient.fetchJSON = jest.fn().mockResolvedValue({ | ||
rows: [{ _id: 'io.cozy.settings.bitwarden', extension_installed: true }] | ||
}) | ||
}) | ||
|
||
it('should return true', async () => { | ||
const hasInstalledExtension = await checkHasInstalledExtension(client) | ||
|
||
expect(hasInstalledExtension).toBe(true) | ||
}) | ||
}) | ||
|
||
describe('when the extension has not been installed', () => { | ||
let client | ||
|
||
beforeEach(() => { | ||
client = createMockClient({}) | ||
client.stackClient.fetchJSON = jest.fn().mockResolvedValue({ | ||
rows: [ | ||
{ _id: 'io.cozy.settings.bitwarden', extension_installed: false } | ||
] | ||
}) | ||
}) | ||
|
||
it('should return false', async () => { | ||
const hasInstalledExtension = await checkHasInstalledExtension(client) | ||
|
||
expect(hasInstalledExtension).toBe(false) | ||
}) | ||
}) | ||
|
||
describe('when there is an error while fetching settings', () => { | ||
let client | ||
|
||
beforeEach(() => { | ||
client = createMockClient({}) | ||
client.stackClient.fetchJSON = jest | ||
.fn() | ||
.mockRejectedValue({ message: 'mock error' }) | ||
}) | ||
|
||
it('should return false', async () => { | ||
const hasInstalledExtension = await checkHasInstalledExtension(client) | ||
|
||
expect(hasInstalledExtension).toBe(false) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters