Skip to content
Browse files

Add in proper JSON support with matching test for post handling

- 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...
1 parent f9b7670 commit 5a2ec3b6648963ef7e4a99740768c3137e240749 @queso queso committed Feb 9, 2011
Showing with 93 additions and 11 deletions.
  1. +9 −8 lib/cijoe/server.rb
  2. +1 −1 lib/cijoe/views/template.erb
  3. +52 −0 test/fixtures/payload.json
  4. +31 −2 test/test_cijoe_server.rb
View
17 lib/cijoe/server.rb
@@ -1,5 +1,6 @@
require 'sinatra/base'
require 'erb'
+require 'json'
class CIJoe
class Server < Sinatra::Base
@@ -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
View
2 lib/cijoe/views/template.erb
@@ -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 %>
View
52 test/fixtures/payload.json
@@ -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
+ }
+}
View
33 test/test_cijoe_server.rb
@@ -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
@@ -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"
@@ -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

0 comments on commit 5a2ec3b

Please sign in to comment.
Something went wrong with that request. Please try again.