1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2024-12-01 12:24:28 +01:00
fab-manager/test/integration/settings_test.rb

94 lines
3.3 KiB
Ruby
Raw Normal View History

2020-03-13 17:10:38 +01:00
# frozen_string_literal: true
2023-02-24 17:26:55 +01:00
require 'test_helper'
2020-03-13 17:10:38 +01:00
class SettingsTest < ActionDispatch::IntegrationTest
# Called before every test method runs. Can be used
# to set up fixture information.
def setup
@admin = User.find_by(username: 'admin')
login_as(@admin, scope: :user)
end
test 'update setting value' do
put '/api/settings/fablab_name',
2020-03-13 17:10:38 +01:00
params: {
setting: {
value: 'Test Fablab'
}
2018-12-17 16:02:02 +01:00
}
assert_equal 200, response.status
2023-02-24 17:26:55 +01:00
assert_match Mime[:json].to_s, response.content_type
resp = json_response(response.body)
assert_equal 'fablab_name', resp[:setting][:name]
assert_equal 'Test Fablab', resp[:setting][:value]
2018-12-17 16:02:02 +01:00
# Check record
2022-03-23 16:55:38 +01:00
setting = Setting.find_by(name: resp[:setting][:name])
2018-12-17 16:02:02 +01:00
assert_not_nil setting, 'setting was not found in database'
assert_equal 2, setting.history_values.count, 'all historical values were not found'
assert_includes setting.history_values.map(&:value), 'Fab Lab de La Casemate', 'previous parameter was not saved'
assert_includes setting.history_values.map(&:value), 'Test Fablab', 'current parameter was not saved'
end
2023-04-04 17:40:53 +02:00
test 'bulk update some settings' do
patch '/api/settings/bulk_update',
params: {
settings: [
{ name: 'fablab_name', value: 'Test Fablab' },
{ name: 'name_genre', value: 'male' },
{ name: 'main_color', value: '#ea519a' }
]
}
assert_equal 200, response.status
assert_match Mime[:json].to_s, response.content_type
resp = json_response(response.body)
assert(resp[:settings].any? { |s| s[:name] == 'fablab_name' && s[:value] == 'Test Fablab' })
assert(resp[:settings].any? { |s| s[:name] == 'name_genre' && s[:value] == 'male' })
assert(resp[:settings].any? { |s| s[:name] == 'main_color' && s[:value] == '#ea519a' })
end
test 'transactional bulk update fails' do
old_css = Setting.get('home_css')
old_color = Setting.get('main_color')
2023-04-04 17:40:53 +02:00
patch '/api/settings/bulk_update?transactional=true',
params: {
settings: [
{ name: 'home_css', value: 'INVALID CSS{{!!' },
{ name: 'main_color', value: '#ea519a' }
]
}
assert_equal 200, response.status
assert_match Mime[:json].to_s, response.content_type
resp = json_response(response.body)
assert_not_nil resp[:settings].first[:error]
assert_match(/Error: Invalid CSS after/, resp[:settings].first[:error].first)
# Check values havn't changed
assert_equal old_css, Setting.get('home_css')
assert_equal old_color, Setting.get('main_color')
2023-04-04 17:40:53 +02:00
end
test 'update setting with wrong name' do
put '/api/settings/does_not_exists',
2020-03-13 17:10:38 +01:00
params: {
setting: {
value: 'ERROR EXPECTED'
}
2018-12-17 16:02:02 +01:00
}
assert_equal 422, response.status
2023-02-24 17:26:55 +01:00
assert_match(/Name is not included in the list/, response.body)
end
2018-12-17 16:02:02 +01:00
test 'show setting' do
get '/api/settings/fablab_name'
assert_equal 200, response.status
2023-02-24 17:26:55 +01:00
assert_match Mime[:json].to_s, response.content_type
2018-12-17 16:02:02 +01:00
resp = json_response(response.body)
assert_equal 'fablab_name', resp[:setting][:name], 'wrong parameter name'
assert_equal 'Fab Lab de La Casemate', resp[:setting][:value], 'wrong parameter value'
end
end