2019-04-30 08:27:42 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-07-28 10:27:38 +08:00
|
|
|
RSpec.describe DestroyTask do
|
2018-03-31 08:32:21 +08:00
|
|
|
describe "destroy topics" do
|
2019-08-06 18:26:54 +08:00
|
|
|
fab!(:c) { Fabricate(:category_with_definition) }
|
2019-05-07 11:12:20 +08:00
|
|
|
fab!(:t) { Fabricate(:topic, category: c) }
|
2018-06-05 15:29:17 +08:00
|
|
|
let!(:p) { Fabricate(:post, topic: t) }
|
2019-08-06 18:26:54 +08:00
|
|
|
fab!(:c2) { Fabricate(:category_with_definition) }
|
2019-05-07 11:12:20 +08:00
|
|
|
fab!(:t2) { Fabricate(:topic, category: c2) }
|
2018-06-05 15:29:17 +08:00
|
|
|
let!(:p2) { Fabricate(:post, topic: t2) }
|
2019-08-06 18:26:54 +08:00
|
|
|
fab!(:sc) { Fabricate(:category_with_definition, parent_category: c2) }
|
2019-05-07 11:12:20 +08:00
|
|
|
fab!(:t3) { Fabricate(:topic, category: sc) }
|
2018-09-10 19:52:14 +08:00
|
|
|
let!(:p3) { Fabricate(:post, topic: t3) }
|
2018-03-31 08:32:21 +08:00
|
|
|
|
|
|
|
it "destroys all topics in a category" do
|
2019-07-18 02:41:13 +08:00
|
|
|
destroy_task = DestroyTask.new(StringIO.new)
|
|
|
|
expect { destroy_task.destroy_topics(c.slug) }.to change {
|
2018-09-10 19:52:14 +08:00
|
|
|
Topic.where(category_id: c.id).count
|
2023-12-07 06:25:00 +08:00
|
|
|
}.by(-1)
|
2018-09-10 19:52:14 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "destroys all topics in a sub category" do
|
2019-07-18 02:41:13 +08:00
|
|
|
destroy_task = DestroyTask.new(StringIO.new)
|
|
|
|
expect { destroy_task.destroy_topics(sc.slug, c2.slug) }.to change {
|
2018-09-10 19:52:14 +08:00
|
|
|
Topic.where(category_id: sc.id).count
|
|
|
|
}.by(-1)
|
2018-03-31 08:32:21 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't destroy system topics" do
|
2019-07-18 02:41:13 +08:00
|
|
|
destroy_task = DestroyTask.new(StringIO.new)
|
|
|
|
destroy_task.destroy_topics(c2.slug)
|
2018-03-31 08:32:21 +08:00
|
|
|
expect(Topic.where(category_id: c2.id).count).to eq 1
|
|
|
|
end
|
|
|
|
|
|
|
|
it "destroys topics in all categories" do
|
2019-07-18 02:41:13 +08:00
|
|
|
destroy_task = DestroyTask.new(StringIO.new)
|
|
|
|
destroy_task.destroy_topics_all_categories
|
2018-09-10 19:52:14 +08:00
|
|
|
expect(Post.where(topic_id: [t.id, t2.id, t3.id]).count).to eq 0
|
2018-03-31 08:32:21 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-07-18 02:41:13 +08:00
|
|
|
describe "destroy categories" do
|
2019-08-06 18:26:54 +08:00
|
|
|
fab!(:c) { Fabricate(:category_with_definition) }
|
2019-07-18 02:41:13 +08:00
|
|
|
fab!(:t) { Fabricate(:topic, category: c) }
|
|
|
|
let!(:p) { Fabricate(:post, topic: t) }
|
2019-08-06 18:26:54 +08:00
|
|
|
fab!(:c2) { Fabricate(:category_with_definition) }
|
2019-07-18 02:41:13 +08:00
|
|
|
fab!(:t2) { Fabricate(:topic, category: c) }
|
|
|
|
let!(:p2) { Fabricate(:post, topic: t2) }
|
2019-08-06 18:26:54 +08:00
|
|
|
fab!(:sc) { Fabricate(:category_with_definition, parent_category: c2) }
|
2019-07-18 02:41:13 +08:00
|
|
|
fab!(:t3) { Fabricate(:topic, category: sc) }
|
|
|
|
let!(:p3) { Fabricate(:post, topic: t3) }
|
|
|
|
|
|
|
|
it "destroys specified category" do
|
|
|
|
destroy_task = DestroyTask.new(StringIO.new)
|
|
|
|
|
|
|
|
expect { destroy_task.destroy_category(c.id) }.to change {
|
|
|
|
Category.where(id: c.id).count
|
2023-12-07 06:25:00 +08:00
|
|
|
}.by(-1)
|
2019-07-18 02:41:13 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "destroys sub-categories when destroying parent category" do
|
|
|
|
destroy_task = DestroyTask.new(StringIO.new)
|
|
|
|
|
|
|
|
expect { destroy_task.destroy_category(c2.id) }.to change {
|
|
|
|
Category.where(id: sc.id).count
|
2023-12-07 06:25:00 +08:00
|
|
|
}.by(-1)
|
2019-07-18 02:41:13 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-03-31 08:32:21 +08:00
|
|
|
describe "private messages" do
|
|
|
|
let!(:pm) { Fabricate(:private_message_post) }
|
|
|
|
let!(:pm2) { Fabricate(:private_message_post) }
|
|
|
|
|
|
|
|
it "destroys all private messages" do
|
2019-07-18 02:41:13 +08:00
|
|
|
destroy_task = DestroyTask.new(StringIO.new)
|
|
|
|
destroy_task.destroy_private_messages
|
2018-03-31 08:32:21 +08:00
|
|
|
expect(Topic.where(archetype: "private_message").count).to eq 0
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "groups" do
|
|
|
|
let!(:g) { Fabricate(:group) }
|
|
|
|
let!(:g2) { Fabricate(:group) }
|
|
|
|
|
|
|
|
it "destroys all groups" do
|
2019-07-18 02:41:13 +08:00
|
|
|
destroy_task = DestroyTask.new(StringIO.new)
|
|
|
|
destroy_task.destroy_groups
|
2018-03-31 08:32:21 +08:00
|
|
|
expect(Group.where(automatic: false).count).to eq 0
|
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't destroy default groups" do
|
2019-07-18 02:41:13 +08:00
|
|
|
destroy_task = DestroyTask.new(StringIO.new)
|
2018-03-31 08:32:21 +08:00
|
|
|
before_count = Group.count
|
2019-07-18 02:41:13 +08:00
|
|
|
destroy_task.destroy_groups
|
2018-03-31 08:32:21 +08:00
|
|
|
expect(Group.count).to eq before_count - 2
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "users" do
|
|
|
|
it "destroys all non-admin users" do
|
2018-04-11 13:45:27 +08:00
|
|
|
before_count = User.count
|
|
|
|
|
|
|
|
Fabricate(:user)
|
|
|
|
Fabricate(:user)
|
|
|
|
Fabricate(:admin)
|
|
|
|
|
2019-07-18 02:41:13 +08:00
|
|
|
destroy_task = DestroyTask.new(StringIO.new)
|
|
|
|
destroy_task.destroy_users
|
2018-03-31 08:32:21 +08:00
|
|
|
expect(User.where(admin: false).count).to eq 0
|
2021-05-21 09:43:47 +08:00
|
|
|
# admin does not get destroyed
|
2018-04-11 13:45:27 +08:00
|
|
|
expect(User.count).to eq before_count + 1
|
2018-03-31 08:32:21 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "stats" do
|
|
|
|
it "destroys all site stats" do
|
2019-07-18 02:41:13 +08:00
|
|
|
destroy_task = DestroyTask.new(StringIO.new)
|
|
|
|
destroy_task.destroy_stats
|
2018-03-31 08:32:21 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|