UserThirdPartyIdentityTests.js 6.2 KB

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