about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMatt Jankowski <mjankowski@thoughtbot.com>2017-04-10 15:27:03 -0400
committerEugen <eugen@zeonfederated.com>2017-04-10 21:27:03 +0200
commitdbe9f33fdc9a995b07ff3b1dcd93ad02cd336649 (patch)
treed89768083aba71c27789dfb08651ef27811954cf
parent1be6aa0c7fdac51e81ff7ee0c2b9184ed29ca3de (diff)
Admin base controller (#1465)
* Add Admin::BaseController to wrap admin area

Extracts the setting of the `admin` layout and verifying that users are admins
to a common base class for the admin/ controllers.

* Add basic coverage for admin/reports and admin/settings controllers
-rw-r--r--app/controllers/admin/accounts_controller.rb93
-rw-r--r--app/controllers/admin/base_controller.rb9
-rw-r--r--app/controllers/admin/domain_blocks_controller.rb42
-rw-r--r--app/controllers/admin/pubsubhubbub_controller.rb12
-rw-r--r--app/controllers/admin/reports_controller.rb81
-rw-r--r--app/controllers/admin/settings_controller.rb46
-rw-r--r--spec/controllers/admin/reports_controller_spec.rb14
-rw-r--r--spec/controllers/admin/settings_controller_spec.rb14
8 files changed, 170 insertions, 141 deletions
diff --git a/app/controllers/admin/accounts_controller.rb b/app/controllers/admin/accounts_controller.rb
index df2c7bebf..60b631ece 100644
--- a/app/controllers/admin/accounts_controller.rb
+++ b/app/controllers/admin/accounts_controller.rb
@@ -1,51 +1,50 @@
 # frozen_string_literal: true
 
-class Admin::AccountsController < ApplicationController
-  before_action :require_admin!
-  before_action :set_account, except: :index
-
-  layout 'admin'
-
-  def index
-    @accounts = Account.alphabetic.paginate(page: params[:page], per_page: 40)
-
-    @accounts = @accounts.local                             if params[:local].present?
-    @accounts = @accounts.remote                            if params[:remote].present?
-    @accounts = @accounts.where(domain: params[:by_domain]) if params[:by_domain].present?
-    @accounts = @accounts.silenced                          if params[:silenced].present?
-    @accounts = @accounts.recent                            if params[:recent].present?
-    @accounts = @accounts.suspended                         if params[:suspended].present?
-  end
-
-  def show; end
-
-  def suspend
-    Admin::SuspensionWorker.perform_async(@account.id)
-    redirect_to admin_accounts_path
-  end
-
-  def unsuspend
-    @account.update(suspended: false)
-    redirect_to admin_accounts_path
-  end
-
-  def silence
-    @account.update(silenced: true)
-    redirect_to admin_accounts_path
-  end
-
-  def unsilence
-    @account.update(silenced: false)
-    redirect_to admin_accounts_path
-  end
-
-  private
-
-  def set_account
-    @account = Account.find(params[:id])
-  end
-
-  def account_params
-    params.require(:account).permit(:silenced, :suspended)
+module Admin
+  class AccountsController < BaseController
+    before_action :set_account, except: :index
+
+    def index
+      @accounts = Account.alphabetic.paginate(page: params[:page], per_page: 40)
+
+      @accounts = @accounts.local                             if params[:local].present?
+      @accounts = @accounts.remote                            if params[:remote].present?
+      @accounts = @accounts.where(domain: params[:by_domain]) if params[:by_domain].present?
+      @accounts = @accounts.silenced                          if params[:silenced].present?
+      @accounts = @accounts.recent                            if params[:recent].present?
+      @accounts = @accounts.suspended                         if params[:suspended].present?
+    end
+
+    def show; end
+
+    def suspend
+      Admin::SuspensionWorker.perform_async(@account.id)
+      redirect_to admin_accounts_path
+    end
+
+    def unsuspend
+      @account.update(suspended: false)
+      redirect_to admin_accounts_path
+    end
+
+    def silence
+      @account.update(silenced: true)
+      redirect_to admin_accounts_path
+    end
+
+    def unsilence
+      @account.update(silenced: false)
+      redirect_to admin_accounts_path
+    end
+
+    private
+
+    def set_account
+      @account = Account.find(params[:id])
+    end
+
+    def account_params
+      params.require(:account).permit(:silenced, :suspended)
+    end
   end
 end
diff --git a/app/controllers/admin/base_controller.rb b/app/controllers/admin/base_controller.rb
new file mode 100644
index 000000000..11fe326bc
--- /dev/null
+++ b/app/controllers/admin/base_controller.rb
@@ -0,0 +1,9 @@
+# frozen_string_literal: true
+
+module Admin
+  class BaseController < ApplicationController
+    before_action :require_admin!
+
+    layout 'admin'
+  end
+end
diff --git a/app/controllers/admin/domain_blocks_controller.rb b/app/controllers/admin/domain_blocks_controller.rb
index 1f4432847..58f1efa5b 100644
--- a/app/controllers/admin/domain_blocks_controller.rb
+++ b/app/controllers/admin/domain_blocks_controller.rb
@@ -1,32 +1,30 @@
 # frozen_string_literal: true
 
-class Admin::DomainBlocksController < ApplicationController
-  before_action :require_admin!
-
-  layout 'admin'
-
-  def index
-    @blocks = DomainBlock.paginate(page: params[:page], per_page: 40)
-  end
+module Admin
+  class DomainBlocksController < BaseController
+    def index
+      @blocks = DomainBlock.paginate(page: params[:page], per_page: 40)
+    end
 
-  def new
-    @domain_block = DomainBlock.new
-  end
+    def new
+      @domain_block = DomainBlock.new
+    end
 
-  def create
-    @domain_block = DomainBlock.new(resource_params)
+    def create
+      @domain_block = DomainBlock.new(resource_params)
 
-    if @domain_block.save
-      DomainBlockWorker.perform_async(@domain_block.id)
-      redirect_to admin_domain_blocks_path, notice: 'Domain block is now being processed'
-    else
-      render action: :new
+      if @domain_block.save
+        DomainBlockWorker.perform_async(@domain_block.id)
+        redirect_to admin_domain_blocks_path, notice: 'Domain block is now being processed'
+      else
+        render action: :new
+      end
     end
-  end
 
-  private
+    private
 
-  def resource_params
-    params.require(:domain_block).permit(:domain, :severity)
+    def resource_params
+      params.require(:domain_block).permit(:domain, :severity)
+    end
   end
 end
diff --git a/app/controllers/admin/pubsubhubbub_controller.rb b/app/controllers/admin/pubsubhubbub_controller.rb
index b9e840ffe..95f79c520 100644
--- a/app/controllers/admin/pubsubhubbub_controller.rb
+++ b/app/controllers/admin/pubsubhubbub_controller.rb
@@ -1,11 +1,9 @@
 # frozen_string_literal: true
 
-class Admin::PubsubhubbubController < ApplicationController
-  before_action :require_admin!
-
-  layout 'admin'
-
-  def index
-    @subscriptions = Subscription.order('id desc').includes(:account).paginate(page: params[:page], per_page: 40)
+module Admin
+  class PubsubhubbubController < BaseController
+    def index
+      @subscriptions = Subscription.order('id desc').includes(:account).paginate(page: params[:page], per_page: 40)
+    end
   end
 end
diff --git a/app/controllers/admin/reports_controller.rb b/app/controllers/admin/reports_controller.rb
index 2b3b1809f..5a37d8e6e 100644
--- a/app/controllers/admin/reports_controller.rb
+++ b/app/controllers/admin/reports_controller.rb
@@ -1,45 +1,44 @@
 # frozen_string_literal: true
 
-class Admin::ReportsController < ApplicationController
-  before_action :require_admin!
-  before_action :set_report, except: [:index]
-
-  layout 'admin'
-
-  def index
-    @reports = Report.includes(:account, :target_account).order('id desc').paginate(page: params[:page], per_page: 40)
-    @reports = params[:action_taken].present? ? @reports.resolved : @reports.unresolved
-  end
-
-  def show
-    @statuses = Status.where(id: @report.status_ids)
-  end
-
-  def resolve
-    @report.update(action_taken: true, action_taken_by_account_id: current_account.id)
-    redirect_to admin_report_path(@report)
-  end
-
-  def suspend
-    Admin::SuspensionWorker.perform_async(@report.target_account.id)
-    Report.unresolved.where(target_account: @report.target_account).update_all(action_taken: true, action_taken_by_account_id: current_account.id)
-    redirect_to admin_report_path(@report)
-  end
-
-  def silence
-    @report.target_account.update(silenced: true)
-    Report.unresolved.where(target_account: @report.target_account).update_all(action_taken: true, action_taken_by_account_id: current_account.id)
-    redirect_to admin_report_path(@report)
-  end
-
-  def remove
-    RemovalWorker.perform_async(params[:status_id])
-    redirect_to admin_report_path(@report)
-  end
-
-  private
-
-  def set_report
-    @report = Report.find(params[:id])
+module Admin
+  class ReportsController < BaseController
+    before_action :set_report, except: [:index]
+
+    def index
+      @reports = Report.includes(:account, :target_account).order('id desc').paginate(page: params[:page], per_page: 40)
+      @reports = params[:action_taken].present? ? @reports.resolved : @reports.unresolved
+    end
+
+    def show
+      @statuses = Status.where(id: @report.status_ids)
+    end
+
+    def resolve
+      @report.update(action_taken: true, action_taken_by_account_id: current_account.id)
+      redirect_to admin_report_path(@report)
+    end
+
+    def suspend
+      Admin::SuspensionWorker.perform_async(@report.target_account.id)
+      Report.unresolved.where(target_account: @report.target_account).update_all(action_taken: true, action_taken_by_account_id: current_account.id)
+      redirect_to admin_report_path(@report)
+    end
+
+    def silence
+      @report.target_account.update(silenced: true)
+      Report.unresolved.where(target_account: @report.target_account).update_all(action_taken: true, action_taken_by_account_id: current_account.id)
+      redirect_to admin_report_path(@report)
+    end
+
+    def remove
+      RemovalWorker.perform_async(params[:status_id])
+      redirect_to admin_report_path(@report)
+    end
+
+    private
+
+    def set_report
+      @report = Report.find(params[:id])
+    end
   end
 end
diff --git a/app/controllers/admin/settings_controller.rb b/app/controllers/admin/settings_controller.rb
index 7615c781d..6cca5c3e3 100644
--- a/app/controllers/admin/settings_controller.rb
+++ b/app/controllers/admin/settings_controller.rb
@@ -1,35 +1,33 @@
 # frozen_string_literal: true
 
-class Admin::SettingsController < ApplicationController
-  before_action :require_admin!
-
-  layout 'admin'
+module Admin
+  class SettingsController < BaseController
+    def index
+      @settings = Setting.all_as_records
+    end
 
-  def index
-    @settings = Setting.all_as_records
-  end
+    def update
+      @setting = Setting.where(var: params[:id]).first_or_initialize(var: params[:id])
+      value    = settings_params[:value]
 
-  def update
-    @setting = Setting.where(var: params[:id]).first_or_initialize(var: params[:id])
-    value    = settings_params[:value]
+      # Special cases
+      value = value == 'true' if @setting.var == 'open_registrations'
 
-    # Special cases
-    value = value == 'true' if @setting.var == 'open_registrations'
+      if @setting.value != value
+        @setting.value = value
+        @setting.save
+      end
 
-    if @setting.value != value
-      @setting.value = value
-      @setting.save
+      respond_to do |format|
+        format.html { redirect_to admin_settings_path }
+        format.json { respond_with_bip(@setting) }
+      end
     end
 
-    respond_to do |format|
-      format.html { redirect_to admin_settings_path }
-      format.json { respond_with_bip(@setting) }
-    end
-  end
-
-  private
+    private
 
-  def settings_params
-    params.require(:setting).permit(:value)
+    def settings_params
+      params.require(:setting).permit(:value)
+    end
   end
 end
diff --git a/spec/controllers/admin/reports_controller_spec.rb b/spec/controllers/admin/reports_controller_spec.rb
new file mode 100644
index 000000000..622ea87c1
--- /dev/null
+++ b/spec/controllers/admin/reports_controller_spec.rb
@@ -0,0 +1,14 @@
+require 'rails_helper'
+
+RSpec.describe Admin::ReportsController, type: :controller do
+  describe 'GET #index' do
+    before do
+      sign_in Fabricate(:user, admin: true), scope: :user
+    end
+
+    it 'returns http success' do
+      get :index
+      expect(response).to have_http_status(:success)
+    end
+  end
+end
diff --git a/spec/controllers/admin/settings_controller_spec.rb b/spec/controllers/admin/settings_controller_spec.rb
new file mode 100644
index 000000000..c126b645b
--- /dev/null
+++ b/spec/controllers/admin/settings_controller_spec.rb
@@ -0,0 +1,14 @@
+require 'rails_helper'
+
+RSpec.describe Admin::SettingsController, type: :controller do
+  describe 'GET #index' do
+    before do
+      sign_in Fabricate(:user, admin: true), scope: :user
+    end
+
+    it 'returns http success' do
+      get :index
+      expect(response).to have_http_status(:success)
+    end
+  end
+end