docbrown/spec/models/settings/authentication_spec.rb
Jeremy Friesen 3782db891a
Adding ability to block domains from registration (#15397)
Prior to this commit, we had filtering options for email registration:

1. Specific allowed domains.
2. Implicitly allow all domains if no allowed domains specified.

With this commit, we add another option: The adminstrator may block one
or more domains from registering.

Closes forem/rfcs#281
2021-11-16 18:56:01 -05:00

71 lines
2.1 KiB
Ruby

require "rails_helper"
RSpec.describe Settings::Authentication, type: :model do
describe "#acceptable_domain?" do
subject { described_class.acceptable_domain?(domain: domain) }
let(:domain) { "hello.com" }
context "with blocked domain" do
before do
allow(described_class).to receive(:blocked_registration_email_domains).and_return([domain])
end
it { is_expected.to be_falsey }
end
context "with allowed domain" do
before do
allow(described_class).to receive(:allowed_registration_email_domains).and_return([domain])
end
it { is_expected.to be_truthy }
end
context "with no domains blocked nor explicitly allowed" do
before do
allow(described_class).to receive(:allowed_registration_email_domains).and_return([])
end
it { is_expected.to be_truthy }
end
context "with no domains blocked but an explicitly allowed domain" do
before do
allow(described_class).to receive(:allowed_registration_email_domains).and_return(["wonka.vision"])
end
it { is_expected.to be_falsey }
end
end
describe "validations" do
describe "#blocked_registration_email_domains" do
it "allows valid domain lists" do
expect do
described_class.blocked_registration_email_domains = "example.com, example2.com"
end.not_to raise_error
end
it "rejects invalid domain lists" do
expect do
described_class.blocked_registration_email_domains = "example.com, e.c"
end.to raise_error(/must be a comma-separated list of valid domains/)
end
end
describe "#allowed_registration_email_domains" do
it "allows valid domain lists" do
expect do
described_class.allowed_registration_email_domains = "example.com, example2.com"
end.not_to raise_error
end
it "rejects invalid domain lists" do
expect do
described_class.allowed_registration_email_domains = "example.com, e.c"
end.to raise_error(/must be a comma-separated list of valid domains/)
end
end
end
end