|
@@ -3,7 +3,7 @@ async = require("async")
|
|
|
User = require "./helpers/User"
|
|
User = require "./helpers/User"
|
|
|
request = require "./helpers/request"
|
|
request = require "./helpers/request"
|
|
|
settings = require "settings-sharelatex"
|
|
settings = require "settings-sharelatex"
|
|
|
-rclient = require("redis-sharelatex").createClient(settings.redis.web)
|
|
|
|
|
|
|
+{db, ObjectId} = require("../../../app/js/infrastructure/mongojs")
|
|
|
|
|
|
|
|
describe "UserEmails", ->
|
|
describe "UserEmails", ->
|
|
|
beforeEach (done) ->
|
|
beforeEach (done) ->
|
|
@@ -32,10 +32,15 @@ describe "UserEmails", ->
|
|
|
expect(body[1].confirmedAt).to.not.exist
|
|
expect(body[1].confirmedAt).to.not.exist
|
|
|
cb()
|
|
cb()
|
|
|
(cb) =>
|
|
(cb) =>
|
|
|
- rclient.keys 'email_confirmation_token:*', (error, keys) ->
|
|
|
|
|
|
|
+ db.tokens.find {
|
|
|
|
|
+ use: 'email_confirmation',
|
|
|
|
|
+ usedAt: { $exists: false }
|
|
|
|
|
+ }, (error, tokens) =>
|
|
|
# There should only be one confirmation token at the moment
|
|
# There should only be one confirmation token at the moment
|
|
|
- expect(keys.length).to.equal 1
|
|
|
|
|
- token = keys[0].split(':')[1]
|
|
|
|
|
|
|
+ expect(tokens.length).to.equal 1
|
|
|
|
|
+ expect(tokens[0].data.email).to.equal 'newly-added-email@example.com'
|
|
|
|
|
+ expect(tokens[0].data.user_id).to.equal @user._id
|
|
|
|
|
+ token = tokens[0].token
|
|
|
cb()
|
|
cb()
|
|
|
(cb) =>
|
|
(cb) =>
|
|
|
@user.request {
|
|
@user.request {
|
|
@@ -54,9 +59,12 @@ describe "UserEmails", ->
|
|
|
expect(body[1].confirmedAt).to.exist
|
|
expect(body[1].confirmedAt).to.exist
|
|
|
cb()
|
|
cb()
|
|
|
(cb) =>
|
|
(cb) =>
|
|
|
- rclient.keys 'email_confirmation_token:*', (error, keys) ->
|
|
|
|
|
|
|
+ db.tokens.find {
|
|
|
|
|
+ use: 'email_confirmation',
|
|
|
|
|
+ usedAt: { $exists: false }
|
|
|
|
|
+ }, (error, tokens) =>
|
|
|
# Token should be deleted after use
|
|
# Token should be deleted after use
|
|
|
- expect(keys.length).to.equal 0
|
|
|
|
|
|
|
+ expect(tokens.length).to.equal 0
|
|
|
cb()
|
|
cb()
|
|
|
], done
|
|
], done
|
|
|
|
|
|
|
@@ -75,11 +83,15 @@ describe "UserEmails", ->
|
|
|
json: {@email}
|
|
json: {@email}
|
|
|
}, cb
|
|
}, cb
|
|
|
(cb) =>
|
|
(cb) =>
|
|
|
- rclient.keys 'email_confirmation_token:*', (error, keys) ->
|
|
|
|
|
- # There should only be one confirmation token at the moment,
|
|
|
|
|
- # for the first user
|
|
|
|
|
- expect(keys.length).to.equal 1
|
|
|
|
|
- token1 = keys[0].split(':')[1]
|
|
|
|
|
|
|
+ db.tokens.find {
|
|
|
|
|
+ use: 'email_confirmation',
|
|
|
|
|
+ usedAt: { $exists: false }
|
|
|
|
|
+ }, (error, tokens) =>
|
|
|
|
|
+ # There should only be one confirmation token at the moment
|
|
|
|
|
+ expect(tokens.length).to.equal 1
|
|
|
|
|
+ expect(tokens[0].data.email).to.equal @email
|
|
|
|
|
+ expect(tokens[0].data.user_id).to.equal @user._id
|
|
|
|
|
+ token1 = tokens[0].token
|
|
|
cb()
|
|
cb()
|
|
|
(cb) =>
|
|
(cb) =>
|
|
|
# Delete the email from the first user
|
|
# Delete the email from the first user
|
|
@@ -107,14 +119,19 @@ describe "UserEmails", ->
|
|
|
expect(response.statusCode).to.equal 404
|
|
expect(response.statusCode).to.equal 404
|
|
|
cb()
|
|
cb()
|
|
|
(cb) =>
|
|
(cb) =>
|
|
|
- rclient.keys 'email_confirmation_token:*', (error, keys) ->
|
|
|
|
|
|
|
+ db.tokens.find {
|
|
|
|
|
+ use: 'email_confirmation',
|
|
|
|
|
+ usedAt: { $exists: false }
|
|
|
|
|
+ }, (error, tokens) =>
|
|
|
# The first token has been used, so this should be token2 now
|
|
# The first token has been used, so this should be token2 now
|
|
|
- expect(keys.length).to.equal 1
|
|
|
|
|
- token2 = keys[0].split(':')[1]
|
|
|
|
|
|
|
+ expect(tokens.length).to.equal 1
|
|
|
|
|
+ expect(tokens[0].data.email).to.equal @email
|
|
|
|
|
+ expect(tokens[0].data.user_id).to.equal @user2._id
|
|
|
|
|
+ token2 = tokens[0].token
|
|
|
cb()
|
|
cb()
|
|
|
(cb) =>
|
|
(cb) =>
|
|
|
# Second user should be able to confirm the email
|
|
# Second user should be able to confirm the email
|
|
|
- @user2.request {
|
|
|
|
|
|
|
+ @user2.request {
|
|
|
method: 'POST',
|
|
method: 'POST',
|
|
|
url: '/user/emails/confirm',
|
|
url: '/user/emails/confirm',
|
|
|
json:
|
|
json:
|
|
@@ -130,3 +147,48 @@ describe "UserEmails", ->
|
|
|
expect(body[1].confirmedAt).to.exist
|
|
expect(body[1].confirmedAt).to.exist
|
|
|
cb()
|
|
cb()
|
|
|
], done
|
|
], done
|
|
|
|
|
+
|
|
|
|
|
+ describe "with an expired token", ->
|
|
|
|
|
+ it 'should not confirm the email', (done) ->
|
|
|
|
|
+ token = null
|
|
|
|
|
+ async.series [
|
|
|
|
|
+ (cb) =>
|
|
|
|
|
+ @user.request {
|
|
|
|
|
+ method: 'POST',
|
|
|
|
|
+ url: '/user/emails',
|
|
|
|
|
+ json:
|
|
|
|
|
+ email: @email = 'expired-token-email@example.com'
|
|
|
|
|
+ }, (error, response, body) =>
|
|
|
|
|
+ return done(error) if error?
|
|
|
|
|
+ expect(response.statusCode).to.equal 204
|
|
|
|
|
+ cb()
|
|
|
|
|
+ (cb) =>
|
|
|
|
|
+ db.tokens.find {
|
|
|
|
|
+ use: 'email_confirmation',
|
|
|
|
|
+ usedAt: { $exists: false }
|
|
|
|
|
+ }, (error, tokens) =>
|
|
|
|
|
+ # There should only be one confirmation token at the moment
|
|
|
|
|
+ expect(tokens.length).to.equal 1
|
|
|
|
|
+ expect(tokens[0].data.email).to.equal @email
|
|
|
|
|
+ expect(tokens[0].data.user_id).to.equal @user._id
|
|
|
|
|
+ token = tokens[0].token
|
|
|
|
|
+ cb()
|
|
|
|
|
+ (cb) =>
|
|
|
|
|
+ db.tokens.update {
|
|
|
|
|
+ token: token
|
|
|
|
|
+ }, {
|
|
|
|
|
+ $set: {
|
|
|
|
|
+ expiresAt: new Date(Date.now() - 1000000)
|
|
|
|
|
+ }
|
|
|
|
|
+ }, cb
|
|
|
|
|
+ (cb) =>
|
|
|
|
|
+ @user.request {
|
|
|
|
|
+ method: 'POST',
|
|
|
|
|
+ url: '/user/emails/confirm',
|
|
|
|
|
+ json:
|
|
|
|
|
+ token: token
|
|
|
|
|
+ }, (error, response, body) =>
|
|
|
|
|
+ return done(error) if error?
|
|
|
|
|
+ expect(response.statusCode).to.equal 404
|
|
|
|
|
+ cb()
|
|
|
|
|
+ ], done
|