PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/spec/models/project_services/hipchat_service_spec.rb

https://gitlab.com/subesokun/gitlab-ce
Ruby | 367 lines | 293 code | 74 blank | 0 comment | 8 complexity | 43397e044f40206fc928c93cdb047a05 MD5 | raw file
  1. require 'spec_helper'
  2. describe HipchatService, models: true do
  3. describe "Associations" do
  4. it { is_expected.to belong_to :project }
  5. it { is_expected.to have_one :service_hook }
  6. end
  7. describe 'Validations' do
  8. context 'when service is active' do
  9. before { subject.active = true }
  10. it { is_expected.to validate_presence_of(:token) }
  11. end
  12. context 'when service is inactive' do
  13. before { subject.active = false }
  14. it { is_expected.not_to validate_presence_of(:token) }
  15. end
  16. end
  17. describe "Execute" do
  18. let(:hipchat) { HipchatService.new }
  19. let(:user) { create(:user, username: 'username') }
  20. let(:project) { create(:project, name: 'project') }
  21. let(:api_url) { 'https://hipchat.example.com/v2/room/123456/notification?auth_token=verySecret' }
  22. let(:project_name) { project.name_with_namespace.gsub(/\s/, '') }
  23. let(:token) { 'verySecret' }
  24. let(:server_url) { 'https://hipchat.example.com'}
  25. let(:push_sample_data) do
  26. Gitlab::DataBuilder::Push.build_sample(project, user)
  27. end
  28. before(:each) do
  29. allow(hipchat).to receive_messages(
  30. project_id: project.id,
  31. project: project,
  32. room: 123456,
  33. server: server_url,
  34. token: token
  35. )
  36. WebMock.stub_request(:post, api_url)
  37. end
  38. it 'tests and return errors' do
  39. allow(hipchat).to receive(:execute).and_raise(StandardError, 'no such room')
  40. result = hipchat.test(push_sample_data)
  41. expect(result[:success]).to be_falsey
  42. expect(result[:result].to_s).to eq('no such room')
  43. end
  44. it 'uses v1 if version is provided' do
  45. allow(hipchat).to receive(:api_version).and_return('v1')
  46. expect(HipChat::Client).to receive(:new).with(
  47. token,
  48. api_version: 'v1',
  49. server_url: server_url
  50. ).and_return(double(:hipchat_service).as_null_object)
  51. hipchat.execute(push_sample_data)
  52. end
  53. it 'uses v2 as the version when nothing is provided' do
  54. allow(hipchat).to receive(:api_version).and_return('')
  55. expect(HipChat::Client).to receive(:new).with(
  56. token,
  57. api_version: 'v2',
  58. server_url: server_url
  59. ).and_return(double(:hipchat_service).as_null_object)
  60. hipchat.execute(push_sample_data)
  61. end
  62. context 'push events' do
  63. it "calls Hipchat API for push events" do
  64. hipchat.execute(push_sample_data)
  65. expect(WebMock).to have_requested(:post, api_url).once
  66. end
  67. it "creates a push message" do
  68. message = hipchat.send(:create_push_message, push_sample_data)
  69. push_sample_data[:object_attributes]
  70. branch = push_sample_data[:ref].gsub('refs/heads/', '')
  71. expect(message).to include("#{user.name} pushed to branch " \
  72. "<a href=\"#{project.web_url}/commits/#{branch}\">#{branch}</a> of " \
  73. "<a href=\"#{project.web_url}\">#{project_name}</a>")
  74. end
  75. end
  76. context 'tag_push events' do
  77. let(:push_sample_data) do
  78. Gitlab::DataBuilder::Push.build(
  79. project,
  80. user,
  81. Gitlab::Git::BLANK_SHA,
  82. '1' * 40,
  83. 'refs/tags/test',
  84. [])
  85. end
  86. it "calls Hipchat API for tag push events" do
  87. hipchat.execute(push_sample_data)
  88. expect(WebMock).to have_requested(:post, api_url).once
  89. end
  90. it "creates a tag push message" do
  91. message = hipchat.send(:create_push_message, push_sample_data)
  92. push_sample_data[:object_attributes]
  93. expect(message).to eq("#{user.name} pushed new tag " \
  94. "<a href=\"#{project.web_url}/commits/test\">test</a> to " \
  95. "<a href=\"#{project.web_url}\">#{project_name}</a>\n")
  96. end
  97. end
  98. context 'issue events' do
  99. let(:issue) { create(:issue, title: 'Awesome issue', description: 'please fix') }
  100. let(:issue_service) { Issues::CreateService.new(project, user) }
  101. let(:issues_sample_data) { issue_service.hook_data(issue, 'open') }
  102. it "calls Hipchat API for issue events" do
  103. hipchat.execute(issues_sample_data)
  104. expect(WebMock).to have_requested(:post, api_url).once
  105. end
  106. it "creates an issue message" do
  107. message = hipchat.send(:create_issue_message, issues_sample_data)
  108. obj_attr = issues_sample_data[:object_attributes]
  109. expect(message).to eq("#{user.name} opened " \
  110. "<a href=\"#{obj_attr[:url]}\">issue ##{obj_attr["iid"]}</a> in " \
  111. "<a href=\"#{project.web_url}\">#{project_name}</a>: " \
  112. "<b>Awesome issue</b>" \
  113. "<pre>please fix</pre>")
  114. end
  115. end
  116. context 'merge request events' do
  117. let(:merge_request) { create(:merge_request, description: 'please fix', title: 'Awesome merge request', target_project: project, source_project: project) }
  118. let(:merge_service) { MergeRequests::CreateService.new(project, user) }
  119. let(:merge_sample_data) { merge_service.hook_data(merge_request, 'open') }
  120. it "calls Hipchat API for merge requests events" do
  121. hipchat.execute(merge_sample_data)
  122. expect(WebMock).to have_requested(:post, api_url).once
  123. end
  124. it "creates a merge request message" do
  125. message = hipchat.send(:create_merge_request_message,
  126. merge_sample_data)
  127. obj_attr = merge_sample_data[:object_attributes]
  128. expect(message).to eq("#{user.name} opened " \
  129. "<a href=\"#{obj_attr[:url]}\">merge request !#{obj_attr["iid"]}</a> in " \
  130. "<a href=\"#{project.web_url}\">#{project_name}</a>: " \
  131. "<b>Awesome merge request</b>" \
  132. "<pre>please fix</pre>")
  133. end
  134. end
  135. context "Note events" do
  136. let(:user) { create(:user) }
  137. let(:project) { create(:project, creator_id: user.id) }
  138. context 'when commit comment event triggered' do
  139. let(:commit_note) do
  140. create(:note_on_commit, author: user, project: project,
  141. commit_id: project.repository.commit.id,
  142. note: 'a comment on a commit')
  143. end
  144. it "calls Hipchat API for commit comment events" do
  145. data = Gitlab::DataBuilder::Note.build(commit_note, user)
  146. hipchat.execute(data)
  147. expect(WebMock).to have_requested(:post, api_url).once
  148. message = hipchat.send(:create_message, data)
  149. obj_attr = data[:object_attributes]
  150. commit_id = Commit.truncate_sha(data[:commit][:id])
  151. title = hipchat.send(:format_title, data[:commit][:message])
  152. expect(message).to eq("#{user.name} commented on " \
  153. "<a href=\"#{obj_attr[:url]}\">commit #{commit_id}</a> in " \
  154. "<a href=\"#{project.web_url}\">#{project_name}</a>: " \
  155. "#{title}" \
  156. "<pre>a comment on a commit</pre>")
  157. end
  158. end
  159. context 'when merge request comment event triggered' do
  160. let(:merge_request) do
  161. create(:merge_request, source_project: project,
  162. target_project: project)
  163. end
  164. let(:merge_request_note) do
  165. create(:note_on_merge_request, noteable: merge_request,
  166. project: project,
  167. note: "merge request note")
  168. end
  169. it "calls Hipchat API for merge request comment events" do
  170. data = Gitlab::DataBuilder::Note.build(merge_request_note, user)
  171. hipchat.execute(data)
  172. expect(WebMock).to have_requested(:post, api_url).once
  173. message = hipchat.send(:create_message, data)
  174. obj_attr = data[:object_attributes]
  175. merge_id = data[:merge_request]['iid']
  176. title = data[:merge_request]['title']
  177. expect(message).to eq("#{user.name} commented on " \
  178. "<a href=\"#{obj_attr[:url]}\">merge request !#{merge_id}</a> in " \
  179. "<a href=\"#{project.web_url}\">#{project_name}</a>: " \
  180. "<b>#{title}</b>" \
  181. "<pre>merge request note</pre>")
  182. end
  183. end
  184. context 'when issue comment event triggered' do
  185. let(:issue) { create(:issue, project: project) }
  186. let(:issue_note) do
  187. create(:note_on_issue, noteable: issue, project: project,
  188. note: "issue note")
  189. end
  190. it "calls Hipchat API for issue comment events" do
  191. data = Gitlab::DataBuilder::Note.build(issue_note, user)
  192. hipchat.execute(data)
  193. message = hipchat.send(:create_message, data)
  194. obj_attr = data[:object_attributes]
  195. issue_id = data[:issue]['iid']
  196. title = data[:issue]['title']
  197. expect(message).to eq("#{user.name} commented on " \
  198. "<a href=\"#{obj_attr[:url]}\">issue ##{issue_id}</a> in " \
  199. "<a href=\"#{project.web_url}\">#{project_name}</a>: " \
  200. "<b>#{title}</b>" \
  201. "<pre>issue note</pre>")
  202. end
  203. end
  204. context 'when snippet comment event triggered' do
  205. let(:snippet) { create(:project_snippet, project: project) }
  206. let(:snippet_note) do
  207. create(:note_on_project_snippet, noteable: snippet,
  208. project: project,
  209. note: "snippet note")
  210. end
  211. it "calls Hipchat API for snippet comment events" do
  212. data = Gitlab::DataBuilder::Note.build(snippet_note, user)
  213. hipchat.execute(data)
  214. expect(WebMock).to have_requested(:post, api_url).once
  215. message = hipchat.send(:create_message, data)
  216. obj_attr = data[:object_attributes]
  217. snippet_id = data[:snippet]['id']
  218. title = data[:snippet]['title']
  219. expect(message).to eq("#{user.name} commented on " \
  220. "<a href=\"#{obj_attr[:url]}\">snippet ##{snippet_id}</a> in " \
  221. "<a href=\"#{project.web_url}\">#{project_name}</a>: " \
  222. "<b>#{title}</b>" \
  223. "<pre>snippet note</pre>")
  224. end
  225. end
  226. end
  227. context 'build events' do
  228. let(:pipeline) { create(:ci_empty_pipeline) }
  229. let(:build) { create(:ci_build, pipeline: pipeline) }
  230. let(:data) { Gitlab::DataBuilder::Build.build(build.reload) }
  231. context 'for failed' do
  232. before { build.drop }
  233. it "calls Hipchat API" do
  234. hipchat.execute(data)
  235. expect(WebMock).to have_requested(:post, api_url).once
  236. end
  237. it "creates a build message" do
  238. message = hipchat.send(:create_build_message, data)
  239. project_url = project.web_url
  240. project_name = project.name_with_namespace.gsub(/\s/, '')
  241. sha = data[:sha]
  242. ref = data[:ref]
  243. ref_type = data[:tag] ? 'tag' : 'branch'
  244. duration = data[:commit][:duration]
  245. expect(message).to eq("<a href=\"#{project_url}\">#{project_name}</a>: " \
  246. "Commit <a href=\"#{project_url}/commit/#{sha}/builds\">#{Commit.truncate_sha(sha)}</a> " \
  247. "of <a href=\"#{project_url}/commits/#{ref}\">#{ref}</a> #{ref_type} " \
  248. "by #{data[:commit][:author_name]} failed in #{duration} second(s)")
  249. end
  250. end
  251. context 'for succeeded' do
  252. before do
  253. build.success
  254. end
  255. it "calls Hipchat API" do
  256. hipchat.notify_only_broken_builds = false
  257. hipchat.execute(data)
  258. expect(WebMock).to have_requested(:post, api_url).once
  259. end
  260. it "notifies only broken" do
  261. hipchat.notify_only_broken_builds = true
  262. hipchat.execute(data)
  263. expect(WebMock).not_to have_requested(:post, api_url).once
  264. end
  265. end
  266. end
  267. context "#message_options" do
  268. it "is set to the defaults" do
  269. expect(hipchat.__send__(:message_options)).to eq({ notify: false, color: 'yellow' })
  270. end
  271. it "sets notify to true" do
  272. allow(hipchat).to receive(:notify).and_return('1')
  273. expect(hipchat.__send__(:message_options)).to eq({ notify: true, color: 'yellow' })
  274. end
  275. it "sets the color" do
  276. allow(hipchat).to receive(:color).and_return('red')
  277. expect(hipchat.__send__(:message_options)).to eq({ notify: false, color: 'red' })
  278. end
  279. context 'with a successful build' do
  280. it 'uses the green color' do
  281. build_data = { object_kind: 'build', commit: { status: 'success' } }
  282. expect(hipchat.__send__(:message_options, build_data)).to eq({ notify: false, color: 'green' })
  283. end
  284. end
  285. context 'with a failed build' do
  286. it 'uses the red color' do
  287. build_data = { object_kind: 'build', commit: { status: 'failed' } }
  288. expect(hipchat.__send__(:message_options, build_data)).to eq({ notify: false, color: 'red' })
  289. end
  290. end
  291. end
  292. end
  293. end