Merge pull request #5319 from overleaf/ta-split-test-assignment-date

Validate User's Split Tests Schema

GitOrigin-RevId: 0e37a833696359838c7133cd7cd9411ec520993a
This commit is contained in:
Timothée Alby 2021-10-04 14:28:21 +02:00 committed by Copybot
parent 1ad94859d5
commit 1425d96deb
2 changed files with 42 additions and 0 deletions

View file

@ -169,5 +169,19 @@ const UserSchema = new Schema({
analyticsId: { type: String }, analyticsId: { type: String },
}) })
function formatSplitTestsSchema(next) {
if (this.splitTests) {
for (const splitTestKey of Object.keys(this.splitTests)) {
for (const variantIndex in this.splitTests[splitTestKey]) {
this.splitTests[splitTestKey][variantIndex].assignedAt = new Date(
this.splitTests[splitTestKey][variantIndex].assignedAt
)
}
}
}
next()
}
UserSchema.pre('save', formatSplitTestsSchema)
exports.User = mongoose.model('User', UserSchema) exports.User = mongoose.model('User', UserSchema)
exports.UserSchema = UserSchema exports.UserSchema = UserSchema

View file

@ -17,6 +17,34 @@ describe('mongoose', function () {
await expect(User.create({ email: email })).to.be.rejected await expect(User.create({ email: email })).to.be.rejected
await expect(User.countDocuments({ email: email })).to.eventually.equal(1) await expect(User.countDocuments({ email: email })).to.eventually.equal(1)
}) })
it('formats assignedAt as Date', async function () {
await expect(
User.create({
email,
splitTests: {
'some-test': [
{
variantName: 'control',
versionNumber: 1,
phase: 'release',
assignedAt: '2021-09-24T11:53:18.313Z',
},
{
variantName: 'control',
versionNumber: 2,
phase: 'release',
assignedAt: new Date(),
},
],
},
})
).to.be.fulfilled
const user = await User.findOne({ email })
expect(user.splitTests['some-test'][0].assignedAt).to.be.a('date')
expect(user.splitTests['some-test'][1].assignedAt).to.be.a('date')
})
}) })
describe('Subsription', function () { describe('Subsription', function () {