update_invalid_issuable.rb 1.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
shared_examples 'update invalid issuable' do |klass|
  let(:params) do
    {
      namespace_id: project.namespace.path,
      project_id: project.path,
      id: issuable.iid
    }
  end

  let(:issuable) do
    klass == Issue ? issue : merge_request
  end

  before do
    if klass == Issue
      params.merge!(issue: { title: "any" })
    else
      params.merge!(merge_request: { title: "any" })
    end
  end

  context 'when updating causes conflicts' do
    before do
24 25
      allow_any_instance_of(issuable.class).to receive(:save)
        .and_raise(ActiveRecord::StaleObjectError.new(issuable, :save))
26 27
    end

28 29
    it 'renders edit when format is html' do
      put :update, params
30

31 32
      expect(response).to render_template(:edit)
      expect(assigns[:conflict]).to be_truthy
33 34 35
    end

    it 'renders json error message when format is json' do
Sean McGivern's avatar
Sean McGivern committed
36
      params[:format] = "json"
37 38 39 40 41 42 43 44

      put :update, params

      expect(response.status).to eq(409)
      expect(JSON.parse(response.body)).to have_key('errors')
    end
  end

45 46 47 48 49
  context 'when updating an invalid issuable' do
    before do
      key = klass == Issue ? :issue : :merge_request
      params[key][:title] = ""
    end
50

51 52
    it 'renders edit when merge request is invalid' do
      put :update, params
53

54
      expect(response).to render_template(:edit)
55 56 57
    end
  end
end