Commit 5529507d authored by Chad Woolley's avatar Chad Woolley Committed by Mayra Cabrera

Introduce SSE config image_upload_path

Adds 'image_upload_path' config entry for
Static Site Editor config file
parent 2ac488ff
---
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 ...@@ -13,7 +13,10 @@ module Gitlab
include ::Gitlab::Config::Entry::Configurable include ::Gitlab::Config::Entry::Configurable
include ::Gitlab::Config::Entry::Attributable 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 attributes ALLOWED_KEYS
...@@ -21,6 +24,8 @@ module Gitlab ...@@ -21,6 +24,8 @@ module Gitlab
validates :config, allowed_keys: ALLOWED_KEYS validates :config, allowed_keys: ALLOWED_KEYS
end end
entry :image_upload_path, Entry::ImageUploadPath,
description: 'Configuration of the Static Site Editor image upload path.'
entry :static_site_generator, Entry::StaticSiteGenerator, entry :static_site_generator, Entry::StaticSiteGenerator,
description: 'Configuration of the Static Site Editor static site generator.' description: 'Configuration of the Static Site Editor static site generator.'
end 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 ...@@ -35,18 +35,15 @@ RSpec.describe 'Static Site Editor' do
expect(node['data-project-id']).to eq(project.id.to_s) expect(node['data-project-id']).to eq(project.id.to_s)
# assert default config file values are present # 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') expect(node['data-static-site-generator']).to eq('middleman')
end end
end end
context "when a config file is present" do context "when a config file is present" do
let(:config_file_yml) 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 <<-EOS
image_upload_path: custom-image-upload-path
static_site_generator: middleman static_site_generator: middleman
EOS EOS
end end
...@@ -63,6 +60,7 @@ RSpec.describe 'Static Site Editor' do ...@@ -63,6 +60,7 @@ RSpec.describe 'Static Site Editor' do
node = page.find('#static-site-editor') node = page.find('#static-site-editor')
# assert user-specified config file values are present # 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') expect(node['data-static-site-generator']).to eq('middleman')
end end
end end
......
...@@ -5,6 +5,7 @@ require 'spec_helper' ...@@ -5,6 +5,7 @@ require 'spec_helper'
RSpec.describe Gitlab::StaticSiteEditor::Config::FileConfig::Entry::Global do RSpec.describe Gitlab::StaticSiteEditor::Config::FileConfig::Entry::Global do
let(:global) { described_class.new(hash) } let(:global) { described_class.new(hash) }
let(:default_static_site_generator_value) { 'middleman' } let(:default_static_site_generator_value) { 'middleman' }
let(:default_image_upload_path_value) { 'source/images' }
shared_examples_for 'valid default configuration' do shared_examples_for 'valid default configuration' do
describe '#compose!' do describe '#compose!' do
...@@ -17,17 +18,15 @@ RSpec.describe Gitlab::StaticSiteEditor::Config::FileConfig::Entry::Global do ...@@ -17,17 +18,15 @@ RSpec.describe Gitlab::StaticSiteEditor::Config::FileConfig::Entry::Global do
end end
it 'creates node object for each entry' do it 'creates node object for each entry' do
expect(global.descendants.count).to eq 1 expect(global.descendants.count).to eq 2
end end
it 'creates node object using valid class' do it 'creates node object using valid class' do
expect(global.descendants.first) expect(global.descendants.map(&:class)).to match_array(expected_node_object_classes)
.to be_an_instance_of expected_node_object_class
end end
it 'sets correct description for nodes' do it 'sets a description containing "Static Site Editor" for all nodes' do
expect(global.descendants.first.description) expect(global.descendants.map(&:description)).to all(match(/Static Site Editor/))
.to eq 'Configuration of the Static Site Editor static site generator.'
end end
describe '#leaf?' do describe '#leaf?' do
...@@ -62,6 +61,12 @@ RSpec.describe Gitlab::StaticSiteEditor::Config::FileConfig::Entry::Global do ...@@ -62,6 +61,12 @@ RSpec.describe Gitlab::StaticSiteEditor::Config::FileConfig::Entry::Global do
end end
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 describe '#static_site_generator_value' do
it 'returns correct values' do it 'returns correct values' do
expect(global.static_site_generator_value).to eq(default_static_site_generator_value) 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 ...@@ -77,17 +82,29 @@ RSpec.describe Gitlab::StaticSiteEditor::Config::FileConfig::Entry::Global do
context 'when filtering all the entry/node names' do context 'when filtering all the entry/node names' do
it 'contains the expected node names' do it 'contains the expected node names' do
expect(described_class.nodes.keys) expected_node_names = %i[
.to match_array(%i[static_site_generator]) image_upload_path
static_site_generator
]
expect(described_class.nodes.keys).to match_array(expected_node_names)
end end
end end
end end
context 'when configuration is valid' do context 'when configuration is valid' do
context 'when some entries defined' 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 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 end
it_behaves_like 'valid default configuration' it_behaves_like 'valid default configuration'
...@@ -95,7 +112,13 @@ RSpec.describe Gitlab::StaticSiteEditor::Config::FileConfig::Entry::Global do ...@@ -95,7 +112,13 @@ RSpec.describe Gitlab::StaticSiteEditor::Config::FileConfig::Entry::Global do
end end
context 'when value is an empty hash' do 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) { {} } let(:hash) { {} }
it_behaves_like 'valid default configuration' it_behaves_like 'valid default configuration'
...@@ -106,15 +129,35 @@ RSpec.describe Gitlab::StaticSiteEditor::Config::FileConfig::Entry::Global do ...@@ -106,15 +129,35 @@ RSpec.describe Gitlab::StaticSiteEditor::Config::FileConfig::Entry::Global do
global.compose! global.compose!
end 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 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 end
describe '#errors' do describe '#errors' do
it 'reports errors' do it 'reports errors' do
expect(global.errors) 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 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 ...@@ -17,11 +17,7 @@ RSpec.describe Gitlab::StaticSiteEditor::Config::FileConfig do
describe '#to_hash_with_defaults' do describe '#to_hash_with_defaults' do
it 'returns hash created from string' do it 'returns hash created from string' do
hash = { expect(config.to_hash_with_defaults.fetch(:static_site_generator)).to eq 'middleman'
static_site_generator: 'middleman'
}
expect(config.to_hash_with_defaults).to eq hash
end end
end end
...@@ -40,13 +36,9 @@ RSpec.describe Gitlab::StaticSiteEditor::Config::FileConfig do ...@@ -40,13 +36,9 @@ RSpec.describe Gitlab::StaticSiteEditor::Config::FileConfig do
context 'when a config entry has an empty value' do context 'when a config entry has an empty value' do
let(:yml) { 'static_site_generator: ' } let(:yml) { 'static_site_generator: ' }
describe '#to_hash_with_defaults' do describe '#to_hash' do
it 'returns default values' do it 'returns default value' do
hash = { expect(config.to_hash_with_defaults.fetch(:static_site_generator)).to eq 'middleman'
static_site_generator: 'middleman'
}
expect(config.to_hash_with_defaults).to eq hash
end 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