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