issues_test.rb
336 lines
| 10.7 KiB
| text/x-ruby
|
RubyLexer
|
r3196 | # Redmine - project management software | ||
# Copyright (C) 2006-2010 Jean-Philippe Lang | ||||
# | ||||
# This program is free software; you can redistribute it and/or | ||||
# modify it under the terms of the GNU General Public License | ||||
# as published by the Free Software Foundation; either version 2 | ||||
# of the License, or (at your option) any later version. | ||||
# | ||||
# This program is distributed in the hope that it will be useful, | ||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
# GNU General Public License for more details. | ||||
# | ||||
# You should have received a copy of the GNU General Public License | ||||
# along with this program; if not, write to the Free Software | ||||
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||||
|
r4243 | require "#{File.dirname(__FILE__)}/../../test_helper" | ||
|
r3196 | |||
|
r4243 | class ApiTest::IssuesTest < ActionController::IntegrationTest | ||
|
r3196 | fixtures :projects, | ||
:users, | ||||
:roles, | ||||
:members, | ||||
:member_roles, | ||||
:issues, | ||||
:issue_statuses, | ||||
:versions, | ||||
:trackers, | ||||
:projects_trackers, | ||||
:issue_categories, | ||||
:enabled_modules, | ||||
:enumerations, | ||||
:attachments, | ||||
:workflows, | ||||
:custom_fields, | ||||
:custom_values, | ||||
:custom_fields_projects, | ||||
:custom_fields_trackers, | ||||
:time_entries, | ||||
:journals, | ||||
:journal_details, | ||||
:queries | ||||
def setup | ||||
Setting.rest_api_enabled = '1' | ||||
end | ||||
|
r3651 | |||
|
r4250 | # Use a private project to make sure auth is really working and not just | ||
# only showing public issues. | ||||
|
r3651 | context "/index.xml" do | ||
|
r4250 | should_allow_api_authentication(:get, "/projects/private-child/issues.xml") | ||
|
r3196 | end | ||
|
r3652 | |||
context "/index.json" do | ||||
|
r4250 | should_allow_api_authentication(:get, "/projects/private-child/issues.json") | ||
|
r3652 | end | ||
|
r3651 | context "/index.xml with filter" do | ||
|
r4250 | should_allow_api_authentication(:get, "/projects/private-child/issues.xml?status_id=5") | ||
|
r3651 | should "show only issues with the status_id" do | ||
|
r4250 | get '/issues.xml?status_id=5' | ||
|
r3651 | assert_tag :tag => 'issues', | ||
:children => { :count => Issue.visible.count(:conditions => {:status_id => 5}), | ||||
:only => { :tag => 'issue' } } | ||||
end | ||||
|
r3196 | end | ||
|
r3651 | |||
|
r3652 | context "/index.json with filter" do | ||
|
r4250 | should_allow_api_authentication(:get, "/projects/private-child/issues.json?status_id=5") | ||
|
r3652 | |||
should "show only issues with the status_id" do | ||||
|
r4250 | get '/issues.json?status_id=5' | ||
|
r3652 | json = ActiveSupport::JSON.decode(response.body) | ||
status_ids_used = json.collect {|j| j['status_id'] } | ||||
assert_equal 3, status_ids_used.length | ||||
assert status_ids_used.all? {|id| id == 5 } | ||||
end | ||||
end | ||||
|
r4250 | # Issue 6 is on a private project | ||
context "/issues/6.xml" do | ||||
should_allow_api_authentication(:get, "/issues/6.xml") | ||||
|
r3651 | end | ||
|
r4250 | context "/issues/6.json" do | ||
should_allow_api_authentication(:get, "/issues/6.json") | ||||
|
r3652 | end | ||
|
r3651 | context "POST /issues.xml" do | ||
|
r4251 | should_allow_api_authentication(:post, | ||
'/issues.xml', | ||||
{:issue => {:project_id => 1, :subject => 'API test', :tracker_id => 2, :status_id => 3}}, | ||||
{:success_code => :created}) | ||||
|
r3651 | |||
should "create an issue with the attributes" do | ||||
|
r4251 | assert_difference('Issue.count') do | ||
post '/issues.xml', {:issue => {:project_id => 1, :subject => 'API test', :tracker_id => 2, :status_id => 3}}, :authorization => credentials('jsmith') | ||||
|
r3651 | end | ||
|
r4251 | |||
issue = Issue.first(:order => 'id DESC') | ||||
assert_equal 1, issue.project_id | ||||
assert_equal 2, issue.tracker_id | ||||
assert_equal 3, issue.status_id | ||||
assert_equal 'API test', issue.subject | ||||
|
r3196 | end | ||
end | ||||
|
r3651 | context "POST /issues.xml with failure" do | ||
|
r4251 | should_allow_api_authentication(:post, | ||
'/issues.xml', | ||||
{:issue => {:project_id => 1}}, | ||||
{:success_code => :unprocessable_entity}) | ||||
|
r3651 | |||
should "have an errors tag" do | ||||
|
r4251 | assert_no_difference('Issue.count') do | ||
post '/issues.xml', {:issue => {:project_id => 1}}, :authorization => credentials('jsmith') | ||||
end | ||||
|
r3651 | assert_tag :errors, :child => {:tag => 'error', :content => "Subject can't be blank"} | ||
end | ||||
|
r3196 | end | ||
|
r3652 | |||
context "POST /issues.json" do | ||||
|
r4251 | should_allow_api_authentication(:post, | ||
'/issues.json', | ||||
{:issue => {:project_id => 1, :subject => 'API test', :tracker_id => 2, :status_id => 3}}, | ||||
{:success_code => :created}) | ||||
|
r3652 | |||
should "create an issue with the attributes" do | ||||
|
r4251 | assert_difference('Issue.count') do | ||
post '/issues.json', {:issue => {:project_id => 1, :subject => 'API test', :tracker_id => 2, :status_id => 3}}, :authorization => credentials('jsmith') | ||||
|
r3652 | end | ||
|
r4251 | |||
issue = Issue.first(:order => 'id DESC') | ||||
assert_equal 1, issue.project_id | ||||
assert_equal 2, issue.tracker_id | ||||
assert_equal 3, issue.status_id | ||||
assert_equal 'API test', issue.subject | ||||
|
r3652 | end | ||
|
r4251 | |||
|
r3652 | end | ||
context "POST /issues.json with failure" do | ||||
|
r4251 | should_allow_api_authentication(:post, | ||
'/issues.json', | ||||
{:issue => {:project_id => 1}}, | ||||
{:success_code => :unprocessable_entity}) | ||||
|
r3652 | |||
should "have an errors element" do | ||||
|
r4251 | assert_no_difference('Issue.count') do | ||
post '/issues.json', {:issue => {:project_id => 1}}, :authorization => credentials('jsmith') | ||||
end | ||||
|
r3652 | json = ActiveSupport::JSON.decode(response.body) | ||
assert_equal "can't be blank", json.first['subject'] | ||||
end | ||||
end | ||||
|
r4252 | # Issue 6 is on a private project | ||
context "PUT /issues/6.xml" do | ||||
|
r3651 | setup do | ||
|
r4252 | @parameters = {:issue => {:subject => 'API update', :notes => 'A new note'}} | ||
@headers = { :authorization => credentials('jsmith') } | ||||
|
r3196 | end | ||
|
r3651 | |||
|
r4252 | should_allow_api_authentication(:put, | ||
'/issues/6.xml', | ||||
{:issue => {:subject => 'API update', :notes => 'A new note'}}, | ||||
{:success_code => :ok}) | ||||
|
r3651 | |||
should "not create a new issue" do | ||||
|
r4252 | assert_no_difference('Issue.count') do | ||
put '/issues/6.xml', @parameters, @headers | ||||
end | ||||
|
r3196 | end | ||
|
r3651 | |||
should "create a new journal" do | ||||
|
r4252 | assert_difference('Journal.count') do | ||
put '/issues/6.xml', @parameters, @headers | ||||
end | ||||
|
r3651 | end | ||
|
r3934 | should "add the note to the journal" do | ||
|
r4252 | put '/issues/6.xml', @parameters, @headers | ||
|
r3934 | journal = Journal.last | ||
assert_equal "A new note", journal.notes | ||||
end | ||||
|
r3651 | should "update the issue" do | ||
|
r4252 | put '/issues/6.xml', @parameters, @headers | ||
issue = Issue.find(6) | ||||
assert_equal "API update", issue.subject | ||||
|
r3196 | end | ||
|
r3651 | |||
|
r3196 | end | ||
|
r3651 | |||
|
r4252 | context "PUT /issues/6.xml with failed update" do | ||
|
r3651 | setup do | ||
|
r4252 | @parameters = {:issue => {:subject => ''}} | ||
@headers = { :authorization => credentials('jsmith') } | ||||
|
r3651 | end | ||
|
r4252 | |||
should_allow_api_authentication(:put, | ||||
'/issues/6.xml', | ||||
{:issue => {:subject => ''}}, # Missing subject should fail | ||||
{:success_code => :unprocessable_entity}) | ||||
|
r3651 | should "not create a new issue" do | ||
|
r4252 | assert_no_difference('Issue.count') do | ||
put '/issues/6.xml', @parameters, @headers | ||||
end | ||||
|
r3651 | end | ||
should "not create a new journal" do | ||||
|
r4252 | assert_no_difference('Journal.count') do | ||
put '/issues/6.xml', @parameters, @headers | ||||
end | ||||
|
r3651 | end | ||
should "have an errors tag" do | ||||
|
r4252 | put '/issues/6.xml', @parameters, @headers | ||
|
r3651 | assert_tag :errors, :child => {:tag => 'error', :content => "Subject can't be blank"} | ||
end | ||||
end | ||||
|
r4252 | context "PUT /issues/6.json" do | ||
|
r3652 | setup do | ||
|
r4252 | @parameters = {:issue => {:subject => 'API update', :notes => 'A new note'}} | ||
@headers = { :authorization => credentials('jsmith') } | ||||
|
r3652 | end | ||
|
r4252 | should_allow_api_authentication(:put, | ||
'/issues/6.json', | ||||
{:issue => {:subject => 'API update', :notes => 'A new note'}}, | ||||
{:success_code => :ok}) | ||||
|
r3652 | |||
should "not create a new issue" do | ||||
|
r4252 | assert_no_difference('Issue.count') do | ||
put '/issues/6.json', @parameters, @headers | ||||
end | ||||
|
r3652 | end | ||
should "create a new journal" do | ||||
|
r4252 | assert_difference('Journal.count') do | ||
put '/issues/6.json', @parameters, @headers | ||||
end | ||||
|
r3652 | end | ||
|
r3934 | should "add the note to the journal" do | ||
|
r4252 | put '/issues/6.json', @parameters, @headers | ||
|
r3934 | journal = Journal.last | ||
assert_equal "A new note", journal.notes | ||||
end | ||||
|
r3652 | should "update the issue" do | ||
|
r4252 | put '/issues/6.json', @parameters, @headers | ||
issue = Issue.find(6) | ||||
assert_equal "API update", issue.subject | ||||
|
r3652 | end | ||
|
r4252 | |||
|
r3652 | end | ||
|
r4252 | context "PUT /issues/6.json with failed update" do | ||
|
r3652 | setup do | ||
|
r4252 | @parameters = {:issue => {:subject => ''}} | ||
@headers = { :authorization => credentials('jsmith') } | ||||
|
r3652 | end | ||
|
r4252 | |||
should_allow_api_authentication(:put, | ||||
'/issues/6.json', | ||||
{:issue => {:subject => ''}}, # Missing subject should fail | ||||
{:success_code => :unprocessable_entity}) | ||||
|
r3652 | should "not create a new issue" do | ||
|
r4252 | assert_no_difference('Issue.count') do | ||
put '/issues/6.json', @parameters, @headers | ||||
end | ||||
|
r3652 | end | ||
should "not create a new journal" do | ||||
|
r4252 | assert_no_difference('Journal.count') do | ||
put '/issues/6.json', @parameters, @headers | ||||
end | ||||
|
r3652 | end | ||
should "have an errors attribute" do | ||||
|
r4252 | put '/issues/6.json', @parameters, @headers | ||
|
r3652 | json = ActiveSupport::JSON.decode(response.body) | ||
assert_equal "can't be blank", json.first['subject'] | ||||
end | ||||
end | ||||
|
r3651 | context "DELETE /issues/1.xml" do | ||
|
r4253 | should_allow_api_authentication(:delete, | ||
'/issues/6.xml', | ||||
{}, | ||||
{:success_code => :ok}) | ||||
|
r3651 | |||
should "delete the issue" do | ||||
|
r4253 | assert_difference('Issue.count',-1) do | ||
delete '/issues/6.xml', {}, :authorization => credentials('jsmith') | ||||
end | ||||
assert_nil Issue.find_by_id(6) | ||||
|
r3651 | end | ||
|
r3196 | end | ||
|
r3652 | |||
context "DELETE /issues/1.json" do | ||||
|
r4253 | should_allow_api_authentication(:delete, | ||
'/issues/6.json', | ||||
{}, | ||||
{:success_code => :ok}) | ||||
|
r3652 | |||
should "delete the issue" do | ||||
|
r4253 | assert_difference('Issue.count',-1) do | ||
delete '/issues/6.json', {}, :authorization => credentials('jsmith') | ||||
end | ||||
assert_nil Issue.find_by_id(6) | ||||
|
r3652 | end | ||
end | ||||
|
r3196 | def credentials(user, password=nil) | ||
ActionController::HttpAuthentication::Basic.encode_credentials(user, password || user) | ||||
end | ||||
end | ||||