debian-mirror-gitlab/spec/models/integrations/campfire_spec.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

104 lines
3.5 KiB
Ruby
Raw Normal View History

2019-07-07 11:18:12 +05:30
# frozen_string_literal: true
2016-06-02 11:05:42 +05:30
require 'spec_helper'
2023-05-27 22:25:52 +05:30
RSpec.describe Integrations::Campfire, feature_category: :integrations do
2019-06-05 12:25:43 +05:30
include StubRequests
2022-07-29 17:44:30 +05:30
it_behaves_like Integrations::ResetSecretFields do
let(:integration) { described_class.new }
end
2016-06-02 11:05:42 +05:30
describe 'Validations' do
2022-07-29 17:44:30 +05:30
it { is_expected.to validate_numericality_of(:room).is_greater_than(0).only_integer }
2022-08-27 11:52:29 +05:30
it { is_expected.to validate_length_of(:subdomain).is_at_least(1).is_at_most(63).allow_blank }
2022-07-29 17:44:30 +05:30
it { is_expected.to allow_value("foo").for(:subdomain) }
it { is_expected.not_to allow_value("foo.bar").for(:subdomain) }
it { is_expected.not_to allow_value("foo.bar/#").for(:subdomain) }
2021-09-30 23:02:18 +05:30
context 'when integration 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
2021-09-30 23:02:18 +05:30
context 'when integration 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
2023-01-13 00:05:48 +05:30
let(:user) { build_stubbed(:user) }
let(:project) { build_stubbed(:project, :repository) }
2016-09-13 17:45:13 +05:30
before do
2021-09-04 01:27:46 +05:30
@campfire_integration = described_class.new
allow(@campfire_integration).to receive_messages(
2016-09-13 17:45:13 +05:30
project_id: project.id,
project: project,
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
2022-07-23 23:45:48 +05:30
body = File.read(Rails.root + 'spec/fixtures/integrations/campfire/rooms.json')
2019-06-05 12:25:43 +05:30
stub_full_request(@rooms_url).with(basic_auth: @auth).to_return(
2016-09-13 17:45:13 +05:30
body: body,
status: 200,
headers: @headers
)
2019-06-05 12:25:43 +05:30
2016-09-13 17:45:13 +05:30
# 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'
2019-06-05 12:25:43 +05:30
stub_full_request(speak_url, method: :post).with(basic_auth: @auth)
2016-09-13 17:45:13 +05:30
2021-09-04 01:27:46 +05:30
@campfire_integration.execute(@sample_data)
2016-09-13 17:45:13 +05:30
2019-06-05 12:25:43 +05:30
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-09-13 17:45:13 +05:30
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'
2022-07-23 23:45:48 +05:30
body = File.read(Rails.root + 'spec/fixtures/integrations/campfire/rooms2.json')
2019-06-05 12:25:43 +05:30
stub_full_request(@rooms_url).with(basic_auth: @auth).to_return(
2016-09-13 17:45:13 +05:30
body: body,
status: 200,
headers: @headers
)
2021-09-04 01:27:46 +05:30
@campfire_integration.execute(@sample_data)
2016-09-13 17:45:13 +05:30
2019-06-05 12:25:43 +05:30
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-09-13 17:45:13 +05:30
end
end
2023-05-27 22:25:52 +05:30
describe '#log_error' do
subject { described_class.new.log_error('error') }
it 'logs an error' do
expect(Gitlab::IntegrationsLogger).to receive(:error).with(
hash_including(integration_class: 'Integrations::Campfire', message: 'error')
).and_call_original
is_expected.to be_truthy
end
end
2016-06-02 11:05:42 +05:30
end