Skip to content
Snippets Groups Projects
Commit 9f3e6d07 authored by Rémy Coutable's avatar Rémy Coutable
Browse files

Use WebMock instead of RSpec stubbing


Signed-off-by: default avatarRémy Coutable <remy@rymai.me>
parent c20799e4
No related branches found
No related tags found
No related merge requests found
Loading
Loading
@@ -124,8 +124,6 @@ v 8.6.0
- Add information about `image` and `services` field at `job` level in the `.gitlab-ci.yml` documentation (Pat Turner)
- HTTP error pages work independently from location and config (Artem Sidorenko)
- Update `omniauth-saml` to 1.5.0 to allow for custom response attributes to be set
- Fix avatar stretching by providing a cropping feature (Johann Pardanaud)
- Don't load all of GitLab in mail_room
- Memoize @group in Admin::GroupsController (Yatish Mehta)
- Indicate how much an MR diverged from the target branch (Pierre de La Morinerie)
- Added omniauth-auth0 Gem (Daniel Carraro)
Loading
Loading
Loading
Loading
@@ -120,11 +120,11 @@ class BambooService < CiService
end
 
if status.include?('Success')
:success
'success'
elsif status.include?('Failed')
:failed
'failed'
elsif status.include?('Pending')
:pending
'pending'
else
:error
end
Loading
Loading
Loading
Loading
@@ -124,11 +124,11 @@ class TeamcityService < CiService
end
 
if status.include?('SUCCESS')
:success
'success'
elsif status.include?('FAILURE')
:failed
'failed'
elsif status.include?('Pending')
:pending
'pending'
else
:error
end
Loading
Loading
Loading
Loading
@@ -91,158 +91,150 @@ describe BambooService, models: true do
end
end
 
module BambooServiceSpec
Response = Struct.new(:code, :data) do
def [](key)
data[key]
end
end
end
describe '#build_info' do
let(:bamboo_url) { 'http://gitlab.com' }
let(:response) { BambooServiceSpec::Response.new(200, {}) }
subject do
BambooService.create(
project: create(:project),
properties: {
bamboo_url: bamboo_url,
username: 'mic',
password: 'password'
})
end
before { allow(HTTParty).to receive(:get).and_return(response) }
describe '#build_page' do
let(:bamboo_full_url) { 'http://mic:password@gitlab.com/rest/api/latest/result?label=123&os_authType=basic' }
 
context 'when username and password are blank' do
context 'when response code is not 200' do
before do
WebMock.stub_request(:get, bamboo_full_url).to_return(status: 500)
end
subject do
BambooService.create(
project: create(:project),
project: build_stubbed(:empty_project),
properties: {
bamboo_url: bamboo_url
})
bamboo_url: 'http://gitlab.com',
username: 'mic',
password: 'password',
build_key: 'foo'
}
)
end
 
it { expect(subject.build_info('123')).to eq(response) }
end
context 'when bamboo_url has no trailing slash' do
it { expect(subject.build_info('123')).to eq(response) }
end
context 'when bamboo_url has a trailing slash' do
let(:bamboo_url) { 'http://gitlab.com/' }
it { expect(subject.build_info('123')).to eq(response) }
it { expect(subject.build_page('123', 'unused')).to eq('http://gitlab.com/browse/foo') }
end
end
 
describe '#build_page' do
let(:bamboo_url) { 'http://gitlab.com' }
let(:response_code) { 200 }
let(:response) do
BambooServiceSpec::Response.new(response_code, {
'results' => {
'results' => { 'result' => { 'planResultKey' => { 'key' => '42' } } }
}
})
end
subject do
BambooService.create(
project: create(:project),
properties: {
bamboo_url: bamboo_url,
username: 'mic',
password: 'password',
build_key: 'foo'
})
end
before { allow(HTTParty).to receive(:get).and_return(response) }
context 'when response has no result' do
before do
WebMock.stub_request(:get, bamboo_full_url).to_return(
status: 200,
headers: { 'Content-Type': 'application/json' },
body: %Q({"results":{"results":{"size":"0"}}})
)
end
subject do
BambooService.create(
project: build_stubbed(:empty_project),
properties: {
bamboo_url: 'http://gitlab.com',
username: 'mic',
password: 'password',
build_key: 'foo'
}
)
end
 
context 'when bamboo_url has no trailing slash' do
it { expect(subject.build_page('123', 'unused')).to eq('http://gitlab.com/browse/42') }
it { expect(subject.build_page('123', 'unused')).to eq('http://gitlab.com/browse/foo') }
end
 
context 'when bamboo_url has a trailing slash' do
let(:bamboo_url) { 'http://gitlab.com/' }
context 'when response has result' do
before do
WebMock.stub_request(:get, bamboo_full_url).to_return(
status: 200,
headers: { 'Content-Type': 'application/json' },
body: %Q({"results":{"results":{"result":{"planResultKey":{"key":"42"}}}}})
)
end
subject do
BambooService.create(
project: build_stubbed(:empty_project),
properties: {
bamboo_url: bamboo_url,
username: 'mic',
password: 'password',
build_key: 'foo'
}
)
end
 
it { expect(subject.build_page('123', 'unused')).to eq('http://gitlab.com/browse/42') }
end
context 'when bamboo_url has no trailing slash' do
let(:bamboo_url) { 'http://gitlab.com' }
 
context 'when response code is not 200' do
let(:response_code) { 500 }
it { expect(subject.build_page('123', 'unused')).to eq('http://gitlab.com/browse/42') }
end
 
it { expect(subject.build_page('123', 'unused')).to eq('http://gitlab.com/browse/foo') }
end
context 'when bamboo_url has a trailing slash' do
let(:bamboo_url) { 'http://gitlab.com/' }
 
context 'when response returns no result' do
let(:response) do
BambooServiceSpec::Response.new(response_code, {
'results' => { 'results' => { 'size' => '0' } }
})
it { expect(subject.build_page('123', 'unused')).to eq('http://gitlab.com/browse/42') }
end
it { expect(subject.build_page('123', 'unused')).to eq('http://gitlab.com/browse/foo') }
end
end
 
describe '#commit_status' do
let(:bamboo_url) { 'http://gitlab.com' }
let(:response_code) { 200 }
let(:build_state) { 'YAY Success!' }
let(:response) do
BambooServiceSpec::Response.new(response_code, {
'results' => { 'results' => { 'result' => { 'buildState' => build_state } } }
})
end
let(:bamboo_full_url) { 'http://mic:password@gitlab.com/rest/api/latest/result?label=123&os_authType=basic' }
subject do
BambooService.create(
project: create(:project),
project: build_stubbed(:empty_project),
properties: {
bamboo_url: bamboo_url,
bamboo_url: 'http://gitlab.com',
username: 'mic',
password: 'password',
build_type: 'foo'
})
password: 'password'
}
)
end
before { allow(HTTParty).to receive(:get).and_return(response) }
 
context 'when response code is not 200' do
let(:response_code) { 500 }
before do
WebMock.stub_request(:get, bamboo_full_url).to_return(status: 500)
end
 
it { expect(subject.commit_status('123', 'unused')).to eq(:error) }
end
 
context 'when response has no results' do
let(:response) do
BambooServiceSpec::Response.new(response_code, {
'results' => { 'results' => { 'size' => '0' } }
})
context 'when response code is 404' do
before do
WebMock.stub_request(:get, bamboo_full_url).to_return(status: 404)
end
 
it { expect(subject.commit_status('123', 'unused')).to eq(:pending) }
it { expect(subject.commit_status('123', 'unused')).to eq('pending') }
end
 
context 'when response code is 404' do
let(:response_code) { 404 }
context 'when response has no results' do
before do
WebMock.stub_request(:get, bamboo_full_url).to_return(
status: 200,
headers: { 'Content-Type': 'application/json' },
body: %Q({"results":{"results":{"size":"0"}}})
)
end
 
it { expect(subject.commit_status('123', 'unused')).to eq(:pending) }
it { expect(subject.commit_status('123', 'unused')).to eq('pending') }
end
 
context 'when response code is 200' do
context 'when response has results' do
let(:build_state) { 'YAY Success!' }
before do
WebMock.stub_request(:get, bamboo_full_url).to_return(
status: 200,
headers: { 'Content-Type': 'application/json' },
body: %Q({"results":{"results":{"result":{"buildState":"#{build_state}"}}}})
)
end
context 'when build status contains Success' do
it { expect(subject.commit_status('123', 'unused')).to eq(:success) }
it { expect(subject.commit_status('123', 'unused')).to eq('success') }
end
 
context 'when build status contains Failed' do
let(:build_state) { 'NO Failed!' }
 
it { expect(subject.commit_status('123', 'unused')).to eq(:failed) }
it { expect(subject.commit_status('123', 'unused')).to eq('failed') }
end
 
context 'when build status contains Failed' do
let(:build_state) { 'NO Pending!' }
 
it { expect(subject.commit_status('123', 'unused')).to eq(:pending) }
it { expect(subject.commit_status('123', 'unused')).to eq('pending') }
end
 
context 'when build status contains anything else' do
Loading
Loading
Loading
Loading
@@ -91,126 +91,119 @@ describe TeamcityService, models: true do
end
end
 
module TeamcityServiceSpec
Response = Struct.new(:code, :data) do
def [](key)
data[key]
end
end
end
describe '#build_info' do
let(:teamcity_url) { 'http://gitlab.com' }
let(:response) { TeamcityServiceSpec::Response.new(200, {}) }
subject do
TeamcityService.create(
project: create(:project),
properties: {
teamcity_url: teamcity_url,
username: 'mic',
password: 'password',
build_type: 'foo'
})
end
before { allow(HTTParty).to receive(:get).and_return(response) }
context 'when teamcity_url has no trailing slash' do
it { expect(subject.build_info('123')).to eq(response) }
end
describe '#build_page' do
let(:teamcity_full_url) { 'http://mic:password@gitlab.com/httpAuth/app/rest/builds/branch:unspecified:any,number:123' }
 
context 'when teamcity_url has a trailing slash' do
let(:teamcity_url) { 'http://gitlab.com/' }
context 'when response code is not 200' do
before do
WebMock.stub_request(:get, teamcity_full_url).to_return(status: 500)
end
subject do
TeamcityService.create(
project: build_stubbed(:empty_project),
properties: {
teamcity_url: 'http://gitlab.com/',
username: 'mic',
password: 'password',
build_type: 'foo'
}
)
end
 
it { expect(subject.build_info('123')).to eq(response) }
end
end
it 'returns a specific URL' do
 
describe '#build_page' do
let(:teamcity_url) { 'http://gitlab.com' }
let(:response_code) { 200 }
let(:response) do
TeamcityServiceSpec::Response.new(response_code, { 'build' => { 'id' => '666' } })
end
subject do
TeamcityService.create(
project: create(:project),
properties: {
teamcity_url: teamcity_url,
username: 'mic',
password: 'password',
build_type: 'foo'
})
expect(subject.build_page('123', 'unused')).to eq('http://gitlab.com/viewLog.html?buildTypeId=foo')
end
end
before { allow(HTTParty).to receive(:get).and_return(response) }
 
context 'when teamcity_url has no trailing slash' do
it { expect(subject.build_page('123', 'unused')).to eq('http://gitlab.com/viewLog.html?buildId=666&buildTypeId=foo') }
end
context 'when response has result' do
before do
WebMock.stub_request(:get, teamcity_full_url).to_return(
status: 200,
headers: { 'Content-Type': 'application/json' },
body: %Q({"build":{"id":"666"}})
)
end
subject do
TeamcityService.create(
project: build_stubbed(:empty_project),
properties: {
teamcity_url: teamcity_url,
username: 'mic',
password: 'password',
build_type: 'foo'
}
)
end
 
context 'when teamcity_url has a trailing slash' do
let(:teamcity_url) { 'http://gitlab.com/' }
context 'when teamcity_url has no trailing slash' do
let(:teamcity_url) { 'http://gitlab.com' }
 
it { expect(subject.build_page('123', 'unused')).to eq('http://gitlab.com/viewLog.html?buildId=666&buildTypeId=foo') }
end
it { expect(subject.build_page('123', 'unused')).to eq('http://gitlab.com/viewLog.html?buildId=666&buildTypeId=foo') }
end
 
context 'when response code is not 200' do
let(:response_code) { 500 }
context 'when teamcity_url has a trailing slash' do
let(:teamcity_url) { 'http://gitlab.com/' }
 
it { expect(subject.build_page('123', 'unused')).to eq('http://gitlab.com/viewLog.html?buildTypeId=foo') }
it { expect(subject.build_page('123', 'unused')).to eq('http://gitlab.com/viewLog.html?buildId=666&buildTypeId=foo') }
end
end
end
 
describe '#commit_status' do
let(:teamcity_url) { 'http://gitlab.com' }
let(:response_code) { 200 }
let(:build_status) { 'YAY SUCCESS!' }
let(:response) do
TeamcityServiceSpec::Response.new(response_code, {
'build' => {
'status' => build_status,
'id' => '666'
}
})
end
let(:teamcity_full_url) { 'http://mic:password@gitlab.com/httpAuth/app/rest/builds/branch:unspecified:any,number:123' }
subject do
TeamcityService.create(
project: create(:project),
project: build_stubbed(:empty_project),
properties: {
teamcity_url: teamcity_url,
teamcity_url: 'http://gitlab.com',
username: 'mic',
password: 'password',
build_type: 'foo'
})
}
)
end
before { allow(HTTParty).to receive(:get).and_return(response) }
 
context 'when response code is not 200' do
let(:response_code) { 500 }
before do
WebMock.stub_request(:get, teamcity_full_url).to_return(status: 500)
end
 
it { expect(subject.commit_status('123', 'unused')).to eq(:error) }
end
 
context 'when response code is 404' do
let(:response_code) { 404 }
before do
WebMock.stub_request(:get, teamcity_full_url).to_return(status: 404)
end
 
it { expect(subject.commit_status('123', 'unused')).to eq(:pending) }
it { expect(subject.commit_status('123', 'unused')).to eq('pending') }
end
 
context 'when response code is 200' do
context 'when response has results' do
let(:build_status) { 'YAY SUCCESS!' }
before do
WebMock.stub_request(:get, teamcity_full_url).to_return(
status: 200,
headers: { 'Content-Type': 'application/json' },
body: %Q({"build":{"status":"#{build_status}","id":"666"}})
)
end
context 'when build status contains SUCCESS' do
it { expect(subject.commit_status('123', 'unused')).to eq(:success) }
it { expect(subject.commit_status('123', 'unused')).to eq('success') }
end
 
context 'when build status contains FAILURE' do
let(:build_status) { 'NO FAILURE!' }
 
it { expect(subject.commit_status('123', 'unused')).to eq(:failed) }
it { expect(subject.commit_status('123', 'unused')).to eq('failed') }
end
 
context 'when build status contains Pending' do
let(:build_status) { 'NO Pending!' }
 
it { expect(subject.commit_status('123', 'unused')).to eq(:pending) }
it { expect(subject.commit_status('123', 'unused')).to eq('pending') }
end
 
context 'when build status contains anything else' do
Loading
Loading
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment