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

update_service_spec.rb « alerts « alert_management « services « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e185e67c5cf9f6d6f78ed15a732ff409db9f4ae8 (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
# frozen_string_literal: true

require 'spec_helper'

describe AlertManagement::Alerts::UpdateService do
  let_it_be(:user_with_permissions) { create(:user) }
  let_it_be(:user_without_permissions) { create(:user) }
  let_it_be(:alert, reload: true) { create(:alert_management_alert) }
  let_it_be(:project) { alert.project }

  let(:current_user) { user_with_permissions }
  let(:params) { {} }

  let(:service) { described_class.new(alert, current_user, params) }

  before_all do
    project.add_developer(user_with_permissions)
  end

  describe '#execute' do
    subject(:response) { service.execute }

    context 'when the current_user is nil' do
      let(:current_user) { nil }

      it 'results in an error' do
        expect(response).to be_error
        expect(response.message).to eq('You have no permissions')
      end
    end

    context 'when user does not have permission to update alerts' do
      let(:current_user) { user_without_permissions }

      it 'results in an error' do
        expect(response).to be_error
        expect(response.message).to eq('You have no permissions')
      end
    end

    context 'when no parameters are included' do
      it 'results in an error' do
        expect(response).to be_error
        expect(response.message).to eq('Please provide attributes to update')
      end
    end

    context 'when an error occures during update' do
      let(:params) { { title: nil } }

      it 'results in an error' do
        expect { response }.not_to change { alert.reload.notes.count }
        expect(response).to be_error
        expect(response.message).to eq("Title can't be blank")
      end
    end

    context 'when a model attribute is included without assignees' do
      let(:params) { { title: 'This is an updated alert.' } }

      it 'updates the attribute' do
        original_title = alert.title

        expect { response }.to change { alert.title }.from(original_title).to(params[:title])
        expect(response).to be_success
      end

      it 'skips adding a todo' do
        expect { response }.not_to change(Todo, :count)
      end
    end

    context 'when assignees are included' do
      let(:params) { { assignees: [user_with_permissions] } }

      after do
        alert.assignees = []
      end

      it 'assigns the user' do
        expect { response }.to change { alert.reload.assignees }.from([]).to(params[:assignees])
        expect(response).to be_success
      end

      it 'creates a system note for the assignment' do
        expect { response }.to change { alert.reload.notes.count }.by(1)
      end

      it 'adds a todo' do
        expect { response }.to change { Todo.where(user: user_with_permissions).count }.by(1)
      end

      context 'when current user is not the assignee' do
        let(:assignee_user) { create(:user) }
        let(:params) { { assignees: [assignee_user] } }

        it 'skips adding todo for assignee without permission to read alert' do
          expect { response }.not_to change(Todo, :count)
        end

        context 'when assignee has read permission' do
          before do
            project.add_developer(assignee_user)
          end

          it 'adds a todo' do
            response

            expect(Todo.first.author).to eq(current_user)
          end
        end

        context 'when current_user is nil' do
          let(:current_user) { nil }

          it 'skips adding todo if current_user is nil' do
            project.add_developer(assignee_user)

            expect { response }.not_to change(Todo, :count)
          end
        end
      end

      context 'with multiple users included' do
        let(:params) { { assignees: [user_with_permissions, user_without_permissions] } }

        it 'assigns the first permissioned user' do
          expect { response }.to change { alert.reload.assignees }.from([]).to([user_with_permissions])
          expect(response).to be_success
        end
      end
    end
  end
end