File: update_spec.rb

package info (click to toggle)
gitlab 17.6.5-19
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 629,368 kB
  • sloc: ruby: 1,915,304; javascript: 557,307; sql: 60,639; xml: 6,509; sh: 4,567; makefile: 1,239; python: 406
file content (102 lines) | stat: -rw-r--r-- 3,304 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe 'BranchRuleUpdate', feature_category: :source_code_management do
  include GraphqlHelpers

  let_it_be(:project) { create(:project, :public) }
  let_it_be(:user) { create(:user) }
  let_it_be(:protected_branch) do
    create(:protected_branch, project: project)
  end

  let!(:allow_force_push) { !protected_branch.allow_force_push }

  let(:current_user) { user }
  let(:branch_rule) { Projects::BranchRule.new(project, protected_branch) }
  let(:global_id) { branch_rule.to_global_id }
  let(:name) { 'new_name' }
  let(:merge_access_levels) { [{ access_level: 0 }] }
  let(:push_access_levels) { [{ access_level: 0 }] }
  let(:mutation) { graphql_mutation(:branch_rule_update, params) }
  let(:mutation_response) { graphql_mutation_response(:branch_rule_update) }
  let(:params) do
    {
      id: global_id,
      name: name,
      branch_protection: {
        allow_force_push: allow_force_push,
        merge_access_levels: merge_access_levels,
        push_access_levels: push_access_levels
      }
    }
  end

  subject(:post_mutation) { post_graphql_mutation(mutation, current_user: user) }

  context 'when the user does not have permission' do
    before_all do
      project.add_developer(user)
    end

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

    it 'does not update the branch rule' do
      expect { post_mutation }.not_to change { protected_branch.reload.attributes }
    end
  end

  context 'when the user can update a branch rules' do
    before_all do
      project.add_maintainer(user)
    end

    it 'updates the branch rule' do
      post_mutation

      expect(protected_branch.reload.name).to eq(name)
      expect(protected_branch.allow_force_push).to eq(allow_force_push)

      merge_access_level = an_object_having_attributes(**merge_access_levels.first)
      expect(protected_branch.merge_access_levels).to contain_exactly(merge_access_level)

      push_access_level = an_object_having_attributes(**push_access_levels.first)
      expect(protected_branch.push_access_levels).to contain_exactly(push_access_level)
    end

    it 'returns the updated branch rule' do
      post_mutation

      expect(mutation_response).to have_key('branchRule')
      expect(mutation_response['branchRule']['name']).to eq(name)
      expect(mutation_response['errors']).to be_empty
    end

    context 'when name already exists for the project' do
      before do
        create(:protected_branch, project: project, name: name)
      end

      it 'returns an error' do
        post_mutation

        expect(mutation_response['errors'].first).to eq('Name has already been taken')
      end
    end

    context 'when branch rule cannot be found' do
      let(:global_id) { project.to_gid.to_s }
      let(:error_message) { %("#{global_id}" does not represent an instance of Projects::BranchRule) }
      let(:global_id_error) { a_hash_including('message' => a_string_including(error_message)) }

      it 'returns an error' do
        post_mutation

        expect(graphql_errors).to include(global_id_error)
      end
    end
  end
end