Skip to content

Commit

Permalink
WIP: rewrite job specs
Browse files Browse the repository at this point in the history
  • Loading branch information
Flink committed Oct 24, 2023
1 parent 7af2522 commit b9610a7
Showing 1 changed file with 13 additions and 96 deletions.
109 changes: 13 additions & 96 deletions spec/jobs/regular/assign_notification_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,110 +4,27 @@

RSpec.describe Jobs::AssignNotification do
describe "#execute" do
fab!(:user1) { Fabricate(:user, last_seen_at: 1.day.ago) }
fab!(:user2) { Fabricate(:user, last_seen_at: 1.day.ago) }
fab!(:topic) { Fabricate(:topic, title: "Basic topic title") }
fab!(:post) { Fabricate(:post, topic: topic) }
fab!(:pm_post) { Fabricate(:private_message_post) }
fab!(:pm) { pm_post.topic }
fab!(:assign_allowed_group) { Group.find_by(name: "staff") }
subject(:execute_job) { described_class.new.execute(args) }

def assert_publish_topic_state(topic, user)
message = MessageBus.track_publish("/private-messages/assigned") { yield }.first
let(:args) { { assignment_id: assignment_id } }

expect(message.data[:topic_id]).to eq(topic.id)
expect(message.user_ids).to eq([user.id])
end

before { assign_allowed_group.add(user1) }

describe "User" do
let!(:assignment) do
Fabricate(:topic_assignment, topic: topic, assigned_to: user2, assigned_by_user: user1)
end
let(:newest_notification) { Notification.order(:id).last }

it "sends notification alert" do
messages =
MessageBus.track_publish("/notification-alert/#{user2.id}") do
described_class.new.execute({ assignment_id: assignment.id })
end

expect(messages.length).to eq(1)
expect(messages.first.data[:excerpt]).to eq(
I18n.t("discourse_assign.topic_assigned_excerpt", title: topic.title),
)
end

it "sends a high priority notification to the assignee" do
expect { described_class.new.execute({ assignment_id: assignment.id }) }.to change {
Notification.count
}.by(1)
expect(newest_notification).to be_high_priority
end
context "when `assignment_id` is not provided" do
let(:args) { {} }

context "when assigned to a private message" do
let(:user) { pm.allowed_users.first }
let!(:assignment) do
Fabricate(:topic_assignment, topic: pm, assigned_to: user, assigned_by_user: user1)
end

before { assign_allowed_group.add(user) }

it "publishes the right message" do
assert_publish_topic_state(pm, user) do
described_class.new.execute({ assignment_id: assignment.id })
end
end
it "raises an error" do
expect { execute_job }.to raise_error(Discourse::InvalidParameters, "assignment_id")
end
end

describe "Group" do
fab!(:user3) { Fabricate(:user, last_seen_at: 1.day.ago) }
fab!(:suspended_user) { Fabricate(:user, suspended_till: 1.year.from_now) }
fab!(:group) { Fabricate(:group, name: "Developers") }

let!(:assignment) do
Fabricate(:topic_assignment, topic: topic, assigned_by_user: user1, assigned_to: group)
end

let(:newest_notifications) { Notification.order(:id).last(3) }
context "when `assignment_id` is provided" do
let(:assignment_id) { Fabricate(:topic_assignment).id }
let(:assignment) { stub("assignment").responds_like_instance_of(Assignment) }

before do
group.add(user2)
group.add(user3)
group.add(suspended_user)
end

it "sends notification alert to all group members" do
[user2, user3].each do |user|
messages =
MessageBus.track_publish("/notification-alert/#{user.id}") do
described_class.new.execute({ assignment_id: assignment.id })
end
expect(messages.length).to eq(1)
expect(messages.first.data[:excerpt]).to eq(
I18n.t(
"discourse_assign.topic_group_assigned_excerpt",
title: topic.title,
group: group.name,
),
)
Notification.destroy_all
end

messages =
MessageBus.track_publish("/notification-alert/#{suspended_user.id}") do
described_class.new.execute({ assignment_id: assignment.id })
end
expect(messages.length).to eq(0)
end
before { Assignment.stubs(:find).with(assignment_id).returns(assignment) }

it "sends a high priority notification to all group members" do
expect { described_class.new.execute({ assignment_id: assignment.id }) }.to change {
Notification.count
}.by(3)
expect(newest_notifications).to all(be_high_priority)
it "creates missing notifications for the provided assignment" do
assignment.expects(:create_missing_notifications!)
execute_job
end
end
end
Expand Down

0 comments on commit b9610a7

Please sign in to comment.