The belongs_to_creator macro was used to initialize the creator_id field to the CurrentUser. This made tests complicated because it meant you had to create and set the current user every time you wanted to create an object, when lead to the current user being set over and over again. It also meant you had to constantly be aware of what the CurrentUser was in many different contexts, which was often confusing. Setting creators explicitly simplifies everything greatly.
35 lines
1.0 KiB
Ruby
35 lines
1.0 KiB
Ruby
require 'test_helper'
|
|
|
|
class IpBanTest < ActiveSupport::TestCase
|
|
should "be able to ban a user" do
|
|
ip_ban = create(:ip_ban, ip_addr: "1.2.3.4")
|
|
|
|
assert_equal("1.2.3.4", ip_ban.subnetted_ip)
|
|
assert(IpBan.is_banned?("1.2.3.4"))
|
|
end
|
|
|
|
should "be able to ban a subnet" do
|
|
ip_ban = create(:ip_ban, ip_addr: "1.2.3.4/24")
|
|
|
|
assert_equal("1.2.3.0/24", ip_ban.subnetted_ip)
|
|
assert(IpBan.is_banned?("1.2.3.0"))
|
|
assert(IpBan.is_banned?("1.2.3.255"))
|
|
end
|
|
|
|
context "validation" do
|
|
subject { build(:ip_ban) }
|
|
|
|
should allow_value("1.2.3.4").for(:ip_addr)
|
|
should allow_value("1.2.3.4/24").for(:ip_addr)
|
|
should allow_value("ABCD::1234").for(:ip_addr)
|
|
should allow_value("ABCD::1234/64").for(:ip_addr)
|
|
|
|
should_not allow_value("").for(:ip_addr)
|
|
should_not allow_value("foo").for(:ip_addr)
|
|
should_not allow_value("10.0.0.1").for(:ip_addr)
|
|
should_not allow_value("127.0.0.1").for(:ip_addr)
|
|
should_not allow_value("1.2.3.4/16").for(:ip_addr)
|
|
should_not allow_value("ABCD::1234/32").for(:ip_addr)
|
|
end
|
|
end
|