Просмотр исходного кода

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

Validate User's Split Tests Schema

GitOrigin-RevId: 0e37a833696359838c7133cd7cd9411ec520993a
Timothée Alby 5 лет назад
Родитель
Сommit
1425d96deb
2 измененных файлов с 42 добавлено и 0 удалено
  1. 14 0
      services/web/app/src/models/User.js
  2. 28 0
      services/web/test/acceptance/src/ModelTests.js

+ 14 - 0
services/web/app/src/models/User.js

@@ -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

+ 28 - 0
services/web/test/acceptance/src/ModelTests.js

@@ -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 () {