From a28ce13b3e7da2159fdc4800214910740a66c3bf Mon Sep 17 00:00:00 2001 From: Matt Jankowski Date: Mon, 22 May 2017 17:12:29 -0400 Subject: [PATCH] Coverage for api/web/settings controller (#3238) --- .../api/web/settings_controller.rb | 7 +++++- .../api/web/settings_controller_spec.rb | 24 +++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 spec/controllers/api/web/settings_controller_spec.rb diff --git a/app/controllers/api/web/settings_controller.rb b/app/controllers/api/web/settings_controller.rb index c00e016a4a..7cceb0dfc7 100644 --- a/app/controllers/api/web/settings_controller.rb +++ b/app/controllers/api/web/settings_controller.rb @@ -6,10 +6,15 @@ class Api::Web::SettingsController < ApiController before_action :require_user! def update - setting = ::Web::Setting.where(user: current_user).first_or_initialize(user: current_user) setting.data = params[:data] setting.save! render_empty end + + private + + def setting + @_setting ||= ::Web::Setting.where(user: current_user).first_or_initialize(user: current_user) + end end diff --git a/spec/controllers/api/web/settings_controller_spec.rb b/spec/controllers/api/web/settings_controller_spec.rb new file mode 100644 index 0000000000..ff211c7b1a --- /dev/null +++ b/spec/controllers/api/web/settings_controller_spec.rb @@ -0,0 +1,24 @@ +# frozen_string_literal: true + +require 'rails_helper' + +describe Api::Web::SettingsController do + render_views + + let!(:user) { Fabricate(:user) } + + describe 'PATCH #update' do + it 'redirects to about page' do + sign_in(user) + patch :update, format: :json, params: { data: { 'onboarded' => true } } + + user.reload + expect(response).to have_http_status(:success) + expect(user_web_setting.data['onboarded']).to eq('true') + end + + def user_web_setting + Web::Setting.where(user: user).first + end + end +end