mirror of
https://github.com/naturalcrit/homebrewery.git
synced 2025-12-24 20:42:43 +00:00
reworked to be simpler
This commit is contained in:
@@ -159,15 +159,11 @@ const MetadataEditor = createClass({
|
||||
|
||||
handleDeleteAuthor : function(author){
|
||||
if(!confirm('Are you sure you want to delete this author? They will lose all edit access to this brew.')) return;
|
||||
request.put(`/api/prune/${this.props.metadata.editId}/${author}`)
|
||||
.send()
|
||||
.end((err, res)=>{
|
||||
if(err) {
|
||||
this.props.reportError(err);
|
||||
} else {
|
||||
window.location.reload();
|
||||
}
|
||||
});
|
||||
if(!this.props.metadata.authors.includes(author)) return;
|
||||
this.props.onChange({
|
||||
...this.props.metadata,
|
||||
authors: this.props.metadata.authors.filter(a => a !== author)
|
||||
});
|
||||
},
|
||||
|
||||
renderSystems : function(){
|
||||
|
||||
@@ -378,6 +378,7 @@ app.get('/edit/:id', asyncHandler(getBrew('edit')), asyncHandler(async(req, res,
|
||||
req.brew = req.brew.toObject ? req.brew.toObject() : req.brew;
|
||||
|
||||
req.userThemes = await(getUsersBrewThemes(req.account?.username));
|
||||
|
||||
req.ogMeta = { ...defaultMetaTags,
|
||||
title : req.brew.title || 'Untitled Brew',
|
||||
description : req.brew.description || 'No description.',
|
||||
@@ -564,7 +565,7 @@ const renderPage = async (req, res)=>{
|
||||
enable_themes : config.get('enable_themes'),
|
||||
config : configuration,
|
||||
ogMeta : req.ogMeta,
|
||||
userThemes : req.userThemes,
|
||||
userThemes : req.userThemes
|
||||
};
|
||||
const title = req.brew ? req.brew.title : '';
|
||||
const page = await templateFn('homebrew', title, props)
|
||||
|
||||
@@ -460,28 +460,6 @@ const api = {
|
||||
|
||||
res.status(200).send(saved);
|
||||
},
|
||||
deleteAuthor : async (req, res)=>{
|
||||
try {
|
||||
await api.getBrew('edit')(req, res, ()=>{});
|
||||
} catch (err) {
|
||||
throw { name: 'deleteAuthor Error', message: err, status: 500, brewId: brew._id };
|
||||
}
|
||||
|
||||
let brew = req.brew;
|
||||
const account = req.account;
|
||||
const author = req.params.author;
|
||||
const isOwner = account && (brew.authors[0] === account.username);
|
||||
|
||||
if(brew._id && isOwner) {
|
||||
brew = _.assign(await HomebrewModel.findOne({ _id: brew._id }), brew);
|
||||
brew.authors = _.pull(brew.authors, author);
|
||||
brew.markModified('authors'); //Mongo will not properly update arrays without markModified()
|
||||
await brew.save().catch((err)=>{
|
||||
throw { name: 'deleteAuthor Error', message: err, status: 500, HBErrorCode: '08', brewId: brew._id };
|
||||
});
|
||||
}
|
||||
res.status(204).send();
|
||||
},
|
||||
deleteGoogleBrew : async (account, id, editId, res)=>{
|
||||
const auth = await GoogleActions.authCheck(account, res);
|
||||
await GoogleActions.deleteGoogleBrew(auth, id, editId);
|
||||
@@ -553,7 +531,6 @@ router.post('/api', checkClientVersion, asyncHandler(api.newBrew));
|
||||
router.put('/api/:id', checkClientVersion, asyncHandler(api.getBrew('edit', false)), asyncHandler(api.updateBrew));
|
||||
router.put('/api/update/:id', checkClientVersion, asyncHandler(api.getBrew('edit', false)), asyncHandler(api.updateBrew));
|
||||
router.delete('/api/:id', checkClientVersion, asyncHandler(api.deleteBrew));
|
||||
router.put('/api/prune/:id/:author', checkClientVersion, asyncHandler(api.deleteAuthor));
|
||||
router.get('/api/remove/:id', checkClientVersion, asyncHandler(api.deleteBrew));
|
||||
router.get('/api/theme/:renderer/:id', asyncHandler(api.getThemeBundle));
|
||||
|
||||
|
||||
@@ -908,51 +908,7 @@ brew`);
|
||||
);
|
||||
});
|
||||
});
|
||||
describe('deleteBrewAuthor', ()=>{
|
||||
it('should handle case where fetching the brew returns an error', async ()=>{
|
||||
api.getBrew = jest.fn(()=>async ()=>{ throw { message: 'err', HBErrorCode: '02' }; });
|
||||
api.getId = jest.fn(()=>({ id: '1', googleId: '2' }));
|
||||
model.deleteOne = jest.fn(async ()=>{ });
|
||||
const next = jest.fn();
|
||||
|
||||
await api.deleteBrew(null, null, next);
|
||||
|
||||
expect(next).toHaveBeenCalled();
|
||||
expect(model.deleteOne).toHaveBeenCalledWith({ editId: '1' });
|
||||
});
|
||||
|
||||
it('should remove one author', async ()=>{
|
||||
const brew = {
|
||||
...hbBrew,
|
||||
_id : 'some-id',
|
||||
authors : ['test', 'test2'],
|
||||
markModified : markModifiedFunc,
|
||||
save : saveFunc
|
||||
};
|
||||
|
||||
api.getBrew = jest.fn(()=>async (req)=>{
|
||||
req.brew = brew;
|
||||
});
|
||||
model.findOne = jest.fn(async ()=>modelBrew(brew));
|
||||
model.deleteOne = jest.fn(async ()=>{ });
|
||||
|
||||
const req = {
|
||||
account : { username: 'test' },
|
||||
params : { author: 'test2' }
|
||||
};
|
||||
|
||||
const res = { status: jest.fn(()=>res), send: jest.fn() };
|
||||
|
||||
await api.deleteAuthor(req, res);
|
||||
|
||||
expect(api.getBrew).toHaveBeenCalled();
|
||||
expect(markModifiedFunc).toHaveBeenCalled();
|
||||
expect(model.findOne).toHaveBeenCalled();
|
||||
expect(model.deleteOne).not.toHaveBeenCalled();
|
||||
expect(saveFunc).toHaveBeenCalled();
|
||||
expect(saved.authors).toEqual(['test']);
|
||||
});
|
||||
});
|
||||
|
||||
describe('deleteGoogleBrew', ()=>{
|
||||
it('should check auth and delete brew', async ()=>{
|
||||
const result = await api.deleteGoogleBrew({ username: 'test user' }, 'id', 'editId', res);
|
||||
|
||||
Reference in New Issue
Block a user