diff --git a/server/controllers/CollectionController.js b/server/controllers/CollectionController.js index f01ce6664..e39e34f9f 100644 --- a/server/controllers/CollectionController.js +++ b/server/controllers/CollectionController.js @@ -247,12 +247,17 @@ class CollectionController { return res.status(400).send('Book already in collection') } - // Create collectionBook record - await Database.collectionBookModel.create({ - collectionId: req.collection.id, - bookId: libraryItem.mediaId, - order: collectionBooks.length + 1 - }) + // Create collectionBook record. A second add can pass the check above and hit the unique pair. + try { + await Database.collectionBookModel.create({ + collectionId: req.collection.id, + bookId: libraryItem.mediaId, + order: collectionBooks.length + 1 + }) + } catch (error) { + if (error.name !== 'SequelizeUniqueConstraintError') throw error + return res.status(400).send('Book already in collection') + } const jsonExpanded = await req.collection.getOldJsonExpanded(req.user) if (jsonExpanded) SocketAuthority.clientEmitter(req.user.id, 'collection_updated', jsonExpanded) res.json(jsonExpanded) diff --git a/test/server/controllers/CollectionController.test.js b/test/server/controllers/CollectionController.test.js new file mode 100644 index 000000000..dcef3856c --- /dev/null +++ b/test/server/controllers/CollectionController.test.js @@ -0,0 +1,74 @@ +const { expect } = require('chai') +const { Sequelize } = require('sequelize') +const sinon = require('sinon') + +const Database = require('../../../server/Database') +const Logger = require('../../../server/Logger') +const CollectionController = require('../../../server/controllers/CollectionController') + +describe('CollectionController.addBook', () => { + /** @type {import('../../../server/models/Collection')} */ + let collection + /** @type {import('../../../server/models/LibraryItem')} */ + let libraryItem + + beforeEach(async () => { + global.ServerSettings = {} + Database.sequelize = new Sequelize({ dialect: 'sqlite', storage: ':memory:', logging: false }) + Database.sequelize.uppercaseFirst = (str) => (str ? `${str[0].toUpperCase()}${str.substr(1)}` : '') + await Database.buildModels() + + sinon.stub(Logger, 'info') + sinon.stub(Logger, 'debug') + sinon.stub(Logger, 'warn') + sinon.stub(Logger, 'error') + + const library = await Database.libraryModel.create({ name: 'Test Library', mediaType: 'book' }) + const libraryFolder = await Database.libraryFolderModel.create({ path: '/test-lib', libraryId: library.id }) + const book = await Database.bookModel.create({ title: 'Test Book', audioFiles: [], tags: [], narrators: [], genres: [], chapters: [] }) + libraryItem = await Database.libraryItemModel.create({ + path: '/test-lib/book', + isFile: false, + libraryFiles: [], + mediaId: book.id, + mediaType: 'book', + libraryId: library.id, + libraryFolderId: libraryFolder.id + }) + collection = await Database.collectionModel.create({ name: 'Test Collection', libraryId: library.id }) + await Database.collectionBookModel.create({ collectionId: collection.id, bookId: book.id, order: 1 }) + }) + + afterEach(async () => { + sinon.restore() + await Database.sequelize.sync({ force: true }) + }) + + it('does not throw when the collectionBook link already exists', async () => { + sinon.stub(collection, 'getCollectionBooks').resolves([]) + + const res = { + statusCode: null, + body: null, + status(code) { + this.statusCode = code + return this + }, + send(body) { + this.body = body + return this + }, + json(body) { + this.body = body + return this + } + } + + await CollectionController.addBook({ body: { id: libraryItem.id }, collection, user: { id: 'user-id' } }, res) + + expect(res.statusCode).to.equal(400) + expect(res.body).to.equal('Book already in collection') + const links = await Database.collectionBookModel.findAll({ where: { collectionId: collection.id, bookId: libraryItem.mediaId } }) + expect(links).to.have.lengthOf(1) + }) +})