diff options
author | Howard P. Logsdon <howard@hplogsdon.com> | 2015-04-30 20:17:53 -0600 |
---|---|---|
committer | Howard P. Logsdon <howard@hplogsdon.com> | 2015-04-30 22:02:28 -0600 |
commit | f2e65ba17b6b707111d9848d54f97f4fc4febe21 (patch) | |
tree | 381e049168afd0e6d127314ccc3a1bf9d8847ceb /spec | |
parent | 4c63ef932bc2b573cd9306066993f72c1cb8dc83 (diff) | |
download | gitlab-ci-f2e65ba17b6b707111d9848d54f97f4fc4febe21.tar.gz |
Fix notification issues on HipChatService, add HipChatMessage specs
adding specs for HipChatMessage exposed some issues with the way I was
building the matrix-commit style notification message, so it ended up being
quite a bit more changes than I expected.
The save call from the service configure form submits an empty string as
the server URL if left blank, which I've indicated to do in order to use
the default server, but Hash#merge will happily overwrite a full string with
a blank string if asked, so we need to break that out, along with the worker
options which get mutated into string hash keys, of which the HipChat client
seems to not understand. Additionally, add another spec to make sure we call
the Sidekiq worker with expected arguments.
Diffstat (limited to 'spec')
-rw-r--r-- | spec/models/project_services/hip_chat_message_spec.rb | 65 | ||||
-rw-r--r-- | spec/models/project_services/hip_chat_service_spec.rb | 15 |
2 files changed, 79 insertions, 1 deletions
diff --git a/spec/models/project_services/hip_chat_message_spec.rb b/spec/models/project_services/hip_chat_message_spec.rb new file mode 100644 index 0000000..6096b5c --- /dev/null +++ b/spec/models/project_services/hip_chat_message_spec.rb @@ -0,0 +1,65 @@ +require 'spec_helper' + +describe HipChatMessage do + subject { HipChatMessage.new(build) } + + let(:project) { FactoryGirl.create(:project) } + let(:commit) { FactoryGirl.create(:commit, project: project) } + let(:job) { FactoryGirl.create(:job, project: project) } + let(:build) { FactoryGirl.create(:build, commit: commit, job: job, status: 'success') } + + context 'when build succeeds' do + + before { build.save } + + it 'returns a successful message' do + expect( subject.status_color ).to eq 'green' + expect( subject.notify? ).to be_false + expect( subject.to_s ).to match(/Build '[^']+' #\d+/) + expect( subject.to_s ).to match(/Successful in \d+ second\(s\)\./) + end + end + + context 'when build fails' do + + before do + build.status = 'failed' + build.save + end + + it 'returns a failure message' do + expect( subject.status_color ).to eq 'red' + expect( subject.notify? ).to be_true + expect( subject.to_s ).to match(/Build '[^']+' #\d+/) + expect( subject.to_s ).to match(/Failed in \d+ second\(s\)\./) + end + end + + context 'when all matrix builds succeed' do + let(:job2) { FactoryGirl.create(:job, project: project, name: 'Another Job') } + let(:build2) { FactoryGirl.create(:build, id: 10, commit: commit, job: job2, status: 'success') } + + before { build.save; build2.save } + + it 'returns a successful message' do + expect( subject.status_color ).to eq 'green' + expect( subject.notify? ).to be_false + expect( subject.to_s ).to match(/Commit #\d+/) + expect( subject.to_s ).to match(/Successful in \d+ second\(s\)\./) + end + end + + context 'when at least one matrix build fails' do + let(:job2) { FactoryGirl.create(:job, project: project, name: 'Another Job') } + let(:build2) { FactoryGirl.create(:build, id: 10, commit: commit, job: job2, status: 'failed') } + + before { build.save; build2.save } + + it 'returns a failure message' do + expect( subject.status_color ).to eq 'red' + expect( subject.notify? ).to be_true + expect( subject.to_s ).to match(/Commit #\d+/) + expect( subject.to_s ).to match(/Failed in \d+ second\(s\)\./) + end + end +end diff --git a/spec/models/project_services/hip_chat_service_spec.rb b/spec/models/project_services/hip_chat_service_spec.rb index e578461..5642221 100644 --- a/spec/models/project_services/hip_chat_service_spec.rb +++ b/spec/models/project_services/hip_chat_service_spec.rb @@ -41,7 +41,20 @@ describe HipChatService do service.execute(build) HipChatNotifierWorker.drain - WebMock.should have_requested(:post, api_url).once + expect( WebMock ).to have_requested(:post, api_url).once + end + + it "calls the worker with expected arguments" do + expect( HipChatNotifierWorker ).to receive(:perform_async) \ + .with(an_instance_of(String), hash_including( + token: 'a1b2c3d4e5f6', + room: 123, + server: 'https://api.hipchat.com', + color: 'red', + notify: true + )) + + service.execute(build) end end end |