2023-03-24 04:38:42 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require "rails_helper"
|
|
|
|
|
|
|
|
describe DiscourseDataExplorer::ReportGenerator do
|
2024-02-29 12:15:57 -05:00
|
|
|
fab!(:user)
|
2023-03-24 04:38:42 -04:00
|
|
|
fab!(:unauthorised_user) { Fabricate(:user) }
|
|
|
|
fab!(:unauthorised_group) { Fabricate(:group) }
|
|
|
|
fab!(:group) { Fabricate(:group, users: [user]) }
|
|
|
|
|
|
|
|
fab!(:query) { DiscourseDataExplorer::Query.find(-1) }
|
|
|
|
|
|
|
|
let(:query_params) { [%w[from_days_ago 0], %w[duration_days 15]] }
|
|
|
|
|
2024-10-10 02:09:09 -04:00
|
|
|
before do
|
|
|
|
SiteSetting.data_explorer_enabled = true
|
|
|
|
SiteSetting.authorized_extensions = "csv"
|
|
|
|
end
|
2023-03-24 04:38:42 -04:00
|
|
|
|
|
|
|
describe ".generate" do
|
|
|
|
it "returns [] if the recipient is not in query group" do
|
2024-01-03 01:05:03 -05:00
|
|
|
Fabricate(:query_group, query: query, group: group)
|
2023-03-24 04:38:42 -04:00
|
|
|
result =
|
2024-03-27 05:40:26 -04:00
|
|
|
described_class.generate(
|
2023-03-24 04:38:42 -04:00
|
|
|
query.id,
|
|
|
|
query_params,
|
|
|
|
[unauthorised_user.username, unauthorised_group.name],
|
|
|
|
)
|
|
|
|
|
|
|
|
expect(result).to eq []
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns a list of pms for authorised users" do
|
|
|
|
SiteSetting.personal_message_enabled_groups = group.id
|
|
|
|
DiscourseDataExplorer::ResultToMarkdown.expects(:convert).returns("le table")
|
|
|
|
freeze_time
|
|
|
|
|
2024-03-27 05:40:26 -04:00
|
|
|
result = described_class.generate(query.id, query_params, [user.username])
|
2023-03-24 04:38:42 -04:00
|
|
|
|
|
|
|
expect(result).to eq(
|
|
|
|
[
|
|
|
|
{
|
2024-10-10 02:09:09 -04:00
|
|
|
"title" =>
|
|
|
|
I18n.t(
|
|
|
|
"data_explorer.report_generator.private_message.title",
|
|
|
|
query_name: query.name,
|
|
|
|
),
|
2023-03-24 04:38:42 -04:00
|
|
|
"target_usernames" => [user.username],
|
|
|
|
"raw" =>
|
2024-10-10 02:09:09 -04:00
|
|
|
I18n.t(
|
|
|
|
"data_explorer.report_generator.private_message.body",
|
|
|
|
recipient_name: user.username,
|
|
|
|
query_name: query.name,
|
|
|
|
table: "le table",
|
|
|
|
base_url: Discourse.base_url,
|
|
|
|
query_id: query.id,
|
|
|
|
created_at: Time.zone.now.strftime("%Y-%m-%d at %H:%M:%S"),
|
|
|
|
timezone: Time.zone.name,
|
|
|
|
),
|
2023-03-24 04:38:42 -04:00
|
|
|
},
|
|
|
|
],
|
|
|
|
)
|
|
|
|
end
|
2024-01-03 01:05:03 -05:00
|
|
|
|
|
|
|
it "still returns a list of pms if a group or user does not exist" do
|
2024-03-27 05:40:26 -04:00
|
|
|
Fabricate(:query_group, query: query, group: group)
|
|
|
|
|
2024-01-03 01:05:03 -05:00
|
|
|
SiteSetting.personal_message_enabled_groups = group.id
|
|
|
|
DiscourseDataExplorer::ResultToMarkdown.expects(:convert).returns("le table")
|
|
|
|
freeze_time
|
|
|
|
|
2024-03-27 05:40:26 -04:00
|
|
|
result = described_class.generate(query.id, query_params, [group.name, "non-existent-group"])
|
|
|
|
expect(result).to eq(
|
|
|
|
[
|
|
|
|
{
|
2024-10-10 02:09:09 -04:00
|
|
|
"title" =>
|
|
|
|
I18n.t(
|
|
|
|
"data_explorer.report_generator.private_message.title",
|
|
|
|
query_name: query.name,
|
|
|
|
),
|
2024-03-27 05:40:26 -04:00
|
|
|
"target_group_names" => [group.name],
|
|
|
|
"raw" =>
|
2024-10-10 02:09:09 -04:00
|
|
|
I18n.t(
|
|
|
|
"data_explorer.report_generator.private_message.body",
|
|
|
|
recipient_name: group.name,
|
|
|
|
query_name: query.name,
|
|
|
|
table: "le table",
|
|
|
|
base_url: Discourse.base_url,
|
|
|
|
query_id: query.id,
|
|
|
|
created_at: Time.zone.now.strftime("%Y-%m-%d at %H:%M:%S"),
|
|
|
|
timezone: Time.zone.name,
|
|
|
|
),
|
2024-03-27 05:40:26 -04:00
|
|
|
},
|
|
|
|
],
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "works with email recipients" do
|
|
|
|
DiscourseDataExplorer::ResultToMarkdown.expects(:convert).returns("le table")
|
2024-09-17 21:09:07 -04:00
|
|
|
freeze_time
|
2024-03-27 05:40:26 -04:00
|
|
|
|
|
|
|
email = "john@doe.com"
|
|
|
|
result = described_class.generate(query.id, query_params, [email])
|
2024-01-03 01:05:03 -05:00
|
|
|
|
|
|
|
expect(result).to eq(
|
|
|
|
[
|
|
|
|
{
|
2024-10-10 02:09:09 -04:00
|
|
|
"title" =>
|
|
|
|
I18n.t(
|
|
|
|
"data_explorer.report_generator.private_message.title",
|
|
|
|
query_name: query.name,
|
|
|
|
),
|
2024-03-27 05:40:26 -04:00
|
|
|
"target_emails" => [email],
|
2024-01-03 01:05:03 -05:00
|
|
|
"raw" =>
|
2024-10-10 02:09:09 -04:00
|
|
|
I18n.t(
|
|
|
|
"data_explorer.report_generator.private_message.body",
|
|
|
|
recipient_name: email,
|
|
|
|
query_name: query.name,
|
|
|
|
table: "le table",
|
|
|
|
base_url: Discourse.base_url,
|
|
|
|
query_id: query.id,
|
|
|
|
created_at: Time.zone.now.strftime("%Y-%m-%d at %H:%M:%S"),
|
|
|
|
timezone: Time.zone.name,
|
|
|
|
),
|
2024-01-03 01:05:03 -05:00
|
|
|
},
|
|
|
|
],
|
|
|
|
)
|
|
|
|
end
|
2024-03-27 05:40:26 -04:00
|
|
|
|
|
|
|
it "works with duplicate recipients" do
|
2024-10-10 02:09:09 -04:00
|
|
|
DiscourseDataExplorer::ResultToMarkdown.expects(:convert).returns("le table")
|
2024-09-17 21:09:07 -04:00
|
|
|
freeze_time
|
2024-03-27 05:40:26 -04:00
|
|
|
|
|
|
|
result = described_class.generate(query.id, query_params, [user.username, user.username])
|
|
|
|
|
|
|
|
expect(result).to eq(
|
|
|
|
[
|
|
|
|
{
|
2024-10-10 02:09:09 -04:00
|
|
|
"title" =>
|
|
|
|
I18n.t(
|
|
|
|
"data_explorer.report_generator.private_message.title",
|
|
|
|
query_name: query.name,
|
|
|
|
),
|
2024-03-27 05:40:26 -04:00
|
|
|
"target_usernames" => [user.username],
|
|
|
|
"raw" =>
|
2024-10-10 02:09:09 -04:00
|
|
|
I18n.t(
|
|
|
|
"data_explorer.report_generator.private_message.body",
|
|
|
|
recipient_name: user.username,
|
|
|
|
query_name: query.name,
|
|
|
|
table: "le table",
|
|
|
|
base_url: Discourse.base_url,
|
|
|
|
query_id: query.id,
|
|
|
|
created_at: Time.zone.now.strftime("%Y-%m-%d at %H:%M:%S"),
|
|
|
|
timezone: Time.zone.name,
|
|
|
|
),
|
2024-03-27 05:40:26 -04:00
|
|
|
},
|
|
|
|
],
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "works with multiple recipient types" do
|
|
|
|
Fabricate(:query_group, query: query, group: group)
|
2024-10-10 02:09:09 -04:00
|
|
|
DiscourseDataExplorer::ResultToMarkdown.expects(:convert).returns("le table")
|
2024-03-27 05:40:26 -04:00
|
|
|
|
|
|
|
result =
|
|
|
|
described_class.generate(
|
|
|
|
query.id,
|
|
|
|
query_params,
|
|
|
|
[group.name, user.username, "john@doe.com"],
|
|
|
|
)
|
|
|
|
|
|
|
|
expect(result.length).to eq(3)
|
|
|
|
expect(result[0]["target_usernames"]).to eq([user.username])
|
|
|
|
expect(result[1]["target_group_names"]).to eq([group.name])
|
|
|
|
expect(result[2]["target_emails"]).to eq(["john@doe.com"])
|
|
|
|
end
|
2024-08-26 23:41:12 -04:00
|
|
|
|
|
|
|
it "works with attached csv file" do
|
|
|
|
SiteSetting.personal_message_enabled_groups = group.id
|
|
|
|
DiscourseDataExplorer::ResultToMarkdown.expects(:convert).returns("le table")
|
|
|
|
freeze_time
|
|
|
|
|
|
|
|
result =
|
|
|
|
described_class.generate(query.id, query_params, [user.username], { attach_csv: true })
|
|
|
|
|
|
|
|
filename =
|
|
|
|
"#{query.slug}@#{Slug.for(Discourse.current_hostname, "discourse")}-#{Date.today}.dcqresult.csv"
|
|
|
|
|
2024-10-10 02:09:09 -04:00
|
|
|
expect(result[0]["raw"]).to eq(
|
|
|
|
I18n.t(
|
|
|
|
"data_explorer.report_generator.private_message.body",
|
|
|
|
recipient_name: user.username,
|
|
|
|
query_name: query.name,
|
|
|
|
table: "le table",
|
|
|
|
base_url: Discourse.base_url,
|
|
|
|
query_id: query.id,
|
|
|
|
created_at: Time.zone.now.strftime("%Y-%m-%d at %H:%M:%S"),
|
|
|
|
timezone: Time.zone.name,
|
|
|
|
) + "\n\n" +
|
|
|
|
I18n.t(
|
|
|
|
"data_explorer.report_generator.upload_appendix",
|
|
|
|
filename: filename,
|
|
|
|
short_url: Upload.find_by(original_filename: filename).short_url,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe ".generate_post" do
|
|
|
|
it "works without attached csv file" do
|
|
|
|
DiscourseDataExplorer::ResultToMarkdown.expects(:convert).returns("le table")
|
|
|
|
freeze_time
|
|
|
|
|
|
|
|
result = described_class.generate_post(query.id, query_params)
|
|
|
|
|
|
|
|
expect(result["raw"]).to eq(
|
|
|
|
I18n.t(
|
|
|
|
"data_explorer.report_generator.post.body",
|
|
|
|
query_name: query.name,
|
|
|
|
table: "le table",
|
|
|
|
base_url: Discourse.base_url,
|
|
|
|
query_id: query.id,
|
|
|
|
created_at: Time.zone.now.strftime("%Y-%m-%d at %H:%M:%S"),
|
|
|
|
timezone: Time.zone.name,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "works with attached csv file" do
|
|
|
|
DiscourseDataExplorer::ResultToMarkdown.expects(:convert).returns("le table")
|
|
|
|
freeze_time
|
|
|
|
|
|
|
|
result = described_class.generate_post(query.id, query_params, { attach_csv: true })
|
|
|
|
|
|
|
|
filename =
|
|
|
|
"#{query.slug}@#{Slug.for(Discourse.current_hostname, "discourse")}-#{Date.today}.dcqresult.csv"
|
|
|
|
|
|
|
|
expect(result["raw"]).to eq(
|
|
|
|
I18n.t(
|
|
|
|
"data_explorer.report_generator.post.body",
|
|
|
|
query_name: query.name,
|
|
|
|
table: "le table",
|
|
|
|
base_url: Discourse.base_url,
|
|
|
|
query_id: query.id,
|
|
|
|
created_at: Time.zone.now.strftime("%Y-%m-%d at %H:%M:%S"),
|
|
|
|
timezone: Time.zone.name,
|
|
|
|
) + "\n\n" +
|
|
|
|
I18n.t(
|
|
|
|
"data_explorer.report_generator.upload_appendix",
|
|
|
|
filename: filename,
|
|
|
|
short_url: Upload.find_by(original_filename: filename).short_url,
|
|
|
|
),
|
2024-08-26 23:41:12 -04:00
|
|
|
)
|
|
|
|
end
|
2023-03-24 04:38:42 -04:00
|
|
|
end
|
|
|
|
end
|