summaryrefslogtreecommitdiff
path: root/spec/graphql/mutations/todos/mark_done_spec.rb
blob: 9723ac8af42f69997376c40c78bac28e7cc5473e (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Mutations::Todos::MarkDone do
  include GraphqlHelpers

  let_it_be(:project) { create(:project) }
  let_it_be(:issue) { create(:issue, project: project) }
  let_it_be(:current_user) { create(:user) }
  let_it_be(:author) { create(:user) }
  let_it_be(:other_user) { create(:user) }

  let_it_be(:todo1) { create(:todo, user: current_user, author: author, state: :pending, target: issue) }
  let_it_be(:todo2) { create(:todo, user: current_user, author: author, state: :done, target: issue) }

  let_it_be(:other_user_todo) { create(:todo, user: other_user, author: author, state: :pending) }

  let(:mutation) { described_class.new(object: nil, context: { current_user: current_user }, field: nil) }

  before_all do
    project.add_developer(current_user)
  end

  specify { expect(described_class).to require_graphql_authorizations(:update_todo) }

  describe '#resolve' do
    it 'marks a single todo as done' do
      result = mark_done_mutation(todo1)

      expect(todo1.reload.state).to eq('done')
      expect(todo2.reload.state).to eq('done')
      expect(other_user_todo.reload.state).to eq('pending')

      todo = result[:todo]
      expect(todo.id).to eq(todo1.id)
      expect(todo.state).to eq('done')
    end

    it 'handles a todo which is already done as expected' do
      result = mark_done_mutation(todo2)

      expect(todo1.reload.state).to eq('pending')
      expect(todo2.reload.state).to eq('done')
      expect(other_user_todo.reload.state).to eq('pending')

      todo = result[:todo]
      expect(todo.id).to eq(todo2.id)
      expect(todo.state).to eq('done')
    end

    it 'ignores requests for todos which do not belong to the current user' do
      expect { mark_done_mutation(other_user_todo) }.to raise_error(Gitlab::Graphql::Errors::ResourceNotAvailable)

      expect(todo1.reload.state).to eq('pending')
      expect(todo2.reload.state).to eq('done')
      expect(other_user_todo.reload.state).to eq('pending')
    end

    it 'ignores invalid GIDs' do
      expect { mutation.resolve(id: author.to_global_id.to_s) }
        .to raise_error(::GraphQL::CoercionError)

      expect(todo1.reload.state).to eq('pending')
      expect(todo2.reload.state).to eq('done')
      expect(other_user_todo.reload.state).to eq('pending')
    end
  end

  def mark_done_mutation(todo)
    mutation.resolve(id: global_id_of(todo))
  end
end