2024-02-25 12:15:55 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require "test_helper"
|
2019-02-09 17:00:37 -05:00
|
|
|
|
|
|
|
class EmailBlacklistTest < ActiveSupport::TestCase
|
2019-02-09 21:42:43 -05:00
|
|
|
setup do
|
2022-11-25 15:06:54 -05:00
|
|
|
@user = create(:user)
|
|
|
|
CurrentUser.user = create(:mod_user)
|
2019-02-09 21:42:43 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
should "detect email by suffix" do
|
|
|
|
block = EmailBlacklist.create(creator: @user, domain: '.xyz', reason: 'test')
|
|
|
|
|
|
|
|
assert(EmailBlacklist.is_banned?('spam@what.xyz'))
|
2019-02-09 21:57:35 -05:00
|
|
|
assert_equal(false, EmailBlacklist.is_banned?('good@angelic.com'))
|
|
|
|
end
|
|
|
|
|
|
|
|
should "detect email by mx" do
|
|
|
|
block = EmailBlacklist.create(creator: @user, domain: 'google.com', reason: 'test')
|
2022-05-26 16:49:43 -04:00
|
|
|
EmailBlacklist.stubs(:get_mx_records).returns(['google.com'])
|
2019-02-09 21:57:35 -05:00
|
|
|
assert(EmailBlacklist.is_banned?('spam@e621.net'))
|
2022-05-26 16:49:43 -04:00
|
|
|
|
|
|
|
EmailBlacklist.unstub(:get_mx_records)
|
2019-02-09 21:57:35 -05:00
|
|
|
assert_equal(false, EmailBlacklist.is_banned?('what@me.xynzs'))
|
2019-02-09 21:42:43 -05:00
|
|
|
end
|
2022-05-22 09:21:41 -04:00
|
|
|
|
|
|
|
should "keep accounts verified if there are too many matches" do
|
2023-09-07 11:04:34 -04:00
|
|
|
stub_const(EmailBlacklist, :UNVERIFY_COUNT_TRESHOLD, 5) do
|
|
|
|
users = create_list(:user, EmailBlacklist::UNVERIFY_COUNT_TRESHOLD + 1) do |user, i|
|
|
|
|
user.email = "#{i}@domain.com"
|
|
|
|
end
|
|
|
|
EmailBlacklist.create(creator: @user, domain: "domain.com", reason: "test")
|
|
|
|
users.each(&:reload)
|
|
|
|
assert users.all?(&:is_verified?)
|
2022-05-22 09:21:41 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
should "unverify accounts if there are few matches" do
|
|
|
|
@domain_blocked_user = create(:user, email: "0@domain.com")
|
2022-05-23 02:10:54 -04:00
|
|
|
@other_user1 = create(:user, email: "0@prefix.domain.com")
|
|
|
|
@other_user2 = create(:user, email: "0@somethingelse.xynzs")
|
2022-05-22 09:21:41 -04:00
|
|
|
EmailBlacklist.create(creator: @user, domain: "domain.com", reason: "test")
|
|
|
|
@domain_blocked_user.reload
|
2022-05-23 02:10:54 -04:00
|
|
|
@other_user1.reload
|
|
|
|
@other_user2.reload
|
2022-05-22 09:21:41 -04:00
|
|
|
assert_not @domain_blocked_user.is_verified?
|
2022-05-23 02:10:54 -04:00
|
|
|
assert @other_user1.is_verified?
|
|
|
|
assert @other_user2.is_verified?
|
2022-05-22 09:21:41 -04:00
|
|
|
end
|
2019-02-09 17:00:37 -05:00
|
|
|
end
|