hubot-ingress
Version:
Ingress commands for Hubot
90 lines (80 loc) • 3.11 kB
text/coffeescript
chai = require 'chai'
sinon = require 'sinon'
chai.use require 'sinon-chai'
expect = chai.expect
describe 'ingress: intelmap', ->
user =
name: 'sinon'
id: 'U123'
robot =
respond: sinon.spy()
hear: sinon.spy()
brain:
on: (_, cb) ->
cb()
data: {}
userForName: (who) ->
forName =
name: who
id: 'U234'
httpError =
location: "http_error"
expected: "this error is used"
parseError =
location: "parse_error"
httpResponseNoKey =
body: '{"results": [{ "formatted_address" : "Some, Formatted, Address", "geometry": { "location": { "lat": "LatNoKey", "lng": "LongNoKey" } } }]}'
formatted_address: "Some, Formatted, Address"
lat: "LatNoKey"
long: "LongNoKey"
httpResponseKey =
expectedKey: 'ExpectedGeocodeKey'
body: '{"results": [{ "formatted_address" : "Some, Formatted, Address", "geometry": { "location": { "lat": "LatKey", "lng": "LongKey" } } }]}'
formatted_address: "Some, Formatted, Address"
lat: "LatKey"
long: "LongKey"
beforeEach ->
= user
= robot
= .brain.data
=
send: sinon.spy()
reply: sinon.spy()
envelope:
user:
message:
user:
http: (url) ->
query: (params) ->
get: () -> (cb) ->
return switch params.address
when httpError.location
cb(httpError.expected, null, httpResponseNoKey.body)
when parseError.location
cb("this is ignored", null, '{ blahblah }')
else
if params.key == httpResponseKey.expectedKey
cb(null, null, httpResponseKey.body)
else
cb(null, null, httpResponseNoKey.body)
.googleGeocodeKey = undefined
require('../src/intel-map')(robot)
it 'responds to intelmap when http request results in error', ->
.match = [0, 'intelmap', httpError.location]
.respond.args[0][1]()
expect(.send).to.have.been.calledWith(httpError.expected)
it 'responds to intelmap with not found when there is a different error', ->
.match = [0, 'intelmap', parseError.location]
.respond.args[0][1]()
expect(.send).to.have.been.calledWith("Could not find #{parseError.location}")
it 'responds to intelmap with url when no Google Geocode API key set', ->
.match = [0, 'intelmap', 'boston ma']
.respond.args[0][1]()
expect(.send).to.have.been.calledWith("#{httpResponseKey.formatted_address}\nhttps://www.ingress.com/intel?ll=#{httpResponseNoKey.lat},#{httpResponseNoKey.long}&z=16")
it 'responds to intelmap with url when Google Geocode API key set', ->
.match = [0, 'intelmap', 'boston ma']
.googleGeocodeKey = httpResponseKey.expectedKey
.respond.args[0][1]()
expect(.send).to.have.been.calledWith("#{httpResponseKey.formatted_address}\nhttps://www.ingress.com/intel?ll=#{httpResponseKey.lat},#{httpResponseKey.long}&z=16")