BigW Consortium Gitlab

merge_requests_spec.rb 8.53 KB
Newer Older
1 2
require "spec_helper"

3
describe API::API do
4
  include ApiHelpers
5 6
  before(:each) { ActiveRecord::Base.observers.enable(:user_observer) }
  after(:each) { ActiveRecord::Base.observers.disable(:user_observer) }
7
  let(:user) { create(:user) }
8
  let!(:project) {create(:project, creator_id: user.id, namespace: user.namespace) }
9 10 11 12
  let!(:merge_request) { create(:merge_request, author: user, assignee: user, source_project: project, target_project: project, title: "Test") }
  before {
    project.team << [user, :reporters]
  }
13 14 15 16

  describe "GET /projects/:id/merge_requests" do
    context "when unauthenticated" do
      it "should return authentication error" do
Hiroyuki Sato committed
17
        get api("/projects/#{project.id}/merge_requests")
18 19 20 21 22 23
        response.status.should == 401
      end
    end

    context "when authenticated" do
      it "should return an array of merge_requests" do
Hiroyuki Sato committed
24
        get api("/projects/#{project.id}/merge_requests", user)
25 26 27 28 29 30 31 32 33
        response.status.should == 200
        json_response.should be_an Array
        json_response.first['title'].should == merge_request.title
      end
    end
  end

  describe "GET /projects/:id/merge_request/:merge_request_id" do
    it "should return merge_request" do
Hiroyuki Sato committed
34
      get api("/projects/#{project.id}/merge_request/#{merge_request.id}", user)
35 36
      response.status.should == 200
      json_response['title'].should == merge_request.title
37
      json_response['iid'].should == merge_request.iid
38
    end
39 40

    it "should return a 404 error if merge_request_id not found" do
Hiroyuki Sato committed
41
      get api("/projects/#{project.id}/merge_request/999", user)
42 43
      response.status.should == 404
    end
44 45 46
  end

  describe "POST /projects/:id/merge_requests" do
47 48
    context 'between branches projects' do
      it "should return merge_request" do
Hiroyuki Sato committed
49
        post api("/projects/#{project.id}/merge_requests", user),
50
        title: 'Test merge_request', source_branch: "stable", target_branch: "master", author: user
51 52 53
        response.status.should == 201
        json_response['title'].should == 'Test merge_request'
      end
54

55
      it "should return 422 when source_branch equals target_branch" do
Hiroyuki Sato committed
56
        post api("/projects/#{project.id}/merge_requests", user),
57
        title: "Test merge_request", source_branch: "master", target_branch: "master", author: user
58 59
        response.status.should == 422
      end
60

61
      it "should return 400 when source_branch is missing" do
Hiroyuki Sato committed
62
        post api("/projects/#{project.id}/merge_requests", user),
63
        title: "Test merge_request", target_branch: "master", author: user
64 65
        response.status.should == 400
      end
66

67
      it "should return 400 when target_branch is missing" do
Hiroyuki Sato committed
68
        post api("/projects/#{project.id}/merge_requests", user),
69
        title: "Test merge_request", source_branch: "stable", author: user
70 71 72 73
        response.status.should == 400
      end

      it "should return 400 when title is missing" do
Hiroyuki Sato committed
74
        post api("/projects/#{project.id}/merge_requests", user),
75
        target_branch: 'master', source_branch: 'stable'
76 77
        response.status.should == 400
      end
78
    end
79

80 81 82
    context 'forked projects' do
      let!(:user2) {create(:user)}
      let!(:forked_project_link) { build(:forked_project_link) }
83 84
      let!(:fork_project) {  create(:project, forked_project_link: forked_project_link,  namespace: user2.namespace, creator_id: user2.id)  }
      let!(:unrelated_project) {  create(:project,  namespace: create(:user).namespace, creator_id: user2.id)  }
85 86 87 88 89 90 91 92 93

      before :each do |each|
        fork_project.team << [user2, :reporters]
        forked_project_link.forked_from_project = project
        forked_project_link.forked_to_project = fork_project
        forked_project_link.save!
      end

      it "should return merge_request" do
Hiroyuki Sato committed
94
        post api("/projects/#{fork_project.id}/merge_requests", user2),
95
        title: 'Test merge_request', source_branch: "stable", target_branch: "master", author: user2, target_project_id: project.id
96 97 98 99 100
        response.status.should == 201
        json_response['title'].should == 'Test merge_request'
      end

      it "should not return 422 when source_branch equals target_branch" do
Hiroyuki Sato committed
101
        project.id.should_not == fork_project.id
102 103
        fork_project.forked?.should be_true
        fork_project.forked_from_project.should == project
Hiroyuki Sato committed
104
        post api("/projects/#{fork_project.id}/merge_requests", user2),
105
        title: 'Test merge_request', source_branch: "master", target_branch: "master", author: user2, target_project_id: project.id
106 107 108 109 110
        response.status.should == 201
        json_response['title'].should == 'Test merge_request'
      end

      it "should return 400 when source_branch is missing" do
Hiroyuki Sato committed
111
        post api("/projects/#{fork_project.id}/merge_requests", user2),
112
        title: 'Test merge_request', target_branch: "master", author: user2, target_project_id: project.id
113 114 115 116
        response.status.should == 400
      end

      it "should return 400 when target_branch is missing" do
Hiroyuki Sato committed
117
        post api("/projects/#{fork_project.id}/merge_requests", user2),
118
        title: 'Test merge_request', target_branch: "master", author: user2, target_project_id: project.id
119 120 121 122
        response.status.should == 400
      end

      it "should return 400 when title is missing" do
Hiroyuki Sato committed
123
        post api("/projects/#{fork_project.id}/merge_requests", user2),
124
        target_branch: 'master', source_branch: 'stable', author: user2, target_project_id: project.id
125 126 127 128
        response.status.should == 400
      end

      it "should return 400 when target_branch is specified and not a forked project" do
Hiroyuki Sato committed
129
        post api("/projects/#{project.id}/merge_requests", user),
130
        title: 'Test merge_request', target_branch: 'master', source_branch: 'stable', author: user, target_project_id: fork_project.id
131 132
        response.status.should == 400
      end
Izaak Alpert committed
133 134

      it "should return 400 when target_branch is specified and for a different fork" do
Hiroyuki Sato committed
135
        post api("/projects/#{fork_project.id}/merge_requests", user2),
136
        title: 'Test merge_request', target_branch: 'master', source_branch: 'stable', author: user2, target_project_id: unrelated_project.id
Izaak Alpert committed
137 138 139 140
        response.status.should == 400
      end

      it "should return 201 when target_branch is specified and for the same project" do
Hiroyuki Sato committed
141
        post api("/projects/#{fork_project.id}/merge_requests", user2),
142
        title: 'Test merge_request', target_branch: 'master', source_branch: 'stable', author: user2, target_project_id: fork_project.id
Izaak Alpert committed
143 144
        response.status.should == 201
      end
145
    end
146 147
  end

148 149
  describe "PUT /projects/:id/merge_request/:merge_request_id to close MR" do
    it "should return merge_request" do
Hiroyuki Sato committed
150
      put api("/projects/#{project.id}/merge_request/#{merge_request.id}", user), state_event: "close"
151 152 153 154 155 156 157
      response.status.should == 200
      json_response['state'].should == 'closed'
    end
  end

  describe "PUT /projects/:id/merge_request/:merge_request_id to merge MR" do
    it "should return merge_request" do
Hiroyuki Sato committed
158
      put api("/projects/#{project.id}/merge_request/#{merge_request.id}", user), state_event: "merge"
159 160 161 162 163
      response.status.should == 200
      json_response['state'].should == 'merged'
    end
  end

164 165
  describe "PUT /projects/:id/merge_request/:merge_request_id" do
    it "should return merge_request" do
Hiroyuki Sato committed
166
      put api("/projects/#{project.id}/merge_request/#{merge_request.id}", user), title: "New title"
167
      response.status.should == 200
168
      json_response['title'].should == 'New title'
169
    end
170 171

    it "should return 422 when source_branch and target_branch are renamed the same" do
Hiroyuki Sato committed
172
      put api("/projects/#{project.id}/merge_request/#{merge_request.id}", user),
173
      source_branch: "master", target_branch: "master"
174 175 176 177
      response.status.should == 422
    end

    it "should return merge_request with renamed target_branch" do
Hiroyuki Sato committed
178
      put api("/projects/#{project.id}/merge_request/#{merge_request.id}", user), target_branch: "wiki"
179
      response.status.should == 200
180
      json_response['target_branch'].should == 'wiki'
181
    end
182 183 184 185
  end

  describe "POST /projects/:id/merge_request/:merge_request_id/comments" do
    it "should return comment" do
Hiroyuki Sato committed
186
      post api("/projects/#{project.id}/merge_request/#{merge_request.id}/comments", user), note: "My comment"
187
      response.status.should == 201
188
      json_response['note'].should == 'My comment'
189
    end
190 191

    it "should return 400 if note is missing" do
Hiroyuki Sato committed
192
      post api("/projects/#{project.id}/merge_request/#{merge_request.id}/comments", user)
193 194
      response.status.should == 400
    end
195 196

    it "should return 404 if note is attached to non existent merge request" do
Hiroyuki Sato committed
197
      post api("/projects/#{project.id}/merge_request/111/comments", user), note: "My comment"
198 199
      response.status.should == 404
    end
200 201
  end
end