docbrown/spec/models/reaction_spec.rb
rhymes 7ff882b8ce
Refactor Admin Member Detail view - Tools section (#14283)
* Test with Grid layout

* Use Flexbox

* Replace with utility classes

* Wire up Tools -> Emails

* Stash: will rebase with a better commit message

* Fix transition between Email and Tools component

* Refactor Verify Email Ownership button a bit

* Use respond_to for verify_email_ownership

* Wrap the Preact Snackbar controller in Stimulus and use it from users/tools/email_controller

* Add HTML5 validation to EmailComponent

* Validation and cleanup

* Add Email history list and fix styling

* Additional styling cleanups

* Add error handling

* Close panel after email operations

* Actually use <local-time> GitHub time element correctly

* Add specs for Tools component and controller

* Email to Emails

* Add tests for Admin::Users::Tools::EmailsComponent

* Fix bug with ToolsComponent instantiation in ToolsController

* Add notes to show page

* Add ToolsComponent css

* Use Rails UJS instead of manual Stimulus to connect remote helpers

* Make Notes section come alive by adding its code

* Make Credits section come alive by adding its code

* Go back to vertical flex

* Finalize small restructuring of credits code

* Simplify ToolsComponent instantiation

* Add basic Add user to org functionality

* Make update user permissions form work

* Make remove user from org work

* Use generic Stimulus AjaxController to cleanup code

* Use Stimulus AjaxController for NotesComponent

* Use Stimulus AjaxController for CreditsComponent

* Use Stimulus AjaxController for OrganizationsController

* Add Admin::Users::Tools::ReportsComponent

* Do not display snackbar message if there is no message

* Add Admin::Users::Tools::ReactionsComponent

* Fix EmailsComponent spec

* Add CreditsComponent tests

* Fix quotes

* Add OrganizationsComponent specs

* Add ReportsComponent spec

* Add ReactionsComponent spec

* Fix rubocop violation

* Fix ToolsComponent specs

* Remove unused variable

* More tests

* Use keyword argument for ToolsComponent

* Fill in Tools requests specs

* Use Rspec shared_examples for ToolsController and EmailsController

* Add tests for Admin::Users::Tools::CreditsController

* Add tests for Admin::Users::Tools::NotesController

* Add tests for Admin::Users::Tools::OrganizationsController

* Add tests for Admin::Users::Tools::ReactionsController and ReportsController

* Fix bugs and add tests to Admin::OrganizationMembershipsController

* Add comments to deprecated sections of the UsersController

* Fix bugs and add tests to Admin::UsersController #send_email and #verify_email_ownership

* Add User model tests

* Feature flag fixes

* Add Cypress Tools - Emails tests

* Add Cypress Tools - Notes tests

* Add Cypress Tools - Credits tests

* Add Cypress Tools - Organizations tests

* Add Cypress Tools - Reports and Reactions tests

* Mark the replace target as a polite region

* Update view_component gem

* Tiny fixes

* Fix spec

* Wrap component rendering in render_component

* Move user.related_negative_reactions to a Reaction scope

* Move user.reports to a FeedbackMessage scope

* Move user.last_verification_date as EmailAuthorization class method

* Revert encapsulation to private

* Fix boxes backlinks names

* Add keyboard focus styling to boxes

* Remove duplicate styling

* Remove duplicated header element

* Improve heading hiearchy

* Fix <legend> and labels

* Backlink should be Tools not Users

* Announce section change to screen reader and fix focus

* Fix specs

* Add focus style for backlinks

* Enable email sending in e2e mode

* Use Settings instead of env variable
2021-08-17 18:55:53 +02:00

261 lines
8.8 KiB
Ruby

require "rails_helper"
RSpec.describe Reaction, type: :model do
let(:user) { create(:user) }
let(:article) { create(:article, user: user) }
let(:reaction) { build(:reaction, reactable: article) }
describe "builtin validations" do
subject { build(:reaction, reactable: article, user: user) }
it { is_expected.to belong_to(:user) }
it { is_expected.to validate_inclusion_of(:category).in_array(Reaction::CATEGORIES) }
it { is_expected.to validate_uniqueness_of(:user_id).scoped_to(%i[reactable_id reactable_type category]) }
end
describe "counter_culture" do
context "when a reaction is created" do
it "increments reaction count on user" do
expect do
create(:reaction, user: user)
end.to change { user.reload.reactions_count }.by(1)
end
end
context "when a reaction is destroyed" do
it "decrements reaction count on user" do
reaction = create(:reaction, user: user)
expect do
reaction.destroy
end.to change { user.reload.reactions_count }.by(-1)
end
end
end
describe "validations" do
it "allows like reaction for users without trusted role" do
reaction.category = "like"
expect(reaction).to be_valid
end
it "does not allow reactions outside of allowed list" do
reaction.category = "woozlewazzle"
expect(reaction).not_to be_valid
end
it "does not allow vomit reaction for users without trusted role" do
allow(Settings::General).to receive(:mascot_user_id).and_return(user.id + 1)
reaction.category = "vomit"
expect(reaction).not_to be_valid
end
it "does not allow thumbsdown reaction for users without trusted role" do
allow(Settings::General).to receive(:mascot_user_id).and_return(user.id + 1)
reaction.category = "thumbsdown"
expect(reaction).not_to be_valid
end
it "does not allow reaction on unpublished article" do
reaction = build(:reaction, user: user, reactable: article)
expect(reaction).to be_valid
article.update_column(:published, false)
reaction = build(:reaction, user: user, reactable: article)
expect(reaction).not_to be_valid
end
it "assigns 0 points if reaction is invalid" do
reaction.update(status: "invalid")
expect(reaction.points).to eq(0)
end
it "assigns the correct points if reaction is confirmed" do
reaction_points = reaction.points
reaction.update(status: "confirmed")
expect(reaction.points).to eq(reaction_points * 2)
end
context "when user is trusted" do
before { reaction.user.add_role(:trusted) }
it "allows vomit reactions for users with trusted role" do
reaction.category = "vomit"
expect(reaction).to be_valid
end
it "allows thumbsdown reactions for users with trusted role" do
reaction.category = "thumbsdown"
expect(reaction).to be_valid
end
end
end
describe "#skip_notification_for?" do
let(:receiver) { build(:user) }
let(:reaction) { build(:reaction, reactable: build(:article), user: nil) }
context "when false" do
it "is false when points are positive" do
reaction.points = 1
expect(reaction.skip_notification_for?(receiver)).to be(false)
end
it "is false when the person who reacted is not the same as the reactable owner" do
user_id = User.maximum(:id).to_i + 1
reaction.user_id = user_id
reaction.reactable.user_id = user_id + 1
expect(reaction.skip_notification_for?(user)).to be(false)
end
it "is false when receive_notifications is true" do
reaction.reactable.receive_notifications = true
expect(reaction.skip_notification_for?(receiver)).to be(false)
end
end
context "when true" do
it "is true when points are negative" do
reaction.points = -2
expect(reaction.skip_notification_for?(receiver)).to be(true)
end
it "is true when the person who reacted is the same as the reactable owner" do
user_id = User.maximum(:id).to_i + 1
reaction.user_id = user_id
reaction.reactable.user_id = user_id
expect(reaction.skip_notification_for?(user)).to be(true)
end
end
context "when reactable is a user" do
let(:user) { create(:user) }
let(:reaction) { build(:reaction, reactable: user, user: nil) }
it "returns true if the reactable is the user that reacted" do
reaction.user_id = user.id
expect(reaction.skip_notification_for?(receiver)).to be(true)
end
it "returns false if the reactable is not the user that reacted" do
reaction.user_id = create(:user).id
expect(reaction.skip_notification_for?(receiver)).to be(false)
end
end
end
describe ".count_for_article" do
it "counts the reactions an article has grouped by category" do
create(:reaction, reactable: article, user: user, category: "like")
create(:reaction, reactable: article, user: user, category: "unicorn")
expected_result = [
{ category: "like", count: 1 },
{ category: "readinglist", count: 0 },
{ category: "unicorn", count: 1 },
]
expect(described_class.count_for_article(article.id)).to eq(expected_result)
end
end
context "when callbacks are called after create" do
describe "slack messages" do
let!(:user) { create(:user, :trusted) }
let!(:article) { create(:article, user: user) }
before do
# making sure there are no other enqueued jobs from other tests
sidekiq_perform_enqueued_jobs(only: Slack::Messengers::Worker)
end
it "queues a slack message to be sent for a vomit reaction" do
sidekiq_assert_enqueued_jobs(1, only: Slack::Messengers::Worker) do
create(:reaction, reactable: article, user: user, category: "vomit")
end
end
it "does not queue a message for a like reaction" do
sidekiq_assert_no_enqueued_jobs(only: Slack::Messengers::Worker) do
create(:reaction, reactable: article, user: user, category: "like")
end
end
it "does not queue a message for a thumbsdown reaction" do
sidekiq_assert_no_enqueued_jobs(only: Slack::Messengers::Worker) do
create(:reaction, reactable: article, user: user, category: "thumbsdown")
end
end
end
end
context "when callbacks are called after save" do
let!(:reaction) { build(:reaction, category: "like", reactable: article, user: user) }
describe "enqueues the correct worker" do
it "BustReactableCacheWorker" do
sidekiq_assert_enqueued_with(job: Reactions::BustReactableCacheWorker) do
reaction.save
end
end
it "BustHomepageCacheWorker" do
sidekiq_assert_enqueued_with(job: Reactions::BustHomepageCacheWorker) do
reaction.save
end
end
end
it "updates updated_at if the reactable is a comment" do
sidekiq_perform_enqueued_jobs do
updated_at = 1.day.ago
comment = create(:comment, commentable: article, updated_at: updated_at)
reaction.update(reactable: comment)
expect(comment.reload.updated_at).to be > updated_at
end
end
end
context "when callbacks are called before destroy" do
let(:reaction) { create(:reaction, reactable: article, user: user) }
it "enqueues a ScoreCalcWorker on article reaction destroy" do
sidekiq_assert_enqueued_with(job: Articles::ScoreCalcWorker, args: [article.id]) do
reaction.destroy
end
end
it "updates reactable without delay" do
allow(reaction).to receive(:update_reactable_without_delay)
reaction.destroy
expect(reaction).to have_received(:update_reactable_without_delay)
end
it "busts reactable cache without delay" do
allow(reaction).to receive(:bust_reactable_cache_without_delay)
reaction.destroy
expect(reaction).to have_received(:bust_reactable_cache_without_delay)
end
end
describe ".related_negative_reactions_for_user" do
let(:moderator) { create(:user, :trusted) }
it "returns vomit reactions on user's articles" do
article = create(:article, user: user)
reaction = create(:vomit_reaction, user: moderator, reactable: article)
expect(described_class.related_negative_reactions_for_user(user).first.id).to eq(reaction.id)
end
it "returns vomit reactions on user's comments" do
comment = create(:comment, user: user)
reaction = create(:vomit_reaction, user: moderator, reactable: comment)
expect(described_class.related_negative_reactions_for_user(user).first.id).to eq(reaction.id)
end
it "returns the user's vomit reactions" do
reaction = create(:vomit_reaction, user: moderator, reactable: user)
expect(described_class.related_negative_reactions_for_user(moderator).first.id).to eq(reaction.id)
end
end
end