diff --git a/app/controllers/ajax/moderation_controller.rb b/app/controllers/ajax/moderation_controller.rb index b6c9d9e9..85f23296 100644 --- a/app/controllers/ajax/moderation_controller.rb +++ b/app/controllers/ajax/moderation_controller.rb @@ -118,7 +118,7 @@ class Ajax::ModerationController < AjaxController reason = params[:reason].to_s target_user = User.find_by_screen_name!(params[:user]) unban = params[:ban] == '0' - perma = params[:duration].nil? + perma = params[:duration].blank? if !unban && target_user.has_role?(:administrator) @response[:status] = :nopriv diff --git a/app/models/user.rb b/app/models/user.rb index 669e36c1..0581621a 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -224,7 +224,7 @@ class User < ApplicationRecord # endregion def banned? - self.bans.current.any? + self.bans.current.count > 0 end def unban diff --git a/lib/use_case/user/unban.rb b/lib/use_case/user/unban.rb index d399a940..81c5ca1c 100644 --- a/lib/use_case/user/unban.rb +++ b/lib/use_case/user/unban.rb @@ -9,7 +9,8 @@ module UseCase def call UserBan.current.where(user_id: target_user_id).update_all( - expires_at: DateTime.now + # -1s to account for flakyness with timings in tests + expires_at: DateTime.now - 1.second ) end end diff --git a/spec/controllers/ajax/moderation_controller_spec.rb b/spec/controllers/ajax/moderation_controller_spec.rb index 91edb9e6..7dd713b8 100644 --- a/spec/controllers/ajax/moderation_controller_spec.rb +++ b/spec/controllers/ajax/moderation_controller_spec.rb @@ -424,7 +424,7 @@ describe Ajax::ModerationController, :ajax_controller, type: :controller do before { target_user.ban(nil) } it "unbans the user" do - expect { subject }.to change { target_user.reload.banned? }.from(true).to(false) + expect { subject }.to change { target_user.reload.banned? }.from(true).to(false) end include_examples "returns the expected response" @@ -443,15 +443,14 @@ describe Ajax::ModerationController, :ajax_controller, type: :controller do context "when ban = 1" do let(:ban) { "1" } - let(:duration) { 3 } - let(:duration_unit) { 'hours' } context "when permaban = 0" do - let(:permaban) { "0" } + let(:duration) { 3 } + let(:duration_unit) { 'hours' } it "bans the user for 3 hours" do Timecop.freeze do - expect { subject }.to(change { target_user.reload.banned? }.from(false).to(true)) + expect { subject }.to change { target_user.reload.banned? }.from(false).to(true) expect(target_user.bans.current.first.reason).to eq("just a prank, bro") expect(target_user.bans.current.first.expires_at.to_i).to eq((Time.now.utc + 3.hours).to_i) end