mirror of
https://github.com/naturalcrit/homebrewery.git
synced 2026-01-05 10:12:41 +00:00
add more tests and merge from main
This commit is contained in:
@@ -128,7 +128,7 @@ If you believe you should have access to this brew, ask the file owner to invite
|
|||||||
return modified;
|
return modified;
|
||||||
},
|
},
|
||||||
excludeStubProps : (brew)=>{
|
excludeStubProps : (brew)=>{
|
||||||
const propsToExclude = ['text', 'textBin', 'renderer', 'pageCount', 'version'];
|
const propsToExclude = ['text', 'textBin', 'renderer', 'pageCount'];
|
||||||
for (const prop of propsToExclude) {
|
for (const prop of propsToExclude) {
|
||||||
brew[prop] = undefined;
|
brew[prop] = undefined;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,18 +1,44 @@
|
|||||||
/* eslint-disable max-lines */
|
/* eslint-disable max-lines */
|
||||||
|
|
||||||
|
const modelBrew = (brew, saveFunc = async function() {
|
||||||
|
return { ...this, _id: '1' };
|
||||||
|
})=>({
|
||||||
|
...brew,
|
||||||
|
save : saveFunc,
|
||||||
|
toObject : function() {
|
||||||
|
delete this.save;
|
||||||
|
delete this.toObject;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
const modelBrewThrow = (brew)=>modelBrew(brew, async function() {
|
||||||
|
throw 'err';
|
||||||
|
});
|
||||||
|
|
||||||
describe('Tests for api', ()=>{
|
describe('Tests for api', ()=>{
|
||||||
let api;
|
let api;
|
||||||
let google;
|
let google;
|
||||||
let model;
|
let model;
|
||||||
let hbBrew;
|
let hbBrew;
|
||||||
let googleBrew;
|
let googleBrew;
|
||||||
|
let res;
|
||||||
|
|
||||||
beforeEach(()=>{
|
beforeEach(()=>{
|
||||||
google = require('./googleActions.js');
|
google = require('./googleActions.js');
|
||||||
model = require('./homebrew.model.js').model;
|
model = require('./homebrew.model.js').model;
|
||||||
|
|
||||||
jest.mock('./googleActions.js');
|
jest.mock('./googleActions.js');
|
||||||
|
google.authCheck = jest.fn(()=>'client');
|
||||||
|
google.newGoogleBrew = jest.fn(()=>'id');
|
||||||
|
google.deleteGoogleBrew = jest.fn(()=>true);
|
||||||
|
|
||||||
jest.mock('./homebrew.model.js');
|
jest.mock('./homebrew.model.js');
|
||||||
|
model.mockImplementation((brew)=>modelBrew(brew));
|
||||||
|
|
||||||
|
res = {
|
||||||
|
status : jest.fn(()=>res),
|
||||||
|
send : jest.fn(()=>{})
|
||||||
|
};
|
||||||
|
|
||||||
api = require('./homebrew.api');
|
api = require('./homebrew.api');
|
||||||
|
|
||||||
@@ -115,7 +141,7 @@ describe('Tests for api', ()=>{
|
|||||||
api.getId = jest.fn(()=>({ id: '1', googleId: undefined }));
|
api.getId = jest.fn(()=>({ id: '1', googleId: undefined }));
|
||||||
model.get = jest.fn(()=>new Promise((_, rej)=>rej('Unable to find brew')));
|
model.get = jest.fn(()=>new Promise((_, rej)=>rej('Unable to find brew')));
|
||||||
|
|
||||||
const fn = api.getBrew('share', true);
|
const fn = api.getBrew('share', false);
|
||||||
const req = { brew: {} };
|
const req = { brew: {} };
|
||||||
const next = jest.fn();
|
const next = jest.fn();
|
||||||
let err;
|
let err;
|
||||||
@@ -160,7 +186,9 @@ describe('Tests for api', ()=>{
|
|||||||
err = e;
|
err = e;
|
||||||
}
|
}
|
||||||
|
|
||||||
expect(err).toEqual('Current logged in user does not have access to this brew.');
|
expect(err).toEqual(`The current logged in user does not have editor access to this brew.
|
||||||
|
|
||||||
|
If you believe you should have access to this brew, ask the file owner to invite you as an author by opening the brew, viewing the Properties tab, and adding your username to the "invited authors" list. You can then try to access this document again.`);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('does not throw if no authors', async ()=>{
|
it('does not throw if no authors', async ()=>{
|
||||||
@@ -194,16 +222,28 @@ describe('Tests for api', ()=>{
|
|||||||
model.get = jest.fn(()=>toBrewPromise(stubBrew));
|
model.get = jest.fn(()=>toBrewPromise(stubBrew));
|
||||||
google.getGoogleBrew = jest.fn(()=>new Promise((res)=>res(googleBrew)));
|
google.getGoogleBrew = jest.fn(()=>new Promise((res)=>res(googleBrew)));
|
||||||
|
|
||||||
const fn = api.getBrew('share', true);
|
const fn = api.getBrew('share', false);
|
||||||
const req = { brew: {} };
|
const req = { brew: {} };
|
||||||
const next = jest.fn();
|
const next = jest.fn();
|
||||||
await fn(req, null, next);
|
await fn(req, null, next);
|
||||||
|
|
||||||
expect(req.brew).toEqual({
|
expect(req.brew).toEqual({
|
||||||
title : 'test google brew',
|
title : 'test google brew',
|
||||||
authors : ['a'],
|
authors : ['a'],
|
||||||
text : 'brew text',
|
text : 'brew text',
|
||||||
stubbed : true
|
stubbed : true,
|
||||||
|
description : '',
|
||||||
|
editId : null,
|
||||||
|
pageCount : 1,
|
||||||
|
published : true,
|
||||||
|
renderer : 'legacy',
|
||||||
|
shareId : null,
|
||||||
|
systems : [],
|
||||||
|
tags : [],
|
||||||
|
theme : '5ePHB',
|
||||||
|
thumbnail : '',
|
||||||
|
textBin : undefined,
|
||||||
|
version : undefined
|
||||||
});
|
});
|
||||||
expect(next).toHaveBeenCalled();
|
expect(next).toHaveBeenCalled();
|
||||||
expect(api.getId).toHaveBeenCalledWith(req);
|
expect(api.getId).toHaveBeenCalledWith(req);
|
||||||
@@ -301,6 +341,7 @@ brew`);
|
|||||||
expect(result.owner).toBeUndefined();
|
expect(result.owner).toBeUndefined();
|
||||||
expect(result.views).toBeUndefined();
|
expect(result.views).toBeUndefined();
|
||||||
expect(result.thumbnail).toBeUndefined();
|
expect(result.thumbnail).toBeUndefined();
|
||||||
|
expect(result.version).toBeUndefined();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('excludeStubProps removes the correct keys from the original object', ()=>{
|
it('excludeStubProps removes the correct keys from the original object', ()=>{
|
||||||
@@ -312,7 +353,6 @@ brew`);
|
|||||||
expect(result.textBin).toBeUndefined();
|
expect(result.textBin).toBeUndefined();
|
||||||
expect(result.renderer).toBeUndefined();
|
expect(result.renderer).toBeUndefined();
|
||||||
expect(result.pageCount).toBeUndefined();
|
expect(result.pageCount).toBeUndefined();
|
||||||
expect(result.version).toBeUndefined();
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -360,13 +400,10 @@ brew`);
|
|||||||
|
|
||||||
describe('newGoogleBrew', ()=>{
|
describe('newGoogleBrew', ()=>{
|
||||||
it('should call the correct methods', ()=>{
|
it('should call the correct methods', ()=>{
|
||||||
google.authCheck = jest.fn().mockImplementation(()=>'client');
|
|
||||||
api.excludeGoogleProps = jest.fn(()=>'newBrew');
|
api.excludeGoogleProps = jest.fn(()=>'newBrew');
|
||||||
google.newGoogleBrew = jest.fn(()=>'id');
|
|
||||||
|
|
||||||
const acct = { username: 'test' };
|
const acct = { username: 'test' };
|
||||||
const brew = { title: 'test title' };
|
const brew = { title: 'test title' };
|
||||||
const res = { send: jest.fn(()=>{}) };
|
|
||||||
api.newGoogleBrew(acct, brew, res);
|
api.newGoogleBrew(acct, brew, res);
|
||||||
|
|
||||||
expect(google.authCheck).toHaveBeenCalledWith(acct, res);
|
expect(google.authCheck).toHaveBeenCalledWith(acct, res);
|
||||||
@@ -374,4 +411,99 @@ brew`);
|
|||||||
expect(google.newGoogleBrew).toHaveBeenCalledWith('client', 'newBrew');
|
expect(google.newGoogleBrew).toHaveBeenCalledWith('client', 'newBrew');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('newBrew', ()=>{
|
||||||
|
it('should set up a default brew via Homebrew model', async ()=>{
|
||||||
|
await api.newBrew({ body: { text: 'asdf' }, query: {}, account: { username: 'test user' } }, res);
|
||||||
|
|
||||||
|
expect(res.status).toHaveBeenCalledWith(200);
|
||||||
|
expect(res.send).toHaveBeenCalledWith({
|
||||||
|
_id : '1',
|
||||||
|
authors : ['test user'],
|
||||||
|
description : '',
|
||||||
|
editId : expect.any(String),
|
||||||
|
pageCount : 1,
|
||||||
|
published : false,
|
||||||
|
renderer : 'V3',
|
||||||
|
shareId : expect.any(String),
|
||||||
|
systems : [],
|
||||||
|
tags : [],
|
||||||
|
text : undefined,
|
||||||
|
textBin : expect.objectContaining({}),
|
||||||
|
theme : '5ePHB',
|
||||||
|
thumbnail : '',
|
||||||
|
title : 'asdf',
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should remove edit/share/google ids', async ()=>{
|
||||||
|
await api.newBrew({ body: { editId: '1234', shareId: '1234', googleId: '1234', text: 'asdf', title: '' }, query: {} }, res);
|
||||||
|
|
||||||
|
expect(res.status).toHaveBeenCalledWith(200);
|
||||||
|
expect(res.send).toHaveBeenCalled();
|
||||||
|
const sent = res.send.mock.calls[0][0];
|
||||||
|
expect(sent.editId).not.toEqual('1234');
|
||||||
|
expect(sent.shareId).not.toEqual('1234');
|
||||||
|
expect(sent.googleId).toBeUndefined();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should handle mongo error', async ()=>{
|
||||||
|
model.mockImplementation((brew)=>modelBrewThrow(brew));
|
||||||
|
|
||||||
|
let err;
|
||||||
|
try {
|
||||||
|
await api.newBrew({ body: { editId: '1234', shareId: '1234', googleId: '1234', text: 'asdf', title: '' }, query: {} }, res);
|
||||||
|
} catch (e) {
|
||||||
|
err = e;
|
||||||
|
}
|
||||||
|
|
||||||
|
expect(res.send).not.toHaveBeenCalled();
|
||||||
|
expect(err).not.toBeUndefined();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should save to google if requested', async()=>{
|
||||||
|
await api.newBrew({ body: { text: 'asdf', title: '' }, query: { saveToGoogle: true }, account: { username: 'test user' } }, res);
|
||||||
|
|
||||||
|
expect(google.newGoogleBrew).toHaveBeenCalled();
|
||||||
|
expect(res.status).toHaveBeenCalledWith(200);
|
||||||
|
expect(res.send).toHaveBeenCalledWith({
|
||||||
|
_id : '1',
|
||||||
|
authors : ['test user'],
|
||||||
|
description : '',
|
||||||
|
editId : expect.any(String),
|
||||||
|
pageCount : undefined,
|
||||||
|
published : false,
|
||||||
|
renderer : undefined,
|
||||||
|
shareId : expect.any(String),
|
||||||
|
googleId : expect.any(String),
|
||||||
|
systems : [],
|
||||||
|
tags : [],
|
||||||
|
text : undefined,
|
||||||
|
textBin : undefined,
|
||||||
|
theme : '5ePHB',
|
||||||
|
thumbnail : '',
|
||||||
|
title : 'asdf',
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should handle google error', async()=>{
|
||||||
|
google.newGoogleBrew = jest.fn(()=>{
|
||||||
|
throw 'err';
|
||||||
|
});
|
||||||
|
await api.newBrew({ body: { text: 'asdf', title: '' }, query: { saveToGoogle: true }, account: { username: 'test user' } }, res);
|
||||||
|
|
||||||
|
expect(res.status).toHaveBeenCalledWith(500);
|
||||||
|
expect(res.send).toHaveBeenCalledWith('err');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('deleteGoogleBrew', ()=>{
|
||||||
|
it('should check auth and delete brew', async ()=>{
|
||||||
|
const result = await api.deleteGoogleBrew({ username: 'test user' }, 'id', 'editId', res);
|
||||||
|
|
||||||
|
expect(result).toBe(true);
|
||||||
|
expect(google.authCheck).toHaveBeenCalledWith({ username: 'test user' }, expect.objectContaining({}));
|
||||||
|
expect(google.deleteGoogleBrew).toHaveBeenCalledWith('client', 'id', 'editId');
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user