docbrown/spec/models/message_spec.rb
Molly Struve e87dead7ad
Use new UserSetting and UserNotificationSettings and Ignore Related User Table Fields (#14121)
* schema file undelete description

* feat: v1 of the script

* Flesh out remaining enums under their categories

* complete UsersSettings data update script

* complete DUS for relevant attributes in users and profiles tables

* complete DUS for users_notification_settings

* alphabetize user_settings sql file

* safeguard against null values for "null: false" settings

* Set up actual UsersSettings DUS and specs files

* fix broken DUS script

* complete specs for UsersSetting DUS

* Address QA of specs

* complete specs for users_notification_settings DUS

* fix the typos (thanks Julianna!)

* begin implementation

* still building

* add missing attribute "email_membership_newsletter"

* complete sync code (except race condition for user profile)

* complete implementation, remains tests

* Address PR review and fix Travis fails

* remove superfluous Profile.new

* fix travis fails

* feat: update the users_notification_setting attributes from the user model

* feat: use the config fonts enums to display the fonts

* feat: loop through the keys

* fix profile = nil blowing up; add specs for notification_setting model

* remove unneeded spec

* remove feed validation until after sync code removed; fixes feed_import spec failures

* remove spec associated with feed_url validation in user_setting model

* fix failing spec 😅

* add TODO

* feat: set the user settings in the user controller  and use it in the customization form

* feat: move some update logic to the users settings controller thats being used from customization

* feat: show the updated values form the users_settingd and not the user instance

* Generalize redirect back to current tab

* still trying to reflect changed theme upon refresh

* customizations take effect on refresh

* remove 'with_feed' scope from user model

Co-authored-by: Jamie Gaskins <jamie@forem.com>

* start with takeover for fields previously in profiles table

* Takeover code for `publishing_from_rss` section in Settings (#13914)

* implement takeover code part 1

* implement takeover code

* fix feed fetch

* need rhymes help

* complete implementation; specs pending

* fix STUPID omission that caused so many headaches 😫

* implement profile fields pointing to users_settings 🎉

* run migrations

* implement inbox type & guidelines takeover code; specs pending (#13911)

* Point changes in notification settings to `users_notification_settings` table (#13910)

* implement takeover code; remains specs

* address PR feedback; remove related sync code

* address PR review feedback

* need help with routing and specs

* address pr review

* addressing pr review

* Treat implementation edge cases and omissions 😅

* fix uncommented comment

* fixing implementation cases

* address more PR review feedback

* fixing notifications use-cases

* refactor settings controller

* more pr review changes

* solving bugs

* fix broken onboarding

* handle eperience_level calls

* more fixes

* remove unneeded mappings

* add To-dos for quety updates

* remove done TODO

* purge done TODOs

* update notification_settings-related queries

* start fixing specs

* fixing specs

* fix notification and lrg_forem specs

* fixing broken specs

* still fixing

* fix line dif and remove reloads from user.rb

* run specs

* silence bullet and other fixes

* remove setting migration scripts and specs, fix more settings for specs

* handle missing user for article builder and fix notification specs

* fix some final controller specs and re-add incorrectly removed specs

* remove deprecated data update scripts and related workers, put travis back

* refactor admin tags mods controller, write/move specs for users notifications settings controller

* schema cleanup and other small refactors for consistency

* set field we can invalidate in spec via active record instead of at the db level

* remove I think an uneccessary hook call from subscribe_to_mailchimp_newsletter

* use bnefore_create to setup settings, please dont blow up the test suite

* mailchimp bot fix

* remove decorator in favor of single model method

Co-authored-by: Arit Amana <msarit@gmail.com>
Co-authored-by: Ridhwana <ridhwana.khan16@gmail.com>
Co-authored-by: Arit Amana <32520970+msarit@users.noreply.github.com>
Co-authored-by: Jamie Gaskins <jamie@forem.com>
2021-07-08 09:31:34 -05:00

193 lines
6.6 KiB
Ruby

require "rails_helper"
RSpec.describe Message, type: :model do
let(:user) { create(:user) }
let(:user2) { create(:user) }
let(:tag) { create(:tag) }
let(:chat_channel) { create(:chat_channel) }
let(:message) { create(:message, user: user) }
let(:random_word) { Faker::Lorem.word }
describe "validations" do
context "with automatic validations" do
before do
allow(ChatChannel).to receive(:find).and_return(ChatChannel.new)
end
it { is_expected.to belong_to(:user) }
it { is_expected.to belong_to(:chat_channel) }
it { is_expected.to validate_presence_of(:message_html) }
it { is_expected.to validate_presence_of(:message_markdown) }
end
it "is invalid without channel permission for invite only channels" do
chat_channel.update(channel_type: "invite_only")
message = build(:message, chat_channel: chat_channel, user: user)
expect(message).not_to be_valid
end
it "is valid with channel permission" do
chat_channel.add_users([user])
message = build(:message, chat_channel: chat_channel, user: user)
expect(message).to be_valid
end
it "is invalid with text over 1024 chars" do
message = build(:message, chat_channel: chat_channel, user: user, message_markdown: "x" * 1025)
expect(message).not_to be_valid
end
end
context "when callbacks are triggered before validation" do
let(:article) { create(:article) }
describe "#message_html" do
it "creates rich link with proper link for article" do
link = URL.article(article)
message.message_markdown = "hello #{link}"
message.validate!
expect(message.message_html).to include(
article.title,
"sidecar-article",
link,
)
end
it "creates rich link with proper link for article when Cloudinary is enabled", cloudinary: true do
message.message_markdown = "hello http://#{ApplicationConfig['APP_DOMAIN']}#{article.path}"
message.validate!
expect(message.message_html).to include(
article.title,
"sidecar-article",
"/c_limit,f_auto,fl_progressive,q_auto,w_725/",
)
end
it "creates target blank link" do
message.message_markdown = "hello http://#{ApplicationConfig['APP_DOMAIN']}#{user.path}"
message.validate!
expect(message.message_html).to include("<a target=\"_blank\"")
end
it "creates rich link with proper link for user" do
message.message_markdown = "hello http://#{ApplicationConfig['APP_DOMAIN']}#{user.path}"
message.validate!
expect(message.message_html).to include(user.name)
expect(message.message_html).to include("sidecar-user")
end
it "creates rich embeddable link" do
message.message_markdown = "https://docs.google.com/ https://www.figma.com/file/"
message.validate!
expect(message.message_html).to include("chatchannels__richlink--base")
end
it "creates rich link with proper link for tag" do
message.message_markdown = "hello http://#{ApplicationConfig['APP_DOMAIN']}/t/#{tag.name}"
message.validate!
expect(message.message_html).to include(tag.name)
expect(message.message_html).to include("sidecar-tag")
end
it "creates rich link with non-rich link" do
message.message_markdown = "hello http://#{ApplicationConfig['APP_DOMAIN']}/report-abuse"
message.validate!
expect(message.message_html).not_to include("data-content")
end
it "creates mention if user exists" do
message.message_markdown = "Hello @#{user.username}"
message.validate!
expect(message.message_html).to include "<a"
expect(message.message_html).to include("/#{user.username}")
end
it "doesn't creates mention if user exists" do
message.message_markdown = "Hello @#{random_word}"
message.validate!
expect(message.message_html).not_to include "<a"
expect(message.message_html).not_to include("/#{random_word}")
end
end
end
context "when callbacks are triggered after create" do
before do
allow(ForemInstance).to receive(:smtp_enabled?).and_return(true)
chat_channel.add_users([user, user2])
end
it "sends email if user not recently active on /connect" do
chat_channel.update_column(:channel_type, "direct")
user2.update_column(:updated_at, 1.day.ago)
user2.chat_channel_memberships.last.update_column(:last_opened_at, 2.days.ago)
create(:message, chat_channel: chat_channel, user: user)
expect(EmailMessage.last.subject).to start_with("#{user.name} just messaged you")
end
it "does not send email if user has been recently active" do
expect do
create(:message, chat_channel: chat_channel, user: user)
end.to change(EmailMessage, :count).by(0)
end
it "does not send email if user has email_messages turned off" do
chat_channel.update_column(:channel_type, "direct")
user2.update_columns(updated_at: 1.day.ago)
user2.notification_setting.update_columns(email_connect_messages: false)
user2.chat_channel_memberships.last.update_column(:last_opened_at, 2.days.ago)
expect do
create(:message, chat_channel: chat_channel, user: user)
end.to change(EmailMessage, :count).by(0)
end
end
describe "#left_channel?" do
it "returns true if chat_action is removed_from_channel" do
message.chat_action = "removed_from_channel"
expect(message.left_channel?).to eq(true)
end
it "returns true if chat_action is left_channel" do
message.chat_action = "left_channel"
expect(message.left_channel?).to eq(true)
end
it "returns false if chat_action is NOT removed_from_channel" do
message.chat_action = "joined"
expect(message.left_channel?).to eq(false)
end
end
describe "#after_create" do
context "when chat_action is left_channel" do
it "does not update unopened message statuses" do
chat_channel.add_users([user, user2])
create(:message, chat_channel: chat_channel, user: user, chat_action: "left_channel")
expect(user2.chat_channel_memberships.pluck(:has_unopened_messages).all?(false)).to eq(true)
end
end
context "when chat_action is NOT left_channel" do
it "updates unopened message statuses" do
chat_channel.add_users([user, user2])
create(:message, chat_channel: chat_channel, user: user)
expect(user2.chat_channel_memberships.pluck(:has_unopened_messages).all?(true)).to eq(true)
end
end
end
end