update_spec.rb 4.02 KB
Newer Older
1 2 3 4
# frozen_string_literal: true

require 'spec_helper'

5
RSpec.describe Mutations::Epics::Update do
6 7 8 9
  include GraphqlHelpers

  let_it_be(:current_user) { create(:user) }
  let_it_be(:group) { create(:group) }
10

11 12 13 14
  let(:label_1) { create(:group_label, group: group) }
  let(:label_2) { create(:group_label, group: group) }
  let(:label_3) { create(:group_label, group: group) }
  let(:epic) { create(:epic, group: group, title: 'original title', labels: [label_2]) }
15 16 17 18 19 20 21 22

  let(:attributes) do
    {
      title: 'updated title',
      description: 'some description',
      start_date_fixed: '2019-09-17',
      due_date_fixed: '2019-09-18',
      start_date_is_fixed: true,
23 24
      due_date_is_fixed: true,
      confidential: true
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
    }
  end

  let(:mutation) do
    params = { group_path: group.full_path, iid: epic.iid.to_s }.merge(attributes)

    graphql_mutation(:update_epic, params)
  end

  def mutation_response
    graphql_mutation_response(:update_epic)
  end

  context 'when the user does not have permission' do
    before do
      stub_licensed_features(epics: true)
    end

43
    it_behaves_like 'a mutation that returns a top-level access error'
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81

    it 'does not update the epic' do
      post_graphql_mutation(mutation, current_user: current_user)

      expect(epic.reload.title).to eq('original title')
    end
  end

  context 'when the user has permission' do
    before do
      epic.group.add_developer(current_user)
    end

    context 'when epics are disabled' do
      before do
        stub_licensed_features(epics: false)
      end

      it_behaves_like 'a mutation that returns top-level errors',
        errors: ['The resource that you are attempting to access does not '\
                 'exist or you don\'t have permission to perform this action']
    end

    context 'when epics are enabled' do
      before do
        stub_licensed_features(epics: true)
      end

      it 'updates the epic' do
        post_graphql_mutation(mutation, current_user: current_user)

        epic_hash = mutation_response['epic']
        expect(epic_hash['title']).to eq('updated title')
        expect(epic_hash['description']).to eq('some description')
        expect(epic_hash['startDateFixed']).to eq('2019-09-17')
        expect(epic_hash['startDateIsFixed']).to eq(true)
        expect(epic_hash['dueDateFixed']).to eq('2019-09-18')
        expect(epic_hash['dueDateIsFixed']).to eq(true)
82
        expect(epic_hash['confidential']).to eq(true)
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
      end

      context 'when closing the epic' do
        let(:attributes) { { state_event: 'CLOSE' } }

        it 'closes open epic' do
          post_graphql_mutation(mutation, current_user: current_user)

          expect(epic.reload).to be_closed
        end
      end

      context 'when reopening the epic' do
        let(:attributes) { { state_event: 'REOPEN' } }

        it 'allows epic to be reopend' do
          epic.update!(state: 'closed')

          post_graphql_mutation(mutation, current_user: current_user)

          expect(epic.reload).to be_open
        end
      end

107 108
      context 'when changing labels of the epic' do
        let(:attributes) { { add_label_ids: [label_1.id, label_3.id], remove_label_ids: label_2.id } }
109

110 111 112 113 114 115 116
        it 'adds and removes labels correctly' do
          post_graphql_mutation(mutation, current_user: current_user)

          expect(epic.reload.labels).to match_array([label_1, label_3])
        end
      end

117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
      context 'when there are ActiveRecord validation errors' do
        let(:attributes) { { title: '' } }

        it_behaves_like 'a mutation that returns errors in the response',
          errors: ["Title can't be blank"]

        it 'does not update the epic' do
          post_graphql_mutation(mutation, current_user: current_user)

          expect(mutation_response['epic']['title']).to eq('original title')
        end
      end

      context 'when the list of attributes is empty' do
        let(:attributes) { {} }

        it_behaves_like 'a mutation that returns top-level errors',
134
          errors: ['The list of epic attributes is empty']
135 136 137 138
      end
    end
  end
end