Update tests for unbanning users

This commit is contained in:
Karina Kwiatek 2021-12-30 21:06:21 +01:00
parent a9392dad53
commit 7677ed21a4
4 changed files with 8 additions and 8 deletions

View file

@ -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

View file

@ -224,7 +224,7 @@ class User < ApplicationRecord
# endregion
def banned?
self.bans.current.any?
self.bans.current.count > 0
end
def unban

View file

@ -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

View file

@ -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