summaryrefslogtreecommitdiff
path: root/spec/requests/api/graphql/mutations/custom_emoji/destroy_spec.rb
blob: 7d25206e61714976b494733b151dafab73c057d9 (plain)
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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe 'Deletion of custom emoji' do
  include GraphqlHelpers

  let_it_be(:group) { create(:group) }
  let_it_be(:current_user) { create(:user) }
  let_it_be(:user2) { create(:user) }
  let_it_be_with_reload(:custom_emoji) { create(:custom_emoji, group: group, creator: user2) }

  let(:mutation) do
    variables = {
      id: GitlabSchema.id_from_object(custom_emoji).to_s
    }

    graphql_mutation(:destroy_custom_emoji, variables)
  end

  shared_examples 'does not delete custom emoji' do
    it 'does not change count' do
      expect { post_graphql_mutation(mutation, current_user: current_user) }.not_to change(CustomEmoji, :count)
    end
  end

  shared_examples 'deletes custom emoji' do
    it 'changes count' do
      expect { post_graphql_mutation(mutation, current_user: current_user) }.to change(CustomEmoji, :count).by(-1)
    end
  end

  context 'when the user' do
    context 'has no permissions' do
      it_behaves_like 'does not delete custom emoji'
    end

    context 'when the user is developer and not creator of custom emoji' do
      before do
        group.add_developer(current_user)
      end

      it_behaves_like 'does not delete custom emoji'
    end
  end

  context 'when user' do
    context 'is maintainer' do
      before do
        group.add_maintainer(current_user)
      end

      it_behaves_like 'deletes custom emoji'
    end

    context 'is owner' do
      before do
        group.add_owner(current_user)
      end

      it_behaves_like 'deletes custom emoji'
    end

    context 'is developer and creator of the emoji' do
      before do
        group.add_developer(current_user)
        custom_emoji.update_attribute(:creator, current_user)
      end

      it_behaves_like 'deletes custom emoji'

      context 'when the custom_emoji feature flag is disabled' do
        before do
          stub_feature_flags(custom_emoji: false)
        end

        it_behaves_like 'does not delete custom emoji'

        it 'returns an error' do
          post_graphql_mutation(mutation, current_user: current_user)

          expect_graphql_errors_to_include('Custom emoji feature is disabled')
        end
      end
    end
  end
end