TpdsPollingBackgroundTasksTests.coffee 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. SandboxedModule = require('sandboxed-module')
  2. assert = require('assert')
  3. require('chai').should()
  4. sinon = require('sinon')
  5. modulePath = require('path').join __dirname, '../../../../app/js/Features/ThirdPartyDataStore/TpdsPollingBackgroundTasks.js'
  6. describe 'third party data store', ->
  7. beforeEach ->
  8. @request = post:sinon.stub().callsArgWith(1, null)
  9. @userModel = {}
  10. @redis = auth:->
  11. @settings =
  12. apis:
  13. thirdPartyDataStore: {url: "http://tpds.com"}
  14. redis:
  15. web:{}
  16. @poller = SandboxedModule.require modulePath, requires:
  17. "redis":createClient:=>@redis
  18. "settings-sharelatex":@settings
  19. "request":@request
  20. '../../models/User':User:@userModel
  21. 'logger-sharelatex':
  22. log:->
  23. err:->
  24. describe "polling user have dropbox", ->
  25. it 'should find the users with project', (done)->
  26. users = [{_id:"1234"}, {_id:"213oija"}, {_id:"2iojdsjoidsk"}]
  27. @userModel.find = sinon.stub().callsArgWith(2, null, users)
  28. @poller._sendToTpds = sinon.stub().callsArgWith(1, null)
  29. @poller._markPollHappened = sinon.stub()
  30. @poller.pollUsersWithDropbox (err)=>
  31. @userModel.find.calledWith({"dropbox.access_token.oauth_token_secret":{"$exists":true}, "features.dropbox":true}, "_id").should.equal true
  32. @poller._sendToTpds.calledWith([users[0]._id, users[1]._id, users[2]._id,]).should.equal true
  33. @poller._markPollHappened.called.should.equal true
  34. done()
  35. describe "sending user ids to tpds", ->
  36. it 'should put it into json and post it over', (done)->
  37. users = [{_id:"1234"}, {_id:"213oija"}, {_id:"2iojdsjoidsk"}]
  38. @poller._sendToTpds users, =>
  39. @request.post.calledWith({uri:"#{@settings.apis.thirdPartyDataStore.url}/user/poll", json:{user_ids:users}}).should.equal true
  40. done()
  41. describe "marking the last time the poller was made and get it out again", ->
  42. it "should put the date into redis", (done)->
  43. @redis.set = sinon.stub().callsArgWith(2)
  44. @poller._markPollHappened (err)=>
  45. @redis.set.calledWith("LAST_TIME_POLL_HAPPEND_KEY").should.equal true
  46. done()
  47. it "should get the date from redis", (done)->
  48. date = new Date()
  49. @redis.get = sinon.stub().callsArgWith(1, null, date)
  50. @poller.getLastTimePollHappned (err, lastTime)->
  51. lastTime.should.equal date
  52. done()