UserThirdPartyIdentityTests.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /* eslint-disable
  2. node/handle-callback-err,
  3. max-len,
  4. no-unused-vars,
  5. */
  6. // TODO: This file was created by bulk-decaffeinate.
  7. // Fix any style issues and re-enable lint.
  8. /*
  9. * decaffeinate suggestions:
  10. * DS102: Remove unnecessary code created because of implicit returns
  11. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  12. */
  13. const Errors = require('../../../app/src/Features/Errors/Errors')
  14. const Settings = require('settings-sharelatex')
  15. const User = require('./helpers/User')
  16. const ThirdPartyIdentityManager = require('../../../app/src/Features/User/ThirdPartyIdentityManager')
  17. const { expect } = require('chai')
  18. describe('ThirdPartyIdentityManager', function () {
  19. beforeEach(function (done) {
  20. this.provider = 'provider'
  21. this.externalUserId = 'external-user-id'
  22. this.externalData = { test: 'data' }
  23. this.user = new User()
  24. this.user.ensureUserExists(done)
  25. })
  26. afterEach(function (done) {
  27. return this.user.fullDeleteUser(this.user.email, done)
  28. })
  29. describe('login', function () {
  30. describe('when third party identity exists', function () {
  31. beforeEach(function (done) {
  32. return ThirdPartyIdentityManager.link(
  33. this.user.id,
  34. this.provider,
  35. this.externalUserId,
  36. this.externalData,
  37. { initiatorId: this.user.id, ipAddress: '0:0:0:0' },
  38. done
  39. )
  40. })
  41. it('should return user', function (done) {
  42. ThirdPartyIdentityManager.login(
  43. this.provider,
  44. this.externalUserId,
  45. this.externalData,
  46. (err, user) => {
  47. expect(err).to.be.null
  48. expect(user._id.toString()).to.equal(this.user.id)
  49. return done()
  50. }
  51. )
  52. })
  53. it('should merge external data', function (done) {
  54. this.externalData = {
  55. test: 'different',
  56. another: 'key',
  57. }
  58. ThirdPartyIdentityManager.login(
  59. this.provider,
  60. this.externalUserId,
  61. this.externalData,
  62. (err, user) => {
  63. expect(err).to.be.null
  64. expect(user.thirdPartyIdentifiers[0].externalData).to.deep.equal(
  65. this.externalData
  66. )
  67. return done()
  68. }
  69. )
  70. })
  71. })
  72. describe('when third party identity does not exists', function () {
  73. it('should return error', function (done) {
  74. ThirdPartyIdentityManager.login(
  75. this.provider,
  76. this.externalUserId,
  77. this.externalData,
  78. (err, user) => {
  79. expect(err.name).to.equal('ThirdPartyUserNotFoundError')
  80. return done()
  81. }
  82. )
  83. })
  84. })
  85. })
  86. describe('link', function () {
  87. describe('when provider not already linked', function () {
  88. it('should link provider to user', function (done) {
  89. ThirdPartyIdentityManager.link(
  90. this.user.id,
  91. this.provider,
  92. this.externalUserId,
  93. this.externalData,
  94. { initiatorId: this.user.id, ipAddress: '0:0:0:0' },
  95. (err, res) => {
  96. expect(res.thirdPartyIdentifiers.length).to.equal(1)
  97. return done()
  98. }
  99. )
  100. })
  101. })
  102. describe('when provider is already linked', function () {
  103. beforeEach(function (done) {
  104. ThirdPartyIdentityManager.link(
  105. this.user.id,
  106. this.provider,
  107. this.externalUserId,
  108. this.externalData,
  109. { initiatorId: this.user.id, ipAddress: '0:0:0:0' },
  110. done
  111. )
  112. })
  113. it('should link provider to user', function (done) {
  114. ThirdPartyIdentityManager.link(
  115. this.user.id,
  116. this.provider,
  117. this.externalUserId,
  118. this.externalData,
  119. { initiatorId: this.user.id, ipAddress: '0:0:0:0' },
  120. (err, res) => {
  121. expect(res).to.exist
  122. done()
  123. }
  124. )
  125. })
  126. it('should not create duplicate thirdPartyIdentifiers', function (done) {
  127. ThirdPartyIdentityManager.link(
  128. this.user.id,
  129. this.provider,
  130. this.externalUserId,
  131. this.externalData,
  132. { initiatorId: this.user.id, ipAddress: '0:0:0:0' },
  133. (err, user) => {
  134. expect(user.thirdPartyIdentifiers.length).to.equal(1)
  135. return done()
  136. }
  137. )
  138. })
  139. it('should replace existing data', function (done) {
  140. this.externalData = { replace: 'data' }
  141. return ThirdPartyIdentityManager.link(
  142. this.user.id,
  143. this.provider,
  144. this.externalUserId,
  145. this.externalData,
  146. { initiatorId: this.user.id, ipAddress: '0:0:0:0' },
  147. (err, user) => {
  148. expect(user.thirdPartyIdentifiers.length).to.equal(1)
  149. return done()
  150. }
  151. )
  152. })
  153. // describe('when another account tries to link same provider/externalUserId', function() {
  154. // NOTE: Cannot run this test because we do not have indexes on the test DB
  155. // beforeEach(function(done) {
  156. // this.user2 = new User()
  157. // this.user2.ensureUserExists(done)
  158. // })
  159. // it('should not link provider', function(done) {
  160. // ThirdPartyIdentityManager.link(
  161. // this.user2.id,
  162. // this.provider,
  163. // this.externalUserId,
  164. // this.externalData,
  165. // (err, user) => {
  166. // expect(err.name).to.equal('ThirdPartyIdentityExistsError')
  167. // return done()
  168. // }
  169. // )
  170. // this.user2.fullDeleteUser(this.user2.email, done)
  171. // })
  172. // })
  173. })
  174. })
  175. describe('unlink', function () {
  176. describe('when provider not already linked', function () {
  177. it('should succeed', function (done) {
  178. return ThirdPartyIdentityManager.unlink(
  179. this.user.id,
  180. this.provider,
  181. { initiatorId: this.user.id, ipAddress: '0:0:0:0' },
  182. (err, res) => {
  183. expect(err).to.be.null
  184. expect(res.thirdPartyIdentifiers.length).to.equal(0)
  185. return done()
  186. }
  187. )
  188. })
  189. })
  190. describe('when provider is already linked', function () {
  191. beforeEach(function (done) {
  192. return ThirdPartyIdentityManager.link(
  193. this.user.id,
  194. this.provider,
  195. this.externalUserId,
  196. this.externalData,
  197. { initiatorId: this.user.id, ipAddress: '0:0:0:0' },
  198. done
  199. )
  200. })
  201. it('should remove thirdPartyIdentifiers entry', function (done) {
  202. return ThirdPartyIdentityManager.unlink(
  203. this.user.id,
  204. this.provider,
  205. { initiatorId: this.user.id, ipAddress: '0:0:0:0' },
  206. (err, user) => {
  207. expect(user.thirdPartyIdentifiers.length).to.equal(0)
  208. return done()
  209. }
  210. )
  211. })
  212. })
  213. })
  214. })