Add tests for `UserBan` model

This commit is contained in:
Karina Kwiatek 2022-07-02 13:18:15 +02:00 committed by Karina Kwiatek
parent b899c1aeb3
commit 400994179a
1 changed files with 64 additions and 0 deletions

View File

@ -0,0 +1,64 @@
# frozen_string_literal: true
require "rails_helper"
RSpec.describe UserBan, type: :model do
describe "validations" do
it { should belong_to(:user) }
it { should belong_to(:banned_by).class_name("User").optional }
end
describe "scopes" do
describe ".current" do
let(:user) { FactoryBot.create(:user) }
let!(:current_ban) { UserBan.create(user: user, expires_at: Time.now.utc + 1.day) }
let!(:expired_ban) { UserBan.create(user: user, expires_at: Time.now.utc - 1.day) }
it "returns only current bans" do
expect(UserBan.current).to eq([current_ban])
end
end
end
describe "#permanent?" do
let(:user) { FactoryBot.create(:user) }
let(:ban) { UserBan.create(user: user, expires_at: expires_at) }
context "ban is permanent" do
let(:expires_at) { nil }
it "returns true" do
expect(ban.permanent?).to eq(true)
end
end
context "ban is not permanent" do
let(:expires_at) { Time.now.utc + 1.day }
it "returns false" do
expect(ban.permanent?).to be false
end
end
end
describe "#current?" do
let(:user) { FactoryBot.create(:user) }
let(:ban) { UserBan.create(user: user, expires_at: expires_at) }
context "ban is current" do
let(:expires_at) { Time.now.utc + 1.day }
it "returns true" do
expect(ban.current?).to eq(true)
end
end
context "ban is not current" do
let(:expires_at) { Time.now.utc - 1.day }
it "returns false" do
expect(ban.current?).to eq(false)
end
end
end
end