Commit 6a8133b9 authored by Matija Čupić's avatar Matija Čupić

Stub http request on specs intead of mocking HTTParty

CE mirror of bb2a9fde8e6a4d1df13638fe336f641b9c72ef59
parent 3af363ec
......@@ -3,6 +3,7 @@ module Gitlab
module External
module File
class Remote
include Gitlab::Utils::StrongMemoize
attr_reader :location
def initialize(location, opts = {})
......@@ -16,11 +17,13 @@ module Gitlab
def content
return @content if defined?(@content)
@content ||= begin
HTTParty.get(location)
rescue HTTParty::Error, Timeout::Error
false
end
@content = strong_memoize(:content) do
begin
HTTParty.get(location)
rescue HTTParty::Error, Timeout::Error
false
end
end
end
end
end
......
......@@ -127,7 +127,7 @@ describe Gitlab::Ci::Config do
end
context "when gitlab_ci_yml has valid 'include' defined" do
let(:http_file_content) do
let(:remote_file_content) do
<<~HEREDOC
variables:
AUTO_DEVOPS_DOMAIN: domain.example.com
......@@ -138,11 +138,12 @@ describe Gitlab::Ci::Config do
HEREDOC
end
let(:local_file_content) { File.read("#{Rails.root}/spec/ee/fixtures/gitlab/ci/external_files/.gitlab-ci-template-1.yml") }
let(:yml) do
let(:remote_location) { 'https://gitlab.com/gitlab-org/gitlab-ce/blob/1234/.gitlab-ci-1.yml' }
let(:gitlab_ci_yml) do
<<-EOS
include:
- /spec/fixtures/gitlab/ci/external_files/.gitlab-ci-template-1.yml
- https://gitlab.com/gitlab-org/gitlab-ce/blob/1234/.gitlab-ci-1.yml
- #{remote_location}
image: ruby:2.2
EOS
......@@ -150,7 +151,7 @@ describe Gitlab::Ci::Config do
before do
allow_any_instance_of(::Gitlab::Ci::External::File::Local).to receive(:local_file_content).and_return(local_file_content)
allow(HTTParty).to receive(:get).and_return(http_file_content)
WebMock.stub_request(:get, remote_location).to_return(body: remote_file_content)
end
it 'should return a composed hash' do
......@@ -194,23 +195,24 @@ describe Gitlab::Ci::Config do
end
context "when both external files and gitlab_ci.yml defined the same key" do
let(:remote_location) { 'https://gitlab.com/gitlab-org/gitlab-ce/blob/1234/.gitlab-ci-1.yml' }
let(:gitlab_ci_yml) do
<<~HEREDOC
include:
- https://gitlab.com/gitlab-org/gitlab-ce/blob/1234/.gitlab-ci-1.gitlab_ci_yml
- #{remote_location}
image: ruby:2.2
HEREDOC
end
let(:http_file_content) do
let(:remote_file_content) do
<<~HEREDOC
image: php:5-fpm-alpine
HEREDOC
end
it 'should take precedence' do
allow(HTTParty).to receive(:get).and_return(http_file_content)
WebMock.stub_request(:get, remote_location).to_return(body: remote_file_content)
expect(config.to_hash).to eq({ image: 'ruby:2.2' })
end
end
......
......@@ -36,8 +36,8 @@ describe Gitlab::Ci::External::Processor do
end
context 'with a valid remote external file is defined' do
let(:remote_url) { 'https://gitlab.com/gitlab-org/gitlab-ce/blob/1234/.gitlab-ci-1.yml' }
let(:values) { { include: remote_url, image: 'ruby:2.2' } }
let(:remote_file) { 'https://gitlab.com/gitlab-org/gitlab-ce/blob/1234/.gitlab-ci-1.yml' }
let(:values) { { include: remote_file, image: 'ruby:2.2' } }
let(:external_file_content) do
<<-HEREDOC
before_script:
......@@ -58,7 +58,7 @@ describe Gitlab::Ci::External::Processor do
end
before do
WebMock.stub_request(:get, remote_url).to_return(body: external_file_content)
WebMock.stub_request(:get, remote_file).to_return(body: external_file_content)
end
it 'should append the file to the values' do
......@@ -99,11 +99,11 @@ describe Gitlab::Ci::External::Processor do
end
context 'with multiple external files are defined' do
let(:remote_url) { 'https://gitlab.com/gitlab-org/gitlab-ce/blob/1234/.gitlab-ci-1.yml' }
let(:remote_file) { 'https://gitlab.com/gitlab-org/gitlab-ce/blob/1234/.gitlab-ci-1.yml' }
let(:external_files) do
[
"/spec/ee/fixtures/gitlab/ci/external_files/.gitlab-ci-template-1.yml",
remote_url
remote_file
]
end
let(:values) do
......@@ -125,7 +125,7 @@ describe Gitlab::Ci::External::Processor do
before do
local_file_content = File.read("#{Rails.root}/spec/ee/fixtures/gitlab/ci/external_files/.gitlab-ci-template-1.yml")
allow_any_instance_of(Gitlab::Ci::External::File::Local).to receive(:local_file_content).and_return(local_file_content)
WebMock.stub_request(:get, remote_url).to_return(body: remote_file_content)
WebMock.stub_request(:get, remote_file).to_return(body: remote_file_content)
end
it 'should append the files to the values' do
......@@ -152,9 +152,10 @@ describe Gitlab::Ci::External::Processor do
end
context "when both external files and values defined the same key" do
let(:remote_file) { 'https://gitlab.com/gitlab-org/gitlab-ce/blob/1234/.gitlab-ci-1.yml' }
let(:values) do
{
include: 'https://gitlab.com/gitlab-org/gitlab-ce/blob/1234/.gitlab-ci-1.yml',
include: remote_file,
image: 'ruby:2.2'
}
end
......@@ -166,7 +167,7 @@ describe Gitlab::Ci::External::Processor do
end
it 'should take precedence' do
allow(HTTParty).to receive(:get).and_return(remote_file_content)
WebMock.stub_request(:get, remote_file).to_return(body: remote_file_content)
expect(processor.perform[:image]).to eq('ruby:2.2')
end
end
......
......@@ -2,22 +2,24 @@ require 'spec_helper'
describe BlobViewer::GitlabCiYml do
include FakeBlobHelpers
include RepoHelpers
let(:project) { build_stubbed(:project) }
let(:project) { build_stubbed(:project, :repository) }
let(:data) { File.read(Rails.root.join('spec/support/gitlab_stubs/gitlab_ci.yml')) }
let(:blob) { fake_blob(path: '.gitlab-ci.yml', data: data) }
let(:sha) { sample_commit.id }
subject { described_class.new(blob) }
describe '#validation_message' do
it 'calls prepare! on the viewer' do
expect(subject).to receive(:prepare!)
subject.validation_message(project, project.default_branch)
subject.validation_message(project, sha)
end
context 'when the configuration is valid' do
it 'returns nil' do
expect(subject.validation_message(project, project.default_branch)).to be_nil
expect(subject.validation_message(project, sha)).to be_nil
end
end
......@@ -25,7 +27,7 @@ describe BlobViewer::GitlabCiYml do
let(:data) { 'oof' }
it 'returns the error message' do
expect(subject.validation_message(project, project.default_branch)).to eq('Invalid configuration format')
expect(subject.validation_message(project, sha)).to eq('Invalid configuration format')
end
end
end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment