Skip to content

Commit

Permalink
Add in proper JSON support with matching test for post handling
Browse files Browse the repository at this point in the history
- Add test/fixtures/payload.json from actual github payload
- Add a test for post "/?" method
- Remove the payload.empty? build logic from the post method,
  as it causes everything to run when it gets a post
  • Loading branch information
queso committed Apr 19, 2011
1 parent f9b7670 commit 5a2ec3b
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 11 deletions.
17 changes: 9 additions & 8 deletions lib/cijoe/server.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require 'sinatra/base'
require 'erb'
require 'json'

class CIJoe
class Server < Sinatra::Base
Expand Down Expand Up @@ -27,15 +28,15 @@ class Server < Sinatra::Base
end

post '/?' do
payload = params[:payload].to_s
if payload =~ /"ref":"(.+?)"/
pushed_branch = $1.split('/').last
unless params[:rebuild]
payload = JSON.parse(params[:payload])
pushed_branch = payload["ref"].split('/').last
end

# Only build if we were given an explicit branch via `?branch=blah`,
# no payload exists (we're probably testing), or the payload exists and
# the "ref" property matches our specified build branch.
if params[:branch] || payload.empty? || pushed_branch == joe.git_branch
# Only build if we were given an explicit branch via `?branch=blah`
# or the payload exists and the "ref" property matches our
# specified build branch.
if params[:branch] || params[:rebuild] || pushed_branch == joe.git_branch
joe.build(params[:branch])
end

Expand Down
2 changes: 1 addition & 1 deletion lib/cijoe/views/template.erb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
<% end %>
</li>
<% else %>
<li><form method="POST"><input type="submit" value="Build"/></form></li>
<li><form method="POST"><input type="hidden", name="rebuild" value="true"><input type="submit" value="Build"/></form></li>
<% end %>
<% if joe.last_build %>
Expand Down
52 changes: 52 additions & 0 deletions test/fixtures/payload.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
{
"after": "416cb2f7105e7f989bc223d1a975b93a6491b276",
"before": "0ae4da1eea2d191b33ebfbd5db48e3c1f91953ad",
"commits": [
{
"added": [

],
"author": {
"email": "joshua.owens@gmail.com",
"name": "Josh Owens",
"username": "queso"
},
"id": "09293a1703b3bdb36aba70d38abd5e44396c50a5",
"message": "Update README",
"modified": [
"README.textile"
],
"removed": [

],
"timestamp": "2011-02-07T15:27:35-08:00",
"url": "https:\/\/github.com\/fourbeansoup\/broth\/commit\/09293a1703b3bdb36aba70d38abd5e44396c50a5"
}
],
"compare": "https:\/\/github.com\/fourbeansoup\/broth\/compare\/0ae4da1...416cb2f",
"forced": false,
"ref": "refs\/heads\/master",
"repository": {
"created_at": "2009\/12\/05 10:44:57 -0800",
"description": "FourBeanSoup's Broth Application, a tasty starting point for every app",
"fork": true,
"forks": 4,
"has_downloads": true,
"has_issues": true,
"has_wiki": true,
"homepage": "",
"language": "JavaScript",
"name": "broth",
"open_issues": 2,
"organization": "fourbeansoup",
"owner": {
"email": "josh+scm@fourbeansoup.com",
"name": "fourbeansoup"
},
"private": false,
"pushed_at": "2011\/02\/07 17:17:00 -0800",
"size": 604,
"url": "https:\/\/github.com\/fourbeansoup\/broth",
"watchers": 10
}
}
33 changes: 31 additions & 2 deletions test/test_cijoe_server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,17 @@ class ::CIJoe

def setup
@app = CIJoe::Server.new
joe = @app.joe

# make Build#restore a no-op so we don't overwrite our current/last
# build attributes set from tests.
joe = @app.joe
def joe.restore
end

# make CIJoe#build! and CIJoe#git_update a no-op so we don't overwrite our local changes
# or local commits nor should we run tests.
def joe.build!
end
end

def test_ping
Expand Down Expand Up @@ -64,6 +70,30 @@ def test_ping_should_not_reset_current_build_in_tests
assert_equal current_build, app.joe.current_build
end

def test_post_with_json_works
post '/', :payload => File.read("#{Dir.pwd}/test/fixtures/payload.json")
assert app.joe.building?
assert_equal 302, last_response.status
end

def test_post_does_not_build_on_branch_mismatch
post "/", :payload => {"ref" => "refs/heads/dont_build"}.to_json
assert !app.joe.building?
assert_equal 302, last_response.status
end

def test_post_does_build_on_branch_match
post "/", :payload => {"ref" => "refs/heads/master"}.to_json
assert app.joe.building?
assert_equal 302, last_response.status
end

def test_post_does_build_when_build_button_is_used
post "/", :rebuild => true
assert app.joe.building?
assert_equal 302, last_response.status
end

def test_jsonp_should_return_plain_json_without_param
app.joe.last_build = build :failed
get "/api/json"
Expand All @@ -74,7 +104,6 @@ def test_jsonp_should_return_plain_json_without_param
def test_jsonp_should_return_jsonp_with_param
app.joe.last_build = build :failed
get "/api/json?jsonp=fooberz"
puts last_response.inspect
assert_equal 200, last_response.status
assert_equal 'application/json', last_response.content_type
assert_match /^fooberz\(/, last_response.body
Expand Down

0 comments on commit 5a2ec3b

Please sign in to comment.