diff --git a/services/web/app/coffee/infrastructure/GeoIpLookup.coffee b/services/web/app/coffee/infrastructure/GeoIpLookup.coffee new file mode 100644 index 0000000000..7942c3eba4 --- /dev/null +++ b/services/web/app/coffee/infrastructure/GeoIpLookup.coffee @@ -0,0 +1,29 @@ +request = require("request") +settings = require("settings-sharelatex") +_ = require("underscore") + +currencyMappings = { + "GB":"GBP" + "US":"USD" +} + +# Countries which would likely prefer Euro's +EuroCountries = ["AT", "BE", "BG", "HR", "CY", "CZ", +"DK", "EE", "FI", "FR", "DE", "EL", "HU", "IE", +"IT", "LV", "LT", "LU", "MT", "NL", "PL", "PT", +"RO", "SK", "SI", "ES", "SE"] + +_.each EuroCountries, (country)-> currencyMappings[country] = "EUR" + +module.exports = GeoIpLookup = + + getDetails : (ip, callback)-> + opts = + url: "#{settings.apis.geoIpLookup.url}/#{ip}" + request.get opts, (err, ipDetails)-> + callback(err, ipDetails) + + getCurrencyCode : (ip, callback)-> + GeoIpLookup.getDetails ip, (err, ipDetails)-> + currencyCode = currencyMappings[ipDetails?.country_code?.toUpperCase()] + callback(err, currencyCode) \ No newline at end of file diff --git a/services/web/test/UnitTests/coffee/infrastructure/GeoIpLookupTests.coffee b/services/web/test/UnitTests/coffee/infrastructure/GeoIpLookupTests.coffee new file mode 100644 index 0000000000..5a2a15480f --- /dev/null +++ b/services/web/test/UnitTests/coffee/infrastructure/GeoIpLookupTests.coffee @@ -0,0 +1,79 @@ +should = require('chai').should() +SandboxedModule = require('sandboxed-module') +assert = require('assert') +path = require('path') +sinon = require('sinon') +modulePath = path.join __dirname, "../../../../app/js/infrastructure/GeoIpLookup" +expect = require("chai").expect + +describe "GeoIpLookup", -> + + beforeEach -> + + @settings = + apis: + geoIpLookup: + url:"http://lookup.com" + @request = + get: sinon.stub() + @GeoIpLookup = SandboxedModule.require modulePath, requires: + "request": @request + "settings-sharelatex":@settings + "logger-sharelatex": log:-> + @ipAddress = "123.456.789.123" + + @stubbedResponse = + "ip":@ipAddress + "country_code":"GB" + "country_name":"United Kingdom" + "region_code":"H9" + "region_name":"London, City of" + "city":"London" + "zipcode":"SE16" + "latitude":51.0 + "longitude":-0.0493 + "metro_code":"" + "area_code":"" + + describe "getDetails", -> + beforeEach -> + @request.get.callsArgWith(1, null, @stubbedResponse) + + it "should request the details using the ip", (done)-> + @GeoIpLookup.getDetails @ipAddress, (err)=> + @request.get.calledWith(url:@settings.apis.geoIpLookup.url+"/"+@ipAddress).should.equal true + done() + + it "should return the ip details", (done)-> + @GeoIpLookup.getDetails @ipAddress, (err, returnedDetails)=> + assert.deepEqual returnedDetails, @stubbedResponse + done() + + describe "getCurrencyCode", -> + + it "should return GBP for GB country", (done)-> + @GeoIpLookup.getDetails = sinon.stub().callsArgWith(1, null, @stubbedResponse) + @GeoIpLookup.getCurrencyCode @ipAddress, (err, currencyCode)-> + currencyCode.should.equal "GBP" + done() + + it "should return GBP for gb country", (done)-> + @stubbedResponse.country_code = "gb" + @GeoIpLookup.getDetails = sinon.stub().callsArgWith(1, null, @stubbedResponse) + @GeoIpLookup.getCurrencyCode @ipAddress, (err, currencyCode)-> + currencyCode.should.equal "GBP" + done() + + it "should return USD for US", (done)-> + @stubbedResponse.country_code = "US" + @GeoIpLookup.getDetails = sinon.stub().callsArgWith(1, null, @stubbedResponse) + @GeoIpLookup.getCurrencyCode @ipAddress, (err, currencyCode)-> + currencyCode.should.equal "USD" + done() + + it "should return EUR for DE", (done)-> + @stubbedResponse.country_code = "DE" + @GeoIpLookup.getDetails = sinon.stub().callsArgWith(1, null, @stubbedResponse) + @GeoIpLookup.getCurrencyCode @ipAddress, (err, currencyCode)-> + currencyCode.should.equal "EUR" + done()