/spec/models/integrations/base_chat_notification_spec.rb

https://github.com/gitlabhq/gitlabhq · Ruby · 306 lines · 232 code · 73 blank · 1 comment · 0 complexity · d5cf2f37d6a1d0f67e1cb9676d89cde9 MD5 · raw file

  1. # frozen_string_literal: true
  2. require 'spec_helper'
  3. RSpec.describe Integrations::BaseChatNotification do
  4. describe 'validations' do
  5. before do
  6. allow(subject).to receive(:activated?).and_return(true)
  7. allow(subject).to receive(:default_channel_placeholder).and_return('placeholder')
  8. allow(subject).to receive(:webhook_placeholder).and_return('placeholder')
  9. end
  10. it { is_expected.to validate_presence_of :webhook }
  11. it { is_expected.to validate_inclusion_of(:labels_to_be_notified_behavior).in_array(%w[match_any match_all]).allow_blank }
  12. end
  13. describe '#execute' do
  14. subject(:chat_integration) { described_class.new }
  15. let_it_be(:project) { create(:project, :repository) }
  16. let(:user) { create(:user) }
  17. let(:webhook_url) { 'https://example.gitlab.com/' }
  18. let(:data) { Gitlab::DataBuilder::Push.build_sample(subject.project, user) }
  19. before do
  20. allow(chat_integration).to receive_messages(
  21. project: project,
  22. project_id: project.id,
  23. webhook: webhook_url
  24. )
  25. WebMock.stub_request(:post, webhook_url)
  26. subject.active = true
  27. end
  28. context 'with a repository' do
  29. it 'returns true' do
  30. expect(chat_integration).to receive(:notify).and_return(true)
  31. expect(chat_integration.execute(data)).to be true
  32. end
  33. end
  34. context 'with an empty repository' do
  35. it 'returns true' do
  36. subject.project = create(:project, :empty_repo)
  37. expect(chat_integration).to receive(:notify).and_return(true)
  38. expect(chat_integration.execute(data)).to be true
  39. end
  40. end
  41. context 'with a project with name containing spaces' do
  42. it 'does not remove spaces' do
  43. allow(project).to receive(:full_name).and_return('Project Name')
  44. expect(chat_integration).to receive(:get_message).with(any_args, hash_including(project_name: 'Project Name'))
  45. chat_integration.execute(data)
  46. end
  47. end
  48. context 'when the data object has a label' do
  49. let_it_be(:label) { create(:label, name: 'Bug') }
  50. let_it_be(:label_2) { create(:label, name: 'Community contribution') }
  51. let_it_be(:label_3) { create(:label, name: 'Backend') }
  52. let_it_be(:issue) { create(:labeled_issue, project: project, labels: [label, label_2, label_3]) }
  53. let_it_be(:note) { create(:note, noteable: issue, project: project) }
  54. let(:data) { Gitlab::DataBuilder::Note.build(note, user) }
  55. shared_examples 'notifies the chat integration' do
  56. specify do
  57. expect(chat_integration).to receive(:notify).with(any_args)
  58. chat_integration.execute(data)
  59. end
  60. end
  61. shared_examples 'does not notify the chat integration' do
  62. specify do
  63. expect(chat_integration).not_to receive(:notify).with(any_args)
  64. chat_integration.execute(data)
  65. end
  66. end
  67. it_behaves_like 'notifies the chat integration'
  68. context 'with label filter' do
  69. subject(:chat_integration) { described_class.new(labels_to_be_notified: '~Bug') }
  70. it_behaves_like 'notifies the chat integration'
  71. context 'MergeRequest events' do
  72. let(:data) { create(:merge_request, labels: [label]).to_hook_data(user) }
  73. it_behaves_like 'notifies the chat integration'
  74. end
  75. context 'Issue events' do
  76. let(:data) { issue.to_hook_data(user) }
  77. it_behaves_like 'notifies the chat integration'
  78. end
  79. end
  80. context 'when labels_to_be_notified_behavior is not defined' do
  81. subject(:chat_integration) { described_class.new(labels_to_be_notified: label_filter) }
  82. context 'no matching labels' do
  83. let(:label_filter) { '~some random label' }
  84. it_behaves_like 'does not notify the chat integration'
  85. end
  86. context 'only one label matches' do
  87. let(:label_filter) { '~some random label, ~Bug' }
  88. it_behaves_like 'notifies the chat integration'
  89. end
  90. end
  91. context 'when labels_to_be_notified_behavior is blank' do
  92. subject(:chat_integration) { described_class.new(labels_to_be_notified: label_filter, labels_to_be_notified_behavior: '') }
  93. context 'no matching labels' do
  94. let(:label_filter) { '~some random label' }
  95. it_behaves_like 'does not notify the chat integration'
  96. end
  97. context 'only one label matches' do
  98. let(:label_filter) { '~some random label, ~Bug' }
  99. it_behaves_like 'notifies the chat integration'
  100. end
  101. end
  102. context 'when labels_to_be_notified_behavior is match_any' do
  103. subject(:chat_integration) do
  104. described_class.new(
  105. labels_to_be_notified: label_filter,
  106. labels_to_be_notified_behavior: 'match_any'
  107. )
  108. end
  109. context 'no label filter' do
  110. let(:label_filter) { nil }
  111. it_behaves_like 'notifies the chat integration'
  112. end
  113. context 'no matching labels' do
  114. let(:label_filter) { '~some random label' }
  115. it_behaves_like 'does not notify the chat integration'
  116. end
  117. context 'only one label matches' do
  118. let(:label_filter) { '~some random label, ~Bug' }
  119. it_behaves_like 'notifies the chat integration'
  120. end
  121. end
  122. context 'when labels_to_be_notified_behavior is match_all' do
  123. subject(:chat_integration) do
  124. described_class.new(
  125. labels_to_be_notified: label_filter,
  126. labels_to_be_notified_behavior: 'match_all'
  127. )
  128. end
  129. context 'no label filter' do
  130. let(:label_filter) { nil }
  131. it_behaves_like 'notifies the chat integration'
  132. end
  133. context 'no matching labels' do
  134. let(:label_filter) { '~some random label' }
  135. it_behaves_like 'does not notify the chat integration'
  136. end
  137. context 'only one label matches' do
  138. let(:label_filter) { '~some random label, ~Bug' }
  139. it_behaves_like 'does not notify the chat integration'
  140. end
  141. context 'labels matches exactly' do
  142. let(:label_filter) { '~Bug, ~Backend, ~Community contribution' }
  143. it_behaves_like 'notifies the chat integration'
  144. end
  145. context 'labels matches but object has more' do
  146. let(:label_filter) { '~Bug, ~Backend' }
  147. it_behaves_like 'notifies the chat integration'
  148. end
  149. context 'labels are distributed on multiple objects' do
  150. let(:label_filter) { '~Bug, ~Backend' }
  151. let(:data) do
  152. Gitlab::DataBuilder::Note.build(note, user).merge({
  153. issue: {
  154. labels: [
  155. { title: 'Bug' }
  156. ]
  157. },
  158. merge_request: {
  159. labels: [
  160. {
  161. title: 'Backend'
  162. }
  163. ]
  164. }
  165. })
  166. end
  167. it_behaves_like 'does not notify the chat integration'
  168. end
  169. end
  170. end
  171. context 'with "channel" property' do
  172. before do
  173. allow(chat_integration).to receive(:channel).and_return(channel)
  174. end
  175. context 'empty string' do
  176. let(:channel) { '' }
  177. it 'does not include the channel' do
  178. expect(chat_integration).to receive(:notify).with(any_args, hash_excluding(:channel)).and_return(true)
  179. expect(chat_integration.execute(data)).to be(true)
  180. end
  181. end
  182. context 'empty spaces' do
  183. let(:channel) { ' ' }
  184. it 'does not include the channel' do
  185. expect(chat_integration).to receive(:notify).with(any_args, hash_excluding(:channel)).and_return(true)
  186. expect(chat_integration.execute(data)).to be(true)
  187. end
  188. end
  189. end
  190. shared_examples 'with channel specified' do |channel, expected_channels|
  191. before do
  192. allow(chat_integration).to receive(:push_channel).and_return(channel)
  193. end
  194. it 'notifies all channels' do
  195. expect(chat_integration).to receive(:notify).with(any_args, hash_including(channel: expected_channels)).and_return(true)
  196. expect(chat_integration.execute(data)).to be(true)
  197. end
  198. end
  199. context 'with single channel specified' do
  200. it_behaves_like 'with channel specified', 'slack-integration', ['slack-integration']
  201. end
  202. context 'with multiple channel names specified' do
  203. it_behaves_like 'with channel specified', 'slack-integration,#slack-test', ['slack-integration', '#slack-test']
  204. end
  205. context 'with multiple channel names with spaces specified' do
  206. it_behaves_like 'with channel specified', 'slack-integration, #slack-test, @UDLP91W0A', ['slack-integration', '#slack-test', '@UDLP91W0A']
  207. end
  208. end
  209. describe '#default_channel_placeholder' do
  210. it 'raises an error' do
  211. expect { subject.default_channel_placeholder }.to raise_error(NotImplementedError)
  212. end
  213. end
  214. describe '#webhook_placeholder' do
  215. it 'raises an error' do
  216. expect { subject.webhook_placeholder }.to raise_error(NotImplementedError)
  217. end
  218. end
  219. describe '#event_channel_name' do
  220. it 'returns the channel field name for the given event' do
  221. expect(subject.event_channel_name(:event)).to eq('event_channel')
  222. end
  223. end
  224. describe '#event_channel_value' do
  225. it 'returns the channel field value for the given event' do
  226. subject.push_channel = '#pushes'
  227. expect(subject.event_channel_value(:push)).to eq('#pushes')
  228. end
  229. it 'raises an error for unsupported events' do
  230. expect { subject.event_channel_value(:foo) }.to raise_error(NoMethodError)
  231. end
  232. end
  233. end