Skip to content

Commit

Permalink
Add test
Browse files Browse the repository at this point in the history
  • Loading branch information
yulii committed Dec 10, 2019
1 parent f454198 commit 73b193d
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 21 deletions.
12 changes: 4 additions & 8 deletions lib/circleci.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,7 @@ class CircleCI
_endpoint = "https://circleci.com/api/v1.1/project/#{@vcsType}/#{@owner}/#{@project}/tree/#{@branch}" unless _endpoint?
return _endpoint

notify: (destination) ->
@destination = destination
return @

execute: (robot) ->
execute: (robot, callback) ->
params = JSON.stringify(build_parameters: { CIRCLE_JOB: @job, JOB_USER: robot.name })

robot.http("#{@endpoint()}?circle-token=#{@token}")
Expand All @@ -34,15 +30,15 @@ class CircleCI
.post(params) (error, response, body) ->

if error
robot.send { room: @destination }, "Encountered an error :( `#{error}`"
callback("Encountered an error :( `#{error}`")
return

if response.statusCode < 200 or response.statusCode >= 300
robot.send { room: @destination }, "Request fail :( `#{response.statusCode}: #{response.statusMessage}`"
callback("Request fail :( `#{response.statusCode}: #{response.statusMessage}`")
return

result = JSON.parse(body)
robot.send { room: @destination }, "Created a new build! #{result.build_url}"
callback("Created a new build! #{result.build_url}")

_assert = ->
throw new Error('`owner` is required argument') unless @owner?
Expand Down
2 changes: 1 addition & 1 deletion scripts/ci.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ module.exports = (robot) ->
owner: 'yulii'
job: msg.match[1]
project: msg.match[2]
).notify("##{msg.envelope.room}").execute(robot)
).execute(robot, (message) -> msg.send(message))
catch error
msg.send "#{error.name}: #{error.message}"
8 changes: 4 additions & 4 deletions scripts/webhook.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ module.exports = (robot) ->
['hubot-scripts', 'yulii.github.io'].forEach (name) ->
try
new CircleCI(
owner: 'yulii'
project: name
job: 'update'
).notify('#devops').execute(robot)
owner: 'yulii', project: name, job: 'update'
).execute(robot, (message) ->
robot.send { room: '#devops' }, message
)
catch error
robot.send { room: '#devops' }, "#{error.name}: #{error.message}"

Expand Down
6 changes: 3 additions & 3 deletions test/lib/alpha_vantage.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@ describe 'AlphaVantage', ->

describe 'when response is successful', ->
it 'calls callback with a message object ', () ->
http_stub((f) -> f('error', 'response', helper.fixture.time_series_daily))
http_stub((f) -> f(null, 'response', helper.fixture.time_series_daily))

new AlphaVantage(function: 'FUNCTION', symbol: 'SYMBOL').execute(robot, (message) ->
expect(message).to.be.an.instanceof(AlphaVantageSlackMessage)
)

describe 'when response is failure', ->
it 'calls callback with a message object ', () ->
http_stub((f) -> f('error', 'response', helper.fixture.error_message))
it 'calls callback with a message object', () ->
http_stub((f) -> f(null, 'response', helper.fixture.error_message))

new AlphaVantage(function: 'FUNCTION', symbol: 'SYMBOL').execute(robot, (message) ->
expect(message).to.be.an.instanceof(AlphaVantageErrorMessage)
Expand Down
40 changes: 35 additions & 5 deletions test/lib/circleci.coffee
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
helper = require('../test_helper')
expect = require('chai').expect
sinon = require('sinon')

Robot = require('hubot/src/robot')
CircleCI = helper.require('circleci')
describe 'CircleCI', ->

Expand Down Expand Up @@ -60,8 +62,36 @@ describe 'CircleCI', ->
ci = new CircleCI(owner: 'yulii', job: 'test', project: 'mocha', token: 'circle-token')
expect(ci.endpoint()).to.equal('https://circleci.com/api/v1.1/project/github/yulii/mocha/tree/master')

describe '#notify', ->
it 'sets `destination` property', () ->
ci = new CircleCI(owner: 'yulii', job: 'test', project: 'mocha', token: 'circle-token').notify('address')
expect(ci).to.be.an.instanceof(CircleCI)
expect(ci).to.have.property('destination', 'address')
describe '#execute', ->
robot = undefined
http_stub = undefined

beforeEach ->
process.env.CIRCLE_TOKEN_EXECUTE = 'circle-project-token'
robot = new Robot(null, 'mock-adapter', false, 'hubot')
http_stub = (callback) ->
sinon.stub(robot, 'http').returns(
header: sinon.stub().returnsThis()
post: sinon.stub().callsFake((_) -> return callback)
)

afterEach ->
delete process.env.CIRCLE_TOKEN_EXECUTE
robot.http.restore()

describe 'when response is successful', ->
it 'calls callback with a string ', () ->
http_stub((f) -> f(null, 'response', '{ "build_url": "https://build_url" }'))

new CircleCI(owner: 'yulii', job: 'test', project: 'execute').execute(robot, (message) ->
expect(message).to.equal('Created a new build! https://build_url')
)

describe 'when response is failure', ->
it 'calls callback with a string', () ->
response = { statusCode: 404, statusMessage: 'Not Found' }
http_stub((f) -> f(null, response, 'body'))

new CircleCI(owner: 'yulii', job: 'test', project: 'execute').execute(robot, (message) ->
expect(message).to.equal("Request fail :( `#{response.statusCode}: #{response.statusMessage}`")
)

0 comments on commit 73b193d

Please sign in to comment.