comparison test/integration/api_test/projects_test.rb @ 37:94944d00e43c

* Update to SVN trunk rev 4411
author Chris Cannam <chris.cannam@soundsoftware.ac.uk>
date Fri, 19 Nov 2010 13:24:41 +0000
parents
children af80e5618e9b
comparison
equal deleted inserted replaced
22:40f7cfd4df19 37:94944d00e43c
1 # Redmine - project management software
2 # Copyright (C) 2006-2010 Jean-Philippe Lang
3 #
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
18 require "#{File.dirname(__FILE__)}/../../test_helper"
19
20 class ApiTest::ProjectsTest < ActionController::IntegrationTest
21 fixtures :projects, :versions, :users, :roles, :members, :member_roles, :issues, :journals, :journal_details,
22 :trackers, :projects_trackers, :issue_statuses, :enabled_modules, :enumerations, :boards, :messages,
23 :attachments, :custom_fields, :custom_values, :time_entries
24
25 def setup
26 Setting.rest_api_enabled = '1'
27 end
28
29 def test_index
30 get '/projects.xml'
31 assert_response :success
32 assert_equal 'application/xml', @response.content_type
33 end
34
35 def test_show
36 get '/projects/1.xml'
37 assert_response :success
38 assert_equal 'application/xml', @response.content_type
39 assert_tag 'custom_field', :attributes => {:name => 'Development status'}, :content => 'Stable'
40 end
41
42 def test_show_should_not_display_hidden_custom_fields
43 ProjectCustomField.find_by_name('Development status').update_attribute :visible, false
44 get '/projects/1.xml'
45 assert_response :success
46 assert_equal 'application/xml', @response.content_type
47 assert_no_tag 'custom_field', :attributes => {:name => 'Development status'}
48 end
49
50 def test_create
51 attributes = {:name => 'API test', :identifier => 'api-test'}
52 assert_difference 'Project.count' do
53 post '/projects.xml', {:project => attributes}, :authorization => credentials('admin')
54 end
55
56 project = Project.first(:order => 'id DESC')
57 attributes.each do |attribute, value|
58 assert_equal value, project.send(attribute)
59 end
60
61 assert_response :created
62 assert_equal 'application/xml', @response.content_type
63 assert_tag 'project', :child => {:tag => 'id', :content => project.id.to_s}
64 end
65
66 def test_create_failure
67 attributes = {:name => 'API test'}
68 assert_no_difference 'Project.count' do
69 post '/projects.xml', {:project => attributes}, :authorization => credentials('admin')
70 end
71 assert_response :unprocessable_entity
72 assert_equal 'application/xml', @response.content_type
73 assert_tag :errors, :child => {:tag => 'error', :content => "Identifier can't be blank"}
74 end
75
76 def test_update
77 attributes = {:name => 'API update'}
78 assert_no_difference 'Project.count' do
79 put '/projects/1.xml', {:project => attributes}, :authorization => credentials('jsmith')
80 end
81 assert_response :ok
82 assert_equal 'application/xml', @response.content_type
83 project = Project.find(1)
84 attributes.each do |attribute, value|
85 assert_equal value, project.send(attribute)
86 end
87 end
88
89 def test_update_failure
90 attributes = {:name => ''}
91 assert_no_difference 'Project.count' do
92 put '/projects/1.xml', {:project => attributes}, :authorization => credentials('jsmith')
93 end
94 assert_response :unprocessable_entity
95 assert_equal 'application/xml', @response.content_type
96 assert_tag :errors, :child => {:tag => 'error', :content => "Name can't be blank"}
97 end
98
99 def test_destroy
100 assert_difference 'Project.count', -1 do
101 delete '/projects/2.xml', {}, :authorization => credentials('admin')
102 end
103 assert_response :ok
104 assert_equal 'application/xml', @response.content_type
105 assert_nil Project.find_by_id(2)
106 end
107
108 def credentials(user, password=nil)
109 ActionController::HttpAuthentication::Basic.encode_credentials(user, password || user)
110 end
111 end