From 400994179a528d23ffa05c68030be1e342d30e51 Mon Sep 17 00:00:00 2001 From: Karina Kwiatek Date: Sat, 2 Jul 2022 13:18:15 +0200 Subject: [PATCH] Add tests for `UserBan` model --- spec/models/user_ban_spec.rb | 64 ++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 spec/models/user_ban_spec.rb diff --git a/spec/models/user_ban_spec.rb b/spec/models/user_ban_spec.rb new file mode 100644 index 00000000..71fa153e --- /dev/null +++ b/spec/models/user_ban_spec.rb @@ -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