From 8ea681330046b6d99d024258b79f5fb245e66e5f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Guitaut?= Date: Wed, 15 Nov 2023 16:36:57 +0100 Subject: [PATCH] WIP: refactor specs --- lib/random_assign_utils.rb | 5 - spec/lib/random_assign_utils_spec.rb | 339 +++++++++++---------------- 2 files changed, 142 insertions(+), 202 deletions(-) diff --git a/lib/random_assign_utils.rb b/lib/random_assign_utils.rb index b9107879..e1946875 100644 --- a/lib/random_assign_utils.rb +++ b/lib/random_assign_utils.rb @@ -81,12 +81,7 @@ def self.automation_script!(context, fields, automation) assign_to_user_id = users_ids.shuffle.find { |user_id| RandomAssignUtils.in_working_hours?(user_id) } end - assign_to_user_id ||= users_ids.sample - if assign_to_user_id.blank? - RandomAssignUtils.no_one!(topic_id, group.name) - return - end assign_to = User.find(assign_to_user_id) result = nil diff --git a/spec/lib/random_assign_utils_spec.rb b/spec/lib/random_assign_utils_spec.rb index 6f5c44e0..bf19318f 100644 --- a/spec/lib/random_assign_utils_spec.rb +++ b/spec/lib/random_assign_utils_spec.rb @@ -4,6 +4,10 @@ require_relative "../support/assign_allowed_group" RSpec.describe RandomAssignUtils do + FakeAutomation = Struct.new(:id) + + let!(:automation) { FakeAutomation.new(1) } + around do |example| orig_logger = Rails.logger Rails.logger = FakeLogger.new @@ -13,243 +17,202 @@ before { SiteSetting.assign_enabled = true } - FakeAutomation = Struct.new(:id) + describe ".automation_script!" do + subject(:auto_assign) { described_class.automation_script!(ctx, fields, automation) } - let(:post) { Fabricate(:post) } - let!(:automation) { FakeAutomation.new(1) } + fab!(:post_1) { Fabricate(:post) } + fab!(:topic_1) { post_1.topic } + fab!(:group_1) { Fabricate(:group) } + fab!(:user_1) { Fabricate(:user) } + fab!(:user_2) { Fabricate(:user) } - describe ".automation_script!" do - context "when all users of group are on holidays" do - fab!(:topic_1) { Fabricate(:topic) } - fab!(:group_1) { Fabricate(:group) } - fab!(:user_1) { Fabricate(:user) } + let(:ctx) { {} } + let(:fields) { {} } - before do - SiteSetting.assign_allowed_on_groups = [group_1.id.to_s].join("|") - group_1.add(user_1) - UserCustomField.create!(name: "on_holiday", value: "t", user_id: user_1.id) + before do + SiteSetting.assign_allowed_on_groups = group_1.id.to_s + group_1.add(user_1) + end + + context "when all users of group are on holidays" do + let(:fields) do + { + "assignees_group" => { + "value" => group_1.id, + }, + "assigned_topic" => { + "value" => topic_1.id, + }, + } end + before { UserCustomField.create!(name: "on_holiday", value: "t", user_id: user_1.id) } + it "creates post on the topic" do - described_class.automation_script!( - {}, - { - "assignees_group" => { - "value" => group_1.id, - }, - "assigned_topic" => { - "value" => topic_1.id, - }, - }, - automation, - ) - expect(topic_1.posts.first.raw).to match( + auto_assign + expect(topic_1.posts.last.raw).to match( I18n.t("discourse_automation.scriptables.random_assign.no_one", group: group_1.name), ) end end context "when all users of group have been assigned recently" do - fab!(:post_1) { Fabricate(:post) } - fab!(:topic_1) { post_1.topic } - fab!(:group_1) { Fabricate(:group) } - fab!(:user_1) { Fabricate(:user) } - fab!(:user_2) { Fabricate(:user) } + let(:fields) do + { + "assignees_group" => { + "value" => group_1.id, + }, + "assigned_topic" => { + "value" => topic_1.id, + }, + } + end before do - SiteSetting.assign_allowed_on_groups = [group_1.id.to_s].join("|") - group_1.add(user_1) group_1.add(user_2) freeze_time(10.days.ago) { Assigner.new(topic_1, Discourse.system_user).assign(user_1) } Assigner.new(topic_1, Discourse.system_user).assign(user_2) end it "assigns the least recently assigned user to the topic" do - described_class.automation_script!( - {}, - { - "assignees_group" => { - "value" => group_1.id, - }, - "assigned_topic" => { - "value" => topic_1.id, - }, - }, - automation, - ) + auto_assign expect(topic_1.assignment.assigned_to).to eq(user_2) end end context "when no users can be assigned because none are members of assign_allowed_on_groups groups" do - fab!(:topic_1) { Fabricate(:topic) } - fab!(:group_1) { Fabricate(:group) } - fab!(:user_1) { Fabricate(:user) } + let(:fields) do + { + "assignees_group" => { + "value" => group_1.id, + }, + "assigned_topic" => { + "value" => topic_1.id, + }, + } + end - before { group_1.add(user_1) } + before { SiteSetting.assign_allowed_on_groups = "" } it "creates post on the topic" do - described_class.automation_script!( - {}, + auto_assign + expect(topic_1.posts.last.raw).to match( + I18n.t("discourse_automation.scriptables.random_assign.no_one", group: group_1.name), + ) + end + end + + context "when user can be assigned" do + context "when post_template is set" do + let(:fields) do { + "post_template" => { + "value" => "this is a post template", + }, "assignees_group" => { "value" => group_1.id, }, "assigned_topic" => { "value" => topic_1.id, }, - }, - automation, - ) - expect(topic_1.posts.first.raw).to match( - I18n.t("discourse_automation.scriptables.random_assign.no_one", group: group_1.name), - ) - end - end - - context "when user can be assigned" do - fab!(:group_1) { Fabricate(:group) } - fab!(:user_1) { Fabricate(:user) } - fab!(:topic_1) { Fabricate(:topic) } - - before do - SiteSetting.assign_allowed_on_groups = [group_1.id.to_s].join("|") - group_1.add(user_1) - end + } + end - context "when post_template is set" do it "creates a post with the template and assign the user" do - described_class.automation_script!( - {}, - { - "post_template" => { - "value" => "this is a post template", - }, - "assignees_group" => { - "value" => group_1.id, - }, - "assigned_topic" => { - "value" => topic_1.id, - }, - }, - automation, - ) - expect(topic_1.posts.first.raw).to match("this is a post template") + auto_assign + expect(topic_1.posts.second.raw).to match("this is a post template") end end context "when post_template is not set" do - fab!(:post_1) { Fabricate(:post, topic: topic_1) } + let(:fields) do + { + "assignees_group" => { + "value" => group_1.id, + }, + "assigned_topic" => { + "value" => topic_1.id, + }, + } + end it "assigns the user to the topic" do - described_class.automation_script!( - {}, - { - "assignees_group" => { - "value" => group_1.id, - }, - "assigned_topic" => { - "value" => topic_1.id, - }, - }, - automation, - ) + auto_assign expect(topic_1.assignment.assigned_to_id).to eq(user_1.id) end end end context "when all users are in working hours" do - fab!(:topic_1) { Fabricate(:topic) } - fab!(:group_1) { Fabricate(:group) } - fab!(:user_1) { Fabricate(:user) } + let(:fields) do + { + "in_working_hours" => { + "value" => true, + }, + "assignees_group" => { + "value" => group_1.id, + }, + "assigned_topic" => { + "value" => topic_1.id, + }, + } + end before do freeze_time("2022-10-01 02:00") UserOption.find_by(user_id: user_1.id).update(timezone: "Europe/Paris") - group_1.add(user_1) end - it "creates post on the topic" do - described_class.automation_script!( - {}, - { - "in_working_hours" => { - "value" => true, - }, - "assignees_group" => { - "value" => group_1.id, - }, - "assigned_topic" => { - "value" => topic_1.id, - }, - }, - automation, - ) - expect(topic_1.posts.first.raw).to match( - I18n.t("discourse_automation.scriptables.random_assign.no_one", group: group_1.name), - ) + it "assigns the user to the topic" do + auto_assign + expect(topic_1.assignment.assigned_to).to eq(user_1) end end context "when assignees_group is not provided" do - fab!(:topic_1) { Fabricate(:topic) } + let(:fields) { { "assigned_topic" => { "value" => topic_1.id } } } it "raises an error" do - expect { - described_class.automation_script!( - {}, - { "assigned_topic" => { "value" => topic_1.id } }, - automation, - ) - }.to raise_error(/`assignees_group` not provided/) + expect { auto_assign }.to raise_error(/`assignees_group` not provided/) end end context "when assignees_group not found" do - fab!(:topic_1) { Fabricate(:topic) } + let(:fields) do + { "assigned_topic" => { "value" => topic_1.id }, "assignees_group" => { "value" => -1 } } + end it "raises an error" do - expect { - described_class.automation_script!( - {}, - { - "assigned_topic" => { - "value" => topic_1.id, - }, - "assignees_group" => { - "value" => -1, - }, - }, - automation, - ) - }.to raise_error(/Group\(-1\) not found/) + expect { auto_assign }.to raise_error(/Group\(-1\) not found/) end end context "when assigned_topic not provided" do it "raises an error" do - expect { described_class.automation_script!({}, {}, automation) }.to raise_error( - /`assigned_topic` not provided/, - ) + expect { auto_assign }.to raise_error(/`assigned_topic` not provided/) end end context "when assigned_topic is not found" do + let(:fields) { { "assigned_topic" => { "value" => 1 } } } + it "raises an error" do - expect { - described_class.automation_script!( - {}, - { "assigned_topic" => { "value" => 1 } }, - automation, - ) - }.to raise_error(/Topic\(1\) not found/) + expect { auto_assign }.to raise_error(/Topic\(1\) not found/) end end context "when minimum_time_between_assignments is set" do context "when the topic has been assigned recently" do - fab!(:topic_1) { Fabricate(:topic) } + let(:fields) do + { + "assigned_topic" => { + "value" => topic_1.id, + }, + "minimum_time_between_assignments" => { + "value" => 10, + }, + } + end before do freeze_time @@ -261,18 +224,7 @@ end it "logs a warning" do - described_class.automation_script!( - {}, - { - "assigned_topic" => { - "value" => topic_1.id, - }, - "minimum_time_between_assignments" => { - "value" => 10, - }, - }, - automation, - ) + auto_assign expect(Rails.logger.infos.first).to match( /Topic\(#{topic_1.id}\) has already been assigned recently/, ) @@ -281,40 +233,29 @@ end context "when skip_new_users_for_days is set" do - fab!(:topic_1) { Fabricate(:topic) } - fab!(:post_1) { Fabricate(:post, topic: topic_1) } - fab!(:group_1) { Fabricate(:group) } - fab!(:user_1) { Fabricate(:user) } - - before do - SiteSetting.assign_allowed_on_groups = "#{group_1.id}" - group_1.add(user_1) + let(:fields) do + { + "assignees_group" => { + "value" => group_1.id, + }, + "assigned_topic" => { + "value" => topic_1.id, + }, + "skip_new_users_for_days" => { + "value" => "10", + }, + } end it "creates post on the topic if all users are new" do - described_class.automation_script!( - {}, - { - "assignees_group" => { - "value" => group_1.id, - }, - "assigned_topic" => { - "value" => topic_1.id, - }, - "skip_new_users_for_days" => { - "value" => "10", - }, - }, - automation, - ) + auto_assign expect(topic_1.posts.last.raw).to match( I18n.t("discourse_automation.scriptables.random_assign.no_one", group: group_1.name), ) end - it "assign topic if all users are old" do - described_class.automation_script!( - {}, + context "when all users are old" do + let(:fields) do { "assignees_group" => { "value" => group_1.id, @@ -325,11 +266,13 @@ "skip_new_users_for_days" => { "value" => "0", }, - }, - automation, - ) + } + end - expect(topic_1.assignment).not_to eq(nil) + it "assigns topic" do + auto_assign + expect(topic_1.assignment).not_to be_nil + end end end end @@ -337,6 +280,8 @@ describe ".recently_assigned_users_ids" do subject(:assignees) { described_class.recently_assigned_users_ids(post.topic_id, 2.months.ago) } + let(:post) { Fabricate(:post) } + context "when no one has been assigned" do it "returns an empty array" do expect(assignees).to be_empty