summaryrefslogtreecommitdiff
path: root/spec/requests/api/graphql/mutations/notes/update_spec.rb
blob: 958f640995a2d002ecd5b261b2c5accb3c16807a (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
# frozen_string_literal: true

require 'spec_helper'

describe 'Updating a Note' do
  include GraphqlHelpers

  let!(:note) { create(:note, note: original_body) }
  let(:original_body) { 'Initial body text' }
  let(:updated_body) { 'Updated body text' }
  let(:mutation) do
    variables = {
      id: GitlabSchema.id_from_object(note).to_s,
      body: updated_body
    }

    graphql_mutation(:update_note, variables)
  end

  def mutation_response
    graphql_mutation_response(:update_note)
  end

  context 'when the user does not have permission' do
    let(:current_user) { create(:user) }

    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']

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

      expect(note.reload.note).to eq(original_body)
    end
  end

  context 'when the user has permission' do
    let(:current_user) { note.author }

    it_behaves_like 'a Note mutation when the given resource id is not for a Note'

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

      expect(note.reload.note).to eq(updated_body)
    end

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

      expect(mutation_response['note']['body']).to eq(updated_body)
    end

    context 'when there are ActiveRecord validation errors' do
      let(:updated_body) { '' }

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

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

        expect(note.reload.note).to eq(original_body)
      end

      it 'returns the Note with its original body' do
        post_graphql_mutation(mutation, current_user: current_user)

        expect(mutation_response['note']['body']).to eq(original_body)
      end
    end
  end
end