diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-06-18 14:18:50 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-06-18 14:18:50 +0300 |
commit | 8c7f4e9d5f36cff46365a7f8c4b9c21578c1e781 (patch) | |
tree | a77e7fe7a93de11213032ed4ab1f33a3db51b738 /spec/models/blob_viewer | |
parent | 00b35af3db1abfe813a778f643dad221aad51fca (diff) |
Add latest changes from gitlab-org/gitlab@13-1-stable-ee
Diffstat (limited to 'spec/models/blob_viewer')
-rw-r--r-- | spec/models/blob_viewer/go_mod_spec.rb | 63 | ||||
-rw-r--r-- | spec/models/blob_viewer/metrics_dashboard_yml_spec.rb | 127 |
2 files changed, 190 insertions, 0 deletions
diff --git a/spec/models/blob_viewer/go_mod_spec.rb b/spec/models/blob_viewer/go_mod_spec.rb new file mode 100644 index 00000000000..ba6038533ea --- /dev/null +++ b/spec/models/blob_viewer/go_mod_spec.rb @@ -0,0 +1,63 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe BlobViewer::GoMod do + include FakeBlobHelpers + + let(:project) { build_stubbed(:project) } + let(:data) do + <<-SPEC.strip_heredoc + module #{Settings.build_gitlab_go_url}/#{project.full_path} + SPEC + end + let(:blob) { fake_blob(path: 'go.mod', data: data) } + + subject { described_class.new(blob) } + + describe '#package_name' do + it 'returns the package name' do + expect(subject.package_name).to eq("#{Settings.build_gitlab_go_url}/#{project.full_path}") + end + end + + describe '#package_url' do + it 'returns the package URL' do + expect(subject.package_url).to eq("#{Gitlab.config.gitlab.protocol}://#{Settings.build_gitlab_go_url}/#{project.full_path}/") + end + + context 'when the homepage has an invalid URL' do + let(:data) do + <<-SPEC.strip_heredoc + module javascript:alert() + SPEC + end + + it 'returns nil' do + expect(subject.package_url).to be_nil + end + end + end + + describe '#package_type' do + it 'returns "package"' do + expect(subject.package_type).to eq('go') + end + end + + context 'when the module name does not start with the instance URL' do + let(:data) do + <<-SPEC.strip_heredoc + module example.com/foo/bar + SPEC + end + + subject { described_class.new(blob) } + + describe '#package_url' do + it 'returns the pkg.go.dev URL' do + expect(subject.package_url).to eq("https://pkg.go.dev/example.com/foo/bar") + end + end + end +end diff --git a/spec/models/blob_viewer/metrics_dashboard_yml_spec.rb b/spec/models/blob_viewer/metrics_dashboard_yml_spec.rb new file mode 100644 index 00000000000..f5b8586975d --- /dev/null +++ b/spec/models/blob_viewer/metrics_dashboard_yml_spec.rb @@ -0,0 +1,127 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe BlobViewer::MetricsDashboardYml do + include FakeBlobHelpers + include RepoHelpers + + let_it_be(:project) { create(:project, :repository) } + let(:blob) { fake_blob(path: '.gitlab/dashboards/custom-dashboard.yml', data: data) } + let(:sha) { sample_commit.id } + + subject(:viewer) { described_class.new(blob) } + + context 'when the definition is valid' do + let(:data) { File.read(Rails.root.join('config/prometheus/common_metrics.yml')) } + + describe '#valid?' do + it 'calls prepare! on the viewer' do + allow(PerformanceMonitoring::PrometheusDashboard).to receive(:from_json) + + expect(viewer).to receive(:prepare!) + + viewer.valid? + end + + it 'returns true', :aggregate_failures do + yml = ::Gitlab::Config::Loader::Yaml.new(data).load_raw! + + expect_next_instance_of(::Gitlab::Config::Loader::Yaml, data) do |loader| + expect(loader).to receive(:load_raw!).and_call_original + end + expect(PerformanceMonitoring::PrometheusDashboard) + .to receive(:from_json) + .with(yml) + .and_call_original + expect(viewer.valid?).to be_truthy + end + end + + describe '#errors' do + it 'returns nil' do + allow(PerformanceMonitoring::PrometheusDashboard).to receive(:from_json) + + expect(viewer.errors).to be nil + end + end + end + + context 'when definition is invalid' do + let(:error) { ActiveModel::ValidationError.new(PerformanceMonitoring::PrometheusDashboard.new.tap(&:validate)) } + let(:data) do + <<~YAML + dashboard: + YAML + end + + describe '#valid?' do + it 'returns false' do + expect(PerformanceMonitoring::PrometheusDashboard) + .to receive(:from_json).and_raise(error) + + expect(viewer.valid?).to be_falsey + end + end + + describe '#errors' do + it 'returns validation errors' do + allow(PerformanceMonitoring::PrometheusDashboard) + .to receive(:from_json).and_raise(error) + + expect(viewer.errors).to be error.model.errors + end + end + end + + context 'when YAML syntax is invalid' do + let(:data) do + <<~YAML + dashboard: 'empty metrics' + panel_groups: + - group: 'Group Title' + YAML + end + + describe '#valid?' do + it 'returns false' do + expect(PerformanceMonitoring::PrometheusDashboard).not_to receive(:from_json) + expect(viewer.valid?).to be_falsey + end + end + + describe '#errors' do + it 'returns validation errors' do + yaml_wrapped_errors = { 'YAML syntax': ["(<unknown>): did not find expected key while parsing a block mapping at line 1 column 1"] } + + expect(viewer.errors).to be_kind_of ActiveModel::Errors + expect(viewer.errors.messages).to eql(yaml_wrapped_errors) + end + end + end + + context 'when YAML loader raises error' do + let(:data) do + <<~YAML + large yaml file + YAML + end + + before do + allow(::Gitlab::Config::Loader::Yaml).to receive(:new) + .and_raise(::Gitlab::Config::Loader::Yaml::DataTooLargeError, 'The parsed YAML is too big') + end + + it 'is invalid' do + expect(PerformanceMonitoring::PrometheusDashboard).not_to receive(:from_json) + expect(viewer.valid?).to be(false) + end + + it 'returns validation errors' do + yaml_wrapped_errors = { 'YAML syntax': ["The parsed YAML is too big"] } + + expect(viewer.errors).to be_kind_of(ActiveModel::Errors) + expect(viewer.errors.messages).to eq(yaml_wrapped_errors) + end + end +end |