debian-mirror-gitlab/spec/models/project_services/campfire_service_spec.rb

86 lines
2.9 KiB
Ruby
Raw Normal View History

2016-06-02 11:05:42 +05:30
require 'spec_helper'
2017-09-10 17:25:29 +05:30
describe CampfireService do
2016-06-02 11:05:42 +05:30
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-09-10 17:25:29 +05:30
before do
subject.active = true
end
2016-06-02 11:05:42 +05:30
it { is_expected.to validate_presence_of(:token) }
end
context 'when service is inactive' do
2017-09-10 17:25:29 +05:30
before do
subject.active = false
end
2016-06-02 11:05:42 +05:30
it { is_expected.not_to validate_presence_of(:token) }
end
end
2016-09-13 17:45:13 +05:30
describe "#execute" do
let(:user) { create(:user) }
2017-08-17 22:00:37 +05:30
let(:project) { create(:project, :repository) }
2016-09-13 17:45:13 +05:30
before do
2017-09-10 17:25:29 +05:30
@campfire_service = described_class.new
2016-09-13 17:45:13 +05:30
allow(@campfire_service).to receive_messages(
project_id: project.id,
project: project,
service_hook: true,
token: 'verySecret',
subdomain: 'project-name',
room: 'test-room'
)
@sample_data = Gitlab::DataBuilder::Push.build_sample(project, user)
2017-09-10 17:25:29 +05:30
@rooms_url = 'https://project-name.campfirenow.com/rooms.json'
@auth = %w(verySecret X)
2016-09-13 17:45:13 +05:30
@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')
2017-09-10 17:25:29 +05:30
WebMock.stub_request(:get, @rooms_url).with(basic_auth: @auth).to_return(
2016-09-13 17:45:13 +05:30
body: body,
status: 200,
headers: @headers
)
# stub the speak request with the room id found in the previous request's response
2017-09-10 17:25:29 +05:30
speak_url = 'https://project-name.campfirenow.com/room/123/speak.json'
WebMock.stub_request(:post, speak_url).with(basic_auth: @auth)
2016-09-13 17:45:13 +05:30
@campfire_service.execute(@sample_data)
expect(WebMock).to have_requested(:get, @rooms_url).once
expect(WebMock).to have_requested(:post, speak_url).with(
body: /#{project.path}.*#{@sample_data[:before]}.*#{@sample_data[:after]}/
).once
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')
2017-09-10 17:25:29 +05:30
WebMock.stub_request(:get, @rooms_url).with(basic_auth: @auth).to_return(
2016-09-13 17:45:13 +05:30
body: body,
status: 200,
headers: @headers
)
# we want to make sure no request is sent to the /speak endpoint, here is a basic
# regexp that matches this endpoint
speak_url = 'https://verySecret:X@project-name.campfirenow.com/room/.*/speak.json'
@campfire_service.execute(@sample_data)
expect(WebMock).to have_requested(:get, @rooms_url).once
expect(WebMock).not_to have_requested(:post, /#{speak_url}/)
end
end
2016-06-02 11:05:42 +05:30
end