Skip to content

Commit

Permalink
Rewrite save unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hlissner committed Jan 7, 2019
1 parent c2add09 commit bc15a87
Showing 1 changed file with 35 additions and 40 deletions.
75 changes: 35 additions & 40 deletions tests/__tests__/utilities/save.unit.test.js
Expand Up @@ -4,49 +4,44 @@ const Domain = require('server/utilities/domain');
const save = require('server/utilities/save');
const today = require('moment')().format('YYYY-MM-DD');

describe('saveDomain', () => {
const readFilemock = jest.spyOn(fs, 'readFile');
const writeFileMock = jest.spyOn(fs, 'appendFile');
const domain = Domain.from({ url: 'https://example.com/vulnerable.txt' });

beforeEach(() => {
writeFileMock.mockImplementation((url, data, err) =>
console.log(err || 'Domain Saved To Disk')
);
});

afterEach(() => {
writeFileMock.mockRestore();
readFilemock.mockRestore();
});
describe('utilities.save', () => {
describe('saveDomain', () => {
const readFileMock = jest.spyOn(fs, 'readFile');
const appendFileMock = jest.spyOn(fs, 'appendFile');
const domain = Domain.from({ url: 'https://example.com/vulnerable.txt' });

beforeEach(() => {
readFileMock.mockImplementation(
(_file, ...args) => args[args.length - 1](null, 'https://example.com/vulnerable.txt')
);
appendFileMock.mockImplementation((file, data, ...args) => {
args[args.length - 1](null, data.byteLength, data)
});
});
afterEach(() => {
readFileMock.mockRestore();
appendFileMock.mockRestore();
});

it('Should save domain to file as it does not currently exist inside urls.txt', () => {
readFilemock.mockImplementation((file, option, cb) =>
cb(null, 'https://example.com:1000/test.html\r\nhttp://vulnerable.com/xss')
);
save.saveDomain(domain);
expect(writeFileMock).toHaveBeenCalled();
});
it('should save domain to file as it does not currently exist inside urls.txt', () => {
save.saveDomain(domain);
expect(appendFileMock).toHaveBeenCalled();
});

it('Should not save domain to file as it currently exist inside urls.txt', () => {
readFilemock.mockImplementation((file, option, cb) =>
cb(null, 'https://example.com/vulnerable.txt')
);
save.saveDomain(domain);
expect(writeFileMock).not.toHaveBeenCalled();
it('should not save domain to file as it currently exist inside urls.txt', () => {
save.saveDomain(domain);
expect(appendFileMock).not.toHaveBeenCalled();
});
});
});

describe('saveTodaysDate', () => {
it("should save today's date in a text file", () => {
const writeFileSyncMock = jest.spyOn(fs, 'writeFileSync');
writeFileSyncMock.mockImplementation((date, data, err) => {
console.log(err || 'Todays date was saved in date.txt');
describe('saveTodaysDate', () => {
it("should save today's date in a text file", () => {
const writeFileSyncMock = jest.spyOn(fs, 'writeFileSync');
writeFileSyncMock.mockImplementation(() => {});
save.saveTodaysDate();
expect(writeFileSyncMock.mock.calls[0][1]).toBe(today);
expect(writeFileSyncMock).toHaveBeenCalled();
writeFileSyncMock.mockRestore();
});

save.saveTodaysDate();
expect(writeFileSyncMock.mock.calls[0][1]).toBe(today);
expect(writeFileSyncMock).toHaveBeenCalled();
writeFileSyncMock.mockRestore();
});
});
})

0 comments on commit bc15a87

Please sign in to comment.