2026-03-02 17:50:08 -05:00
|
|
|
const { expect } = require('chai')
|
|
|
|
|
const sinon = require('sinon')
|
|
|
|
|
const User = require('../../../server/models/User')
|
|
|
|
|
|
|
|
|
|
describe('User model', () => {
|
|
|
|
|
describe('getUserByIdOrOldId', () => {
|
|
|
|
|
let originalSequelize
|
|
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
|
originalSequelize = User.sequelize
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
|
User.sequelize = originalSequelize
|
|
|
|
|
sinon.restore()
|
|
|
|
|
})
|
|
|
|
|
|
2026-03-02 18:02:36 -05:00
|
|
|
it('should resolve UUID ids via primary-key lookup on postgres', async () => {
|
|
|
|
|
User.sequelize = {
|
|
|
|
|
getDialect: () => 'postgres',
|
|
|
|
|
models: {
|
|
|
|
|
mediaProgress: {}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const user = { id: 'e8e677b2-da16-4220-ab67-443b7714caf9' }
|
|
|
|
|
const findByPkStub = sinon.stub(User, 'findByPk').resolves(user)
|
|
|
|
|
const findOneStub = sinon.stub(User, 'findOne').resolves(null)
|
|
|
|
|
|
|
|
|
|
const result = await User.getUserByIdOrOldId('e8e677b2-da16-4220-ab67-443b7714caf9')
|
|
|
|
|
|
|
|
|
|
expect(result).to.equal(user)
|
|
|
|
|
expect(findByPkStub.calledOnce).to.equal(true)
|
|
|
|
|
expect(findOneStub.called).to.equal(false)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('should query legacy oldUserId with postgres-safe JSON matcher', async () => {
|
2026-03-02 17:50:08 -05:00
|
|
|
User.sequelize = {
|
|
|
|
|
getDialect: () => 'postgres',
|
|
|
|
|
models: {
|
|
|
|
|
mediaProgress: {}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-02 18:02:36 -05:00
|
|
|
const findByPkStub = sinon.stub(User, 'findByPk').resolves(null)
|
2026-03-02 17:50:08 -05:00
|
|
|
const findOneStub = sinon.stub(User, 'findOne').resolves(null)
|
|
|
|
|
|
|
|
|
|
await User.getUserByIdOrOldId('root')
|
|
|
|
|
|
2026-03-02 18:02:36 -05:00
|
|
|
expect(findByPkStub.called).to.equal(false)
|
2026-03-02 17:50:08 -05:00
|
|
|
expect(findOneStub.calledOnce).to.equal(true)
|
|
|
|
|
|
|
|
|
|
const options = findOneStub.firstCall.args[0]
|
2026-03-02 19:55:23 -05:00
|
|
|
expect(options.where.attribute.val).to.equal("extradata#>>'{oldUserId}'")
|
2026-03-02 18:02:36 -05:00
|
|
|
expect(options.where.logic).to.equal('root')
|
|
|
|
|
})
|
2026-03-02 17:50:08 -05:00
|
|
|
|
2026-03-02 18:02:36 -05:00
|
|
|
it('should keep sqlite oldUserId matcher unchanged', async () => {
|
|
|
|
|
User.sequelize = {
|
|
|
|
|
getDialect: () => 'sqlite',
|
|
|
|
|
models: {
|
|
|
|
|
mediaProgress: {}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const findOneStub = sinon.stub(User, 'findOne').resolves(null)
|
|
|
|
|
|
|
|
|
|
await User.getUserByIdOrOldId('root')
|
|
|
|
|
|
|
|
|
|
expect(findOneStub.calledOnce).to.equal(true)
|
|
|
|
|
|
|
|
|
|
const options = findOneStub.firstCall.args[0]
|
|
|
|
|
expect(options.where).to.deep.equal({ 'extraData.oldUserId': 'root' })
|
2026-03-02 17:50:08 -05:00
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
})
|