UserThirdPartyIdentityTests.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /* eslint-disable
  2. 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.full_delete_user(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. 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', () =>
  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. describe('link', function() {
  86. describe('when provider not already linked', () =>
  87. it('should link provider to user', function(done) {
  88. ThirdPartyIdentityManager.link(
  89. this.user.id,
  90. this.provider,
  91. this.externalUserId,
  92. this.externalData,
  93. function(err, res) {
  94. expect(res.thirdPartyIdentifiers.length).to.equal(1)
  95. return done()
  96. }
  97. )
  98. }))
  99. describe('when provider is already linked', function() {
  100. beforeEach(function(done) {
  101. ThirdPartyIdentityManager.link(
  102. this.user.id,
  103. this.provider,
  104. this.externalUserId,
  105. this.externalData,
  106. done
  107. )
  108. })
  109. it('should link provider to user', function(done) {
  110. ThirdPartyIdentityManager.link(
  111. this.user.id,
  112. this.provider,
  113. this.externalUserId,
  114. this.externalData,
  115. function(err, res) {
  116. expect(res).to.exist
  117. done()
  118. }
  119. )
  120. })
  121. it('should not create duplicate thirdPartyIdentifiers', function(done) {
  122. ThirdPartyIdentityManager.link(
  123. this.user.id,
  124. this.provider,
  125. this.externalUserId,
  126. this.externalData,
  127. function(err, user) {
  128. expect(user.thirdPartyIdentifiers.length).to.equal(1)
  129. return done()
  130. }
  131. )
  132. })
  133. it('should replace existing data', function(done) {
  134. this.externalData = { replace: 'data' }
  135. return ThirdPartyIdentityManager.link(
  136. this.user.id,
  137. this.provider,
  138. this.externalUserId,
  139. this.externalData,
  140. (err, user) => {
  141. expect(user.thirdPartyIdentifiers.length).to.equal(1)
  142. return done()
  143. }
  144. )
  145. })
  146. // describe('when another account tries to link same provider/externalUserId', function() {
  147. // NOTE: Cannot run this test because we do not have indexes on the test DB
  148. // beforeEach(function(done) {
  149. // this.user2 = new User()
  150. // this.user2.ensureUserExists(done)
  151. // })
  152. // it('should not link provider', function(done) {
  153. // ThirdPartyIdentityManager.link(
  154. // this.user2.id,
  155. // this.provider,
  156. // this.externalUserId,
  157. // this.externalData,
  158. // (err, user) => {
  159. // expect(err.name).to.equal('ThirdPartyIdentityExistsError')
  160. // return done()
  161. // }
  162. // )
  163. // this.user2.full_delete_user(this.user2.email, done)
  164. // })
  165. // })
  166. })
  167. })
  168. describe('unlink', function() {
  169. describe('when provider not already linked', () =>
  170. it('should succeed', function(done) {
  171. return ThirdPartyIdentityManager.unlink(
  172. this.user.id,
  173. this.provider,
  174. function(err, res) {
  175. expect(err).to.be.null
  176. expect(res.thirdPartyIdentifiers.length).to.equal(0)
  177. return done()
  178. }
  179. )
  180. }))
  181. describe('when provider is already linked', function() {
  182. beforeEach(function(done) {
  183. return ThirdPartyIdentityManager.link(
  184. this.user.id,
  185. this.provider,
  186. this.externalUserId,
  187. this.externalData,
  188. done
  189. )
  190. })
  191. it('should remove thirdPartyIdentifiers entry', function(done) {
  192. return ThirdPartyIdentityManager.unlink(
  193. this.user.id,
  194. this.provider,
  195. (err, user) => {
  196. expect(user.thirdPartyIdentifiers.length).to.equal(0)
  197. return done()
  198. }
  199. )
  200. })
  201. })
  202. })
  203. })