Chris@909
|
1 # Redmine - project management software
|
Chris@909
|
2 # Copyright (C) 2006-2011 Jean-Philippe Lang
|
Chris@909
|
3 #
|
Chris@909
|
4 # This program is free software; you can redistribute it and/or
|
Chris@909
|
5 # modify it under the terms of the GNU General Public License
|
Chris@909
|
6 # as published by the Free Software Foundation; either version 2
|
Chris@909
|
7 # of the License, or (at your option) any later version.
|
Chris@909
|
8 #
|
Chris@909
|
9 # This program is distributed in the hope that it will be useful,
|
Chris@909
|
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
|
Chris@909
|
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
Chris@909
|
12 # GNU General Public License for more details.
|
Chris@909
|
13 #
|
Chris@909
|
14 # You should have received a copy of the GNU General Public License
|
Chris@909
|
15 # along with this program; if not, write to the Free Software
|
Chris@909
|
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
Chris@909
|
17
|
Chris@909
|
18 require File.expand_path('../../../test_helper', __FILE__)
|
Chris@909
|
19
|
Chris@909
|
20 class ApiTest::VersionsTest < ActionController::IntegrationTest
|
Chris@909
|
21 fixtures :projects, :trackers, :issue_statuses, :issues,
|
Chris@909
|
22 :enumerations, :users, :issue_categories,
|
Chris@909
|
23 :projects_trackers,
|
Chris@909
|
24 :roles,
|
Chris@909
|
25 :member_roles,
|
Chris@909
|
26 :members,
|
Chris@909
|
27 :enabled_modules,
|
Chris@909
|
28 :workflows,
|
Chris@909
|
29 :versions
|
Chris@909
|
30
|
Chris@909
|
31 def setup
|
Chris@909
|
32 Setting.rest_api_enabled = '1'
|
Chris@909
|
33 end
|
Chris@909
|
34
|
Chris@909
|
35 context "/projects/:project_id/versions" do
|
Chris@909
|
36 context "GET" do
|
Chris@909
|
37 should "return project versions" do
|
Chris@909
|
38 get '/projects/1/versions.xml'
|
Chris@909
|
39
|
Chris@909
|
40 assert_response :success
|
Chris@909
|
41 assert_equal 'application/xml', @response.content_type
|
Chris@909
|
42 assert_tag :tag => 'versions',
|
Chris@909
|
43 :attributes => {:type => 'array'},
|
Chris@909
|
44 :child => {
|
Chris@909
|
45 :tag => 'version',
|
Chris@909
|
46 :child => {
|
Chris@909
|
47 :tag => 'id',
|
Chris@909
|
48 :content => '2',
|
Chris@909
|
49 :sibling => {
|
Chris@909
|
50 :tag => 'name',
|
Chris@909
|
51 :content => '1.0'
|
Chris@909
|
52 }
|
Chris@909
|
53 }
|
Chris@909
|
54 }
|
Chris@909
|
55 end
|
Chris@909
|
56 end
|
Chris@909
|
57
|
Chris@909
|
58 context "POST" do
|
Chris@909
|
59 should "create the version" do
|
Chris@909
|
60 assert_difference 'Version.count' do
|
Chris@909
|
61 post '/projects/1/versions.xml', {:version => {:name => 'API test'}}, :authorization => credentials('jsmith')
|
Chris@909
|
62 end
|
Chris@909
|
63
|
Chris@909
|
64 version = Version.first(:order => 'id DESC')
|
Chris@909
|
65 assert_equal 'API test', version.name
|
Chris@909
|
66
|
Chris@909
|
67 assert_response :created
|
Chris@909
|
68 assert_equal 'application/xml', @response.content_type
|
Chris@909
|
69 assert_tag 'version', :child => {:tag => 'id', :content => version.id.to_s}
|
Chris@909
|
70 end
|
Chris@909
|
71
|
Chris@909
|
72 should "create the version with due date" do
|
Chris@909
|
73 assert_difference 'Version.count' do
|
Chris@909
|
74 post '/projects/1/versions.xml', {:version => {:name => 'API test', :due_date => '2012-01-24'}}, :authorization => credentials('jsmith')
|
Chris@909
|
75 end
|
Chris@909
|
76
|
Chris@909
|
77 version = Version.first(:order => 'id DESC')
|
Chris@909
|
78 assert_equal 'API test', version.name
|
Chris@909
|
79 assert_equal Date.parse('2012-01-24'), version.due_date
|
Chris@909
|
80
|
Chris@909
|
81 assert_response :created
|
Chris@909
|
82 assert_equal 'application/xml', @response.content_type
|
Chris@909
|
83 assert_tag 'version', :child => {:tag => 'id', :content => version.id.to_s}
|
Chris@909
|
84 end
|
Chris@909
|
85
|
Chris@909
|
86 context "with failure" do
|
Chris@909
|
87 should "return the errors" do
|
Chris@909
|
88 assert_no_difference('Version.count') do
|
Chris@909
|
89 post '/projects/1/versions.xml', {:version => {:name => ''}}, :authorization => credentials('jsmith')
|
Chris@909
|
90 end
|
Chris@909
|
91
|
Chris@909
|
92 assert_response :unprocessable_entity
|
Chris@909
|
93 assert_tag :errors, :child => {:tag => 'error', :content => "Name can't be blank"}
|
Chris@909
|
94 end
|
Chris@909
|
95 end
|
Chris@909
|
96 end
|
Chris@909
|
97 end
|
Chris@909
|
98
|
Chris@909
|
99 context "/versions/:id" do
|
Chris@909
|
100 context "GET" do
|
Chris@909
|
101 should "return the version" do
|
Chris@909
|
102 get '/versions/2.xml'
|
Chris@909
|
103
|
Chris@909
|
104 assert_response :success
|
Chris@909
|
105 assert_equal 'application/xml', @response.content_type
|
Chris@909
|
106 assert_tag 'version',
|
Chris@909
|
107 :child => {
|
Chris@909
|
108 :tag => 'id',
|
Chris@909
|
109 :content => '2',
|
Chris@909
|
110 :sibling => {
|
Chris@909
|
111 :tag => 'name',
|
Chris@909
|
112 :content => '1.0'
|
Chris@909
|
113 }
|
Chris@909
|
114 }
|
Chris@909
|
115 end
|
Chris@909
|
116 end
|
Chris@909
|
117
|
Chris@909
|
118 context "PUT" do
|
Chris@909
|
119 should "update the version" do
|
Chris@909
|
120 put '/versions/2.xml', {:version => {:name => 'API update'}}, :authorization => credentials('jsmith')
|
Chris@909
|
121
|
Chris@909
|
122 assert_response :ok
|
Chris@909
|
123 assert_equal 'API update', Version.find(2).name
|
Chris@909
|
124 end
|
Chris@909
|
125 end
|
Chris@909
|
126
|
Chris@909
|
127 context "DELETE" do
|
Chris@909
|
128 should "destroy the version" do
|
Chris@909
|
129 assert_difference 'Version.count', -1 do
|
Chris@909
|
130 delete '/versions/3.xml', {}, :authorization => credentials('jsmith')
|
Chris@909
|
131 end
|
Chris@909
|
132
|
Chris@909
|
133 assert_response :ok
|
Chris@909
|
134 assert_nil Version.find_by_id(3)
|
Chris@909
|
135 end
|
Chris@909
|
136 end
|
Chris@909
|
137 end
|
Chris@909
|
138
|
Chris@909
|
139 def credentials(user, password=nil)
|
Chris@909
|
140 ActionController::HttpAuthentication::Basic.encode_credentials(user, password || user)
|
Chris@909
|
141 end
|
Chris@909
|
142 end
|