|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +#-- copyright |
| 4 | +# OpenProject is an open source project management software. |
| 5 | +# Copyright (C) the OpenProject GmbH |
| 6 | +# |
| 7 | +# This program is free software; you can redistribute it and/or |
| 8 | +# modify it under the terms of the GNU General Public License version 3. |
| 9 | +# |
| 10 | +# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows: |
| 11 | +# Copyright (C) 2006-2013 Jean-Philippe Lang |
| 12 | +# Copyright (C) 2010-2013 the ChiliProject Team |
| 13 | +# |
| 14 | +# This program is free software; you can redistribute it and/or |
| 15 | +# modify it under the terms of the GNU General Public License |
| 16 | +# as published by the Free Software Foundation; either version 2 |
| 17 | +# of the License, or (at your option) any later version. |
| 18 | +# |
| 19 | +# This program is distributed in the hope that it will be useful, |
| 20 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 22 | +# GNU General Public License for more details. |
| 23 | +# |
| 24 | +# You should have received a copy of the GNU General Public License |
| 25 | +# along with this program; if not, write to the Free Software |
| 26 | +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 27 | +# |
| 28 | +# See COPYRIGHT and LICENSE files for more details. |
| 29 | +#++ |
| 30 | + |
| 31 | +require "spec_helper" |
| 32 | + |
| 33 | +RSpec.describe OpenProject::BlockedEmailDomains do |
| 34 | + describe ".domain_of" do |
| 35 | + it "returns the lower cased domain" do |
| 36 | + expect(described_class.domain_of("Spam@Example.COM")).to eq "example.com" |
| 37 | + end |
| 38 | + |
| 39 | + it "returns nil for values that are not addresses" do |
| 40 | + expect(described_class.domain_of("example.com")).to be_nil |
| 41 | + expect(described_class.domain_of("@example.com")).to be_nil |
| 42 | + expect(described_class.domain_of(nil)).to be_nil |
| 43 | + expect(described_class.domain_of("")).to be_nil |
| 44 | + end |
| 45 | + end |
| 46 | + |
| 47 | + describe ".domains" do |
| 48 | + context "without configuration" do |
| 49 | + it "is empty" do |
| 50 | + expect(described_class.domains).to eq [] |
| 51 | + end |
| 52 | + end |
| 53 | + |
| 54 | + context "with a list", with_settings: { blocked_email_domains: [" Blocked.COM ", "@other.com", ".third.com", ""] } do |
| 55 | + it "normalizes the entries" do |
| 56 | + expect(described_class.domains).to eq %w[blocked.com other.com third.com] |
| 57 | + end |
| 58 | + end |
| 59 | + |
| 60 | + context "with a separated string", with_settings: { blocked_email_domains: "blocked.com, other.com third.com" } do |
| 61 | + it "splits it" do |
| 62 | + expect(described_class.domains).to eq %w[blocked.com other.com third.com] |
| 63 | + end |
| 64 | + end |
| 65 | + end |
| 66 | + |
| 67 | + describe ".blocked?", with_settings: { blocked_email_domains: ["blocked.com"] } do |
| 68 | + it "blocks the configured domain" do |
| 69 | + expect(described_class).to be_blocked("user@blocked.com") |
| 70 | + end |
| 71 | + |
| 72 | + it "blocks it regardless of case" do |
| 73 | + expect(described_class).to be_blocked("User@Blocked.COM") |
| 74 | + end |
| 75 | + |
| 76 | + it "blocks subdomains of it" do |
| 77 | + expect(described_class).to be_blocked("user@mail.blocked.com") |
| 78 | + end |
| 79 | + |
| 80 | + it "does not block a domain that merely ends in the same characters" do |
| 81 | + expect(described_class).not_to be_blocked("user@notblocked.com") |
| 82 | + end |
| 83 | + |
| 84 | + it "does not block other domains" do |
| 85 | + expect(described_class).not_to be_blocked("user@example.com") |
| 86 | + end |
| 87 | + |
| 88 | + it "does not block blank values" do |
| 89 | + expect(described_class).not_to be_blocked(nil) |
| 90 | + expect(described_class).not_to be_blocked("") |
| 91 | + end |
| 92 | + end |
| 93 | + |
| 94 | + describe ".blocked? without configuration" do |
| 95 | + it "blocks nothing" do |
| 96 | + expect(described_class).not_to be_blocked("user@blocked.com") |
| 97 | + end |
| 98 | + end |
| 99 | +end |
0 commit comments