diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-09-29 09:09:45 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-09-29 09:09:45 +0300 |
commit | cd54f7e81bbedff94ea78092f44fb67bd8c2ac57 (patch) | |
tree | 588056e7ded7fbefe4f1158a550dcc2d581aa178 /spec | |
parent | f574f9a14bb6448935fd27a67fc9f3ca61ffcc86 (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'spec')
7 files changed, 396 insertions, 75 deletions
diff --git a/spec/controllers/projects/static_site_editor_controller_spec.rb b/spec/controllers/projects/static_site_editor_controller_spec.rb index 7883c7e6f81..7170a3466c5 100644 --- a/spec/controllers/projects/static_site_editor_controller_spec.rb +++ b/spec/controllers/projects/static_site_editor_controller_spec.rb @@ -8,6 +8,8 @@ RSpec.describe Projects::StaticSiteEditorController do let(:data) { instance_double(Hash) } describe 'GET show' do + render_views + let(:default_params) do { namespace_id: project.namespace, @@ -82,8 +84,9 @@ RSpec.describe Projects::StaticSiteEditorController do context 'when invalid config file' do let(:service_response) { ServiceResponse.error(message: 'invalid') } - it 'returns 422' do - expect(response).to have_gitlab_http_status(:unprocessable_entity) + it 'redirects to project page and flashes error message' do + expect(response).to redirect_to(project_path(project)) + expect(response).to set_flash[:alert].to('invalid') end end end diff --git a/spec/features/projects/clusters_spec.rb b/spec/features/projects/clusters_spec.rb index a27c9d54e8a..058a5c3e607 100644 --- a/spec/features/projects/clusters_spec.rb +++ b/spec/features/projects/clusters_spec.rb @@ -13,36 +13,6 @@ RSpec.describe 'Clusters', :js do gitlab_sign_in(user) end - context 'when clusters_list_redesign feature flag is disabled' do - before do - stub_feature_flags(clusters_list_redesign: false) - end - - context 'when user does not have a cluster and visits cluster index page' do - before do - visit project_clusters_path(project) - end - - it 'sees empty state' do - expect(page).to have_link('Add Kubernetes cluster') - expect(page).to have_selector('.empty-state') - end - end - - context 'when user has a cluster and visits cluster index page' do - let!(:cluster) { create(:cluster, :project, :provided_by_gcp) } - let(:project) { cluster.project } - - before do - visit project_clusters_path(project) - end - - it 'user sees a table with one cluster' do - expect(page).to have_selector('[data-testid="cluster_list_table"] .card-body', count: 1) - end - end - end - context 'when user does not have a cluster and visits cluster index page' do before do visit project_clusters_path(project) diff --git a/spec/helpers/clusters_helper_spec.rb b/spec/helpers/clusters_helper_spec.rb index 6164f3b5e8d..f0945106ce3 100644 --- a/spec/helpers/clusters_helper_spec.rb +++ b/spec/helpers/clusters_helper_spec.rb @@ -89,32 +89,6 @@ RSpec.describe ClustersHelper do end end - describe '#provider_icon' do - it 'will return GCP logo with gcp argument' do - logo = helper.provider_icon('gcp') - - expect(logo).to match(%r(img alt="Google GKE" data-src="|/illustrations/logos/google_gke|svg)) - end - - it 'will return AWS logo with aws argument' do - logo = helper.provider_icon('aws') - - expect(logo).to match(%r(img alt="Amazon EKS" data-src="|/illustrations/logos/amazon_eks|svg)) - end - - it 'will return default logo with unknown provider' do - logo = helper.provider_icon('unknown') - - expect(logo).to match(%r(img alt="Kubernetes Cluster" data-src="|/illustrations/logos/kubernetes|svg)) - end - - it 'will return default logo when provider is empty' do - logo = helper.provider_icon - - expect(logo).to match(%r(img alt="Kubernetes Cluster" data-src="|/illustrations/logos/kubernetes|svg)) - end - end - describe '#cluster_type_label' do subject { helper.cluster_type_label(cluster_type) } diff --git a/spec/lib/gitlab/static_site_editor/config/file_config/entry/global_spec.rb b/spec/lib/gitlab/static_site_editor/config/file_config/entry/global_spec.rb new file mode 100644 index 00000000000..57edcd8a07b --- /dev/null +++ b/spec/lib/gitlab/static_site_editor/config/file_config/entry/global_spec.rb @@ -0,0 +1,182 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Gitlab::StaticSiteEditor::Config::FileConfig::Entry::Global do + let(:global) { described_class.new(hash) } + let(:default_static_site_generator_value) { 'middleman' } + + shared_examples_for 'valid default configuration' do + describe '#compose!' do + before do + global.compose! + end + + it 'creates nodes hash' do + expect(global.descendants).to be_an Array + end + + it 'creates node object for each entry' do + expect(global.descendants.count).to eq 1 + end + + it 'creates node object using valid class' do + expect(global.descendants.first) + .to be_an_instance_of expected_node_object_class + end + + it 'sets correct description for nodes' do + expect(global.descendants.first.description) + .to eq 'Configuration of the Static Site Editor static site generator.' + end + + describe '#leaf?' do + it 'is not leaf' do + expect(global).not_to be_leaf + end + end + end + + context 'when not composed' do + describe '#static_site_generator_value' do + it 'returns nil' do + expect(global.static_site_generator_value).to be nil + end + end + + describe '#leaf?' do + it 'is leaf' do + expect(global).to be_leaf + end + end + end + + context 'when composed' do + before do + global.compose! + end + + describe '#errors' do + it 'has no errors' do + expect(global.errors).to be_empty + end + end + + describe '#static_site_generator_value' do + it 'returns correct values' do + expect(global.static_site_generator_value).to eq(default_static_site_generator_value) + end + end + end + end + + describe '.nodes' do + it 'returns a hash' do + expect(described_class.nodes).to be_a(Hash) + end + + context 'when filtering all the entry/node names' do + it 'contains the expected node names' do + expect(described_class.nodes.keys) + .to match_array(%i[static_site_generator]) + end + end + end + + context 'when configuration is valid' do + context 'when some entries defined' do + let(:expected_node_object_class) { Gitlab::StaticSiteEditor::Config::FileConfig::Entry::StaticSiteGenerator } + let(:hash) do + { static_site_generator: default_static_site_generator_value } + end + + it_behaves_like 'valid default configuration' + end + end + + context 'when value is an empty hash' do + let(:expected_node_object_class) { Gitlab::Config::Entry::Unspecified } + let(:hash) { {} } + + it_behaves_like 'valid default configuration' + end + + context 'when configuration is not valid' do + before do + global.compose! + end + + context 'when static_site_generator is invalid' do + let(:hash) do + { static_site_generator: { not_a_string: true } } + end + + describe '#errors' do + it 'reports errors' do + expect(global.errors) + .to include 'static_site_generator config should be a string' + end + end + end + + context 'when there is an invalid key' do + let(:hash) do + { invalid_key: true } + end + + describe '#errors' do + it 'reports errors' do + expect(global.errors) + .to include 'global config contains unknown keys: invalid_key' + end + end + end + end + + context 'when value is not a hash' do + let(:hash) { [] } + + describe '#valid?' do + it 'is not valid' do + expect(global).not_to be_valid + end + end + + describe '#errors' do + it 'returns error about invalid type' do + expect(global.errors.first).to match /should be a hash/ + end + end + end + + describe '#specified?' do + it 'is concrete entry that is defined' do + expect(global.specified?).to be true + end + end + + describe '#[]' do + before do + global.compose! + end + + let(:hash) do + { static_site_generator: default_static_site_generator_value } + end + + context 'when entry exists' do + it 'returns correct entry' do + expect(global[:static_site_generator]) + .to be_an_instance_of Gitlab::StaticSiteEditor::Config::FileConfig::Entry::StaticSiteGenerator + expect(global[:static_site_generator].value).to eq default_static_site_generator_value + end + end + + context 'when entry does not exist' do + it 'always return unspecified node' do + expect(global[:some][:unknown][:node]) + .not_to be_specified + end + end + end +end diff --git a/spec/lib/gitlab/static_site_editor/config/file_config/entry/static_site_generator_spec.rb b/spec/lib/gitlab/static_site_editor/config/file_config/entry/static_site_generator_spec.rb new file mode 100644 index 00000000000..a9c730218cf --- /dev/null +++ b/spec/lib/gitlab/static_site_editor/config/file_config/entry/static_site_generator_spec.rb @@ -0,0 +1,50 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Gitlab::StaticSiteEditor::Config::FileConfig::Entry::StaticSiteGenerator do + let(:static_site_generator) { described_class.new(config) } + + describe 'validations' do + context 'when value is valid' do + let(:config) { 'middleman' } + + describe '#value' do + it 'returns a static_site_generator key' do + expect(static_site_generator.value).to eq config + end + end + + describe '#valid?' do + it 'is valid' do + expect(static_site_generator).to be_valid + end + end + end + + context 'when value is invalid' do + let(:config) { 'not-a-valid-generator' } + + describe '#valid?' do + it 'is not valid' do + expect(static_site_generator).not_to be_valid + end + end + end + + context 'when value has a wrong type' do + let(:config) { { not_a_string: true } } + + it 'reports errors about wrong type' do + expect(static_site_generator.errors) + .to include 'static site generator config should be a string' + end + end + end + + describe '.default' do + it 'returns default static_site_generator' do + expect(described_class.default).to eq 'middleman' + end + end +end diff --git a/spec/lib/gitlab/static_site_editor/config/file_config_spec.rb b/spec/lib/gitlab/static_site_editor/config/file_config_spec.rb index 594425c2dab..198ba611cf6 100644 --- a/spec/lib/gitlab/static_site_editor/config/file_config_spec.rb +++ b/spec/lib/gitlab/static_site_editor/config/file_config_spec.rb @@ -3,13 +3,93 @@ require 'spec_helper' RSpec.describe Gitlab::StaticSiteEditor::Config::FileConfig do - subject(:config) { described_class.new } + let(:config) do + described_class.new(yml) + end + + context 'when config is valid' do + context 'when config has valid values' do + let(:yml) do + <<-EOS + static_site_generator: middleman + EOS + end + + describe '#to_hash_with_defaults' do + it 'returns hash created from string' do + hash = { + static_site_generator: 'middleman' + } + + expect(config.to_hash_with_defaults).to eq hash + end + end + + describe '#valid?' do + it 'is valid' do + expect(config).to be_valid + end + + it 'has no errors' do + expect(config.errors).to be_empty + end + end + end + end + + context 'when a config entry has an empty value' do + let(:yml) { 'static_site_generator: ' } + + describe '#to_hash_with_defaults' do + it 'returns default values' do + hash = { + static_site_generator: 'middleman' + } + + expect(config.to_hash_with_defaults).to eq hash + end + end + + describe '#valid?' do + it 'is valid' do + expect(config).to be_valid + end + + it 'has no errors' do + expect(config.errors).to be_empty + end + end + end + + context 'when config is invalid' do + context 'when yml is incorrect' do + let(:yml) { '// invalid' } + + describe '.new' do + it 'raises error' do + expect { config }.to raise_error(described_class::ConfigError, /Invalid configuration format/) + end + end + end + + context 'when config value exists but is not a valid value' do + let(:yml) { 'static_site_generator: "unsupported-generator"' } + + describe '#valid?' do + it 'is not valid' do + expect(config).not_to be_valid + end - describe '#data' do - subject { config.data } + it 'has errors' do + expect(config.errors).not_to be_empty + end + end - it 'returns hardcoded data for now' do - is_expected.to match(static_site_generator: 'middleman') + describe '#errors' do + it 'returns an array of strings' do + expect(config.errors).to all(be_an_instance_of(String)) + end + end end end end diff --git a/spec/services/static_site_editor/config_service_spec.rb b/spec/services/static_site_editor/config_service_spec.rb index 5fff4e0af53..fed373828a1 100644 --- a/spec/services/static_site_editor/config_service_spec.rb +++ b/spec/services/static_site_editor/config_service_spec.rb @@ -7,8 +7,8 @@ RSpec.describe StaticSiteEditor::ConfigService do let_it_be(:user) { create(:user) } # params - let(:ref) { double(:ref) } - let(:path) { double(:path) } + let(:ref) { 'master' } + let(:path) { 'README.md' } let(:return_url) { double(:return_url) } # stub data @@ -42,22 +42,84 @@ RSpec.describe StaticSiteEditor::ConfigService do allow_next_instance_of(Gitlab::StaticSiteEditor::Config::GeneratedConfig) do |config| allow(config).to receive(:data) { generated_data } end + end + + context 'when reading file from repo fails with an unexpected error' do + let(:unexpected_error) { RuntimeError.new('some unexpected error') } - allow_next_instance_of(Gitlab::StaticSiteEditor::Config::FileConfig) do |config| - allow(config).to receive(:data) { file_data } + before do + allow(project.repository).to receive(:blob_data_at).and_raise(unexpected_error) + end + + it 'returns an error response' do + expect(Gitlab::ErrorTracking).to receive(:track_and_raise_exception).with(unexpected_error).and_call_original + expect { execute }.to raise_error(unexpected_error) end end - it 'returns merged generated data and config file data' do - expect(execute).to be_success - expect(execute.payload).to eq(generated: true, file: true) + context 'when file is missing' do + before do + allow(project.repository).to receive(:blob_data_at).and_raise(GRPC::NotFound) + expect_next_instance_of(Gitlab::StaticSiteEditor::Config::FileConfig, '{}') do |config| + allow(config).to receive(:valid?) { true } + allow(config).to receive(:to_hash_with_defaults) { file_data } + end + end + + it 'returns default config' do + expect(execute).to be_success + expect(execute.payload).to eq(generated: true, file: true) + end end - it 'returns an error if any keys would be overwritten by the merge' do - generated_data[:duplicate_key] = true - file_data[:duplicate_key] = true - expect(execute).to be_error - expect(execute.message).to match(/duplicate key.*duplicate_key.*found/i) + context 'when file is present' do + before do + allow(project.repository).to receive(:blob_data_at).with(ref, anything) do + config_content + end + end + + context 'and configuration is not valid' do + let(:config_content) { 'invalid content' } + + before do + expect_next_instance_of(Gitlab::StaticSiteEditor::Config::FileConfig, config_content) do |config| + error = 'error' + allow(config).to receive_message_chain('errors.first') { error } + allow(config).to receive(:valid?) { false } + end + end + + it 'returns an error' do + expect(execute).to be_error + expect(execute.message).to eq('Invalid configuration format') + end + end + + context 'and configuration is valid' do + # NOTE: This has to be a valid config, even though it is mocked, because + # `expect_next_instance_of` executes the constructor logic. + let(:config_content) { 'static_site_generator: middleman' } + + before do + expect_next_instance_of(Gitlab::StaticSiteEditor::Config::FileConfig, config_content) do |config| + allow(config).to receive(:valid?) { true } + allow(config).to receive(:to_hash_with_defaults) { file_data } + end + end + + it 'returns merged generated data and config file data' do + expect(execute).to be_success + expect(execute.payload).to eq(generated: true, file: true) + end + + it 'returns an error if any keys would be overwritten by the merge' do + generated_data[:duplicate_key] = true + file_data[:duplicate_key] = true + expect(execute).to be_error + expect(execute.message).to match(/duplicate key.*duplicate_key.*found/i) + end + end end end end |