TpdsPollingBackgroundTasksTests.coffee 2.2 KB

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