Welcome to mirror list, hosted at ThFree Co, Russian Federation.

toggle_spec.rb « award_emojis « mutations « graphql « api « requests « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 6dba2b5835709815a9fa5679ff2482c3c392c865 (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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe 'Toggling an AwardEmoji', feature_category: :not_owned do
  include GraphqlHelpers

  let_it_be(:current_user) { create(:user) }
  let_it_be(:project, reload: true) { create(:project) }
  let_it_be(:awardable) { create(:note, project: project) }

  let(:emoji_name) { 'thumbsup' }
  let(:mutation) do
    variables = {
      awardable_id: GitlabSchema.id_from_object(awardable).to_s,
      name: emoji_name
    }

    graphql_mutation(:award_emoji_toggle, variables)
  end

  def mutation_response
    graphql_mutation_response(:award_emoji_toggle)
  end

  shared_examples 'a mutation that does not create or destroy an AwardEmoji' do
    specify do
      expect do
        post_graphql_mutation(mutation, current_user: current_user)
      end.not_to change { AwardEmoji.count }
    end
  end

  def create_award_emoji(user)
    create(:award_emoji, name: emoji_name, awardable: awardable, user: user)
  end

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

    context 'when the given awardable is not an Awardable' do
      let(:awardable) { create(:label, project: project) }

      it_behaves_like 'a mutation that does not create or destroy an AwardEmoji'

      it_behaves_like 'a mutation that returns top-level errors' do
        let(:match_errors) { include(/was provided invalid value for awardableId/) }
      end
    end

    context 'when the given awardable is an Awardable but still cannot be awarded an emoji' do
      let(:awardable) { create(:system_note, project: project) }

      it_behaves_like 'a mutation that does not create or destroy an AwardEmoji'

      it_behaves_like 'a mutation that returns top-level errors',
        errors: ['You cannot award emoji to this resource.']
    end

    context 'when the given awardable is an Awardable' do
      context 'when no emoji has been awarded by the current_user yet' do
        # Create an award emoji for another user. This therefore tests that
        # toggling is correctly scoped to the user's emoji only.
        let!(:award_emoji) { create_award_emoji(create(:user)) }

        it 'creates an emoji' do
          expect do
            post_graphql_mutation(mutation, current_user: current_user)
          end.to change { AwardEmoji.count }.by(1)
        end

        it 'returns the emoji' do
          post_graphql_mutation(mutation, current_user: current_user)

          expect(mutation_response['awardEmoji']['name']).to eq(emoji_name)
        end

        it 'returns toggledOn as true' do
          post_graphql_mutation(mutation, current_user: current_user)

          expect(mutation_response['toggledOn']).to eq(true)
        end

        describe 'marking Todos as done' do
          let(:user) { current_user }

          subject { post_graphql_mutation(mutation, current_user: user) }

          include_examples 'creating award emojis marks Todos as done'
        end

        context 'when there were active record validation errors' do
          before do
            expect_next_instance_of(AwardEmoji) do |award|
              expect(award).to receive(:valid?).at_least(:once).and_return(false)
              expect(award).to receive_message_chain(:errors, :full_messages).and_return(['Error 1', 'Error 2'])
            end
          end

          it_behaves_like 'a mutation that does not create or destroy an AwardEmoji'

          it_behaves_like 'a mutation that returns errors in the response', errors: ['Error 1', 'Error 2']

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

            expect(mutation_response).to have_key('awardEmoji')
            expect(mutation_response['awardEmoji']).to be_nil
          end
        end
      end

      context 'when an emoji has been awarded by the current_user' do
        let!(:award_emoji) { create_award_emoji(current_user) }

        it 'removes the emoji' do
          expect do
            post_graphql_mutation(mutation, current_user: current_user)
          end.to change { AwardEmoji.count }.by(-1)
        end

        it 'returns no errors' do
          post_graphql_mutation(mutation, current_user: current_user)

          expect(graphql_errors).to be_nil
        end

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

          expect(mutation_response).to have_key('awardEmoji')
          expect(mutation_response['awardEmoji']).to be_nil
        end

        it 'returns toggledOn as false' do
          post_graphql_mutation(mutation, current_user: current_user)

          expect(mutation_response['toggledOn']).to eq(false)
        end
      end
    end
  end

  context 'when the user does not have permission' do
    it_behaves_like 'a mutation that does not create or destroy an AwardEmoji'
    it_behaves_like 'a mutation that returns a top-level access error'
  end
end