To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.

Statistics Download as Zip
| Branch: | Tag: | Revision:

root / .svn / pristine / 79 / 79339062a596d0cdaa28d0573ebbdb890c156ff0.svn-base @ 1297:0a574315af3e

History | View | Annotate | Download (10.1 KB)

1 1296:038ba2d95de8 Chris
# Redmine - project management software
2
# Copyright (C) 2006-2012  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.expand_path('../../../test_helper', __FILE__)
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, :issue_categories
24
25
  def setup
26
    Setting.rest_api_enabled = '1'
27
    set_tmp_attachments_directory
28
  end
29
30
  context "GET /projects" do
31
    context ".xml" do
32
      should "return projects" do
33
        get '/projects.xml'
34
        assert_response :success
35
        assert_equal 'application/xml', @response.content_type
36
37
        assert_tag :tag => 'projects',
38
          :child => {:tag => 'project', :child => {:tag => 'id', :content => '1'}}
39
      end
40
    end
41
42
    context ".json" do
43
      should "return projects" do
44
        get '/projects.json'
45
        assert_response :success
46
        assert_equal 'application/json', @response.content_type
47
48
        json = ActiveSupport::JSON.decode(response.body)
49
        assert_kind_of Hash, json
50
        assert_kind_of Array, json['projects']
51
        assert_kind_of Hash, json['projects'].first
52
        assert json['projects'].first.has_key?('id')
53
      end
54
    end
55
  end
56
57
  context "GET /projects/:id" do
58
    context ".xml" do
59
      # TODO: A private project is needed because should_allow_api_authentication
60
      # actually tests that authentication is *required*, not just allowed
61
      should_allow_api_authentication(:get, "/projects/2.xml")
62
63
      should "return requested project" do
64
        get '/projects/1.xml'
65
        assert_response :success
66
        assert_equal 'application/xml', @response.content_type
67
68
        assert_tag :tag => 'project',
69
          :child => {:tag => 'id', :content => '1'}
70
        assert_tag :tag => 'custom_field',
71
          :attributes => {:name => 'Development status'}, :content => 'Stable'
72
73
        assert_no_tag 'trackers'
74
        assert_no_tag 'issue_categories'
75
      end
76
77
      context "with hidden custom fields" do
78
        setup do
79
          ProjectCustomField.find_by_name('Development status').update_attribute :visible, false
80
        end
81
82
        should "not display hidden custom fields" do
83
          get '/projects/1.xml'
84
          assert_response :success
85
          assert_equal 'application/xml', @response.content_type
86
87
          assert_no_tag 'custom_field',
88
            :attributes => {:name => 'Development status'}
89
        end
90
      end
91
92
      should "return categories with include=issue_categories" do
93
        get '/projects/1.xml?include=issue_categories'
94
        assert_response :success
95
        assert_equal 'application/xml', @response.content_type
96
97
        assert_tag 'issue_categories',
98
          :attributes => {:type => 'array'},
99
          :child => {
100
            :tag => 'issue_category',
101
            :attributes => {
102
              :id => '2',
103
              :name => 'Recipes'
104
            }
105
          }
106
      end
107
108
      should "return trackers with include=trackers" do
109
        get '/projects/1.xml?include=trackers'
110
        assert_response :success
111
        assert_equal 'application/xml', @response.content_type
112
113
        assert_tag 'trackers',
114
          :attributes => {:type => 'array'},
115
          :child => {
116
            :tag => 'tracker',
117
            :attributes => {
118
              :id => '2',
119
              :name => 'Feature request'
120
            }
121
          }
122
      end
123
    end
124
125
    context ".json" do
126
      should_allow_api_authentication(:get, "/projects/2.json")
127
128
      should "return requested project" do
129
        get '/projects/1.json'
130
131
        json = ActiveSupport::JSON.decode(response.body)
132
        assert_kind_of Hash, json
133
        assert_kind_of Hash, json['project']
134
        assert_equal 1, json['project']['id']
135
      end
136
    end
137
  end
138
139
  context "POST /projects" do
140
    context "with valid parameters" do
141
      setup do
142
        Setting.default_projects_modules = ['issue_tracking', 'repository']
143
        @parameters = {:project => {:name => 'API test', :identifier => 'api-test'}}
144
      end
145
146
      context ".xml" do
147
        should_allow_api_authentication(:post,
148
                                        '/projects.xml',
149
                                        {:project => {:name => 'API test', :identifier => 'api-test'}},
150
                                        {:success_code => :created})
151
152
153
        should "create a project with the attributes" do
154
          assert_difference('Project.count') do
155
            post '/projects.xml', @parameters, credentials('admin')
156
          end
157
158
          project = Project.first(:order => 'id DESC')
159
          assert_equal 'API test', project.name
160
          assert_equal 'api-test', project.identifier
161
          assert_equal ['issue_tracking', 'repository'], project.enabled_module_names.sort
162
          assert_equal Tracker.all.size, project.trackers.size
163
164
          assert_response :created
165
          assert_equal 'application/xml', @response.content_type
166
          assert_tag 'project', :child => {:tag => 'id', :content => project.id.to_s}
167
        end
168
169
        should "accept enabled_module_names attribute" do
170
          @parameters[:project].merge!({:enabled_module_names => ['issue_tracking', 'news', 'time_tracking']})
171
172
          assert_difference('Project.count') do
173
            post '/projects.xml', @parameters, credentials('admin')
174
          end
175
176
          project = Project.first(:order => 'id DESC')
177
          assert_equal ['issue_tracking', 'news', 'time_tracking'], project.enabled_module_names.sort
178
        end
179
180
        should "accept tracker_ids attribute" do
181
          @parameters[:project].merge!({:tracker_ids => [1, 3]})
182
183
          assert_difference('Project.count') do
184
            post '/projects.xml', @parameters, credentials('admin')
185
          end
186
187
          project = Project.first(:order => 'id DESC')
188
          assert_equal [1, 3], project.trackers.map(&:id).sort
189
        end
190
      end
191
    end
192
193
    context "with invalid parameters" do
194
      setup do
195
        @parameters = {:project => {:name => 'API test'}}
196
      end
197
198
      context ".xml" do
199
        should "return errors" do
200
          assert_no_difference('Project.count') do
201
            post '/projects.xml', @parameters, credentials('admin')
202
          end
203
204
          assert_response :unprocessable_entity
205
          assert_equal 'application/xml', @response.content_type
206
          assert_tag 'errors', :child => {:tag => 'error', :content => "Identifier can't be blank"}
207
        end
208
      end
209
    end
210
  end
211
212
  context "PUT /projects/:id" do
213
    context "with valid parameters" do
214
      setup do
215
        @parameters = {:project => {:name => 'API update'}}
216
      end
217
218
      context ".xml" do
219
        should_allow_api_authentication(:put,
220
                                        '/projects/2.xml',
221
                                        {:project => {:name => 'API update'}},
222
                                        {:success_code => :ok})
223
224
        should "update the project" do
225
          assert_no_difference 'Project.count' do
226
            put '/projects/2.xml', @parameters, credentials('jsmith')
227
          end
228
          assert_response :ok
229
          assert_equal '', @response.body
230
          assert_equal 'application/xml', @response.content_type
231
          project = Project.find(2)
232
          assert_equal 'API update', project.name
233
        end
234
235
        should "accept enabled_module_names attribute" do
236
          @parameters[:project].merge!({:enabled_module_names => ['issue_tracking', 'news', 'time_tracking']})
237
238
          assert_no_difference 'Project.count' do
239
            put '/projects/2.xml', @parameters, credentials('admin')
240
          end
241
          assert_response :ok
242
          assert_equal '', @response.body
243
          project = Project.find(2)
244
          assert_equal ['issue_tracking', 'news', 'time_tracking'], project.enabled_module_names.sort
245
        end
246
247
        should "accept tracker_ids attribute" do
248
          @parameters[:project].merge!({:tracker_ids => [1, 3]})
249
250
          assert_no_difference 'Project.count' do
251
            put '/projects/2.xml', @parameters, credentials('admin')
252
          end
253
          assert_response :ok
254
          assert_equal '', @response.body
255
          project = Project.find(2)
256
          assert_equal [1, 3], project.trackers.map(&:id).sort
257
        end
258
      end
259
    end
260
261
    context "with invalid parameters" do
262
      setup do
263
        @parameters = {:project => {:name => ''}}
264
      end
265
266
      context ".xml" do
267
        should "return errors" do
268
          assert_no_difference('Project.count') do
269
            put '/projects/2.xml', @parameters, credentials('admin')
270
          end
271
272
          assert_response :unprocessable_entity
273
          assert_equal 'application/xml', @response.content_type
274
          assert_tag 'errors', :child => {:tag => 'error', :content => "Name can't be blank"}
275
        end
276
      end
277
    end
278
  end
279
280
  context "DELETE /projects/:id" do
281
    context ".xml" do
282
      should_allow_api_authentication(:delete,
283
                                      '/projects/2.xml',
284
                                      {},
285
                                      {:success_code => :ok})
286
287
      should "delete the project" do
288
        assert_difference('Project.count',-1) do
289
          delete '/projects/2.xml', {}, credentials('admin')
290
        end
291
        assert_response :ok
292
        assert_equal '', @response.body
293
        assert_nil Project.find_by_id(2)
294
      end
295
    end
296
  end
297
end