Skip to content

Commit

Permalink
Add more tests (#21)
Browse files Browse the repository at this point in the history
* Update getDerivedKey.unit.test.ts

* Update ChromeMacosCookieProvider.unit.test.ts
  • Loading branch information
Kalininator committed Nov 7, 2021
1 parent c2b57e1 commit 5969569
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 1 deletion.
52 changes: 52 additions & 0 deletions src/chrome/ChromeMacosCookieProvider.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,56 @@ describe('chrome macos cookie provider', () => {

expect(mockFindCookie).toHaveBeenCalledWith('some_cookie', '.test.com');
});

it('should return undefined if cookie not found', async () => {
const mockFindCookie = jest.fn().mockReturnValue(undefined);
const mockDb: ChromeCookieRepository = {
findCookie: mockFindCookie,
listCookies(): ChromeCookie[] {
throw new Error('Function not implemented.');
},
};

const provider = new ChromeMacosCookieProvider(mockDb);

const cookie = await provider.getCookie('.test.com', 'some_cookie');

expect(cookie).toEqual(undefined);

expect(mockFindCookie).toHaveBeenCalledWith('some_cookie', '.test.com');
});

it('should list and decrypt cookies', async () => {
const mockListCookies = jest.fn().mockImplementation((): ChromeCookie[] => {
return [
{
host_key: '.some.url',
path: '/',
name: 'someCookie',
encrypted_value: fakeBuffer,
},
];
});
const mockDb: ChromeCookieRepository = {
findCookie() {
throw new Error('function not implemented');
},
listCookies: mockListCookies,
};

const provider = new ChromeMacosCookieProvider(mockDb);

const cookies = await provider.listCookies();

expect(cookies).toEqual([
{
value: 'foo',
host: '.some.url',
path: '/',
name: 'someCookie',
},
]);

expect(mockListCookies).toHaveBeenCalledWith();
});
});
14 changes: 13 additions & 1 deletion src/chrome/getDerivedKey.unit.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { mockPlatform, restorePlatform } from '../../test/util';
import { getDerivedKey } from './getDerivedKey';
import { getDerivedKey, getMacDerivedKey } from './getDerivedKey';
import { getKeytar } from './optionalDependencies';

/* MacOS pbkdf2
Expand Down Expand Up @@ -32,6 +32,18 @@ describe('get derived key', () => {
restorePlatform();
});

it('shoud throw if failed to get chrome password', async () => {
(getKeytar as jest.Mock).mockReturnValue({
getPassword: jest.fn().mockResolvedValue(undefined),
});

expect(
getMacDerivedKey(15, 1001),
).rejects.toThrowErrorMatchingInlineSnapshot(
`"Could not fetch chrome password"`,
);
});

it('should return mac derived key', async () => {
const getPasswordFunction = jest.fn().mockReturnValue('foo');
(getKeytar as jest.Mock).mockReturnValue({
Expand Down

0 comments on commit 5969569

Please sign in to comment.