Skip to content

Commit

Permalink
Adding json responses in servers_controller. Refs issue 143.
Browse files Browse the repository at this point in the history
  • Loading branch information
daronco committed Jun 7, 2011
1 parent 3178a9f commit d82407f
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 11 deletions.
11 changes: 10 additions & 1 deletion app/controllers/bigbluebutton/servers_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
class Bigbluebutton::ServersController < ApplicationController

respond_to :html
respond_to :json, :only => [:index, :show, :new, :create, :update, :destroy]

def index
respond_with(@servers = BigbluebuttonServer.all)
Expand All @@ -27,8 +28,10 @@ def create
message = t('bigbluebutton_rails.servers.notice.create.success')
redirect_to(@server, :notice => message)
}
format.json { render :json => @server, :status => :created }
else
format.html { render :action => "new" }
format.json { render :json => @server.errors, :status => :unprocessable_entity }
end
end
end
Expand All @@ -42,15 +45,21 @@ def update
message = t('bigbluebutton_rails.servers.notice.update.success')
redirect_to(@server, :notice => message)
}
format.json { head :ok }
else
format.html { render :action => "edit" }
format.json { render :json => @server.errors, :status => :unprocessable_entity }
end
end
end

def destroy
@server = BigbluebuttonServer.find(params[:id])
@server.destroy
redirect_to(bigbluebutton_servers_url)

respond_with do |format|
format.html { redirect_to(bigbluebutton_servers_url) }
format.json { head :ok }
end
end
end
20 changes: 10 additions & 10 deletions spec/controllers/bigbluebutton/rooms_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -677,11 +677,15 @@ def build_running_json(value, error=nil)

# verify all JSON responses
context "json responses for " do
describe "#show" do
before(:each) { get :show, :server_id => server.to_param, :id => room.to_param, :format => 'json' }
describe "#index" do
before do
@room1 = Factory.create(:bigbluebutton_room, :server => server)
@room2 = Factory.create(:bigbluebutton_room, :server => server)
end
before(:each) { get :index, :server_id => server.to_param, :format => 'json' }
it { should respond_with(:success) }
it { should respond_with_content_type(:json) }
it { response.body.should == room.to_json }
it { should respond_with_json([@room1, @room2].to_json) }
end

describe "#new" do
Expand All @@ -695,15 +699,11 @@ def build_running_json(value, error=nil)
}
end

describe "#index" do
before do
@room1 = Factory.create(:bigbluebutton_room, :server => server)
@room2 = Factory.create(:bigbluebutton_room, :server => server)
end
before(:each) { get :index, :server_id => server.to_param, :format => 'json' }
describe "#show" do
before(:each) { get :show, :server_id => server.to_param, :id => room.to_param, :format => 'json' }
it { should respond_with(:success) }
it { should respond_with_content_type(:json) }
it { should respond_with_json([@room1, @room2].to_json) }
it { should respond_with_json(room.to_json) }
end

describe "#create" do
Expand Down
91 changes: 91 additions & 0 deletions spec/controllers/bigbluebutton/servers_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,5 +74,96 @@
}
end

# verify all JSON responses
context "json responses for " do

describe "#index" do
before do
@server1 = Factory.create(:bigbluebutton_server)
@server2 = Factory.create(:bigbluebutton_server)
end
before(:each) { get :index, :format => 'json' }
it { should respond_with(:success) }
it { should respond_with_content_type(:json) }
it { should respond_with_json([@server1, @server2].to_json) }
end

describe "#new" do
before(:each) { get :new, :format => 'json' }
it { should respond_with(:success) }
it { should respond_with_content_type(:json) }
it { should respond_with_json(BigbluebuttonServer.new.to_json).ignoring_values }
end

describe "#show" do
before(:each) { get :show, :id => server.to_param, :format => 'json' }
it { should respond_with(:success) }
it { should respond_with_content_type(:json) }
it { should respond_with_json(server.to_json) }
end

describe "#create" do
let(:new_server) { Factory.build(:bigbluebutton_server) }

context "on success" do
before(:each) {
post :create, :bigbluebutton_server => new_server.attributes, :format => 'json'
}
it { should respond_with(:created) }
it { should respond_with_content_type(:json) }
it { should respond_with_json(new_server.to_json).ignoring_attributes }
end

context "on failure" do
before(:each) {
new_server.url = nil # invalid
post :create, :bigbluebutton_server => new_server.attributes, :format => 'json'
}
it { should respond_with(:unprocessable_entity) }
it { should respond_with_content_type(:json) }
it {
new_server.save # should fail
should respond_with_json(new_server.errors.to_json)
}
end
end

describe "#update" do
let(:new_server) { Factory.build(:bigbluebutton_server) }
before { @server = server }

context "on success" do
before(:each) {
put :update, :id => @server.to_param, :bigbluebutton_server => new_server.attributes, :format => 'json'
}
it { should respond_with(:success) }
it { should respond_with_content_type(:json) }
end

context "on failure" do
before(:each) {
new_server.url = nil # invalid
put :update, :id => @server.to_param, :bigbluebutton_server => new_server.attributes, :format => 'json'
}
it { should respond_with(:unprocessable_entity) }
it { should respond_with_content_type(:json) }
it {
new_server.save # should fail
should respond_with_json(new_server.errors.to_json)
}
end
end

describe "#destroy" do
before :each do
@server = server
delete :destroy, :id => @server.to_param, :format => 'json'
end
it { should respond_with(:success) }
it { should respond_with_content_type(:json) }
end

end # json responses

end

0 comments on commit d82407f

Please sign in to comment.