Merge pull request #3989 from overleaf/jel-reconfirm-tests

Fix reconfirm tests

GitOrigin-RevId: bbcaf97f339dc563a7b49db14252cc8de601878d
This commit is contained in:
Miguel Serrano 2021-05-05 15:20:40 +02:00 committed by Copybot
parent 4bcab34580
commit c081db1ddf
2 changed files with 57 additions and 19 deletions

View file

@ -1006,14 +1006,6 @@ describe('UserEmails', function () {
describe('notification period', function () { describe('notification period', function () {
let defaultEmail, userHelper, email1, email2, email3 let defaultEmail, userHelper, email1, email2, email3
const maxConfirmationMonths = 12 const maxConfirmationMonths = 12
const lastDayToReconfirm = moment()
.subtract(maxConfirmationMonths, 'months')
.toDate()
const oneDayBeforeLastDayToReconfirm = moment(lastDayToReconfirm)
.add(1, 'day')
.toDate()
const daysToBackdate = moment().diff(oneDayBeforeLastDayToReconfirm, 'day')
const daysToBackdateForAfterDate = daysToBackdate + 1
beforeEach(async function () { beforeEach(async function () {
if (!Features.hasFeature('affiliations')) { if (!Features.hasFeature('affiliations')) {
@ -1047,12 +1039,20 @@ describe('UserEmails', function () {
await userHelper.addEmailAndConfirm(userId, email1) await userHelper.addEmailAndConfirm(userId, email1)
await userHelper.addEmailAndConfirm(userId, email2) await userHelper.addEmailAndConfirm(userId, email2)
await userHelper.addEmailAndConfirm(userId, email3) await userHelper.addEmailAndConfirm(userId, email3)
await userHelper.backdateConfirmation(userId, email1, daysToBackdate) await userHelper.changeConfirmedToNotificationPeriod(
await userHelper.backdateConfirmation(userId, email2, daysToBackdate) userId,
await userHelper.backdateConfirmation( email1,
maxConfirmationMonths
)
await userHelper.changeConfirmedToNotificationPeriod(
userId,
email2,
maxConfirmationMonths
)
await userHelper.changeConfirmedToPastReconfirmation(
userId, userId,
email3, email3,
daysToBackdateForAfterDate maxConfirmationMonths
) )
}) })
@ -1096,17 +1096,17 @@ describe('UserEmails', function () {
.add(14, 'days') .add(14, 'days')
.toDate() .toDate()
const backdatedDays = moment().diff(dateInPeriodButNotExpired, 'days') const backdatedDays = moment().diff(dateInPeriodButNotExpired, 'days')
await userHelper.backdateConfirmation( await userHelper.changeConfirmationDate(
userHelper.user._id, userHelper.user._id,
email1, email1,
backdatedDays backdatedDays
) )
await userHelper.backdateConfirmation( await userHelper.changeConfirmationDate(
userHelper.user._id, userHelper.user._id,
email2, email2,
backdatedDays backdatedDays
) )
await userHelper.backdateConfirmation( await userHelper.changeConfirmationDate(
userHelper.user._id, userHelper.user._id,
email3, email3,
backdatedDays backdatedDays

View file

@ -324,21 +324,59 @@ class UserHelper {
await this.confirmEmail(userId, email) await this.confirmEmail(userId, email)
} }
async backdateConfirmation(userId, email, days) { async changeConfirmationDate(userId, email, date) {
const confirmedDate = moment().subtract(days, 'days').toDate()
const query = { const query = {
_id: userId, _id: userId,
'emails.email': email, 'emails.email': email,
} }
const update = { const update = {
$set: { $set: {
'emails.$.confirmedAt': confirmedDate, 'emails.$.confirmedAt': date,
'emails.$.reconfirmedAt': confirmedDate, 'emails.$.reconfirmedAt': date,
}, },
} }
await UserUpdater.promises.updateUser(query, update) await UserUpdater.promises.updateUser(query, update)
} }
async changeConfirmedToNotificationPeriod(
userId,
email,
maxConfirmationMonths
) {
// set a user's confirmation date so that
// it is within the notification period to reconfirm
// but not older than the last day to reconfirm
const notificationDays = Settings.reconfirmNotificationDays
if (!notificationDays) return
const middleOfNotificationPeriod = Math.ceil(notificationDays / 2)
// use the middle of the notification rather than the start or end due to
// variations in days in months.
const lastDayToReconfirm = moment().subtract(
maxConfirmationMonths,
'months'
)
const notificationsStart = lastDayToReconfirm
.add(middleOfNotificationPeriod, 'days')
.toDate()
await this.changeConfirmationDate(userId, email, notificationsStart)
}
async changeConfirmedToPastReconfirmation(
userId,
email,
maxConfirmationMonths
) {
// set a user's confirmation date so that they are past the reconfirmation window
const date = moment()
.subtract(maxConfirmationMonths, 'months')
.subtract(1, 'week')
.toDate()
await this.changeConfirmationDate(userId, email, date)
}
async confirmEmail(userId, email) { async confirmEmail(userId, email) {
let response let response
// UserHelper.createUser does not create a confirmation token // UserHelper.createUser does not create a confirmation token