diff --git a/app/controllers/subscriptions_controller.rb b/app/controllers/subscriptions_controller.rb index e5c4fa34a..4ec340cff 100644 --- a/app/controllers/subscriptions_controller.rb +++ b/app/controllers/subscriptions_controller.rb @@ -5,6 +5,12 @@ class SubscriptionsController < ApplicationController def edit end + def update + @user.update!(subscriptions_params) + redirect_to edit_subscriptions_path(token: @user.subscriptions_token), + notice: t("flash.actions.save_changes.notice") + end + private def set_user @@ -14,4 +20,12 @@ class SubscriptionsController < ApplicationController current_user || raise(CanCan::AccessDenied) end end + + def subscriptions_params + params.require(:user).permit(allowed_params) + end + + def allowed_params + [:email_on_comment, :email_on_comment_reply, :email_on_direct_message, :email_digest, :newsletter] + end end diff --git a/config/routes/account.rb b/config/routes/account.rb index 128107207..e4baa306e 100644 --- a/config/routes/account.rb +++ b/config/routes/account.rb @@ -2,4 +2,4 @@ resource :account, controller: "account", only: [:show, :update, :delete] do get :erase, on: :collection end -resource :subscriptions, only: [:edit] +resource :subscriptions, only: [:edit, :update] diff --git a/spec/system/subscriptions_spec.rb b/spec/system/subscriptions_spec.rb new file mode 100644 index 000000000..d480f8a38 --- /dev/null +++ b/spec/system/subscriptions_spec.rb @@ -0,0 +1,30 @@ +require "rails_helper" + +describe "Subscriptions" do + let(:user) { create(:user, subscriptions_token: SecureRandom.base58(32)) } + + context "Update" do + scenario "Allow updating the status notification" do + user.update!(email_on_comment: false, + email_on_comment_reply: true, + newsletter: true, + email_digest: false, + email_on_direct_message: true) + visit edit_subscriptions_path(token: user.subscriptions_token) + + check "Notify me by email when someone comments on my proposals or debates" + uncheck "Notify me by email when someone replies to my comments" + uncheck "Receive by email website relevant information" + check "Receive a summary of proposal notifications" + uncheck "Receive emails about direct messages" + click_button "Save changes" + + expect(page).to have_content "Changes saved" + expect(page).to have_field "Notify me by email when someone comments on my contents", checked: true + expect(page).to have_field "Notify me by email when someone replies to my comments", checked: false + expect(page).to have_field "Receive by email website relevant information", checked: false + expect(page).to have_field "Receive a summary of proposal notifications", checked: true + expect(page).to have_field "Receive emails about direct messages", checked: false + end + end +end