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 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("