base_test.rb 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. # frozen_string_literal: true
  2. require "test_helper"
  3. # rubocop:disable Lint/ConstantDefinitionInBlock
  4. class BaseTest < ActiveSupport::TestCase
  5. def find_value(var_name)
  6. Setting.where(var: var_name).take
  7. end
  8. def direct_update_record(var, value)
  9. record = find_value(var) || Setting.new(var: var)
  10. record[:value] = YAML.dump(value)
  11. record.save!(validate: false)
  12. end
  13. def assert_no_record(var)
  14. record = find_value(:admin_emails)
  15. assert_nil record, message: "#{var} should not have database record."
  16. end
  17. def assert_record_value(var, val)
  18. record = find_value(var)
  19. assert_not_nil record
  20. assert_equal val.to_yaml, record[:value]
  21. assert_equal val, record.value
  22. end
  23. test "define setting with protected keys" do
  24. assert_raise(RailsSettings::ProcetedKeyError, "Can't use var as setting key.") do
  25. class NewSetting < RailsSettings::Base
  26. field :var
  27. end
  28. end
  29. assert_raise(RailsSettings::ProcetedKeyError, "Can't use value as setting key.") do
  30. class NewSetting < RailsSettings::Base
  31. field :value
  32. end
  33. end
  34. end
  35. test "cache_prefix and cache_key" do
  36. assert_equal "rails-settings-cached/v1", Setting.cache_key
  37. Setting.cache_prefix { "v2" }
  38. assert_equal "rails-settings-cached/v2", Setting.cache_key
  39. end
  40. test "all_settings" do
  41. assert_equal({}, Setting.send(:_all_settings))
  42. end
  43. test "setting_keys" do
  44. assert_equal 15, Setting.keys.size
  45. assert_includes(Setting.keys, "host")
  46. assert_includes(Setting.keys, "readonly_item")
  47. assert_includes(Setting.keys, "default_tags")
  48. assert_includes(Setting.keys, "omniauth_google_options")
  49. assert_equal 12, Setting.editable_keys.size
  50. assert_includes(Setting.editable_keys, "host")
  51. assert_includes(Setting.editable_keys, "default_tags")
  52. assert_equal 3, Setting.readonly_keys.size
  53. assert_includes(Setting.readonly_keys, "readonly_item")
  54. assert_includes(Setting.readonly_keys, "readonly_item_with_proc")
  55. assert_includes(Setting.readonly_keys, "omniauth_google_options")
  56. end
  57. test "get_field" do
  58. assert_equal({}, Setting.get_field("foooo"))
  59. assert_equal(
  60. {scope: :application, key: "host", default: "http://example.com", type: :string, readonly: false, options: {}},
  61. Setting.get_field("host")
  62. )
  63. assert_equal(
  64. {scope: :omniauth, key: "omniauth_google_options", default: {client_id: "the-client-id", client_secret: "the-client-secret"}, type: :hash, readonly: true, options: {}},
  65. Setting.get_field("omniauth_google_options")
  66. )
  67. end
  68. test "defined_fields and scope" do
  69. scopes = Setting.defined_fields.select { |field| !field[:readonly] }.group_by { |field| field[:scope] || :none }
  70. # assert_equal 2, groups.length
  71. assert_equal %i[application contents mailer none], scopes.keys
  72. assert_equal 4, scopes[:application].length
  73. assert_equal 5, scopes[:contents].length
  74. assert_equal 2, scopes[:mailer].length
  75. end
  76. test "not exist field" do
  77. assert_raise(NoMethodError) { Setting.not_exist_method }
  78. end
  79. test "readonly field" do
  80. assert_equal 100, Setting.readonly_item
  81. assert_raise(NoMethodError) { Setting.readonly_item = 1 }
  82. assert_equal 103, Setting.readonly_item_with_proc
  83. assert_kind_of Hash, Setting.omniauth_google_options
  84. assert_equal "the-client-id", Setting.omniauth_google_options[:client_id]
  85. assert_equal "the-client-secret", Setting.omniauth_google_options[:client_secret]
  86. assert_raise(NoMethodError) { Setting.omniauth_google_options = {foo: 1} }
  87. end
  88. test "instance method get field" do
  89. setting = Setting.new
  90. assert_equal Setting.host, setting.host
  91. assert_equal Setting.default_tags, setting.default_tags
  92. assert_equal Setting.readonly_item, setting.readonly_item
  93. assert_equal 103, setting.readonly_item_with_proc
  94. end
  95. test "value serialize" do
  96. assert_equal 1, Setting.user_limits
  97. Setting.user_limits = 12
  98. assert_equal 12, Setting.user_limits
  99. assert_record_value :user_limits, 12
  100. end
  101. test "string field" do
  102. assert_equal "http://example.com", Setting.host
  103. Setting.host = "https://www.example.com"
  104. assert_equal "https://www.example.com", Setting.host
  105. Setting.host = "https://www.rubyonrails.org"
  106. assert_equal "https://www.rubyonrails.org", Setting.host
  107. end
  108. test "integer field" do
  109. assert_equal 1, Setting.user_limits
  110. assert_instance_of Integer, Setting.user_limits
  111. assert_no_record :user_limits
  112. Setting.user_limits = 12
  113. assert_equal 12, Setting.user_limits
  114. assert_instance_of Integer, Setting.user_limits
  115. assert_record_value :user_limits, 12
  116. Setting.user_limits = "27"
  117. assert_equal 27, Setting.user_limits
  118. assert_instance_of Integer, Setting.user_limits
  119. assert_record_value :user_limits, 27
  120. Setting.user_limits = 2.7
  121. assert_equal 2, Setting.user_limits
  122. assert_instance_of Integer, Setting.user_limits
  123. assert_record_value :user_limits, 2
  124. assert_equal 2, Setting.default_value_with_block
  125. Setting.default_value_with_block = 100
  126. assert_equal 100, Setting.default_value_with_block
  127. end
  128. test "float field" do
  129. assert_equal 7, Setting.float_item
  130. assert_instance_of Float, Setting.float_item
  131. assert_no_record :float_item
  132. Setting.float_item = 9
  133. assert_equal 9, Setting.float_item
  134. assert_instance_of Float, Setting.float_item
  135. assert_record_value :float_item, 9.to_f
  136. Setting.float_item = 2.9
  137. assert_equal 2.9, Setting.float_item
  138. assert_instance_of Float, Setting.float_item
  139. assert_record_value :float_item, 2.9
  140. Setting.float_item = "2.9"
  141. assert_equal 2.9, Setting.float_item
  142. assert_instance_of Float, Setting.float_item
  143. assert_record_value :float_item, "2.9".to_f
  144. end
  145. test "big decimal field" do
  146. assert_equal 9, Setting.big_decimal_item
  147. assert_instance_of BigDecimal, Setting.big_decimal_item
  148. assert_no_record :big_decimal_item
  149. Setting.big_decimal_item = 7
  150. assert_equal 7, Setting.big_decimal_item
  151. assert_instance_of BigDecimal, Setting.big_decimal_item
  152. assert_record_value :big_decimal_item, 7.to_d
  153. Setting.big_decimal_item = 2.9
  154. assert_equal 2.9, Setting.big_decimal_item
  155. assert_instance_of BigDecimal, Setting.big_decimal_item
  156. assert_record_value :big_decimal_item, 2.9.to_d
  157. Setting.big_decimal_item = "2.9"
  158. assert_equal 2.9, Setting.big_decimal_item
  159. assert_instance_of BigDecimal, Setting.big_decimal_item
  160. assert_record_value :big_decimal_item, "2.9".to_d
  161. end
  162. test "array field" do
  163. assert_equal %w[admin@rubyonrails.org], Setting.admin_emails
  164. assert_no_record :admin_emails
  165. new_emails = %w[admin@rubyonrails.org huacnlee@gmail.com]
  166. Setting.admin_emails = new_emails
  167. assert_equal new_emails, Setting.admin_emails
  168. assert_record_value :admin_emails, new_emails
  169. Setting.admin_emails = new_emails.join("\n")
  170. assert_equal new_emails, Setting.admin_emails
  171. assert_record_value :admin_emails, new_emails
  172. Setting.admin_emails = new_emails.join(",")
  173. assert_equal new_emails, Setting.admin_emails
  174. assert_record_value :admin_emails, new_emails
  175. Setting.admin_emails = new_emails.join(";")
  176. assert_equal new_emails, Setting.admin_emails
  177. assert_record_value :admin_emails, new_emails
  178. Setting.admin_emails = new_emails.join(" , ")
  179. assert_equal new_emails, Setting.admin_emails
  180. assert_record_value :admin_emails, new_emails
  181. end
  182. test "hash field" do
  183. default_value = {
  184. host: "foo.com",
  185. username: "foo@bar.com",
  186. password: "123456"
  187. }
  188. assert_equal default_value, Setting.smtp_settings
  189. assert_no_record :smtp_settings
  190. # sym keys
  191. new_value = {
  192. title: "123",
  193. name: "456"
  194. }
  195. Setting.smtp_settings = new_value
  196. record = find_value(:smtp_settings)
  197. assert_equal new_value.deep_stringify_keys, Setting.smtp_settings
  198. assert_record_value :smtp_settings, new_value
  199. # string keys
  200. new_value = {
  201. "title" => "456",
  202. "age" => 32,
  203. "name" => "Jason Lee"
  204. }
  205. Setting.smtp_settings = new_value
  206. assert_equal new_value.deep_stringify_keys, Setting.smtp_settings
  207. assert_equal "456", Setting.smtp_settings[:title]
  208. assert_equal "456", Setting.smtp_settings["title"]
  209. assert_equal 32, Setting.smtp_settings[:age]
  210. assert_equal 32, Setting.smtp_settings["age"]
  211. assert_equal "Jason Lee", Setting.smtp_settings[:name]
  212. assert_equal "Jason Lee", Setting.smtp_settings["name"]
  213. assert_record_value :smtp_settings, new_value
  214. # JSON key
  215. new_value = {
  216. "sym" => :symbol,
  217. "str" => "string",
  218. "num" => 27.72,
  219. "float" => 9.to_f,
  220. "big_decimal" => 2.9.to_d
  221. }
  222. Setting.smtp_settings = new_value
  223. assert_equal new_value.deep_stringify_keys, Setting.smtp_settings
  224. assert_equal :symbol, Setting.smtp_settings[:sym]
  225. assert_equal :symbol, Setting.smtp_settings["sym"]
  226. assert_equal "string", Setting.smtp_settings["str"]
  227. assert_equal "string", Setting.smtp_settings[:str]
  228. assert_equal 27.72, Setting.smtp_settings["num"]
  229. assert_equal 9.to_f, Setting.smtp_settings["float"]
  230. assert_equal 2.9.to_d, Setting.smtp_settings["big_decimal"]
  231. assert_record_value :smtp_settings, new_value
  232. Setting.find_by(var: :smtp_settings).update(value: new_value.to_json)
  233. assert_equal({"sym" => "symbol", "str" => "string", "num" => 27.72, "float" => 9.to_f, "big_decimal" => "2.9"}, Setting.smtp_settings)
  234. assert_equal "symbol", Setting.smtp_settings[:sym]
  235. assert_equal "symbol", Setting.smtp_settings["sym"]
  236. end
  237. test "boolean field" do
  238. assert_equal true, Setting.captcha_enable
  239. assert_no_record :captcha_enable
  240. Setting.captcha_enable = "0"
  241. assert_equal false, Setting.captcha_enable
  242. assert_equal false, Setting.captcha_enable?
  243. Setting.captcha_enable = "1"
  244. assert_equal true, Setting.captcha_enable
  245. assert_equal true, Setting.captcha_enable?
  246. Setting.captcha_enable = "false"
  247. assert_equal false, Setting.captcha_enable
  248. assert_equal false, Setting.captcha_enable?
  249. Setting.captcha_enable = "true"
  250. assert_equal true, Setting.captcha_enable
  251. assert_equal true, Setting.captcha_enable?
  252. Setting.captcha_enable = 0
  253. assert_equal false, Setting.captcha_enable
  254. assert_equal false, Setting.captcha_enable?
  255. Setting.captcha_enable = 1
  256. assert_equal true, Setting.captcha_enable
  257. assert_equal true, Setting.captcha_enable?
  258. Setting.captcha_enable = false
  259. assert_equal false, Setting.captcha_enable
  260. assert_equal false, Setting.captcha_enable?
  261. Setting.captcha_enable = true
  262. assert_equal true, Setting.captcha_enable
  263. assert_equal true, Setting.captcha_enable?
  264. end
  265. test "string value in db compatible" do
  266. # array
  267. direct_update_record(:admin_emails, "foo@gmail.com,bar@dar.com\naaa@bbb.com")
  268. assert_equal 3, Setting.admin_emails.length
  269. assert_kind_of Array, Setting.admin_emails
  270. assert_equal %w[foo@gmail.com bar@dar.com aaa@bbb.com], Setting.admin_emails
  271. # integer
  272. direct_update_record(:user_limits, "100")
  273. assert_equal 100, Setting.user_limits
  274. assert_kind_of Integer, Setting.user_limits
  275. # boolean
  276. direct_update_record(:captcha_enable, "0")
  277. assert_equal false, Setting.captcha_enable
  278. direct_update_record(:captcha_enable, "false")
  279. assert_equal false, Setting.captcha_enable
  280. direct_update_record(:captcha_enable, "true")
  281. assert_equal true, Setting.captcha_enable
  282. direct_update_record(:captcha_enable, "1")
  283. assert_equal true, Setting.captcha_enable
  284. end
  285. test "array with separator" do
  286. value = <<~TIP
  287. Hello this is first line, and have comma.
  288. This is second line.
  289. TIP
  290. direct_update_record(:tips, value)
  291. assert_equal 2, Setting.tips.length
  292. assert_equal "Hello this is first line, and have comma.", Setting.tips[0]
  293. assert_equal "This is second line.", Setting.tips[1]
  294. value = "Ruby Rails,GitHub"
  295. direct_update_record(:default_tags, value)
  296. assert_equal %w[Ruby Rails GitHub], Setting.default_tags
  297. end
  298. test "key with complex options" do
  299. assert_equal %w[foo bar], Setting.key_with_more_options
  300. field = Setting.get_field(:key_with_more_options)
  301. assert_equal({scope: nil, key: "key_with_more_options", default: ["foo", "bar"], type: :array, readonly: false, options: {foo: 1, section: :theme}}, field)
  302. end
  303. test "rails_scope" do
  304. assert_kind_of ActiveRecord::Relation, Setting.ordered
  305. assert_equal %(SELECT "settings".* FROM "settings" ORDER BY "settings"."id" DESC), Setting.ordered.to_sql
  306. assert_equal %(SELECT "settings".* FROM "settings" WHERE (var like 'readonly_%')), Setting.by_prefix("readonly_").to_sql
  307. assert_equal "foo", Setting.by_prefix("readonly_").foo
  308. end
  309. end