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 () {
let defaultEmail, userHelper, email1, email2, email3
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 () {
if (!Features.hasFeature('affiliations')) {
@ -1047,12 +1039,20 @@ describe('UserEmails', function () {
await userHelper.addEmailAndConfirm(userId, email1)
await userHelper.addEmailAndConfirm(userId, email2)
await userHelper.addEmailAndConfirm(userId, email3)
await userHelper.backdateConfirmation(userId, email1, daysToBackdate)
await userHelper.backdateConfirmation(userId, email2, daysToBackdate)
await userHelper.backdateConfirmation(
await userHelper.changeConfirmedToNotificationPeriod(
userId,
email1,
maxConfirmationMonths
)
await userHelper.changeConfirmedToNotificationPeriod(
userId,
email2,
maxConfirmationMonths
)
await userHelper.changeConfirmedToPastReconfirmation(
userId,
email3,
daysToBackdateForAfterDate
maxConfirmationMonths
)
})
@ -1096,17 +1096,17 @@ describe('UserEmails', function () {
.add(14, 'days')
.toDate()
const backdatedDays = moment().diff(dateInPeriodButNotExpired, 'days')
await userHelper.backdateConfirmation(
await userHelper.changeConfirmationDate(
userHelper.user._id,
email1,
backdatedDays
)
await userHelper.backdateConfirmation(
await userHelper.changeConfirmationDate(
userHelper.user._id,
email2,
backdatedDays
)
await userHelper.backdateConfirmation(
await userHelper.changeConfirmationDate(
userHelper.user._id,
email3,
backdatedDays

View file

@ -324,21 +324,59 @@ class UserHelper {
await this.confirmEmail(userId, email)
}
async backdateConfirmation(userId, email, days) {
const confirmedDate = moment().subtract(days, 'days').toDate()
async changeConfirmationDate(userId, email, date) {
const query = {
_id: userId,
'emails.email': email,
}
const update = {
$set: {
'emails.$.confirmedAt': confirmedDate,
'emails.$.reconfirmedAt': confirmedDate,
'emails.$.confirmedAt': date,
'emails.$.reconfirmedAt': date,
},
}
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) {
let response
// UserHelper.createUser does not create a confirmation token