create_spec.rb 4.85 KB
Newer Older
1 2 3 4
# frozen_string_literal: true

require 'spec_helper'

5
RSpec.describe 'Creating an Iteration' do
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
  include GraphqlHelpers

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

  let(:start_date) { Time.now.strftime('%F') }
  let(:end_date) { 1.day.from_now.strftime('%F') }
  let(:attributes) do
    {
        title: 'title',
        description: 'some description',
        start_date: start_date,
        due_date: end_date
    }
  end

22 23 24 25 26
  let(:params) do
    {
      group_path: group.full_path
    }
  end
27

28 29
  let(:mutation) do
    graphql_mutation(:create_iteration, params.merge(attributes))
30 31 32 33 34 35 36 37 38 39 40
  end

  def mutation_response
    graphql_mutation_response(:create_iteration)
  end

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

41
    it_behaves_like 'a mutation that returns a top-level access error'
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67

    it 'does not create iteration' do
      expect { post_graphql_mutation(mutation, current_user: current_user) }.not_to change(Iteration, :count)
    end
  end

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

    context 'when iterations are disabled' do
      before do
        stub_licensed_features(iterations: 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 iterations are enabled' do
      before do
        stub_licensed_features(iterations: true)
      end

68
      it 'creates the iteration for a group' do
69 70 71 72 73 74 75 76 77 78 79
        post_graphql_mutation(mutation, current_user: current_user)

        iteration_hash = mutation_response['iteration']
        aggregate_failures do
          expect(iteration_hash['title']).to eq('title')
          expect(iteration_hash['description']).to eq('some description')
          expect(iteration_hash['startDate']).to eq(start_date)
          expect(iteration_hash['dueDate']).to eq(end_date)
        end
      end

80 81 82 83 84 85 86 87 88
      context 'when a project_path is given' do
        let_it_be(:project) { create(:project, namespace: group) }
        let(:params) { { project_path: project.full_path } }

        before do
          project.add_developer(current_user)
        end

        it 'creates the iteration for a project' do
89 90 91 92
          allow_next_instance_of(Iteration) do |iteration|
            allow(iteration).to receive(:skip_project_validation).and_return(true)
          end

93 94 95 96 97 98 99 100 101 102 103 104
          post_graphql_mutation(mutation, current_user: current_user)

          iteration_hash = mutation_response['iteration']
          aggregate_failures do
            expect(iteration_hash['title']).to eq('title')
            expect(iteration_hash['description']).to eq('some description')
            expect(iteration_hash['startDate']).to eq(start_date)
            expect(iteration_hash['dueDate']).to eq(end_date)
          end
        end
      end

105 106 107 108
      context 'when there are ActiveRecord validation errors' do
        let(:attributes) { { title: '' } }

        it_behaves_like 'a mutation that returns errors in the response',
109
                        errors: ["Start date can't be blank", "Due date can't be blank", "Title can't be blank"]
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125

        it 'does not create the iteration' do
          expect { post_graphql_mutation(mutation, current_user: current_user) }.not_to change(Iteration, :count)
        end
      end

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

        it_behaves_like 'a mutation that returns top-level errors',
                        errors: ['The list of iteration attributes is empty']

        it 'does not create the iteration' do
          expect { post_graphql_mutation(mutation, current_user: current_user) }.not_to change(Iteration, :count)
        end
      end
126 127 128 129 130

      context 'when the params contains neither group nor project path' do
        let(:params) { {} }

        it_behaves_like 'a mutation that returns top-level errors',
131
                        errors: ['Exactly one of group_path or project_path arguments is required']
132 133 134 135 136 137 138 139 140 141

        it 'does not create the iteration' do
          expect { post_graphql_mutation(mutation, current_user: current_user) }.not_to change(Iteration, :count)
        end
      end

      context 'when the params contains both group and project path' do
        let(:params) { { group_path: group.full_path, project_path: 'doesnotreallymatter' } }

        it_behaves_like 'a mutation that returns top-level errors',
142
                        errors: ['Exactly one of group_path or project_path arguments is required']
143 144 145 146 147

        it 'does not create the iteration' do
          expect { post_graphql_mutation(mutation, current_user: current_user) }.not_to change(Iteration, :count)
        end
      end
148 149 150
    end
  end
end