Commit eaf6bb1e authored by Jean-Philippe Lang's avatar Jean-Philippe Lang

Projects API tests rewriting.

git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@4457 e93f8b46-1217-0410-a6f0-8f06a7374b81
parent a1f12e3a
...@@ -26,42 +26,96 @@ class ApiTest::ProjectsTest < ActionController::IntegrationTest ...@@ -26,42 +26,96 @@ class ApiTest::ProjectsTest < ActionController::IntegrationTest
Setting.rest_api_enabled = '1' Setting.rest_api_enabled = '1'
end end
def test_index context "GET /projects" do
context ".xml" do
should "return projects" do
get '/projects.xml' get '/projects.xml'
assert_response :success assert_response :success
assert_equal 'application/xml', @response.content_type assert_equal 'application/xml', @response.content_type
assert_tag :tag => 'projects',
:child => {:tag => 'project', :child => {:tag => 'id', :content => '1'}}
end
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 # TODO: A private project is needed because should_allow_api_authentication
# actually tests that authentication is *required*, not just allowed # actually tests that authentication is *required*, not just allowed
should_allow_api_authentication(:get, "/projects/2.xml") should_allow_api_authentication(:get, "/projects/2.xml")
end
def test_show should "return requested project" do
get '/projects/1.xml' get '/projects/1.xml'
assert_response :success assert_response :success
assert_equal 'application/xml', @response.content_type 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 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 ProjectCustomField.find_by_name('Development status').update_attribute :visible, false
end
should "not display hidden custom fields" do
get '/projects/1.xml' get '/projects/1.xml'
assert_response :success assert_response :success
assert_equal 'application/xml', @response.content_type 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
end
context ".json" do
should_allow_api_authentication(:get, "/projects/2.json")
should "return requested project" do
get '/projects/1.json'
context "POST /projects.xml" do 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, should_allow_api_authentication(:post,
'/projects.xml', '/projects.xml',
{:project => {:name => 'API test', :identifier => 'api-test'}}, {:project => {:name => 'API test', :identifier => 'api-test'}},
{:success_code => :created}) {:success_code => :created})
should "create a project with the attributes" do should "create a project with the attributes" do
assert_difference('Project.count') 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 end
project = Project.first(:order => 'id DESC') project = Project.first(:order => 'id DESC')
...@@ -73,26 +127,42 @@ class ApiTest::ProjectsTest < ActionController::IntegrationTest ...@@ -73,26 +127,42 @@ class ApiTest::ProjectsTest < ActionController::IntegrationTest
assert_tag 'project', :child => {:tag => 'id', :content => project.id.to_s} assert_tag 'project', :child => {:tag => 'id', :content => project.id.to_s}
end end
end end
end
def test_create_failure context "with invalid parameters" do
attributes = {:name => 'API test'} setup do
assert_no_difference 'Project.count' do @parameters = {:project => {:name => 'API test'}}
post '/projects.xml', {:project => attributes}, :authorization => credentials('admin') end
context ".xml" do
should "return errors" do
assert_no_difference('Project.count') do
post '/projects.xml', @parameters, :authorization => credentials('admin')
end end
assert_response :unprocessable_entity assert_response :unprocessable_entity
assert_equal 'application/xml', @response.content_type 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 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, should_allow_api_authentication(:put,
'/projects/2.xml', '/projects/2.xml',
{:project => {:name => 'API test'}}, {:project => {:name => 'API update'}},
{:success_code => :ok}) {:success_code => :ok})
should "update the project" do should "update the project" do
assert_no_difference 'Project.count' 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 end
assert_response :ok assert_response :ok
assert_equal 'application/xml', @response.content_type assert_equal 'application/xml', @response.content_type
...@@ -100,18 +170,29 @@ class ApiTest::ProjectsTest < ActionController::IntegrationTest ...@@ -100,18 +170,29 @@ class ApiTest::ProjectsTest < ActionController::IntegrationTest
assert_equal 'API update', project.name assert_equal 'API update', project.name
end end
end end
end
def test_update_failure context "with invalid parameters" do
attributes = {:name => ''} setup do
assert_no_difference 'Project.count' do @parameters = {:project => {:name => ''}}
put '/projects/1.xml', {:project => attributes}, :authorization => credentials('jsmith') end
context ".xml" do
should "return errors" do
assert_no_difference('Project.count') do
put '/projects/2.xml', @parameters, :authorization => credentials('admin')
end end
assert_response :unprocessable_entity assert_response :unprocessable_entity
assert_equal 'application/xml', @response.content_type 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 end
context "DELETE /projects/2.xml" do context "DELETE /projects/:id" do
context ".xml" do
should_allow_api_authentication(:delete, should_allow_api_authentication(:delete,
'/projects/2.xml', '/projects/2.xml',
{}, {},
...@@ -125,6 +206,7 @@ class ApiTest::ProjectsTest < ActionController::IntegrationTest ...@@ -125,6 +206,7 @@ class ApiTest::ProjectsTest < ActionController::IntegrationTest
assert_nil Project.find_by_id(2) assert_nil Project.find_by_id(2)
end end
end end
end
def credentials(user, password=nil) def credentials(user, password=nil)
ActionController::HttpAuthentication::Basic.encode_credentials(user, password || user) ActionController::HttpAuthentication::Basic.encode_credentials(user, password || user)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment