2019-05-29 05:21:06 -04:00
|
|
|
/* eslint-disable
|
|
|
|
handle-callback-err,
|
|
|
|
max-len,
|
|
|
|
no-unused-vars,
|
|
|
|
*/
|
|
|
|
// TODO: This file was created by bulk-decaffeinate.
|
|
|
|
// Fix any style issues and re-enable lint.
|
|
|
|
/*
|
|
|
|
* decaffeinate suggestions:
|
|
|
|
* DS102: Remove unnecessary code created because of implicit returns
|
|
|
|
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
|
|
|
*/
|
|
|
|
const Errors = require('../../../app/src/Features/Errors/Errors')
|
|
|
|
const Settings = require('settings-sharelatex')
|
|
|
|
const User = require('./helpers/User')
|
|
|
|
const ThirdPartyIdentityManager = require('../../../app/src/Features/User/ThirdPartyIdentityManager')
|
|
|
|
const chai = require('chai')
|
|
|
|
|
|
|
|
const { expect } = chai
|
|
|
|
|
|
|
|
describe('ThirdPartyIdentityManager', function() {
|
|
|
|
beforeEach(function(done) {
|
|
|
|
this.provider = 'provider'
|
|
|
|
this.externalUserId = 'external-user-id'
|
|
|
|
this.externalData = { test: 'data' }
|
|
|
|
this.user = new User()
|
2019-07-01 18:11:07 -04:00
|
|
|
this.user.ensureUserExists(done)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
afterEach(function(done) {
|
2019-09-30 10:46:15 -04:00
|
|
|
return this.user.fullDeleteUser(this.user.email, done)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
describe('login', function() {
|
|
|
|
describe('when third party identity exists', function() {
|
|
|
|
beforeEach(function(done) {
|
|
|
|
return ThirdPartyIdentityManager.link(
|
|
|
|
this.user.id,
|
|
|
|
this.provider,
|
|
|
|
this.externalUserId,
|
|
|
|
this.externalData,
|
2020-10-06 09:50:07 -04:00
|
|
|
{ initiatorId: this.user.id, ipAddress: '0:0:0:0' },
|
2019-05-29 05:21:06 -04:00
|
|
|
done
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should return user', function(done) {
|
|
|
|
ThirdPartyIdentityManager.login(
|
|
|
|
this.provider,
|
|
|
|
this.externalUserId,
|
|
|
|
this.externalData,
|
|
|
|
(err, user) => {
|
|
|
|
expect(err).to.be.null
|
|
|
|
expect(user._id.toString()).to.equal(this.user.id)
|
|
|
|
return done()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2019-06-21 09:46:09 -04:00
|
|
|
it('should merge external data', function(done) {
|
2019-05-29 05:21:06 -04:00
|
|
|
this.externalData = {
|
|
|
|
test: 'different',
|
|
|
|
another: 'key'
|
|
|
|
}
|
|
|
|
ThirdPartyIdentityManager.login(
|
|
|
|
this.provider,
|
|
|
|
this.externalUserId,
|
|
|
|
this.externalData,
|
|
|
|
(err, user) => {
|
|
|
|
expect(err).to.be.null
|
|
|
|
expect(user.thirdPartyIdentifiers[0].externalData).to.deep.equal(
|
|
|
|
this.externalData
|
|
|
|
)
|
|
|
|
return done()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2019-08-07 10:04:04 -04:00
|
|
|
describe('when third party identity does not exists', function() {
|
2019-05-29 05:21:06 -04:00
|
|
|
it('should return error', function(done) {
|
|
|
|
ThirdPartyIdentityManager.login(
|
|
|
|
this.provider,
|
|
|
|
this.externalUserId,
|
|
|
|
this.externalData,
|
|
|
|
(err, user) => {
|
|
|
|
expect(err.name).to.equal('ThirdPartyUserNotFoundError')
|
|
|
|
return done()
|
|
|
|
}
|
|
|
|
)
|
2019-08-07 10:04:04 -04:00
|
|
|
})
|
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
describe('link', function() {
|
2019-08-07 10:04:04 -04:00
|
|
|
describe('when provider not already linked', function() {
|
2019-05-29 05:21:06 -04:00
|
|
|
it('should link provider to user', function(done) {
|
2019-07-11 11:22:25 -04:00
|
|
|
ThirdPartyIdentityManager.link(
|
2019-05-29 05:21:06 -04:00
|
|
|
this.user.id,
|
|
|
|
this.provider,
|
|
|
|
this.externalUserId,
|
|
|
|
this.externalData,
|
2020-10-06 09:50:07 -04:00
|
|
|
{ initiatorId: this.user.id, ipAddress: '0:0:0:0' },
|
2019-08-07 10:04:04 -04:00
|
|
|
(err, res) => {
|
2019-07-11 11:22:25 -04:00
|
|
|
expect(res.thirdPartyIdentifiers.length).to.equal(1)
|
2019-05-29 05:21:06 -04:00
|
|
|
return done()
|
|
|
|
}
|
|
|
|
)
|
2019-08-07 10:04:04 -04:00
|
|
|
})
|
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2019-06-21 09:46:09 -04:00
|
|
|
describe('when provider is already linked', function() {
|
2019-05-29 05:21:06 -04:00
|
|
|
beforeEach(function(done) {
|
2019-07-11 11:22:25 -04:00
|
|
|
ThirdPartyIdentityManager.link(
|
2019-05-29 05:21:06 -04:00
|
|
|
this.user.id,
|
|
|
|
this.provider,
|
|
|
|
this.externalUserId,
|
|
|
|
this.externalData,
|
2020-10-06 09:50:07 -04:00
|
|
|
{ initiatorId: this.user.id, ipAddress: '0:0:0:0' },
|
2019-05-29 05:21:06 -04:00
|
|
|
done
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should link provider to user', function(done) {
|
2019-07-11 11:22:25 -04:00
|
|
|
ThirdPartyIdentityManager.link(
|
2019-05-29 05:21:06 -04:00
|
|
|
this.user.id,
|
|
|
|
this.provider,
|
|
|
|
this.externalUserId,
|
|
|
|
this.externalData,
|
2020-10-06 09:50:07 -04:00
|
|
|
{ initiatorId: this.user.id, ipAddress: '0:0:0:0' },
|
2019-08-07 10:04:04 -04:00
|
|
|
(err, res) => {
|
2019-07-11 11:22:25 -04:00
|
|
|
expect(res).to.exist
|
|
|
|
done()
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should not create duplicate thirdPartyIdentifiers', function(done) {
|
2019-07-11 11:22:25 -04:00
|
|
|
ThirdPartyIdentityManager.link(
|
2019-05-29 05:21:06 -04:00
|
|
|
this.user.id,
|
|
|
|
this.provider,
|
|
|
|
this.externalUserId,
|
|
|
|
this.externalData,
|
2020-10-06 09:50:07 -04:00
|
|
|
{ initiatorId: this.user.id, ipAddress: '0:0:0:0' },
|
2019-08-07 10:04:04 -04:00
|
|
|
(err, user) => {
|
2019-07-11 11:22:25 -04:00
|
|
|
expect(user.thirdPartyIdentifiers.length).to.equal(1)
|
|
|
|
return done()
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2019-06-21 09:46:09 -04:00
|
|
|
it('should replace existing data', function(done) {
|
2019-05-29 05:21:06 -04:00
|
|
|
this.externalData = { replace: 'data' }
|
|
|
|
return ThirdPartyIdentityManager.link(
|
|
|
|
this.user.id,
|
|
|
|
this.provider,
|
|
|
|
this.externalUserId,
|
|
|
|
this.externalData,
|
2020-10-06 09:50:07 -04:00
|
|
|
{ initiatorId: this.user.id, ipAddress: '0:0:0:0' },
|
2019-07-11 11:22:25 -04:00
|
|
|
(err, user) => {
|
|
|
|
expect(user.thirdPartyIdentifiers.length).to.equal(1)
|
|
|
|
return done()
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
2019-07-01 18:11:07 -04:00
|
|
|
|
|
|
|
// describe('when another account tries to link same provider/externalUserId', function() {
|
|
|
|
// NOTE: Cannot run this test because we do not have indexes on the test DB
|
|
|
|
// beforeEach(function(done) {
|
|
|
|
// this.user2 = new User()
|
|
|
|
// this.user2.ensureUserExists(done)
|
|
|
|
// })
|
|
|
|
// it('should not link provider', function(done) {
|
|
|
|
// ThirdPartyIdentityManager.link(
|
|
|
|
// this.user2.id,
|
|
|
|
// this.provider,
|
|
|
|
// this.externalUserId,
|
|
|
|
// this.externalData,
|
|
|
|
// (err, user) => {
|
|
|
|
// expect(err.name).to.equal('ThirdPartyIdentityExistsError')
|
|
|
|
// return done()
|
|
|
|
// }
|
|
|
|
// )
|
2019-09-30 10:46:15 -04:00
|
|
|
// this.user2.fullDeleteUser(this.user2.email, done)
|
2019-07-01 18:11:07 -04:00
|
|
|
// })
|
|
|
|
// })
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2019-06-21 09:46:09 -04:00
|
|
|
describe('unlink', function() {
|
2019-08-07 10:04:04 -04:00
|
|
|
describe('when provider not already linked', function() {
|
2019-05-29 05:21:06 -04:00
|
|
|
it('should succeed', function(done) {
|
|
|
|
return ThirdPartyIdentityManager.unlink(
|
|
|
|
this.user.id,
|
|
|
|
this.provider,
|
2020-10-06 09:50:07 -04:00
|
|
|
{ initiatorId: this.user.id, ipAddress: '0:0:0:0' },
|
2019-08-07 10:04:04 -04:00
|
|
|
(err, res) => {
|
2019-05-29 05:21:06 -04:00
|
|
|
expect(err).to.be.null
|
2019-07-11 11:22:25 -04:00
|
|
|
expect(res.thirdPartyIdentifiers.length).to.equal(0)
|
2019-05-29 05:21:06 -04:00
|
|
|
return done()
|
|
|
|
}
|
|
|
|
)
|
2019-08-07 10:04:04 -04:00
|
|
|
})
|
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2019-06-21 09:46:09 -04:00
|
|
|
describe('when provider is already linked', function() {
|
2019-05-29 05:21:06 -04:00
|
|
|
beforeEach(function(done) {
|
|
|
|
return ThirdPartyIdentityManager.link(
|
|
|
|
this.user.id,
|
|
|
|
this.provider,
|
|
|
|
this.externalUserId,
|
|
|
|
this.externalData,
|
2020-10-06 09:50:07 -04:00
|
|
|
{ initiatorId: this.user.id, ipAddress: '0:0:0:0' },
|
2019-05-29 05:21:06 -04:00
|
|
|
done
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2019-06-21 09:46:09 -04:00
|
|
|
it('should remove thirdPartyIdentifiers entry', function(done) {
|
2019-05-29 05:21:06 -04:00
|
|
|
return ThirdPartyIdentityManager.unlink(
|
|
|
|
this.user.id,
|
|
|
|
this.provider,
|
2020-10-06 09:50:07 -04:00
|
|
|
{ initiatorId: this.user.id, ipAddress: '0:0:0:0' },
|
2019-07-11 11:22:25 -04:00
|
|
|
(err, user) => {
|
|
|
|
expect(user.thirdPartyIdentifiers.length).to.equal(0)
|
|
|
|
return done()
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|