DEV: find_each in CSV exports (#22573)

So we have to order by calling `find_each(order: :desc)`.
Note that that will order rows by Id, not by `last_match_at`
as we tried before (though that didn't work).
This commit is contained in:
Andrei Prigorshnev 2023-08-17 06:33:52 +04:00 committed by GitHub
parent 20840c341f
commit f4e424d7d4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 60 additions and 49 deletions

View File

@ -168,17 +168,17 @@ module Jobs
def screened_email_export
return enum_for(:screened_email_export) unless block_given?
ScreenedEmail
.order("last_match_at DESC")
.find_each { |screened_email| yield get_screened_email_fields(screened_email) }
ScreenedEmail.find_each(order: :desc) do |screened_email|
yield get_screened_email_fields(screened_email)
end
end
def screened_ip_export
return enum_for(:screened_ip_export) unless block_given?
ScreenedIpAddress
.order("id DESC")
.each { |screened_ip| yield get_screened_ip_fields(screened_ip) }
ScreenedIpAddress.find_each(order: :desc) do |screened_ip|
yield get_screened_ip_fields(screened_ip)
end
end
def screened_url_export

View File

@ -3,4 +3,8 @@
Fabricator(:screened_email) do
email { sequence(:email) { |n| "bad#{n}@spammers.org" } }
action_type ScreenedEmail.actions[:block]
match_count { sequence(:match_count) { |n| n } }
last_match_at { sequence(:last_match_at) { |n| Time.now + n.days } }
created_at { sequence(:created_at) { |n| Time.now + n.days } }
ip_address { sequence(:ip_address) { |i| "99.232.23.#{i % 254}" } }
end

View File

@ -1,5 +1,9 @@
# frozen_string_literal: true
Fabricator(:screened_ip_address) do
ip_address { sequence(:ip_address) { |n| "123.#{(n * 3) % 255}.#{(n * 2) % 255}.#{n % 255}" } }
action_type ScreenedIpAddress.actions[:block]
ip_address { sequence(:ip_address) { |i| "99.232.23.#{i % 254}" } }
match_count { sequence(:match_count) { |n| n } }
last_match_at { sequence(:last_match_at) { |n| Time.now + n.days } }
created_at { sequence(:created_at) { |n| Time.now + n.days } }
end

View File

@ -217,6 +217,7 @@ RSpec.describe ScreenedIpAddress do
:screened_ip_address,
ip_address: "111.234.23.11",
action_type: described_class.actions[:block],
match_count: 0,
)
expect(described_class.should_block?("222.12.12.12")).to eq(false)
expect(screened_ip_address.reload.match_count).to eq(0)
@ -257,6 +258,7 @@ RSpec.describe ScreenedIpAddress do
:screened_ip_address,
ip_address: "111.234.23.11",
action_type: described_class.actions[:do_nothing],
match_count: 0,
)
expect(described_class.should_block?("111.234.23.11")).to eq(false)
expect(screened_ip_address.reload.match_count).to eq(0)
@ -268,6 +270,7 @@ RSpec.describe ScreenedIpAddress do
:screened_ip_address,
ip_address: "111.234.23.11",
action_type: described_class.actions[:block],
match_count: 0,
)
expect(described_class.should_block?("111.234.23.11")).to eq(true)
expect(screened_ip_address.reload.match_count).to eq(1)
@ -281,6 +284,7 @@ RSpec.describe ScreenedIpAddress do
:screened_ip_address,
ip_address: "2001:db8::ff00:42:8329",
action_type: described_class.actions[:do_nothing],
match_count: 0,
)
expect(described_class.should_block?("2001:db8::ff00:42:8329")).to eq(false)
expect(screened_ip_address.reload.match_count).to eq(0)
@ -292,6 +296,7 @@ RSpec.describe ScreenedIpAddress do
:screened_ip_address,
ip_address: "2001:db8::ff00:42:8329",
action_type: described_class.actions[:block],
match_count: 0,
)
expect(described_class.should_block?("2001:db8::ff00:42:8329")).to eq(true)
expect(screened_ip_address.reload.match_count).to eq(1)
@ -310,6 +315,7 @@ RSpec.describe ScreenedIpAddress do
:screened_ip_address,
ip_address: "111.234.23.11",
action_type: described_class.actions[:do_nothing],
match_count: 0,
)
expect(described_class.is_allowed?("222.12.12.12")).to eq(false)
expect(screened_ip_address.reload.match_count).to eq(0)
@ -322,6 +328,7 @@ RSpec.describe ScreenedIpAddress do
:screened_ip_address,
ip_address: "111.234.23.11",
action_type: described_class.actions[:do_nothing],
match_count: 0,
)
expect(described_class.is_allowed?("111.234.23.11")).to eq(true)
expect(screened_ip_address.reload.match_count).to eq(1)
@ -333,6 +340,7 @@ RSpec.describe ScreenedIpAddress do
:screened_ip_address,
ip_address: "111.234.23.11",
action_type: described_class.actions[:block],
match_count: 0,
)
expect(described_class.is_allowed?("111.234.23.11")).to eq(false)
expect(screened_ip_address.reload.match_count).to eq(0)
@ -346,6 +354,7 @@ RSpec.describe ScreenedIpAddress do
:screened_ip_address,
ip_address: "2001:db8::ff00:42:8329",
action_type: described_class.actions[:do_nothing],
match_count: 0,
)
expect(described_class.is_allowed?("2001:db8::ff00:42:8329")).to eq(true)
expect(screened_ip_address.reload.match_count).to eq(1)
@ -357,6 +366,7 @@ RSpec.describe ScreenedIpAddress do
:screened_ip_address,
ip_address: "2001:db8::ff00:42:8329",
action_type: described_class.actions[:block],
match_count: 0,
)
expect(described_class.is_allowed?("2001:db8::ff00:42:8329")).to eq(false)
expect(screened_ip_address.reload.match_count).to eq(0)

View File

@ -3,8 +3,7 @@
RSpec.describe "CSV Exports", type: :system do
fab!(:admin) { Fabricate(:admin) }
let(:csv_export_pm_page) { PageObjects::Pages::CSVExportPM.new }
time_format = "%Y-%m-%d %H:%M:%S UTC"
let(:time_format) { "%Y-%m-%d %H:%M:%S UTC" }
before do
Jobs.run_immediately!
@ -200,16 +199,8 @@ RSpec.describe "CSV Exports", type: :system do
end
context "with screened emails" do
fab!(:screened_email) do
Fabricate(
:screened_email,
action_type: ScreenedEmail.actions[:do_nothing],
match_count: 5,
last_match_at: Time.now,
created_at: Time.now,
ip_address: "94.99.101.228",
)
end
fab!(:screened_email_1) { Fabricate(:screened_email) }
fab!(:screened_email_2) { Fabricate(:screened_email) }
xit "exports data" do
visit "admin/logs/screened_emails"
@ -220,36 +211,33 @@ RSpec.describe "CSV Exports", type: :system do
expect(csv_export_pm_page).to have_download_link
exported_data = csv_export_pm_page.download_and_extract
expect(exported_data.length).to be(2)
expect(exported_data.length).to be(3)
expect(exported_data.first).to eq(
%w[email action match_count last_match_at created_at ip_address],
)
expect(exported_data.second).to eq(
[
screened_email.email,
"do_nothing",
screened_email.match_count.to_s,
screened_email.last_match_at.strftime(time_format),
screened_email.created_at.strftime(time_format),
screened_email.ip_address.to_s,
],
)
assert_export(exported_data.second, screened_email_2)
assert_export(exported_data.third, screened_email_1)
ensure
csv_export_pm_page.clear_downloads
end
def assert_export(exported_email, email)
expect(exported_email).to eq(
[
email.email,
ScreenedEmail.actions[email.action_type].to_s,
email.match_count.to_s,
email.last_match_at.strftime(time_format),
email.created_at.strftime(time_format),
email.ip_address.to_s,
],
)
end
end
context "with screened ips" do
fab!(:screened_ip) do
Fabricate(
:screened_ip_address,
action_type: ScreenedIpAddress.actions[:do_nothing],
match_count: 5,
ip_address: "99.232.23.124",
last_match_at: Time.now,
created_at: Time.now,
)
end
fab!(:screened_ip_1) { Fabricate(:screened_ip_address) }
fab!(:screened_ip_2) { Fabricate(:screened_ip_address) }
xit "exports data" do
visit "admin/logs/screened_ip_addresses"
@ -261,18 +249,23 @@ RSpec.describe "CSV Exports", type: :system do
exported_data = csv_export_pm_page.download_and_extract
expect(exported_data.first).to eq(%w[ip_address action match_count last_match_at created_at])
expect(exported_data.second).to eq(
[
screened_ip.ip_address.to_s,
"do_nothing",
screened_ip.match_count.to_s,
screened_ip.last_match_at.strftime(time_format),
screened_ip.created_at.strftime(time_format),
],
)
assert_exported_row(exported_data.second, screened_ip_2)
assert_exported_row(exported_data.third, screened_ip_1)
ensure
csv_export_pm_page.clear_downloads
end
def assert_exported_row(exported_ip, ip)
expect(exported_ip).to eq(
[
ip.ip_address.to_s,
ScreenedIpAddress.actions[ip.action_type].to_s,
ip.match_count.to_s,
ip.last_match_at.strftime(time_format),
ip.created_at.strftime(time_format),
],
)
end
end
context "with screened urls" do