Commit 5a19da6f authored by Mayra Cabrera's avatar Mayra Cabrera

Merge branch '241165-sse-config-image-upload-path' into 'master'

Introduce configuration value for image upload path

See merge request gitlab-org/gitlab!43481
parents ade7aa1c 5529507d
---
title: Introduce 'image_upload_path' entry support for '.gitlab/static-site-editor.yml' config file.
merge_request: 43481
author:
type: added
......@@ -13,7 +13,10 @@ module Gitlab
include ::Gitlab::Config::Entry::Configurable
include ::Gitlab::Config::Entry::Attributable
ALLOWED_KEYS = %i[static_site_generator].freeze
ALLOWED_KEYS = %i[
image_upload_path
static_site_generator
].freeze
attributes ALLOWED_KEYS
......@@ -21,6 +24,8 @@ module Gitlab
validates :config, allowed_keys: ALLOWED_KEYS
end
entry :image_upload_path, Entry::ImageUploadPath,
description: 'Configuration of the Static Site Editor image upload path.'
entry :static_site_generator, Entry::StaticSiteGenerator,
description: 'Configuration of the Static Site Editor static site generator.'
end
......
# frozen_string_literal: true
module Gitlab
module StaticSiteEditor
module Config
class FileConfig
module Entry
##
# Entry that represents the path to which images will be uploaded
#
class ImageUploadPath < ::Gitlab::Config::Entry::Node
include ::Gitlab::Config::Entry::Validatable
validations do
validates :config, type: String
end
def self.default
'source/images'
end
end
end
end
end
end
end
......@@ -35,18 +35,15 @@ RSpec.describe 'Static Site Editor' do
expect(node['data-project-id']).to eq(project.id.to_s)
# assert default config file values are present
expect(node['data-image-upload-path']).to eq('source/images')
expect(node['data-static-site-generator']).to eq('middleman')
end
end
context "when a config file is present" do
let(:config_file_yml) do
# NOTE: There isn't currently any support for a non-default config value, but this can be
# manually tested by temporarily adding an additional supported valid value in
# lib/gitlab/static_site_editor/config/file_config/entry/static_site_generator.rb.
# As soon as there is a real non-default value supported by the config file,
# this test can be updated to include it.
<<-EOS
image_upload_path: custom-image-upload-path
static_site_generator: middleman
EOS
end
......@@ -63,6 +60,7 @@ RSpec.describe 'Static Site Editor' do
node = page.find('#static-site-editor')
# assert user-specified config file values are present
expect(node['data-image-upload-path']).to eq('custom-image-upload-path')
expect(node['data-static-site-generator']).to eq('middleman')
end
end
......
......@@ -5,6 +5,7 @@ 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' }
let(:default_image_upload_path_value) { 'source/images' }
shared_examples_for 'valid default configuration' do
describe '#compose!' do
......@@ -17,17 +18,15 @@ RSpec.describe Gitlab::StaticSiteEditor::Config::FileConfig::Entry::Global do
end
it 'creates node object for each entry' do
expect(global.descendants.count).to eq 1
expect(global.descendants.count).to eq 2
end
it 'creates node object using valid class' do
expect(global.descendants.first)
.to be_an_instance_of expected_node_object_class
expect(global.descendants.map(&:class)).to match_array(expected_node_object_classes)
end
it 'sets correct description for nodes' do
expect(global.descendants.first.description)
.to eq 'Configuration of the Static Site Editor static site generator.'
it 'sets a description containing "Static Site Editor" for all nodes' do
expect(global.descendants.map(&:description)).to all(match(/Static Site Editor/))
end
describe '#leaf?' do
......@@ -62,6 +61,12 @@ RSpec.describe Gitlab::StaticSiteEditor::Config::FileConfig::Entry::Global do
end
end
describe '#image_upload_path_value' do
it 'returns correct values' do
expect(global.image_upload_path_value).to eq(default_image_upload_path_value)
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)
......@@ -77,17 +82,29 @@ RSpec.describe Gitlab::StaticSiteEditor::Config::FileConfig::Entry::Global do
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])
expected_node_names = %i[
image_upload_path
static_site_generator
]
expect(described_class.nodes.keys).to match_array(expected_node_names)
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(:expected_node_object_classes) do
[
Gitlab::StaticSiteEditor::Config::FileConfig::Entry::ImageUploadPath,
Gitlab::StaticSiteEditor::Config::FileConfig::Entry::StaticSiteGenerator
]
end
let(:hash) do
{ static_site_generator: default_static_site_generator_value }
{
image_upload_path: default_image_upload_path_value,
static_site_generator: default_static_site_generator_value
}
end
it_behaves_like 'valid default configuration'
......@@ -95,7 +112,13 @@ RSpec.describe Gitlab::StaticSiteEditor::Config::FileConfig::Entry::Global do
end
context 'when value is an empty hash' do
let(:expected_node_object_class) { Gitlab::Config::Entry::Unspecified }
let(:expected_node_object_classes) do
[
Gitlab::Config::Entry::Unspecified,
Gitlab::Config::Entry::Unspecified
]
end
let(:hash) { {} }
it_behaves_like 'valid default configuration'
......@@ -106,15 +129,35 @@ RSpec.describe Gitlab::StaticSiteEditor::Config::FileConfig::Entry::Global do
global.compose!
end
context 'when static_site_generator is invalid' do
context 'when a single entry is invalid' do
let(:hash) do
{ image_upload_path: { not_a_string: true } }
end
describe '#errors' do
it 'reports errors' do
expect(global.errors)
.to include 'image_upload_path config should be a string'
end
end
end
context 'when a multiple entries are invalid' do
let(:hash) do
{ static_site_generator: { not_a_string: true } }
{
image_upload_path: { not_a_string: true },
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'
.to match_array([
'image_upload_path config should be a string',
'static_site_generator config should be a string',
"static_site_generator config should be 'middleman'"
])
end
end
end
......
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Gitlab::StaticSiteEditor::Config::FileConfig::Entry::ImageUploadPath do
subject(:image_upload_path_entry) { described_class.new(config) }
describe 'validations' do
context 'with a valid config' do
let(:config) { 'an-image-upload-path' }
it { is_expected.to be_valid }
describe '#value' do
it 'returns a image_upload_path key' do
expect(image_upload_path_entry.value).to eq config
end
end
end
context 'with an invalid config' do
let(:config) { { not_a_string: true } }
it { is_expected.not_to be_valid }
it 'reports errors about wrong type' do
expect(image_upload_path_entry.errors)
.to include 'image upload path config should be a string'
end
end
end
describe '.default' do
it 'returns default image_upload_path' do
expect(described_class.default).to eq 'source/images'
end
end
end
......@@ -17,11 +17,7 @@ RSpec.describe Gitlab::StaticSiteEditor::Config::FileConfig do
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
expect(config.to_hash_with_defaults.fetch(:static_site_generator)).to eq 'middleman'
end
end
......@@ -40,13 +36,9 @@ RSpec.describe Gitlab::StaticSiteEditor::Config::FileConfig do
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
describe '#to_hash' do
it 'returns default value' do
expect(config.to_hash_with_defaults.fetch(:static_site_generator)).to eq 'middleman'
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