From 9e0c44573a1ae0e548a6a1758bb6d73dc9e5240f Mon Sep 17 00:00:00 2001 From: Shane Kilkelly Date: Fri, 22 Jul 2016 16:08:56 +0100 Subject: [PATCH] Remove `expiresAt`, use mongo TTL instead. --- .../CollaboratorsInviteController.coffee | 2 +- .../CollaboratorsInviteHandler.coffee | 6 ----- .../app/coffee/models/ProjectInvite.coffee | 12 ++++----- .../CollaboratorsInviteControllerTests.coffee | 2 -- .../CollaboratorsInviteHandlerTests.coffee | 25 ++++--------------- 5 files changed, 12 insertions(+), 35 deletions(-) diff --git a/services/web/app/coffee/Features/Collaborators/CollaboratorsInviteController.coffee b/services/web/app/coffee/Features/Collaborators/CollaboratorsInviteController.coffee index 76657c1f60..1c8e3adec7 100644 --- a/services/web/app/coffee/Features/Collaborators/CollaboratorsInviteController.coffee +++ b/services/web/app/coffee/Features/Collaborators/CollaboratorsInviteController.coffee @@ -47,7 +47,7 @@ module.exports = CollaboratorsInviteController = if err? logger.err {projectId, token}, "error getting invite by token" return next(err) - # TODO: should we render an expired view instead? + # TODO: render a not-valid view instead if !invite logger.log {projectId, token}, "no invite found for token" return res.sendStatus(404) diff --git a/services/web/app/coffee/Features/Collaborators/CollaboratorsInviteHandler.coffee b/services/web/app/coffee/Features/Collaborators/CollaboratorsInviteHandler.coffee index 9e92fce26c..80c9f10f8a 100644 --- a/services/web/app/coffee/Features/Collaborators/CollaboratorsInviteHandler.coffee +++ b/services/web/app/coffee/Features/Collaborators/CollaboratorsInviteHandler.coffee @@ -47,13 +47,7 @@ module.exports = CollaboratorsInviteHandler = logger.err {err, projectId}, "error fetching invite" return callback(err) if !invite - err = new Errors.NotFoundError("no invite found for token") logger.err {err, projectId, token: tokenString}, "no invite found" - return callback(err) - now = new Date() - # TODO: re-assess whether we should return null or a notfounderror - if invite.expiresAt < now - logger.log {projectId, inviteId: invite._id, expiresAt: invite.expiresAt}, "invite expired" return callback(null, null) callback(null, invite) diff --git a/services/web/app/coffee/models/ProjectInvite.coffee b/services/web/app/coffee/models/ProjectInvite.coffee index 1181454d74..decea99f46 100644 --- a/services/web/app/coffee/models/ProjectInvite.coffee +++ b/services/web/app/coffee/models/ProjectInvite.coffee @@ -1,14 +1,12 @@ mongoose = require 'mongoose' Settings = require 'settings-sharelatex' + Schema = mongoose.Schema ObjectId = Schema.ObjectId -THIRTY_DAYS_IN_SECONDS = 60 * 60 * 24 * 30 -makeExpirationDate = () -> - nowInMillis = Date.now() - new Date(nowInMillis + (1000 * THIRTY_DAYS_IN_SECONDS)) +THIRTY_DAYS_IN_SECONDS = 60 * 60 * 24 * 30 ProjectInviteSchema = new Schema @@ -17,13 +15,15 @@ ProjectInviteSchema = new Schema sendingUserId: ObjectId projectId: ObjectId privileges: String - createdAt: {type: Date, default: Date.now} - expiresAt: {type: Date, default: makeExpirationDate} + createdAt: {type: Date, default: Date.now, index: {expiresAfterSeconds: THIRTY_DAYS_IN_SECONDS}} + conn = mongoose.createConnection(Settings.mongo.url, server: poolSize: Settings.mongo.poolSize || 10) + ProjectInvite = conn.model('ProjectInvite', ProjectInviteSchema) + mongoose.model 'ProjectInvite', ProjectInviteSchema exports.ProjectInvite = ProjectInvite exports.ProjectInviteSchema = ProjectInviteSchema diff --git a/services/web/test/UnitTests/coffee/Collaborators/CollaboratorsInviteControllerTests.coffee b/services/web/test/UnitTests/coffee/Collaborators/CollaboratorsInviteControllerTests.coffee index 5ee04aeadf..38d07269df 100644 --- a/services/web/test/UnitTests/coffee/Collaborators/CollaboratorsInviteControllerTests.coffee +++ b/services/web/test/UnitTests/coffee/Collaborators/CollaboratorsInviteControllerTests.coffee @@ -45,7 +45,6 @@ describe "CollaboratorsInviteController", -> projectId: @targetEmail, targetEmail: 'user@example.com' createdAt: new Date(), - expiresAt: new Date() } @LimitationsManager.canAddXCollaborators = sinon.stub().callsArgWith(2, null, true) @CollaboratorsInviteHandler.inviteToProject = sinon.stub().callsArgWith(4, null, @invite) @@ -133,7 +132,6 @@ describe "CollaboratorsInviteController", -> projectId: @projectId, targetEmail: 'user@example.com' createdAt: new Date(), - expiresAt: new Date() } @CollaboratorsInviteHandler.getInviteByToken = sinon.stub().callsArgWith(2, null, @invite) @callback = sinon.stub() diff --git a/services/web/test/UnitTests/coffee/Collaborators/CollaboratorsInviteHandlerTests.coffee b/services/web/test/UnitTests/coffee/Collaborators/CollaboratorsInviteHandlerTests.coffee index 728a75894d..8d7814a4c9 100644 --- a/services/web/test/UnitTests/coffee/Collaborators/CollaboratorsInviteHandlerTests.coffee +++ b/services/web/test/UnitTests/coffee/Collaborators/CollaboratorsInviteHandlerTests.coffee @@ -130,8 +130,6 @@ describe "CollaboratorsInviteHandler", -> describe 'getInviteByToken', -> beforeEach -> - @theDarkFuture = new Date() - @theDarkFuture.setYear(40000) @fakeInvite = _id: @inviteId email: @email @@ -140,7 +138,6 @@ describe "CollaboratorsInviteHandler", -> projectId: @projectId privileges: @privileges createdAt: new Date() - expiresAt: @theDarkFuture @ProjectInvite.findOne.callsArgWith(1, null, @fakeInvite) @call = (callback) => @CollaboratorsInviteHandler.getInviteByToken @projectId, @token, callback @@ -150,7 +147,7 @@ describe "CollaboratorsInviteHandler", -> beforeEach -> it 'should not produce an error', (done) -> - @call (err) => + @call (err, invite) => expect(err).to.not.be.instanceof Error expect(err).to.be.oneOf [null, undefined] done() @@ -161,7 +158,7 @@ describe "CollaboratorsInviteHandler", -> done() it 'should call ProjectInvite.findOne', (done) -> - @call (err) => + @call (err, invite) => @ProjectInvite.findOne.callCount.should.equal 1 @ProjectInvite.findOne.calledWith({projectId: @projectId, token: @token}).should.equal true done() @@ -172,7 +169,7 @@ describe "CollaboratorsInviteHandler", -> @ProjectInvite.findOne.callsArgWith(1, new Error('woops')) it 'should produce an error', (done) -> - @call (err) => + @call (err, invite) => expect(err).to.be.instanceof Error done() @@ -181,26 +178,14 @@ describe "CollaboratorsInviteHandler", -> beforeEach -> @ProjectInvite.findOne.callsArgWith(1, null, null) - it 'should produce an error', (done) -> - @call (err) => - expect(err).to.be.instanceof Error - done() - - describe 'when the invite is expired', -> - - beforeEach -> - @theDeepPast = new Date() - @theDeepPast.setYear(1977) - @fakeInvite.expiresAt = @theDeepPast - @ProjectInvite.findOne.callsArgWith(1, null, @fakeInvite) - it 'should not produce an error', (done) -> - @call (err) => + @call (err, invite) => expect(err).to.not.be.instanceof Error expect(err).to.be.oneOf [null, undefined] done() it 'should not produce an invite object', (done) -> @call (err, invite) => + expect(invite).to.not.be.instanceof Error expect(invite).to.be.oneOf [null, undefined] done()