GeoIpLookupTests.coffee 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. should = require('chai').should()
  2. SandboxedModule = require('sandboxed-module')
  3. assert = require('assert')
  4. path = require('path')
  5. sinon = require('sinon')
  6. modulePath = path.join __dirname, "../../../../app/js/infrastructure/GeoIpLookup"
  7. expect = require("chai").expect
  8. describe "GeoIpLookup", ->
  9. beforeEach ->
  10. @settings =
  11. apis:
  12. geoIpLookup:
  13. url:"http://lookup.com"
  14. @request =
  15. get: sinon.stub()
  16. @GeoIpLookup = SandboxedModule.require modulePath, requires:
  17. "request": @request
  18. "settings-sharelatex":@settings
  19. "logger-sharelatex":
  20. log:->
  21. err:->
  22. @ipAddress = "123.456.789.123"
  23. @stubbedResponse =
  24. "ip":@ipAddress
  25. "country_code":"GB"
  26. "country_name":"United Kingdom"
  27. "region_code":"H9"
  28. "region_name":"London, City of"
  29. "city":"London"
  30. "zipcode":"SE16"
  31. "latitude":51.0
  32. "longitude":-0.0493
  33. "metro_code":""
  34. "area_code":""
  35. describe "getDetails", ->
  36. beforeEach ->
  37. @request.get.callsArgWith(1, null, null, @stubbedResponse)
  38. it "should request the details using the ip", (done)->
  39. @GeoIpLookup.getDetails @ipAddress, (err)=>
  40. @request.get.calledWith({url:@settings.apis.geoIpLookup.url+"/"+@ipAddress, timeout:1000, json:true}).should.equal true
  41. done()
  42. it "should return the ip details", (done)->
  43. @GeoIpLookup.getDetails @ipAddress, (err, returnedDetails)=>
  44. assert.deepEqual returnedDetails, @stubbedResponse
  45. done()
  46. it "should take the first ip in the string", (done)->
  47. @GeoIpLookup.getDetails " #{@ipAddress} 456.312.452.102 432.433.888.234", (err)=>
  48. @request.get.calledWith({url:@settings.apis.geoIpLookup.url+"/"+@ipAddress, timeout:1000, json:true}).should.equal true
  49. done()
  50. describe "getCurrencyCode", ->
  51. it "should return GBP for GB country", (done)->
  52. @GeoIpLookup.getDetails = sinon.stub().callsArgWith(1, null, @stubbedResponse)
  53. @GeoIpLookup.getCurrencyCode @ipAddress, (err, currencyCode)->
  54. currencyCode.should.equal "GBP"
  55. done()
  56. it "should return GBP for gb country", (done)->
  57. @stubbedResponse.country_code = "gb"
  58. @GeoIpLookup.getDetails = sinon.stub().callsArgWith(1, null, @stubbedResponse)
  59. @GeoIpLookup.getCurrencyCode @ipAddress, (err, currencyCode)->
  60. currencyCode.should.equal "GBP"
  61. done()
  62. it "should return USD for US", (done)->
  63. @stubbedResponse.country_code = "US"
  64. @GeoIpLookup.getDetails = sinon.stub().callsArgWith(1, null, @stubbedResponse)
  65. @GeoIpLookup.getCurrencyCode @ipAddress, (err, currencyCode)->
  66. currencyCode.should.equal "USD"
  67. done()
  68. it "should return EUR for DE", (done)->
  69. @stubbedResponse.country_code = "DE"
  70. @GeoIpLookup.getDetails = sinon.stub().callsArgWith(1, null, @stubbedResponse)
  71. @GeoIpLookup.getCurrencyCode @ipAddress, (err, currencyCode)->
  72. currencyCode.should.equal "EUR"
  73. done()
  74. it "should default to USD if there is an error", (done)->
  75. @GeoIpLookup.getDetails = sinon.stub().callsArgWith(1, "problem")
  76. @GeoIpLookup.getCurrencyCode @ipAddress, (err, currencyCode)->
  77. currencyCode.should.equal "USD"
  78. done()
  79. it "should default to USD if there are no details", (done)->
  80. @GeoIpLookup.getDetails = sinon.stub().callsArgWith(1)
  81. @GeoIpLookup.getCurrencyCode @ipAddress, (err, currencyCode)->
  82. currencyCode.should.equal "USD"
  83. done()
  84. it "should default to USD if there is no match for their country", (done)->
  85. @stubbedResponse.country_code = "Non existant"
  86. @GeoIpLookup.getDetails = sinon.stub().callsArgWith(1, null, @stubbedResponse)
  87. @GeoIpLookup.getCurrencyCode @ipAddress, (err, currencyCode)->
  88. currencyCode.should.equal "USD"
  89. done()