jam-cloud/ruby/spec/jam_ruby/models/notification_spec.rb

367 lines
12 KiB
Ruby

require 'spec_helper'
describe Notification do
before(:each) do
UserMailer.deliveries.clear
end
def count_publish_to_user_calls
result = {count: 0}
MQRouter.any_instance.stub(:publish_to_user) do |receiver_id, msg|
result[:count] += 1
result[:msg] = msg
end
result
end
describe "send friend request" do
let(:receiver) {FactoryGirl.create(:user)}
let(:sender) {FactoryGirl.create(:user)}
let(:friend_request) {FactoryGirl.create(:friend_request, user:sender, friend:receiver)}
it "success when offline" do
calls = count_publish_to_user_calls
notification = Notification.send_friend_request(friend_request.id, sender.id, receiver.id)
notification.errors.any?.should be_false
UserMailer.deliveries.length.should == 1
calls[:count].should == 0
end
it "success when online" do
receiver_connection = FactoryGirl.create(:connection, user: receiver)
calls = count_publish_to_user_calls
notification = Notification.send_friend_request(friend_request.id, sender.id, receiver.id)
notification.errors.any?.should be_false
UserMailer.deliveries.length.should == 0
calls[:count].should == 1
end
end
describe "send_text_message" do
it "success when offline" do
receiver = FactoryGirl.create(:user)
sender = FactoryGirl.create(:user)
message = "Just a test message!"
calls = count_publish_to_user_calls
notification = Notification.send_text_message(message, sender, receiver)
notification.errors.any?.should be_false
UserMailer.deliveries.length.should == 1
calls[:count].should == 0
end
it "success when online" do
receiver = FactoryGirl.create(:user)
receiver_connection = FactoryGirl.create(:connection, user: receiver)
sender = FactoryGirl.create(:user)
message = "Just a test message!"
calls = count_publish_to_user_calls
notification = Notification.send_text_message(message, sender, receiver)
notification.errors.any?.should be_false
UserMailer.deliveries.length.should == 0
calls[:count].should == 1
calls[:msg].text_message.msg.should == message
calls[:msg].text_message.photo_url.should == ''
calls[:msg].text_message.sender_name.should == sender.name
calls[:msg].text_message.notification_id.should == notification.id
calls[:msg].text_message.created_at = notification.created_date
calls[:msg].text_message.clipped_msg.should be_false
end
it "success when online with long message" do
receiver = FactoryGirl.create(:user)
receiver_connection = FactoryGirl.create(:connection, user: receiver)
sender = FactoryGirl.create(:user)
message = "0" * 203 # 200 is clip size
calls = count_publish_to_user_calls
notification = Notification.send_text_message(message, sender, receiver)
notification.errors.any?.should be_false
UserMailer.deliveries.length.should == 0
calls[:count].should == 1
calls[:msg].text_message.msg.should == "0" * 200
calls[:msg].text_message.photo_url.should == ''
calls[:msg].text_message.sender_name.should == sender.name
calls[:msg].text_message.notification_id.should == notification.id
calls[:msg].text_message.created_at = notification.created_date
calls[:msg].text_message.clipped_msg.should be_true
end
it "fails with profanity" do
receiver = FactoryGirl.create(:user)
sender = FactoryGirl.create(:user)
message = "ass"
calls = count_publish_to_user_calls
notification = Notification.send_text_message(message, sender, receiver)
notification.errors.any?.should be_true
notification.errors[:message].should == ['cannot contain profanity']
UserMailer.deliveries.length.should == 0
calls[:count].should == 0
end
it "fails when target is same as receiver" do
receiver = FactoryGirl.create(:user)
sender = FactoryGirl.create(:user)
message = "yo"
calls = count_publish_to_user_calls
notification = Notification.send_text_message(message, sender, sender)
notification.errors.any?.should be_true
notification.errors[:target_user].should == [ValidationMessages::DIFFERENT_SOURCE_TARGET]
UserMailer.deliveries.length.should == 0
calls[:count].should == 0
end
it "fails when there is no message" do
receiver = FactoryGirl.create(:user)
sender = FactoryGirl.create(:user)
message = ''
calls = count_publish_to_user_calls
notification = Notification.send_text_message(message, sender, receiver)
notification.errors.any?.should be_true
notification.errors[:message].should == ['is too short (minimum is 1 characters)']
UserMailer.deliveries.length.should == 0
calls[:count].should == 0
end
end
describe "send scheduled session invitation" do
it "sends live notification when user is online" do
end
it "sends email notification when user is offline" do
end
it "sends no notification if session is nil" do
sender = FactoryGirl.create(:user)
calls = count_publish_to_user_calls
notification = Notification.send_scheduled_session_invitation(nil, sender)
UserMailer.deliveries.length.should == 0
calls[:count].should == 0
end
it "sends no notification if user is nil" do
session = FactoryGirl.create(:music_session)
calls = count_publish_to_user_calls
notification = Notification.send_scheduled_session_invitation(session, nil)
UserMailer.deliveries.length.should == 0
calls[:count].should == 0
end
end
describe "send scheduled session rsvp" do
it "sends live notification when user is online" do
end
it "sends email notification when user is offline" do
end
it "sends no notification if session is nil" do
sender = FactoryGirl.create(:user)
calls = count_publish_to_user_calls
notification = Notification.send_scheduled_session_rsvp(nil, sender, nil)
UserMailer.deliveries.length.should == 0
calls[:count].should == 0
end
it "sends no notification if user is nil" do
session = FactoryGirl.create(:music_session)
calls = count_publish_to_user_calls
notification = Notification.send_scheduled_session_rsvp(session, nil, nil)
UserMailer.deliveries.length.should == 0
calls[:count].should == 0
end
end
describe "send scheduled session rsvp approved" do
it "sends live notification when user is online" do
end
it "sends email notification when user is offline" do
end
it "sends no notification if session is nil" do
receiver = FactoryGirl.create(:user)
calls = count_publish_to_user_calls
notification = Notification.send_scheduled_session_rsvp_approved(nil, receiver)
UserMailer.deliveries.length.should == 0
calls[:count].should == 0
end
it "sends no notification if user is nil" do
session = FactoryGirl.create(:music_session)
calls = count_publish_to_user_calls
notification = Notification.send_scheduled_session_rsvp_approved(session, nil)
UserMailer.deliveries.length.should == 0
calls[:count].should == 0
end
end
describe "send scheduled session rsvp cancellation" do
it "sends live notification when user is online" do
end
it "sends email notification when user is offline" do
end
it "sends no notification if session is nil" do
sender = FactoryGirl.create(:user)
calls = count_publish_to_user_calls
notification = Notification.send_scheduled_session_rsvp_cancelled(nil, sender)
UserMailer.deliveries.length.should == 0
calls[:count].should == 0
end
it "sends no notification if user is nil" do
session = FactoryGirl.create(:music_session)
calls = count_publish_to_user_calls
notification = Notification.send_scheduled_session_rsvp_cancelled(session, nil)
UserMailer.deliveries.length.should == 0
calls[:count].should == 0
end
end
describe "send scheduled session rsvp cancellation by organizer" do
it "sends live notification when user is online" do
end
it "sends email notification when user is offline" do
end
it "sends no notification if session is nil" do
receiver = FactoryGirl.create(:user)
calls = count_publish_to_user_calls
notification = Notification.send_scheduled_session_rsvp_cancelled_org(nil, receiver)
UserMailer.deliveries.length.should == 0
calls[:count].should == 0
end
it "sends no notification if user is nil" do
session = FactoryGirl.create(:music_session)
calls = count_publish_to_user_calls
notification = Notification.send_scheduled_session_rsvp_cancelled_org(session, nil)
UserMailer.deliveries.length.should == 0
calls[:count].should == 0
end
end
describe "send scheduled session cancellation" do
it "sends live notification when user is online" do
end
it "sends email notification when user is offline" do
end
it "sends no notification if session is nil" do
calls = count_publish_to_user_calls
notification = Notification.send_scheduled_session_cancelled(nil)
UserMailer.deliveries.length.should == 0
calls[:count].should == 0
end
it "sends no notification if there are no rsvp requests" do
session = FactoryGirl.create(:music_session)
calls = count_publish_to_user_calls
notification = Notification.send_scheduled_session_cancelled(session)
UserMailer.deliveries.length.should == 0
calls[:count].should == 0
end
end
describe "send scheduled session rescheduled" do
it "sends live notification when user is online" do
end
it "sends email notification when user is offline" do
end
it "sends no notification if session is nil" do
calls = count_publish_to_user_calls
notification = Notification.send_scheduled_session_rescheduled(nil)
UserMailer.deliveries.length.should == 0
calls[:count].should == 0
end
it "sends no notification if there are no rsvp requests" do
session = FactoryGirl.create(:music_session)
calls = count_publish_to_user_calls
notification = Notification.send_scheduled_session_rescheduled(session)
UserMailer.deliveries.length.should == 0
calls[:count].should == 0
end
end
describe "send scheduled session reminder" do
it "sends live notification when user is online" do
end
it "sends email notification when user is offline" do
end
it "sends no notification if session is nil" do
calls = count_publish_to_user_calls
notification = Notification.send_scheduled_session_reminder(nil)
UserMailer.deliveries.length.should == 0
calls[:count].should == 0
end
it "sends no notification if there are no rsvp requests" do
session = FactoryGirl.create(:music_session)
calls = count_publish_to_user_calls
notification = Notification.send_scheduled_session_reminder(session)
UserMailer.deliveries.length.should == 0
calls[:count].should == 0
end
end
describe "send scheduled session comment" do
it "sends live notification when user is online" do
end
it "sends email notification when user is offline" do
end
it "sends no notification if session is nil" do
calls = count_publish_to_user_calls
notification = Notification.send_scheduled_session_comment(nil, 'when are we playing?')
UserMailer.deliveries.length.should == 0
calls[:count].should == 0
end
it "sends no notification if comment is empty" do
session = FactoryGirl.create(:music_session)
calls = count_publish_to_user_calls
notification = Notification.send_scheduled_session_comment(nil, session)
UserMailer.deliveries.length.should == 0
calls[:count].should == 0
end
end
end