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