mirror of
https://github.com/internetee/registry.git
synced 2025-06-06 20:55:44 +02:00
Allow only letters and underscores in Setting code
This commit is contained in:
parent
c977872c44
commit
abea8abe84
4 changed files with 101 additions and 2 deletions
|
@ -4,6 +4,7 @@ class SettingEntry < ApplicationRecord
|
||||||
validates :format, presence: true
|
validates :format, presence: true
|
||||||
validates :group, presence: true
|
validates :group, presence: true
|
||||||
validate :valid_value_format
|
validate :valid_value_format
|
||||||
|
validates_format_of :code, with: /([a-z])[a-z|_]+[a-z]/
|
||||||
|
|
||||||
VALUE_FORMATS = {
|
VALUE_FORMATS = {
|
||||||
string: :string_format,
|
string: :string_format,
|
||||||
|
|
|
@ -2,7 +2,7 @@ class CreateSettingEntries < ActiveRecord::Migration[6.0]
|
||||||
def change
|
def change
|
||||||
create_table :setting_entries do |t|
|
create_table :setting_entries do |t|
|
||||||
t.string :code, null: false, index: { unique: true }
|
t.string :code, null: false, index: { unique: true }
|
||||||
t.string :value, null: false, default: ''
|
t.string :value
|
||||||
t.string :group, null: false
|
t.string :group, null: false
|
||||||
t.string :format, null: false
|
t.string :format, null: false
|
||||||
|
|
||||||
|
|
|
@ -2307,7 +2307,7 @@ CREATE TABLE public.schema_migrations (
|
||||||
CREATE TABLE public.setting_entries (
|
CREATE TABLE public.setting_entries (
|
||||||
id bigint NOT NULL,
|
id bigint NOT NULL,
|
||||||
code character varying NOT NULL,
|
code character varying NOT NULL,
|
||||||
value character varying DEFAULT ''::character varying,
|
value character varying,
|
||||||
"group" character varying NOT NULL,
|
"group" character varying NOT NULL,
|
||||||
format character varying NOT NULL,
|
format character varying NOT NULL,
|
||||||
creator_str character varying,
|
creator_str character varying,
|
||||||
|
|
|
@ -2,9 +2,107 @@ require 'test_helper'
|
||||||
|
|
||||||
class SettingEntryTest < ActiveSupport::TestCase
|
class SettingEntryTest < ActiveSupport::TestCase
|
||||||
def setup
|
def setup
|
||||||
|
@new_setting = SettingEntry.new(code: 'new_setting', value: 'looks great', format: 'string', group: 'other')
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_fixture_is_valid
|
def test_fixture_is_valid
|
||||||
assert setting_entries(:legal_document_is_mandatory).valid?
|
assert setting_entries(:legal_document_is_mandatory).valid?
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_can_be_retrieved_via_class_method
|
||||||
|
setting = setting_entries(:legal_document_is_mandatory)
|
||||||
|
assert setting.retrieve, Setting.legal_document_is_mandatory
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_can_be_updated_via_class_method
|
||||||
|
setting = setting_entries(:legal_document_is_mandatory)
|
||||||
|
setting.update(value: 'false')
|
||||||
|
setting.reload
|
||||||
|
|
||||||
|
Setting.legal_document_is_mandatory = true
|
||||||
|
setting.reload
|
||||||
|
assert true, setting.retrieve
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_setting_code_is_required
|
||||||
|
assert @new_setting.valid?
|
||||||
|
@new_setting.code = nil
|
||||||
|
assert_not @new_setting.valid?
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_setting_code_can_only_include_underscore_and_characters
|
||||||
|
assert @new_setting.valid?
|
||||||
|
@new_setting.code = 'a b'
|
||||||
|
assert_not @new_setting.valid?
|
||||||
|
|
||||||
|
@new_setting.code = 'a_b'
|
||||||
|
assert @new_setting.valid?
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_setting_value_can_be_nil
|
||||||
|
assert @new_setting.valid?
|
||||||
|
@new_setting.value = nil
|
||||||
|
assert @new_setting.valid?
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_setting_format_is_required
|
||||||
|
assert @new_setting.valid?
|
||||||
|
@new_setting.format = nil
|
||||||
|
assert_not @new_setting.valid?
|
||||||
|
|
||||||
|
@new_setting.format = 'nonexistant'
|
||||||
|
assert_not @new_setting.valid?
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_setting_group_is_required
|
||||||
|
assert @new_setting.valid?
|
||||||
|
@new_setting.group = nil
|
||||||
|
assert_not @new_setting.valid?
|
||||||
|
|
||||||
|
@new_setting.group = 'random'
|
||||||
|
assert @new_setting.valid?
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_returns_nil_for_unknown_setting
|
||||||
|
assert_nil Setting.unknown_and_definitely_not_saved_setting
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_throws_error_if_updating_unknown_setting
|
||||||
|
assert_raises ActiveRecord::RecordNotFound do
|
||||||
|
Setting.unknown_and_definitely_not_saved_setting = 'hope it fails'
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_parses_string_format
|
||||||
|
Setting.create(code: 'string_format', value: '1', format: 'string', group: 'random')
|
||||||
|
assert Setting.string_format.is_a? String
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_parses_integer_format
|
||||||
|
Setting.create(code: 'integer_format', value: '1', format: 'integer', group: 'random')
|
||||||
|
assert Setting.integer_format.is_a? Integer
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_parses_float_format
|
||||||
|
Setting.create(code: 'float_format', value: '0.5', format: 'float', group: 'random')
|
||||||
|
assert Setting.float_format.is_a? Float
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_parses_boolean_format
|
||||||
|
Setting.create(code: 'boolean_format', value: 'true', format: 'boolean', group: 'random')
|
||||||
|
assert_equal true, Setting.boolean_format
|
||||||
|
|
||||||
|
Setting.boolean_format = 'false'
|
||||||
|
assert_equal false, Setting.boolean_format
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_parses_hash_format
|
||||||
|
Setting.create(code: 'hash_format', value: '{"hello": "there"}', format: 'hash', group: 'random')
|
||||||
|
assert Setting.hash_format.is_a? Hash
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_parses_array_format
|
||||||
|
Setting.create(code: 'array_format', value: '[1, 2, 3]', format: 'array', group: 'random')
|
||||||
|
assert Setting.array_format.is_a? Array
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue