delete affiliations on user deletion

This commit is contained in:
Tim Alby 2018-06-27 18:43:20 +02:00
parent 837f614df4
commit 1d80878bf8
4 changed files with 42 additions and 0 deletions

View file

@ -31,6 +31,14 @@ module.exports = UserAffiliationsManager =
}, callback
deleteAffiliations: (userId, callback = (error) ->) ->
makeAffiliationRequest {
method: 'DELETE'
path: "/api/v2/users/#{userId.toString()}/affiliations"
defaultErrorMessage: "Couldn't delete affiliations"
}, callback
makeAffiliationRequest = (requestOptions, callback = (error) ->) ->
return callback(null) unless settings?.apis?.v1?.url # service is not configured
requestOptions.extraSuccessStatusCodes ||= []

View file

@ -4,6 +4,7 @@ ProjectDeleter = require("../Project/ProjectDeleter")
logger = require("logger-sharelatex")
SubscriptionHandler = require("../Subscription/SubscriptionHandler")
async = require("async")
{ deleteAffiliations } = require("./UserAffiliationsManager")
module.exports =
@ -22,6 +23,8 @@ module.exports =
ProjectDeleter.deleteUsersProjects user._id, cb
(cb)->
SubscriptionHandler.cancelSubscription user, cb
(cb)->
deleteAffiliations user._id, cb
(cb)->
user.remove cb
], (err)->

View file

@ -103,3 +103,24 @@ describe "UserAffiliationsManager", ->
should.exist(err)
err.message.should.exist
done()
describe 'deleteAffiliations', ->
it 'delete affiliations', (done)->
@request.callsArgWith(1, null, { statusCode: 200 })
@UserAffiliationsManager.deleteAffiliations @stubbedUser._id, (err) =>
should.not.exist(err)
@request.calledOnce.should.equal true
requestOptions = @request.lastCall.args[0]
expectedUrl = "v1.url/api/v2/users/#{@stubbedUser._id}/affiliations"
requestOptions.url.should.equal expectedUrl
requestOptions.method.should.equal 'DELETE'
done()
it 'handle error', (done)->
body = errors: 'affiliation error message'
@request.callsArgWith(1, null, { statusCode: 518 }, body)
@UserAffiliationsManager.deleteAffiliations @stubbedUser._id, (err) =>
should.exist(err)
err.message.should.have.string 518
err.message.should.have.string body.errors
done()

View file

@ -23,11 +23,16 @@ describe "UserDeleter", ->
@SubscriptionHandler =
cancelSubscription: sinon.stub().callsArgWith(1)
@deleteAffiliations = sinon.stub().callsArgWith(1)
@UserDeleter = SandboxedModule.require modulePath, requires:
"../../models/User": User: @User
"../Newsletter/NewsletterManager": @NewsletterManager
"../Subscription/SubscriptionHandler": @SubscriptionHandler
"../Project/ProjectDeleter": @ProjectDeleter
"./UserAffiliationsManager":
deleteAffiliations: @deleteAffiliations
"logger-sharelatex": @logger = { log: sinon.stub() }
describe "deleteUser", ->
@ -52,3 +57,8 @@ describe "UserDeleter", ->
@UserDeleter.deleteUser @user._id, (err)=>
@SubscriptionHandler.cancelSubscription.calledWith(@user).should.equal true
done()
it "should delete user affiliations", (done)->
@UserDeleter.deleteUser @user._id, (err)=>
@deleteAffiliations.calledWith(@user._id).should.equal true
done()