Projects API tests rewriting.

git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@4457 e93f8b46-1217-0410-a6f0-8f06a7374b81
This commit is contained in:
Jean-Philippe Lang 2010-12-03 12:49:07 +00:00
parent a1f12e3ade
commit eaf6bb1e9b
1 changed files with 169 additions and 87 deletions

View File

@ -26,42 +26,96 @@ class ApiTest::ProjectsTest < ActionController::IntegrationTest
Setting.rest_api_enabled = '1'
end
def test_index
context "GET /projects" do
context ".xml" do
should "return projects" do
get '/projects.xml'
assert_response :success
assert_equal 'application/xml', @response.content_type
assert_tag :tag => 'projects',
:child => {:tag => 'project', :child => {:tag => 'id', :content => '1'}}
end
end
context "GET /projects/2.xml" do
context ".json" do
should "return projects" do
get '/projects.json'
assert_response :success
assert_equal 'application/json', @response.content_type
json = ActiveSupport::JSON.decode(response.body)
assert_kind_of Hash, json
assert_kind_of Array, json['projects']
assert_kind_of Hash, json['projects'].first
assert json['projects'].first.has_key?('id')
end
end
end
context "GET /projects/:id" do
context ".xml" do
# TODO: A private project is needed because should_allow_api_authentication
# actually tests that authentication is *required*, not just allowed
should_allow_api_authentication(:get, "/projects/2.xml")
end
def test_show
should "return requested project" do
get '/projects/1.xml'
assert_response :success
assert_equal 'application/xml', @response.content_type
assert_tag 'custom_field', :attributes => {:name => 'Development status'}, :content => 'Stable'
assert_tag :tag => 'project',
:child => {:tag => 'id', :content => '1'}
assert_tag :tag => 'custom_field',
:attributes => {:name => 'Development status'}, :content => 'Stable'
end
def test_show_should_not_display_hidden_custom_fields
context "with hidden custom fields" do
setup do
ProjectCustomField.find_by_name('Development status').update_attribute :visible, false
end
should "not display hidden custom fields" do
get '/projects/1.xml'
assert_response :success
assert_equal 'application/xml', @response.content_type
assert_no_tag 'custom_field', :attributes => {:name => 'Development status'}
assert_no_tag 'custom_field',
:attributes => {:name => 'Development status'}
end
end
end
context "POST /projects.xml" do
context ".json" do
should_allow_api_authentication(:get, "/projects/2.json")
should "return requested project" do
get '/projects/1.json'
json = ActiveSupport::JSON.decode(response.body)
assert_kind_of Hash, json
assert_kind_of Hash, json['project']
assert_equal 1, json['project']['id']
end
end
end
context "POST /projects" do
context "with valid parameters" do
setup do
@parameters = {:project => {:name => 'API test', :identifier => 'api-test'}}
end
context ".xml" do
should_allow_api_authentication(:post,
'/projects.xml',
{:project => {:name => 'API test', :identifier => 'api-test'}},
{:success_code => :created})
should "create a project with the attributes" do
assert_difference('Project.count') do
post '/projects.xml', {:project => {:name => 'API test', :identifier => 'api-test'}}, :authorization => credentials('admin')
post '/projects.xml', @parameters, :authorization => credentials('admin')
end
project = Project.first(:order => 'id DESC')
@ -73,26 +127,42 @@ class ApiTest::ProjectsTest < ActionController::IntegrationTest
assert_tag 'project', :child => {:tag => 'id', :content => project.id.to_s}
end
end
def test_create_failure
attributes = {:name => 'API test'}
assert_no_difference 'Project.count' do
post '/projects.xml', {:project => attributes}, :authorization => credentials('admin')
end
context "with invalid parameters" do
setup do
@parameters = {:project => {:name => 'API test'}}
end
context ".xml" do
should "return errors" do
assert_no_difference('Project.count') do
post '/projects.xml', @parameters, :authorization => credentials('admin')
end
assert_response :unprocessable_entity
assert_equal 'application/xml', @response.content_type
assert_tag :errors, :child => {:tag => 'error', :content => "Identifier can't be blank"}
assert_tag 'errors', :child => {:tag => 'error', :content => "Identifier can't be blank"}
end
end
end
end
context "PUT /projects/2.xml" do
context "PUT /projects/:id" do
context "with valid parameters" do
setup do
@parameters = {:project => {:name => 'API update'}}
end
context ".xml" do
should_allow_api_authentication(:put,
'/projects/2.xml',
{:project => {:name => 'API test'}},
{:project => {:name => 'API update'}},
{:success_code => :ok})
should "update the project" do
assert_no_difference 'Project.count' do
put '/projects/2.xml', {:project => {:name => 'API update'}}, :authorization => credentials('jsmith')
put '/projects/2.xml', @parameters, :authorization => credentials('jsmith')
end
assert_response :ok
assert_equal 'application/xml', @response.content_type
@ -100,18 +170,29 @@ class ApiTest::ProjectsTest < ActionController::IntegrationTest
assert_equal 'API update', project.name
end
end
def test_update_failure
attributes = {:name => ''}
assert_no_difference 'Project.count' do
put '/projects/1.xml', {:project => attributes}, :authorization => credentials('jsmith')
end
context "with invalid parameters" do
setup do
@parameters = {:project => {:name => ''}}
end
context ".xml" do
should "return errors" do
assert_no_difference('Project.count') do
put '/projects/2.xml', @parameters, :authorization => credentials('admin')
end
assert_response :unprocessable_entity
assert_equal 'application/xml', @response.content_type
assert_tag :errors, :child => {:tag => 'error', :content => "Name can't be blank"}
assert_tag 'errors', :child => {:tag => 'error', :content => "Name can't be blank"}
end
end
end
end
context "DELETE /projects/2.xml" do
context "DELETE /projects/:id" do
context ".xml" do
should_allow_api_authentication(:delete,
'/projects/2.xml',
{},
@ -125,6 +206,7 @@ class ApiTest::ProjectsTest < ActionController::IntegrationTest
assert_nil Project.find_by_id(2)
end
end
end
def credentials(user, password=nil)
ActionController::HttpAuthentication::Basic.encode_credentials(user, password || user)