All: Fixes #657: Disallow giving name of existing tag to another tag

pull/773/head
Laurent Cozic 2018-09-06 18:33:21 +01:00
parent ef1ae63233
commit 32c81ad8c2
3 changed files with 50 additions and 0 deletions

View File

@ -28,6 +28,7 @@ npm test tests-build/HtmlToMd.js
npm test tests-build/markdownUtils.js
npm test tests-build/models_Folder.js
npm test tests-build/models_Note.js
npm test tests-build/models_Tag.js
npm test tests-build/models_Setting.js
npm test tests-build/services_InteropService.js
npm test tests-build/services_ResourceService.js

View File

@ -0,0 +1,45 @@
require('app-module-path').addPath(__dirname);
const { time } = require('lib/time-utils.js');
const { asyncTest, fileContentEqual, setupDatabase, setupDatabaseAndSynchronizer, db, synchronizer, fileApi, sleep, clearDatabase, switchClient, syncTargetId, objectsEqual, checkThrowAsync } = require('test-utils.js');
const Folder = require('lib/models/Folder.js');
const Note = require('lib/models/Note.js');
const Tag = require('lib/models/Tag.js');
const BaseModel = require('lib/BaseModel.js');
const { shim } = require('lib/shim');
process.on('unhandledRejection', (reason, p) => {
console.log('Unhandled Rejection at: Promise', p, 'reason:', reason);
});
describe('models_Tag', function() {
beforeEach(async (done) => {
await setupDatabaseAndSynchronizer(1);
await switchClient(1);
done();
});
it('should add tags by title', asyncTest(async () => {
let folder1 = await Folder.save({ title: "folder1" });
let note1 = await Note.save({ title: 'ma note', parent_id: folder1.id });
await Tag.setNoteTagsByTitles(note1.id, ['un', 'deux']);
const noteTags = await Tag.tagsByNoteId(note1.id);
expect(noteTags.length).toBe(2);
}));
it('should not allow renaming tag to existing tag names', asyncTest(async () => {
let folder1 = await Folder.save({ title: "folder1" });
let note1 = await Note.save({ title: 'ma note', parent_id: folder1.id });
await Tag.setNoteTagsByTitles(note1.id, ['un', 'deux']);
const tagUn = await Tag.loadByTitle('un');
const hasThrown = await checkThrowAsync(async () => await Tag.save({ id: tagUn.id, title: 'deux' }, { userSideValidation: true }));
expect(hasThrown).toBe(true);
}));
});

View File

@ -3,6 +3,7 @@ const BaseItem = require('lib/models/BaseItem.js');
const NoteTag = require('lib/models/NoteTag.js');
const Note = require('lib/models/Note.js');
const { time } = require('lib/time-utils.js');
const { _ } = require('lib/locale');
class Tag extends BaseItem {
@ -153,6 +154,9 @@ class Tag extends BaseItem {
if (options && options.userSideValidation) {
if ('title' in o) {
o.title = o.title.trim().toLowerCase();
const existingTag = await Tag.loadByTitle(o.title);
if (existingTag && existingTag.id !== o.id) throw new Error(_('The tag "%s" already exists. Please choose a different name.', o.title));
}
}