UserThirdPartyIdentityTests.js 6.8 KB

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