Skip to content

Commit

Permalink
DRY up api tests for rubygems + api_keys controllers
Browse files Browse the repository at this point in the history
add missing tests for yaml + json to rubygems#index
  • Loading branch information
cldwalker committed Aug 29, 2011
1 parent 25ba0d0 commit 39d99be
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 159 deletions.
74 changes: 29 additions & 45 deletions test/functional/api/v1/api_keys_controller_test.rb
Expand Up @@ -22,11 +22,14 @@ class Api::V1::ApiKeysControllerTest < ActionController::TestCase
end
end

def authorize_with(str)
@request.env["HTTP_AUTHORIZATION"] = "Basic " + Base64::encode64(str)
end

context "on GET to show with unconfirmed user" do
setup do
@user = Factory(:user)
@request.env["HTTP_AUTHORIZATION"] = "Basic " +
Base64::encode64("#{@user.email}:#{@user.password}")
authorize_with("#{@user.email}:#{@user.password}")
get :show
end
should "deny access" do
Expand All @@ -38,8 +41,7 @@ class Api::V1::ApiKeysControllerTest < ActionController::TestCase
context "on GET to show with bad credentials" do
setup do
@user = Factory(:user)
@request.env["HTTP_AUTHORIZATION"] = "Basic " +
Base64::encode64("bad:creds")
authorize_with("bad:creds")
get :show
end
should "deny access" do
Expand All @@ -51,8 +53,7 @@ class Api::V1::ApiKeysControllerTest < ActionController::TestCase
context "on GET to show with confirmed user" do
setup do
@user = Factory(:email_confirmed_user)
@request.env["HTTP_AUTHORIZATION"] = "Basic " +
Base64::encode64("#{@user.email}:#{@user.password}")
authorize_with("#{@user.email}:#{@user.password}")
get :show
end
should respond_with :success
Expand All @@ -61,51 +62,34 @@ class Api::V1::ApiKeysControllerTest < ActionController::TestCase
end
end

context "on GET to show with JSON with confirmed user" do
setup do
@user = Factory(:email_confirmed_user)
@request.env["HTTP_AUTHORIZATION"] = "Basic " +
Base64::encode64("#{@user.email}:#{@user.password}")
get :show, :format => 'json'
end
should respond_with :success
should "return API key" do
response = JSON.parse(@response.body)
assert_not_nil response
assert_kind_of Hash, response
assert_equal @user.api_key, response["rubygems_api_key"]
def self.should_respond_to(format, to_meth = :to_s)
context "with #{format.to_s.upcase} with confirmed user" do
setup do
@user = Factory(:email_confirmed_user)
authorize_with("#{@user.email}:#{@user.password}")
get :show, :format => format
end
should respond_with :success
should "return API key" do
response = yield(@response.body)
assert_not_nil response
assert_kind_of Hash, response
assert_equal @user.api_key, response["rubygems_api_key".send(to_meth)]
end
end
end

context "on GET to show with XML with confirmed user" do
setup do
@user = Factory(:email_confirmed_user)
@request.env["HTTP_AUTHORIZATION"] = "Basic " +
Base64::encode64("#{@user.email}:#{@user.password}")
get :show, :format => 'xml'
context "on GET to show" do
should_respond_to(:json) do |body|
JSON.parse body
end
should respond_with :success
should "return API key" do
response = Nokogiri.parse(@response.body).root
assert_not_nil response
assert_kind_of Nokogiri::XML::Element, response
assert_equal @user.api_key, response.children[1].text
end
end

context "on GET to show with YAML with confirmed user" do
setup do
@user = Factory(:email_confirmed_user)
@request.env["HTTP_AUTHORIZATION"] = "Basic " +
Base64::encode64("#{@user.email}:#{@user.password}")
get :show, :format => 'yaml'
should_respond_to(:yaml, :to_sym) do |body|
YAML.load body
end
should respond_with :success
should "return API key" do
response = YAML.load(@response.body)
assert_not_nil response
assert_kind_of Hash, response
assert_equal @user.api_key, response[:rubygems_api_key]

should_respond_to(:xml) do |body|
Hash.from_xml(Nokogiri.parse(body).to_xml)['hash']
end
end

Expand Down
190 changes: 76 additions & 114 deletions test/functional/api/v1/rubygems_controller_test.rb
Expand Up @@ -9,119 +9,65 @@ class Api::V1::RubygemsControllerTest < ActionController::TestCase
assert_recognizes(post_route, :path => '/api/v1/gems', :method => :post)
end

context "When logged in" do
setup do
@user = Factory(:email_confirmed_user)
sign_in_as(@user)
end

context "On GET to show with JSON for a gem that's hosted" do
setup do
@rubygem = Factory(:rubygem, :name => "foo")
Factory(:version, :rubygem => @rubygem)
get :show, :id => @rubygem.to_param, :format => "json"
end

should assign_to(:rubygem) { @rubygem }
should respond_with :success
should "return a JSON hash" do
response = JSON.parse(@response.body)
assert_not_nil response
assert_kind_of Hash, response
end
end

context "On GET to show with JSON for a gem that's hosted with a period in its name" do
setup do
@rubygem = Factory(:rubygem, :name => "foo.rb")
Factory(:version, :rubygem => @rubygem)
get :show, :id => @rubygem.to_param, :format => "json"
end

should assign_to(:rubygem) { @rubygem }
should respond_with :success
should "return a JSON hash" do
response = JSON.parse(@response.body)
assert_not_nil response
assert_kind_of Hash, response
end
def self.should_respond_to_show(format, &block)
should assign_to(:rubygem) { @rubygem }
should respond_with :success
should "return a hash" do
response = yield(@response.body)
assert_not_nil response
assert_kind_of Hash, response
end
end

context "On GET to show with XML for a gem that's hosted" do
def self.should_respond_to(format, &block)
context "with #{format.to_s.upcase} for a hosted gem" do
setup do
@rubygem = Factory(:rubygem)
Factory(:version, :rubygem => @rubygem)
get :show, :id => @rubygem.to_param, :format => "xml"
get :show, :id => @rubygem.to_param, :format => format
end

should assign_to(:rubygem) { @rubygem }
should respond_with :success
should "return an XML document" do
response = Nokogiri.parse(@response.body).root
assert_not_nil response
assert_kind_of Nokogiri::XML::Element, response
end
should_respond_to_show(format, &block)
end

context "On GET to show with XML for a gem that's hosted with a period in its name" do
context "with #{format.to_s.upcase} for a hosted gem with a period in its name" do
setup do
@rubygem = Factory(:rubygem, :name => "foo.rb")
@rubygem = Factory(:rubygem, :name => 'foo.rb')
Factory(:version, :rubygem => @rubygem)
get :show, :id => @rubygem.to_param, :format => "xml"
get :show, :id => @rubygem.to_param, :format => format
end

should assign_to(:rubygem) { @rubygem }
should respond_with :success
should "return an XML document" do
response = Nokogiri.parse(@response.body).root
assert_not_nil response
assert_kind_of Nokogiri::XML::Element, response
end
should_respond_to_show(format, &block)
end

context "On GET to show with YAML for a gem that's hosted" do
context "with #{format.to_s.upcase} for a gem that doesn't match the slug" do
setup do
@rubygem = Factory(:rubygem)
@rubygem = Factory(:rubygem, :name => "ZenTest", :slug => "zentest")
Factory(:version, :rubygem => @rubygem)
get :show, :id => @rubygem.to_param, :format => "yaml"
get :show, :id => "ZenTest", :format => format
end

should assign_to(:rubygem) { @rubygem }
should respond_with :success
should "return a YAML hash" do
response = YAML.load(@response.body)
assert_not_nil response
assert_kind_of Hash, response
end
should_respond_to_show(format, &block)
end
end

context "On GET to show with YAML for a gem that's hosted with a period in its name" do
setup do
@rubygem = Factory(:rubygem, :name => "foo.rb")
Factory(:version, :rubygem => @rubygem)
get :show, :id => @rubygem.to_param, :format => "yaml"
end
context "When logged in" do
setup do
@user = Factory(:email_confirmed_user)
sign_in_as(@user)
end

should assign_to(:rubygem) { @rubygem }
should respond_with :success
should "return an YAML hash" do
response = YAML.load(@response.body)
assert_not_nil response
assert_kind_of Hash, response
context "On GET to show" do
should_respond_to(:json) do |body|
JSON.parse body
end
end

context "On GET to show for a gem that doesn't match the slug" do
setup do
@rubygem = Factory(:rubygem, :name => "ZenTest", :slug => "zentest")
Factory(:version, :rubygem => @rubygem)
get :show, :id => "ZenTest", :format => "json"
should_respond_to(:yaml) do |body|
YAML.load body
end

should assign_to(:rubygem) { @rubygem }
should respond_with :success
should "return a JSON hash" do
assert_not_nil JSON.parse(@response.body)
should_respond_to(:xml) do |body|
Hash.from_xml(Nokogiri.parse(body).to_xml)
end
end

Expand Down Expand Up @@ -153,12 +99,55 @@ class Api::V1::RubygemsControllerTest < ActionController::TestCase
end
end

def self.should_respond_to(format)
context "with #{format.to_s.upcase} for a list of gems" do
setup do
@mygems = [ Factory(:rubygem, :name => "SomeGem"), Factory(:rubygem, :name => "AnotherGem") ]
@mygems.each do |rubygem|
Factory(:version, :rubygem => rubygem)
Factory(:ownership, :user => @user, :rubygem => rubygem, :approved => true)
end

@other_user = Factory(:email_confirmed_user)
@not_my_rubygem = Factory(:rubygem, :name => "NotMyGem")
Factory(:version, :rubygem => @not_my_rubygem)
Factory(:ownership, :user => @other_user, :rubygem => @not_my_rubygem, :approved => true)

get :index, :format => format
end

should assign_to(:rubygems) { [@rubygem] }
should respond_with :success
should "return a hash" do
assert_not_nil yield(@response.body)
end
should "only return my gems" do
gem_names = yield(@response.body).map { |rubygem| rubygem['name'] }.sort
assert_equal ["AnotherGem", "SomeGem"], gem_names
end
end
end

context "with a confirmed user authenticated" do
setup do
@user = Factory(:email_confirmed_user)
@request.env["HTTP_AUTHORIZATION"] = @user.api_key
end

context "On GET to index" do
should_respond_to :json do |body|
JSON.parse body
end

should_respond_to :yaml do |body|
YAML.load body
end

should_respond_to :xml do |body|
Hash.from_xml(Nokogiri.parse(body).to_xml)['rubygems']
end
end

context "On POST to create for new gem" do
setup do
@request.env["RAW_POST_DATA"] = gem_file.read
Expand Down Expand Up @@ -385,33 +374,6 @@ class Api::V1::RubygemsControllerTest < ActionController::TestCase
should respond_with :unprocessable_entity
end
end

context "On GET to index with JSON for a list of owned gems" do
setup do
@mygems = [ Factory(:rubygem, :name => "SomeGem"), Factory(:rubygem, :name => "AnotherGem") ]
@mygems.each do |rubygem|
Factory(:version, :rubygem => rubygem)
Factory(:ownership, :user => @user, :rubygem => rubygem, :approved => true)
end

@other_user = Factory(:email_confirmed_user)
@not_my_rubygem = Factory(:rubygem, :name => "NotMyGem")
Factory(:version, :rubygem => @not_my_rubygem)
Factory(:ownership, :user => @other_user, :rubygem => @not_my_rubygem, :approved => true)

get :index, :format => "json"
end

should assign_to(:rubygems) { [@rubygem] }
should respond_with :success
should "return a JSON hash" do
assert_not_nil JSON.parse(@response.body)
end
should "only return my gems" do
gem_names = JSON.parse(@response.body).map { |rubygem| rubygem['name'] }.sort
assert_equal ["AnotherGem", "SomeGem"], gem_names
end
end
end

def should_return_latest_gems(gems)
Expand Down

0 comments on commit 39d99be

Please sign in to comment.