require 'rails_helper' RSpec.describe 'user settings', type: :request do describe 'GET /users/settings' do it 'requires a current user' do sign_out get '/users/settings' expect(response).to have_http_status(:unauthorized) end it 'returns defaults and creates a missing settings row' do user = create(:user) sign_in_as(user) expect { get '/users/settings' }.to change(Setting, :count).by(1) expect(response).to have_http_status(:ok) expect(json).to eq( 'theme' => 'system', 'auto_fetch_title' => 'manual', 'auto_fetch_thumbnail' => 'manual', 'wiki_editor_mode' => 'split') expect(user.reload.setting).to be_present end end describe 'PATCH /users/settings' do it 'requires a current user' do sign_out patch '/users/settings', params: { theme: 'dark' }, as: :json expect(response).to have_http_status(:unauthorized) end it 'updates multiple typed settings at once' do user = create(:user) sign_in_as(user) patch '/users/settings', params: { theme: 'dark', auto_fetch_title: 'auto', auto_fetch_thumbnail: 'off', wiki_editor_mode: 'preview' }, as: :json expect(response).to have_http_status(:ok) expect(json).to include( 'theme' => 'dark', 'auto_fetch_title' => 'auto', 'auto_fetch_thumbnail' => 'off', 'wiki_editor_mode' => 'preview') expect(user.reload.setting).to have_attributes( theme: 'dark', auto_fetch_title: 'auto', auto_fetch_thumbnail: 'off', wiki_editor_mode: 'preview') end it 'does not treat Rails parameter wrapping as an editable setting' do user = create(:user) sign_in_as(user) patch '/users/settings', params: { theme: 'light', user_setting: { theme: 'dark' } }, as: :json expect(response).to have_http_status(:ok) expect(user.reload.setting.theme).to eq('light') end it 'returns validation_error for type mismatches' do sign_in_as(create(:user)) patch '/users/settings', params: { theme: 1 }, as: :json expect(response).to have_http_status(:unprocessable_entity) expect(json).to include('type' => 'validation_error') expect(json.fetch('errors')).to include( 'theme' => ['値の型が不正です.']) end it 'returns validation_error for invalid enum values' do sign_in_as(create(:user)) patch '/users/settings', params: { theme: 'neon' }, as: :json expect(response).to have_http_status(:unprocessable_entity) expect(json).to include('type' => 'validation_error') expect(json.fetch('errors').fetch('theme')).to be_present end end end