comparison test/integration/projects_api_test.rb @ 0:513646585e45

* Import Redmine trunk SVN rev 3859
author Chris Cannam
date Fri, 23 Jul 2010 15:52:44 +0100
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:513646585e45
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 ProjectsApiTest < 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 end
40
41 def test_create
42 attributes = {:name => 'API test', :identifier => 'api-test'}
43 assert_difference 'Project.count' do
44 post '/projects.xml', {:project => attributes}, :authorization => credentials('admin')
45 end
46 assert_response :created
47 assert_equal 'application/xml', @response.content_type
48 project = Project.first(:order => 'id DESC')
49 attributes.each do |attribute, value|
50 assert_equal value, project.send(attribute)
51 end
52 end
53
54 def test_create_failure
55 attributes = {:name => 'API test'}
56 assert_no_difference 'Project.count' do
57 post '/projects.xml', {:project => attributes}, :authorization => credentials('admin')
58 end
59 assert_response :unprocessable_entity
60 assert_equal 'application/xml', @response.content_type
61 assert_tag :errors, :child => {:tag => 'error', :content => "Identifier can't be blank"}
62 end
63
64 def test_update
65 attributes = {:name => 'API update'}
66 assert_no_difference 'Project.count' do
67 put '/projects/1.xml', {:project => attributes}, :authorization => credentials('jsmith')
68 end
69 assert_response :ok
70 assert_equal 'application/xml', @response.content_type
71 project = Project.find(1)
72 attributes.each do |attribute, value|
73 assert_equal value, project.send(attribute)
74 end
75 end
76
77 def test_update_failure
78 attributes = {:name => ''}
79 assert_no_difference 'Project.count' do
80 put '/projects/1.xml', {:project => attributes}, :authorization => credentials('jsmith')
81 end
82 assert_response :unprocessable_entity
83 assert_equal 'application/xml', @response.content_type
84 assert_tag :errors, :child => {:tag => 'error', :content => "Name can't be blank"}
85 end
86
87 def test_destroy
88 assert_difference 'Project.count', -1 do
89 delete '/projects/2.xml', {}, :authorization => credentials('admin')
90 end
91 assert_response :ok
92 assert_equal 'application/xml', @response.content_type
93 assert_nil Project.find_by_id(2)
94 end
95
96 def credentials(user, password=nil)
97 ActionController::HttpAuthentication::Basic.encode_credentials(user, password || user)
98 end
99 end