2019-03-30 03:23:56 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-04-21 11:13:14 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 14:09:03 -04:00
|
|
|
RSpec.describe CampfireService do
|
2019-04-21 06:03:26 -04:00
|
|
|
include StubRequests
|
|
|
|
|
2016-04-21 11:13:14 -04:00
|
|
|
describe 'Associations' do
|
|
|
|
it { is_expected.to belong_to :project }
|
|
|
|
it { is_expected.to have_one :service_hook }
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'Validations' do
|
|
|
|
context 'when service is active' do
|
2017-06-14 14:18:56 -04:00
|
|
|
before do
|
|
|
|
subject.active = true
|
|
|
|
end
|
2016-04-21 11:13:14 -04:00
|
|
|
|
|
|
|
it { is_expected.to validate_presence_of(:token) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when service is inactive' do
|
2017-06-14 14:18:56 -04:00
|
|
|
before do
|
|
|
|
subject.active = false
|
|
|
|
end
|
2016-04-21 11:13:14 -04:00
|
|
|
|
|
|
|
it { is_expected.not_to validate_presence_of(:token) }
|
|
|
|
end
|
|
|
|
end
|
2016-08-10 06:25:01 -04:00
|
|
|
|
|
|
|
describe "#execute" do
|
|
|
|
let(:user) { create(:user) }
|
2017-01-26 17:44:58 -05:00
|
|
|
let(:project) { create(:project, :repository) }
|
2016-08-10 06:25:01 -04:00
|
|
|
|
|
|
|
before do
|
2017-07-25 13:09:00 -04:00
|
|
|
@campfire_service = described_class.new
|
2016-08-10 06:25:01 -04:00
|
|
|
allow(@campfire_service).to receive_messages(
|
|
|
|
project_id: project.id,
|
|
|
|
project: project,
|
|
|
|
service_hook: true,
|
|
|
|
token: 'verySecret',
|
|
|
|
subdomain: 'project-name',
|
|
|
|
room: 'test-room'
|
|
|
|
)
|
2016-08-12 04:09:29 -04:00
|
|
|
@sample_data = Gitlab::DataBuilder::Push.build_sample(project, user)
|
2017-06-26 20:59:32 -04:00
|
|
|
@rooms_url = 'https://project-name.campfirenow.com/rooms.json'
|
|
|
|
@auth = %w(verySecret X)
|
2016-08-10 06:25:01 -04:00
|
|
|
@headers = { 'Content-Type' => 'application/json; charset=utf-8' }
|
|
|
|
end
|
|
|
|
|
|
|
|
it "calls Campfire API to get a list of rooms and speak in a room" do
|
|
|
|
# make sure a valid list of rooms is returned
|
|
|
|
body = File.read(Rails.root + 'spec/fixtures/project_services/campfire/rooms.json')
|
2019-04-21 06:03:26 -04:00
|
|
|
|
|
|
|
stub_full_request(@rooms_url).with(basic_auth: @auth).to_return(
|
2016-08-10 06:25:01 -04:00
|
|
|
body: body,
|
|
|
|
status: 200,
|
|
|
|
headers: @headers
|
|
|
|
)
|
2019-04-21 06:03:26 -04:00
|
|
|
|
2016-08-10 06:25:01 -04:00
|
|
|
# stub the speak request with the room id found in the previous request's response
|
2017-06-26 20:59:32 -04:00
|
|
|
speak_url = 'https://project-name.campfirenow.com/room/123/speak.json'
|
2019-04-21 06:03:26 -04:00
|
|
|
stub_full_request(speak_url, method: :post).with(basic_auth: @auth)
|
2016-08-10 06:25:01 -04:00
|
|
|
|
|
|
|
@campfire_service.execute(@sample_data)
|
|
|
|
|
2019-04-21 06:03:26 -04:00
|
|
|
expect(WebMock).to have_requested(:get, stubbed_hostname(@rooms_url)).once
|
|
|
|
expect(WebMock).to have_requested(:post, stubbed_hostname(speak_url))
|
|
|
|
.with(body: /#{project.path}.*#{@sample_data[:before]}.*#{@sample_data[:after]}/).once
|
2016-08-10 06:25:01 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "calls Campfire API to get a list of rooms but shouldn't speak in a room" do
|
|
|
|
# return a list of rooms that do not contain a room named 'test-room'
|
|
|
|
body = File.read(Rails.root + 'spec/fixtures/project_services/campfire/rooms2.json')
|
2019-04-21 06:03:26 -04:00
|
|
|
stub_full_request(@rooms_url).with(basic_auth: @auth).to_return(
|
2016-08-10 06:25:01 -04:00
|
|
|
body: body,
|
|
|
|
status: 200,
|
|
|
|
headers: @headers
|
|
|
|
)
|
|
|
|
|
|
|
|
@campfire_service.execute(@sample_data)
|
|
|
|
|
2019-04-21 06:03:26 -04:00
|
|
|
expect(WebMock).to have_requested(:get, 'https://8.8.8.9/rooms.json').once
|
|
|
|
expect(WebMock).not_to have_requested(:post, '*/room/.*/speak.json')
|
2016-08-10 06:25:01 -04:00
|
|
|
end
|
|
|
|
end
|
2016-04-21 11:13:14 -04:00
|
|
|
end
|