summaryrefslogtreecommitdiff
path: root/spec/requests/api/graphql/mutations/design_management/delete_spec.rb
blob: 10376305b3e9c6a104ed5188129049aec63ab0ec (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
# frozen_string_literal: true

require "spec_helper"

describe "deleting designs" do
  include GraphqlHelpers
  include DesignManagementTestHelpers

  let(:developer) { create(:user) }
  let(:current_user) { developer }
  let(:issue) { create(:issue) }
  let(:project) { issue.project }
  let(:designs) { create_designs }
  let(:variables) { {} }

  let(:mutation) do
    input = {
      project_path: project.full_path,
      iid: issue.iid,
      filenames: designs.map(&:filename)
    }.merge(variables)
    graphql_mutation(:design_management_delete, input)
  end

  let(:mutation_response) { graphql_mutation_response(:design_management_delete) }

  def mutate!
    post_graphql_mutation(mutation, current_user: current_user)
  end

  before do
    enable_design_management

    project.add_developer(developer)
  end

  shared_examples 'a failed request' do
    let(:the_error) { be_present }

    it 'reports an error' do
      mutate!

      expect(graphql_errors).to include(a_hash_including('message' => the_error))
    end
  end

  context 'the designs list is empty' do
    it_behaves_like 'a failed request' do
      let(:designs) { [] }
      let(:the_error) { a_string_matching %r/was provided invalid value/ }
    end
  end

  context 'the designs list contains filenames we cannot find' do
    it_behaves_like 'a failed request' do
      let(:designs) { %w/foo bar baz/.map { |fn| OpenStruct.new(filename: fn) } }
      let(:the_error) { a_string_matching %r/filenames were not found/ }
    end
  end

  context 'the current user does not have developer access' do
    it_behaves_like 'a failed request' do
      let(:current_user) { create(:user) }
      let(:the_error) { a_string_matching %r/you don't have permission/ }
    end
  end

  context "when the issue does not exist" do
    it_behaves_like 'a failed request' do
      let(:variables) { { iid: "1234567890" } }
      let(:the_error) { a_string_matching %r/does not exist/ }
    end
  end

  context "when saving the designs raises an error" do
    let(:designs) { create_designs(1) }

    it "responds with errors" do
      expect_next_instance_of(::DesignManagement::DeleteDesignsService) do |service|
        expect(service)
          .to receive(:execute)
          .and_return({ status: :error, message: "Something went wrong" })
      end

      mutate!

      expect(mutation_response).to include('errors' => include(eq "Something went wrong"))
    end
  end

  context 'one of the designs is already deleted' do
    let(:designs) do
      create_designs(2).push(create(:design, :with_file, deleted: true, issue: issue))
    end

    it 'reports an error' do
      mutate!

      expect(graphql_errors).to be_present
    end
  end

  context 'when the user names designs to delete' do
    before do
      create_designs(1)
    end

    let!(:designs) { create_designs(2) }

    it 'deletes the designs' do
      expect { mutate! }
        .to change { issue.reset.designs.current.count }.from(3).to(1)
    end

    it 'has no errors' do
      mutate!

      expect(mutation_response).to include('errors' => be_empty)
    end
  end

  private

  def create_designs(how_many = 2)
    create_list(:design, how_many, :with_file, issue: issue)
  end
end