changes to implement instructions
This commit is contained in:
@@ -0,0 +1,132 @@
|
||||
const assert = require('node:assert/strict')
|
||||
const { test } = require('node:test')
|
||||
|
||||
const userId = '507f1f77bcf86cd799439011'
|
||||
const otherUserId = '507f191e810c19729de860ea'
|
||||
const { requireDocumentId, requireUserId } = require('../worker-tenant')
|
||||
|
||||
test('queue IDs reject missing values and MongoDB query operators', () => {
|
||||
assert.equal(requireUserId(userId), userId)
|
||||
assert.equal(requireDocumentId(otherUserId), otherUserId)
|
||||
assert.throws(() => requireUserId({ $ne: otherUserId }), /userId/)
|
||||
assert.throws(() => requireDocumentId(undefined), /document ID/)
|
||||
})
|
||||
|
||||
const factories = [
|
||||
require('../voice-cloning-job-handler/voice_cloning/voice_cloning_service'),
|
||||
require('../voice-cloning-job-handler/user_audio_profile/user_audio_profile_service'),
|
||||
require('../voice-synthsizer-job-handler/user_audio_profile/user_audio_profile_service'),
|
||||
require('../voice-synthsizer-job-handler/job/job_service'),
|
||||
]
|
||||
|
||||
for (const [index, factory] of factories.entries()) {
|
||||
test(`worker service ${index} scopes reads and writes`, async () => {
|
||||
const calls = []
|
||||
class Model {
|
||||
static async findOne(filter) { calls.push(['findOne', filter]) }
|
||||
static async find(filter) { calls.push(['find', filter]); return [] }
|
||||
static async findOneAndUpdate(filter, update) {
|
||||
calls.push(['findOneAndUpdate', filter, update])
|
||||
}
|
||||
static async updateMany(filter, update) {
|
||||
calls.push(['updateMany', filter, update])
|
||||
}
|
||||
static async insertMany(data) { calls.push(['insertMany', data]) }
|
||||
}
|
||||
const service = factory(Model)
|
||||
|
||||
await service.read({ _id: 'record', userId })
|
||||
await service.find({ userId })
|
||||
await service.update({ _id: 'record', userId, status: 'completed' })
|
||||
await service.remove({ _id: 'record', userId })
|
||||
await service.removeMany({ userId })
|
||||
await service.insertMany([{ userId, status: 'created' }])
|
||||
|
||||
for (const call of calls.slice(0, 5)) {
|
||||
assert.equal(call[1].userId, userId)
|
||||
}
|
||||
assert.equal(calls[2][1].deleted, false)
|
||||
assert.deepEqual(calls[2][2], { $set: { status: 'completed' } })
|
||||
assert.equal(calls[5][1][0].userId, userId)
|
||||
|
||||
const originalLog = console.log
|
||||
const before = calls.length
|
||||
console.log = () => {}
|
||||
try {
|
||||
await assert.rejects(service.read({ _id: 'record' }), /userId/)
|
||||
await assert.rejects(
|
||||
service.update({ _id: 'record', userId: { $ne: otherUserId } }),
|
||||
/userId/
|
||||
)
|
||||
await assert.rejects(service.remove({ _id: 'record' }), /userId/)
|
||||
await assert.rejects(
|
||||
service.insertMany([{ userId }, { status: 'created' }]),
|
||||
/userId/
|
||||
)
|
||||
} finally {
|
||||
console.log = originalLog
|
||||
}
|
||||
assert.equal(calls.length, before)
|
||||
})
|
||||
}
|
||||
|
||||
test('salutation upsert stays within the job tenant', async () => {
|
||||
const calls = []
|
||||
class Model {
|
||||
constructor(data) { calls.push(['create', data]) }
|
||||
async save() { return this }
|
||||
static async findOne(filter) { calls.push(['findOne', filter]); return null }
|
||||
}
|
||||
const service = require('../voice-synthsizer-job-handler/salutation/salutation_service')(Model)
|
||||
await service.updateOrCreate({
|
||||
firstName: 'Ada',
|
||||
salutationVideo: 'url',
|
||||
userAudioProfileId: 'profile',
|
||||
}, userId)
|
||||
assert.equal(calls[0][1].userId, userId)
|
||||
assert.equal(calls[0][1].deleted, false)
|
||||
assert.equal(calls[1][1].userId, userId)
|
||||
})
|
||||
|
||||
test('salutation upsert updates an existing record within the job tenant', async () => {
|
||||
const calls = []
|
||||
class Model {
|
||||
static async findOne(filter) {
|
||||
calls.push(['findOne', filter])
|
||||
return { _id: 'salutation', userId }
|
||||
}
|
||||
static async findOneAndUpdate(filter, update) {
|
||||
calls.push(['findOneAndUpdate', filter, update])
|
||||
}
|
||||
}
|
||||
const service = require('../voice-synthsizer-job-handler/salutation/salutation_service')(Model)
|
||||
await service.updateOrCreate({
|
||||
firstName: 'Ada',
|
||||
salutationVideo: 'url',
|
||||
userAudioProfileId: 'profile',
|
||||
}, userId)
|
||||
assert.equal(calls[0][1].userId, userId)
|
||||
assert.deepEqual(calls[1][1], { _id: 'salutation', userId, deleted: false })
|
||||
assert.deepEqual(calls[1][2], { $set: { salutationVideo: 'url' } })
|
||||
})
|
||||
|
||||
test('salutation updates cannot change ownership', async () => {
|
||||
const calls = []
|
||||
class Model {
|
||||
static async findOneAndUpdate(filter, update) {
|
||||
calls.push([filter, update])
|
||||
}
|
||||
}
|
||||
const service = require('../voice-synthsizer-job-handler/salutation/salutation_service')(Model)
|
||||
await service.update({
|
||||
_id: 'salutation',
|
||||
userId: otherUserId,
|
||||
salutationVideo: 'url',
|
||||
}, userId)
|
||||
assert.deepEqual(calls[0], [
|
||||
{ _id: 'salutation', userId, deleted: false },
|
||||
{ $set: { salutationVideo: 'url' } },
|
||||
])
|
||||
await assert.rejects(service.modify({ _id: 'salutation' }), /userId/)
|
||||
assert.equal(calls.length, 1)
|
||||
})
|
||||
Reference in New Issue
Block a user