Project

General

Profile

Feature #22008 ยป 22008-add-api.patch

Yuichi HARADA, 2020-02-12 06:09

View differences:

app/controllers/repositories_controller.rb
35 35
  before_action :find_changeset, :only => [:revision, :add_related_issue, :remove_related_issue]
36 36
  before_action :authorize
37 37
  accept_rss_auth :revisions
38
  accept_api_auth :add_related_issue, :remove_related_issue
38 39

  
39 40
  rescue_from Redmine::Scm::Adapters::CommandFailed, :with => :show_error_command_failed
40 41

  
......
227 228
      @issue = nil
228 229
    end
229 230

  
230
    if @issue
231
      @changeset.issues << @issue
231
    respond_to do |format|
232
      if @issue
233
        @changeset.issues << @issue
234
        format.api { render_api_ok }
235
      else
236
        format.api { render_api_errors "#{l(:label_issue)} #{l('activerecord.errors.messages.invalid')}" }
237
      end
238
      format.js
232 239
    end
233 240
  end
234 241

  
......
239 246
    if @issue
240 247
      @changeset.issues.delete(@issue)
241 248
    end
249
    respond_to do |format|
250
      format.api { render_api_ok }
251
      format.js
252
    end
242 253
  end
243 254

  
244 255
  def diff
test/integration/api_test/api_routing_test.rb
117 117
    should_route 'GET /queries' => 'queries#index'
118 118
  end
119 119

  
120
  def test_repositories
121
    should_route 'POST /projects/1/repository/2/revisions/3/issues' => 'repositories#add_related_issue', :id => '1', :repository_id => '2', :rev => '3'
122
    should_route 'DELETE /projects/1/repository/2/revisions/3/issues/4' => 'repositories#remove_related_issue', :id => '1', :repository_id => '2', :rev => '3', :issue_id => '4'
123
  end
124

  
120 125
  def test_roles
121 126
    should_route 'GET /roles' => 'roles#index'
122 127
    should_route 'GET /roles/2' => 'roles#show', :id => '2'
test/integration/api_test/repositories_test.rb
1
# frozen_string_literal: true
2

  
3
# Redmine - project management software
4
# Copyright (C) 2006-2019  Jean-Philippe Lang
5
#
6
# This program is free software; you can redistribute it and/or
7
# modify it under the terms of the GNU General Public License
8
# as published by the Free Software Foundation; either version 2
9
# of the License, or (at your option) any later version.
10
#
11
# This program is distributed in the hope that it will be useful,
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
# GNU General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License
17
# along with this program; if not, write to the Free Software
18
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19

  
20
require File.expand_path('../../../test_helper', __FILE__)
21

  
22
class Redmine::ApiTest::RepositoriesTest < Redmine::ApiTest::Base
23
  fixtures :users,
24
           :projects, :enabled_modules,
25
           :members, :roles, :member_roles,
26
           :issues,
27
           :repositories, :changesets, :changes
28

  
29
  test 'POST /projects/:id/repository/:repository_id/revisions/:rev/issues.xml should add related issue' do
30
    changeset = Changeset.find(103)
31
    assert_equal [], changeset.issue_ids
32
    assert_difference 'Changeset.find(103).issues.size' do
33
      post '/projects/1/repository/10/revisions/4/issues.xml', :headers => credentials('jsmith'), :params => {:issue_id => '2'}
34
    end
35
    assert_response :no_content
36
    assert_equal [2], changeset.reload.issue_ids
37
  end
38

  
39
  test 'POST /projects/:id/repository/:repository_id/revisions/:rev/issues.json should add related issue' do
40
    changeset = Changeset.find(103)
41
    assert_equal [], changeset.issue_ids
42
    assert_difference 'Changeset.find(103).issues.size' do
43
      post '/projects/1/repository/10/revisions/4/issues.json', :headers => credentials('jsmith'), :params => {:issue_id => '2'}
44
    end
45
    assert_response :no_content
46
    assert_equal [2], changeset.reload.issue_ids
47
  end
48

  
49
  test 'POST /projects/:id/repository/:repository_id/revisions/:rev/issues.xml should accept issue_id with sharp' do
50
    changeset = Changeset.find(103)
51
    assert_equal [], changeset.issue_ids
52
    assert_difference 'Changeset.find(103).issues.size' do
53
      post '/projects/1/repository/10/revisions/4/issues.xml', :headers => credentials('jsmith'), :params => {:issue_id => '#2'}
54
    end
55
    assert_response :no_content
56
    assert_equal [2], changeset.reload.issue_ids
57
  end
58

  
59
  test 'POST /projects/:id/repository/:repository_id/revisions/:rev/issues.json should accept issue_id with sharp' do
60
    changeset = Changeset.find(103)
61
    assert_equal [], changeset.issue_ids
62
    assert_difference 'Changeset.find(103).issues.size' do
63
      post '/projects/1/repository/10/revisions/4/issues.json', :headers => credentials('jsmith'), :params => {:issue_id => '#2'}
64
    end
65
    assert_response :no_content
66
    assert_equal [2], changeset.reload.issue_ids
67
  end
68

  
69
  test 'POST /projects/:id/repository/:repository_id/revisions/:rev/issues.xml with invalid issue_id' do
70
    assert_no_difference 'Changeset.find(103).issues.size' do
71
      post '/projects/1/repository/10/revisions/4/issues.xml', :headers => credentials('jsmith'), :params => {:issue_id => '9999'}
72
    end
73
    assert_response :unprocessable_entity
74
    assert_select 'errors error', :text => 'Issue is invalid'
75
  end
76

  
77
  test 'POST /projects/:id/repository/:repository_id/revisions/:rev/issues.json with invalid issue_id' do
78
    assert_no_difference 'Changeset.find(103).issues.size' do
79
      post '/projects/1/repository/10/revisions/4/issues.json', :headers => credentials('jsmith'), :params => {:issue_id => '9999'}
80
    end
81
    assert_response :unprocessable_entity
82
    json = ActiveSupport::JSON.decode(response.body)
83
    assert json['errors'].include?('Issue is invalid')
84
  end
85

  
86
  test 'DELETE /projects/:id/repository/:repository_id/revisions/:rev/issues/:issue_id.xml should remove related issue' do
87
    changeset = Changeset.find(103)
88
    changeset.issues << Issue.find(1)
89
    changeset.issues << Issue.find(2)
90
    assert_difference 'Changeset.find(103).issues.size', -1 do
91
      delete '/projects/1/repository/10/revisions/4/issues/2.xml', :headers => credentials('jsmith')
92
    end
93
    assert_response :no_content
94
    assert_equal [1], changeset.reload.issue_ids
95
  end
96

  
97
  test 'DELETE /projects/:id/repository/:repository_id/revisions/:rev/issues/:issue_id.json should remove related issue' do
98
    changeset = Changeset.find(103)
99
    changeset.issues << Issue.find(1)
100
    changeset.issues << Issue.find(2)
101
    assert_difference 'Changeset.find(103).issues.size', -1 do
102
      delete '/projects/1/repository/10/revisions/4/issues/2.json', :headers => credentials('jsmith')
103
    end
104
    assert_response :no_content
105
    assert_equal [1], changeset.reload.issue_ids
106
  end
107
end
    (1-1/1)