added more tests, minor refactor, only send scheduled session notifications if target user is online

This commit is contained in:
Brian Smith 2014-09-11 21:27:41 -04:00
parent 632d806825
commit c55413d349
2 changed files with 349 additions and 330 deletions

View File

@ -555,7 +555,7 @@ module JamRuby
# remove anyone in the session and invited musicians
friends_and_followers = friends_and_followers - music_session.unique_users - music_session.invited_musicians
online_ff, offline_ff = [], [], []
online_ff, offline_ff = [], []
notification_msg = format_msg(NotificationTypes::MUSICIAN_SESSION_JOIN, {:user => user})
friends_and_followers.each do |ff|
@ -614,18 +614,20 @@ module JamRuby
notification_msg = format_msg(notification.description, {:user => source_user, :session => music_session})
msg = @@message_factory.scheduled_session_invitation(
target_user.id,
music_session.id,
source_user.photo_url,
notification_msg,
music_session.name,
music_session.pretty_scheduled_start(false),
notification.id,
notification.created_date
)
if target_user.online
msg = @@message_factory.scheduled_session_invitation(
target_user.id,
music_session.id,
source_user.photo_url,
notification_msg,
music_session.name,
music_session.pretty_scheduled_start(false),
notification.id,
notification.created_date
)
@@mq_router.publish_to_user(target_user.id, msg)
@@mq_router.publish_to_user(target_user.id, msg)
end
begin
UserMailer.scheduled_session_invitation(target_user, notification_msg, music_session).deliver
@ -654,20 +656,23 @@ module JamRuby
notification_msg = format_msg(notification.description, {:user => source_user, :session => music_session})
msg = @@message_factory.scheduled_session_rsvp(
target_user.id,
music_session.id,
source_user.photo_url,
notification_msg,
source_user.id,
instruments.join('|'),
music_session.name,
music_session.pretty_scheduled_start(false),
notification.id,
notification.created_date
)
if target_user.online
msg = @@message_factory.scheduled_session_rsvp(
target_user.id,
music_session.id,
source_user.photo_url,
notification_msg,
source_user.id,
instruments.join('|'),
music_session.name,
music_session.pretty_scheduled_start(false),
notification.id,
notification.created_date
)
@@mq_router.publish_to_user(target_user.id, msg)
end
@@mq_router.publish_to_user(target_user.id, msg)
begin
UserMailer.scheduled_session_rsvp(target_user, notification_msg, music_session).deliver
rescue => e
@ -695,17 +700,20 @@ module JamRuby
notification_msg = format_msg(notification.description, {:session => music_session})
msg = @@message_factory.scheduled_session_rsvp_approved(
target_user.id,
music_session.id,
notification_msg,
music_session.name,
music_session.pretty_scheduled_start(false),
notification.id,
notification.created_date
)
if target_user.online
msg = @@message_factory.scheduled_session_rsvp_approved(
target_user.id,
music_session.id,
notification_msg,
music_session.name,
music_session.pretty_scheduled_start(false),
notification.id,
notification.created_date
)
@@mq_router.publish_to_user(target_user.id, msg)
end
@@mq_router.publish_to_user(target_user.id, msg)
begin
UserMailer.scheduled_session_rsvp_approved(target_user, notification_msg, music_session).deliver
rescue => e
@ -729,17 +737,19 @@ module JamRuby
notification_msg = format_msg(notification.description, {:session => music_session})
msg = @@message_factory.scheduled_session_rsvp_cancelled(
target_user.id,
music_session.id,
notification_msg,
music_session.name,
music_session.pretty_scheduled_start(false),
notification.id,
notification.created_date
)
if target_user.online
msg = @@message_factory.scheduled_session_rsvp_cancelled(
target_user.id,
music_session.id,
notification_msg,
music_session.name,
music_session.pretty_scheduled_start(false),
notification.id,
notification.created_date
)
@@mq_router.publish_to_user(target_user.id, msg)
@@mq_router.publish_to_user(target_user.id, msg)
end
begin
UserMailer.scheduled_session_rsvp_cancelled(target_user, notification_msg, music_session).deliver
@ -764,17 +774,19 @@ module JamRuby
notification_msg = format_msg(notification.description, {:session => music_session})
msg = @@message_factory.scheduled_session_rsvp_cancelled_org(
target_user.id,
music_session.id,
notification_msg,
music_session.name,
music_session.pretty_scheduled_start(false),
notification.id,
notification.created_date
)
if target_user.online
msg = @@message_factory.scheduled_session_rsvp_cancelled_org(
target_user.id,
music_session.id,
notification_msg,
music_session.name,
music_session.pretty_scheduled_start(false),
notification.id,
notification.created_date
)
@@mq_router.publish_to_user(target_user.id, msg)
@@mq_router.publish_to_user(target_user.id, msg)
end
begin
UserMailer.scheduled_session_rsvp_cancelled_org(target_user, notification_msg, music_session).deliver
@ -805,17 +817,19 @@ module JamRuby
notification_msg = format_msg(notification.description, {:session => music_session})
msg = @@message_factory.scheduled_session_cancelled(
target_user.id,
music_session.id,
notification_msg,
music_session.name,
music_session.pretty_scheduled_start(false),
notification.id,
notification.created_date
)
if target_user.online
msg = @@message_factory.scheduled_session_cancelled(
target_user.id,
music_session.id,
notification_msg,
music_session.name,
music_session.pretty_scheduled_start(false),
notification.id,
notification.created_date
)
@@mq_router.publish_to_user(target_user.id, msg)
@@mq_router.publish_to_user(target_user.id, msg)
end
begin
UserMailer.scheduled_session_cancelled(target_user, notification_msg, music_session).deliver
@ -848,17 +862,19 @@ module JamRuby
notification_msg = format_msg(notification.description, {:session => music_session})
msg = @@message_factory.scheduled_session_rescheduled(
target_user.id,
music_session.id,
notification_msg,
music_session.name,
music_session.pretty_scheduled_start(false),
notification.id,
notification.created_date
)
if target_user.online
msg = @@message_factory.scheduled_session_rescheduled(
target_user.id,
music_session.id,
notification_msg,
music_session.name,
music_session.pretty_scheduled_start(false),
notification.id,
notification.created_date
)
@@mq_router.publish_to_user(target_user.id, msg)
@@mq_router.publish_to_user(target_user.id, msg)
end
begin
UserMailer.scheduled_session_rescheduled(target_user, notification_msg, music_session).deliver
@ -890,17 +906,19 @@ module JamRuby
notification_msg = format_msg(notification.description, {:session => music_session})
msg = @@message_factory.scheduled_session_reminder(
target_user.id,
music_session.id,
notification_msg,
music_session.name,
music_session.pretty_scheduled_start(false),
notification.id,
notification.created_date
)
if target_user.online
msg = @@message_factory.scheduled_session_reminder(
target_user.id,
music_session.id,
notification_msg,
music_session.name,
music_session.pretty_scheduled_start(false),
notification.id,
notification.created_date
)
@@mq_router.publish_to_user(target_user.id, msg)
@@mq_router.publish_to_user(target_user.id, msg)
end
begin
UserMailer.scheduled_session_reminder(target_user, notification_msg, music_session).deliver
@ -934,19 +952,21 @@ module JamRuby
notification_msg = format_msg(notification.description, {:session => music_session})
msg = @@message_factory.scheduled_session_comment(
target_user.id,
music_session.id,
target_user.photo_url,
notification_msg,
comment,
music_session.name,
music_session.pretty_scheduled_start(false),
notification.id,
notification.created_date
)
if target_user.online
msg = @@message_factory.scheduled_session_comment(
target_user.id,
music_session.id,
target_user.photo_url,
notification_msg,
comment,
music_session.name,
music_session.pretty_scheduled_start(false),
notification.id,
notification.created_date
)
@@mq_router.publish_to_user(target_user.id, msg)
@@mq_router.publish_to_user(target_user.id, msg)
end
begin
UserMailer.scheduled_session_comment(target_user, notification_msg, comment, music_session).deliver
@ -961,7 +981,7 @@ module JamRuby
# if the session is private, don't send any notifications
if music_session.musician_access || music_session.fan_access
notifications, online_followers, offline_followers = [], [], []
online_followers, offline_followers = [], []
notification_msg = format_msg(NotificationTypes::BAND_SESSION_JOIN, {:band => band})
followers = band.followers.map { |bf| bf.user }
@ -1022,7 +1042,7 @@ module JamRuby
follower_users = user_followers.map { |uf| uf.user }
friends_and_followers = friend_users.concat(follower_users).uniq
notifications, online_ff, offline_ff = [], [], []
online_ff, offline_ff = [], []
notification_msg = format_msg(NotificationTypes::MUSICIAN_RECORDING_SAVED, {:user => user})
friends_and_followers.each do |ff|
@ -1238,7 +1258,7 @@ module JamRuby
end
def send_band_invitation_accepted(band, band_invitation, sender, receiver)
notification = Notification.new
notification.band_id = band.id
notification.description = NotificationTypes::BAND_INVITATION_ACCEPTED

View File

@ -4,6 +4,21 @@ describe Notification do
before(:each) do
UserMailer.deliveries.clear
MusicSession.delete_all
Recording.delete_all
Band.delete_all
BandInvitation.delete_all
Friendship.delete_all
Follow.delete_all
User.delete_all
@receiver = FactoryGirl.create(:user)
@sender = FactoryGirl.create(:user)
@recording = FactoryGirl.create(:recording)
@session = FactoryGirl.create(:music_session)
@band = FactoryGirl.create(:band)
@friend_request = FactoryGirl.create(:friend_request, user: @sender, friend: @receiver)
end
def count_publish_to_user_calls
@ -16,14 +31,9 @@ describe Notification do
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 "sends email when user is offline and subscribes to emails" do
calls = count_publish_to_user_calls
notification = Notification.send_friend_request(friend_request.id, sender.id, receiver.id)
notification = Notification.send_friend_request(@friend_request.id, @sender.id, @receiver.id)
notification.errors.any?.should be_false
UserMailer.deliveries.length.should == 1
@ -31,11 +41,11 @@ describe Notification do
end
it "does not send email when user is offline and opts out of emails" do
receiver.subscribe_email = false
receiver.save!
@receiver.subscribe_email = false
@receiver.save!
calls = count_publish_to_user_calls
notification = Notification.send_friend_request(friend_request.id, sender.id, receiver.id)
notification = Notification.send_friend_request(@friend_request.id, @sender.id, @receiver.id)
notification.errors.any?.should be_false
UserMailer.deliveries.length.should == 0
@ -43,9 +53,9 @@ describe Notification do
end
it "success when online" do
receiver_connection = FactoryGirl.create(:connection, user: receiver)
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 = Notification.send_friend_request(@friend_request.id, @sender.id, @receiver.id)
notification.errors.any?.should be_false
UserMailer.deliveries.length.should == 0
@ -54,25 +64,20 @@ describe Notification do
end
describe "send friend request accepted" do
let(:receiver) {FactoryGirl.create(:user)}
let(:sender) {FactoryGirl.create(:user)}
let(:friend_request) {FactoryGirl.create(:friend_request, user:sender, friend:receiver)}
it "sends email when user is offline and subscribes to emails" do
calls = count_publish_to_user_calls
Notification.send_friend_request_accepted(receiver.id, sender.id)
Notification.send_friend_request_accepted(@receiver.id, @sender.id)
UserMailer.deliveries.length.should == 1
calls[:count].should == 0
end
it "does not send email when user is offline and opts out of emails" do
receiver.subscribe_email = false
receiver.save!
@receiver.subscribe_email = false
@receiver.save!
calls = count_publish_to_user_calls
Notification.send_friend_request_accepted(receiver.id, sender.id)
Notification.send_friend_request_accepted(@receiver.id, @sender.id)
UserMailer.deliveries.length.should == 0
calls[:count].should == 0
@ -80,24 +85,20 @@ describe Notification do
end
describe "send new user follower" do
let(:receiver) {FactoryGirl.create(:user)}
let(:sender) {FactoryGirl.create(:user)}
it "sends email when user is offline and subscribes to emails" do
calls = count_publish_to_user_calls
Notification.send_new_user_follower(sender, receiver)
Notification.send_new_user_follower(@sender, @receiver)
UserMailer.deliveries.length.should == 1
calls[:count].should == 0
end
it "does not send email when user is offline and opts out of emails" do
receiver.subscribe_email = false
receiver.save!
@receiver.subscribe_email = false
@receiver.save!
calls = count_publish_to_user_calls
Notification.send_new_user_follower(sender, receiver)
Notification.send_new_user_follower(@sender, @receiver)
UserMailer.deliveries.length.should == 0
calls[:count].should == 0
@ -105,26 +106,24 @@ describe Notification do
end
describe "send new band follower" do
let(:member) {FactoryGirl.create(:user)}
let(:sender) {FactoryGirl.create(:user)}
let(:band) {FactoryGirl.create(:band)}
it "sends email when user is offline and subscribes to emails" do
band.users << member
@band.users << @receiver
calls = count_publish_to_user_calls
Notification.send_new_band_follower(sender, band)
Notification.send_new_band_follower(@sender, @band)
UserMailer.deliveries.length.should == 1
calls[:count].should == 0
end
it "does not send email when user is offline and opts out of emails" do
member.subscribe_email = false
member.save!
@band.users << @receiver
@receiver.subscribe_email = false
@receiver.save!
band.users << member
calls = count_publish_to_user_calls
Notification.send_new_band_follower(sender, band)
Notification.send_new_band_follower(@sender, @band)
UserMailer.deliveries.length.should == 0
calls[:count].should == 0
@ -132,25 +131,20 @@ describe Notification do
end
describe "send session invitation" do
let(:receiver) {FactoryGirl.create(:user)}
let(:sender) {FactoryGirl.create(:user)}
let(:session) {FactoryGirl.create(:music_session)}
it "sends email when user is offline and subscribes to emails" do
calls = count_publish_to_user_calls
Notification.send_session_invitation(receiver, sender, session.id)
Notification.send_session_invitation(@receiver, @sender, @session.id)
UserMailer.deliveries.length.should == 1
calls[:count].should == 0
end
it "does not send email when user is offline and opts out of emails" do
receiver.subscribe_email = false
receiver.save!
@receiver.subscribe_email = false
@receiver.save!
calls = count_publish_to_user_calls
Notification.send_session_invitation(receiver, sender, session.id)
Notification.send_session_invitation(@receiver, @sender, @session.id)
UserMailer.deliveries.length.should == 0
calls[:count].should == 0
@ -158,67 +152,59 @@ describe Notification do
end
describe "send musician session join" do
let(:receiver) {FactoryGirl.create(:user)}
let(:sender) {FactoryGirl.create(:user)}
let(:session) {FactoryGirl.create(:music_session)}
it "sends email when user is offline and subscribes to emails" do
session.creator = sender
session.save!
@session.creator = @sender
@session.save!
FactoryGirl.create(:friendship, :user => receiver, :friend => sender)
FactoryGirl.create(:friendship, :user => sender, :friend => receiver)
FactoryGirl.create(:friendship, :user => @receiver, :friend => @sender)
FactoryGirl.create(:friendship, :user => @sender, :friend => @receiver)
calls = count_publish_to_user_calls
Notification.send_musician_session_join(session, sender)
Notification.send_musician_session_join(@session, @sender)
UserMailer.deliveries.length.should == 1
calls[:count].should == 0
end
it "does not send email when user is offline and opts out of emails" do
session.creator = sender
session.save!
FactoryGirl.create(:friendship, :user => @receiver, :friend => @sender)
FactoryGirl.create(:friendship, :user => @sender, :friend => @receiver)
FactoryGirl.create(:friendship, :user => receiver, :friend => sender)
FactoryGirl.create(:friendship, :user => sender, :friend => receiver)
@session.creator = @sender
@session.save!
receiver.subscribe_email = false
receiver.save!
@receiver.subscribe_email = false
@receiver.save!
calls = count_publish_to_user_calls
Notification.send_musician_session_join(session, sender)
Notification.send_musician_session_join(@session, @sender)
UserMailer.deliveries.length.should == 0
calls[:count].should == 0
end
end
describe "send musician recording saved join" do
let(:receiver) {FactoryGirl.create(:user)}
let(:recording) {FactoryGirl.create(:recording)}
describe "send musician recording saved" do
it "sends email when user is offline and subscribes to emails" do
FactoryGirl.create(:friendship, :user => receiver, :friend => recording.owner)
FactoryGirl.create(:friendship, :user => recording.owner, :friend => receiver)
FactoryGirl.create(:friendship, :user => @receiver, :friend => @recording.owner)
FactoryGirl.create(:friendship, :user => @recording.owner, :friend => @receiver)
calls = count_publish_to_user_calls
Notification.send_musician_recording_saved(recording)
Notification.send_musician_recording_saved(@recording)
UserMailer.deliveries.length.should == 1
calls[:count].should == 0
end
it "does not send email when user is offline and opts out of emails" do
FactoryGirl.create(:friendship, :user => receiver, :friend => recording.owner)
FactoryGirl.create(:friendship, :user => recording.owner, :friend => receiver)
FactoryGirl.create(:friendship, :user => @receiver, :friend => @recording.owner)
FactoryGirl.create(:friendship, :user => @recording.owner, :friend => @receiver)
receiver.subscribe_email = false
receiver.save!
@receiver.subscribe_email = false
@receiver.save!
calls = count_publish_to_user_calls
Notification.send_musician_recording_saved(recording)
Notification.send_musician_recording_saved(@recording)
UserMailer.deliveries.length.should == 0
calls[:count].should == 0
@ -227,67 +213,60 @@ describe Notification do
describe "send band session join" do
let(:follower) {FactoryGirl.create(:user)}
let(:sender) {FactoryGirl.create(:user)}
let(:band) {FactoryGirl.create(:band)}
let(:session) {FactoryGirl.create(:music_session)}
it "sends email when user is offline and subscribes to emails" do
session.creator = sender
session.save!
@session.creator = @sender
@session.save!
f = Follow.new
f.user_id = follower.id
f.followable_id = band.id
f.followable_id = @band.id
f.followable_type = "JamRuby::Band"
f.save!
calls = count_publish_to_user_calls
Notification.send_band_session_join(session, band)
Notification.send_band_session_join(@session, @band)
UserMailer.deliveries.length.should == 1
calls[:count].should == 0
end
it "does not send email when user is offline and opts out of emails" do
session.creator = sender
session.save!
@session.creator = @sender
@session.save!
follower.subscribe_email = false
follower.save!
f = Follow.new
f.user_id = follower.id
f.followable_id = band.id
f.followable_id = @band.id
f.followable_type = "JamRuby::Band"
f.save!
band.users << sender
calls = count_publish_to_user_calls
Notification.send_band_session_join(session, band)
Notification.send_band_session_join(@session, @band)
UserMailer.deliveries.length.should == 0
calls[:count].should == 0
end
end
describe "send band recording saved join" do
describe "send band recording saved" do
let(:follower) {FactoryGirl.create(:user)}
let(:band) {FactoryGirl.create(:band)}
let(:recording) {FactoryGirl.create(:recording)}
it "sends email when user is offline and subscribes to emails" do
f = Follow.new
f.user_id = follower.id
f.followable_id = band.id
f.followable_id = @band.id
f.followable_type = "JamRuby::Band"
f.save!
recording.band = band
recording.save!
@recording.band = @band
@recording.save!
calls = count_publish_to_user_calls
Notification.send_band_recording_saved(recording)
Notification.send_band_recording_saved(@recording)
UserMailer.deliveries.length.should == 1
calls[:count].should == 0
@ -299,18 +278,18 @@ describe Notification do
f = Follow.new
f.user_id = follower.id
f.followable_id = band.id
f.followable_id = @band.id
f.followable_type = "JamRuby::Band"
f.save!
recording.band = band
recording.save!
@recording.band = @band
@recording.save!
follower.subscribe_email = false
follower.save!
calls = count_publish_to_user_calls
Notification.send_band_recording_saved(recording)
Notification.send_band_recording_saved(@recording)
UserMailer.deliveries.length.should == 0
calls[:count].should == 0
@ -318,44 +297,108 @@ describe Notification do
end
describe "send band invitation" do
end
describe "send band invitation accepted" do
end
describe "send scheduled session invitation" do
let(:receiver) {FactoryGirl.create(:user)}
let(:sender) {FactoryGirl.create(:user)}
let(:session) {FactoryGirl.create(:music_session)}
it "sends email when user is offline and subscribes to emails" do
session.creator = sender
session.save!
band_invitation = BandInvitation.new
band_invitation.receiver = @receiver
band_invitation.sender = @sender
band_invitation.band = @band
band_invitation.save!
@band.users << @sender
calls = count_publish_to_user_calls
notification = Notification.send_scheduled_session_invitation(session, receiver)
notification = Notification.send_band_invitation(@band, band_invitation, @sender, @receiver)
UserMailer.deliveries.length.should == 1
calls[:count].should == 1
calls[:count].should == 0
end
it "does not send email when user is offline and opts out of emails" do
session.creator = sender
session.save!
band_invitation = BandInvitation.new
band_invitation.receiver = @receiver
band_invitation.sender = @sender
band_invitation.band = @band
band_invitation.save!
receiver.subscribe_email = false
receiver.save!
@band.users << @sender
@receiver.subscribe_email = false
@receiver.save!
calls = count_publish_to_user_calls
notification = Notification.send_scheduled_session_invitation(session, receiver)
notification = Notification.send_band_invitation(@band, band_invitation, @sender, @receiver)
UserMailer.deliveries.length.should == 0
calls[:count].should == 1
calls[:count].should == 0
end
end
describe "send band invitation accepted" do
it "sends email when user is offline and subscribes to emails" do
band_invitation = BandInvitation.new
band_invitation.receiver = @receiver
band_invitation.sender = @sender
band_invitation.band = @band
band_invitation.save!
@band.users << @sender
calls = count_publish_to_user_calls
notification = Notification.send_band_invitation_accepted(@band, band_invitation, @receiver, @sender)
UserMailer.deliveries.length.should == 1
calls[:count].should == 0
end
it "does not send email when user is offline and opts out of emails" do
band_invitation = BandInvitation.new
band_invitation.receiver = @receiver
band_invitation.sender = @sender
band_invitation.band = @band
band_invitation.save!
@band.users << @sender
@sender.subscribe_email = false
@sender.save!
calls = count_publish_to_user_calls
notification = Notification.send_band_invitation_accepted(@band, band_invitation, @receiver, @sender)
UserMailer.deliveries.length.should == 0
calls[:count].should == 0
end
end
describe "send scheduled session invitation" do
it "sends email when user is offline and subscribes to emails" do
@session.creator = @sender
@session.save!
calls = count_publish_to_user_calls
notification = Notification.send_scheduled_session_invitation(@session, @receiver)
UserMailer.deliveries.length.should == 1
calls[:count].should == 0
end
it "does not send email when user is offline and opts out of emails" do
@session.creator = @sender
@session.save!
@receiver.subscribe_email = false
@receiver.save!
calls = count_publish_to_user_calls
notification = Notification.send_scheduled_session_invitation(@session, @receiver)
UserMailer.deliveries.length.should == 0
calls[:count].should == 0
end
it "sends no notification if session is nil" do
calls = count_publish_to_user_calls
notification = Notification.send_scheduled_session_invitation(nil, sender)
notification = Notification.send_scheduled_session_invitation(nil, @sender)
UserMailer.deliveries.length.should == 0
calls[:count].should == 0
@ -363,7 +406,7 @@ describe Notification do
it "sends no notification if user is nil" do
calls = count_publish_to_user_calls
notification = Notification.send_scheduled_session_invitation(session, nil)
notification = Notification.send_scheduled_session_invitation(@session, nil)
UserMailer.deliveries.length.should == 0
calls[:count].should == 0
@ -371,38 +414,34 @@ describe Notification do
end
describe "send scheduled session rsvp" do
let(:receiver) {FactoryGirl.create(:user)}
let(:sender) {FactoryGirl.create(:user)}
let(:session) {FactoryGirl.create(:music_session)}
it "sends email when user is offline and subscribes to emails" do
session.creator = sender
session.save!
@session.creator = @sender
@session.save!
calls = count_publish_to_user_calls
notification = Notification.send_scheduled_session_rsvp(session, receiver, nil)
notification = Notification.send_scheduled_session_rsvp(@session, @receiver, nil)
UserMailer.deliveries.length.should == 1
calls[:count].should == 1
calls[:count].should == 0
end
it "does not send email when user is offline and opts out of emails" do
session.creator = sender
session.save!
@session.creator = @sender
@session.save!
session.creator.subscribe_email = false
session.creator.save!
@session.creator.subscribe_email = false
@session.creator.save!
calls = count_publish_to_user_calls
notification = Notification.send_scheduled_session_rsvp(session, receiver, nil)
notification = Notification.send_scheduled_session_rsvp(@session, @receiver, nil)
UserMailer.deliveries.length.should == 0
calls[:count].should == 1
calls[:count].should == 0
end
it "sends no notification if session is nil" do
calls = count_publish_to_user_calls
notification = Notification.send_scheduled_session_rsvp(nil, receiver, nil)
notification = Notification.send_scheduled_session_rsvp(nil, @receiver, nil)
UserMailer.deliveries.length.should == 0
calls[:count].should == 0
@ -410,7 +449,7 @@ describe Notification do
it "sends no notification if user is nil" do
calls = count_publish_to_user_calls
notification = Notification.send_scheduled_session_rsvp(session, nil, nil)
notification = Notification.send_scheduled_session_rsvp(@session, nil, nil)
UserMailer.deliveries.length.should == 0
calls[:count].should == 0
@ -418,38 +457,34 @@ describe Notification do
end
describe "send scheduled session rsvp approved" do
let(:receiver) {FactoryGirl.create(:user)}
let(:sender) {FactoryGirl.create(:user)}
let(:session) {FactoryGirl.create(:music_session)}
it "sends email when user is offline and subscribes to emails" do
session.creator = sender
session.save!
@session.creator = @sender
@session.save!
calls = count_publish_to_user_calls
notification = Notification.send_scheduled_session_rsvp_approved(session, receiver, nil)
notification = Notification.send_scheduled_session_rsvp_approved(@session, @receiver, nil)
UserMailer.deliveries.length.should == 1
calls[:count].should == 1
calls[:count].should == 0
end
it "does not send email when user is offline and opts out of emails" do
session.creator = sender
session.save!
@session.creator = @sender
@session.save!
receiver.subscribe_email = false
receiver.save!
@receiver.subscribe_email = false
@receiver.save!
calls = count_publish_to_user_calls
notification = Notification.send_scheduled_session_rsvp_approved(session, receiver, nil)
notification = Notification.send_scheduled_session_rsvp_approved(@session, @receiver, nil)
UserMailer.deliveries.length.should == 0
calls[:count].should == 1
calls[:count].should == 0
end
it "sends no notification if session is nil" do
calls = count_publish_to_user_calls
notification = Notification.send_scheduled_session_rsvp_approved(nil, receiver, nil)
notification = Notification.send_scheduled_session_rsvp_approved(nil, @receiver, nil)
UserMailer.deliveries.length.should == 0
calls[:count].should == 0
@ -457,7 +492,7 @@ describe Notification do
it "sends no notification if user is nil" do
calls = count_publish_to_user_calls
notification = Notification.send_scheduled_session_rsvp_approved(session, nil, nil)
notification = Notification.send_scheduled_session_rsvp_approved(@session, nil, nil)
UserMailer.deliveries.length.should == 0
calls[:count].should == 0
@ -465,38 +500,34 @@ describe Notification do
end
describe "send scheduled session rsvp cancellation" do
let(:receiver) {FactoryGirl.create(:user)}
let(:sender) {FactoryGirl.create(:user)}
let(:session) {FactoryGirl.create(:music_session)}
it "sends email when user is offline and subscribes to emails" do
session.creator = sender
session.save!
@session.creator = @sender
@session.save!
calls = count_publish_to_user_calls
notification = Notification.send_scheduled_session_rsvp_cancelled(session, receiver)
notification = Notification.send_scheduled_session_rsvp_cancelled(@session, @receiver)
UserMailer.deliveries.length.should == 1
calls[:count].should == 1
calls[:count].should == 0
end
it "does not send email when user is offline and opts out of emails" do
session.creator = sender
session.save!
@session.creator = @sender
@session.save!
session.creator.subscribe_email = false
session.creator.save!
@session.creator.subscribe_email = false
@session.creator.save!
calls = count_publish_to_user_calls
notification = Notification.send_scheduled_session_rsvp_cancelled(session, receiver)
notification = Notification.send_scheduled_session_rsvp_cancelled(@session, @receiver)
UserMailer.deliveries.length.should == 0
calls[:count].should == 1
calls[:count].should == 0
end
it "sends no notification if session is nil" do
calls = count_publish_to_user_calls
notification = Notification.send_scheduled_session_rsvp_cancelled(nil, receiver)
notification = Notification.send_scheduled_session_rsvp_cancelled(nil, @receiver)
UserMailer.deliveries.length.should == 0
calls[:count].should == 0
@ -504,7 +535,7 @@ describe Notification do
it "sends no notification if user is nil" do
calls = count_publish_to_user_calls
notification = Notification.send_scheduled_session_rsvp_cancelled(session, nil)
notification = Notification.send_scheduled_session_rsvp_cancelled(@session, nil)
UserMailer.deliveries.length.should == 0
calls[:count].should == 0
@ -512,38 +543,34 @@ describe Notification do
end
describe "send scheduled session rsvp cancellation by organizer" do
let(:receiver) {FactoryGirl.create(:user)}
let(:sender) {FactoryGirl.create(:user)}
let(:session) {FactoryGirl.create(:music_session)}
it "sends email when user is offline and subscribes to emails" do
session.creator = sender
session.save!
@session.creator = @sender
@session.save!
calls = count_publish_to_user_calls
notification = Notification.send_scheduled_session_rsvp_cancelled_org(session, receiver)
notification = Notification.send_scheduled_session_rsvp_cancelled_org(@session, @receiver)
UserMailer.deliveries.length.should == 1
calls[:count].should == 1
calls[:count].should == 0
end
it "does not send email when user is offline and opts out of emails" do
session.creator = sender
session.save!
@session.creator = @sender
@session.save!
receiver.subscribe_email = false
receiver.save!
@receiver.subscribe_email = false
@receiver.save!
calls = count_publish_to_user_calls
notification = Notification.send_scheduled_session_rsvp_cancelled_org(session, receiver)
notification = Notification.send_scheduled_session_rsvp_cancelled_org(@session, @receiver)
UserMailer.deliveries.length.should == 0
calls[:count].should == 1
calls[:count].should == 0
end
it "sends no notification if session is nil" do
calls = count_publish_to_user_calls
notification = Notification.send_scheduled_session_rsvp_cancelled_org(nil, receiver)
notification = Notification.send_scheduled_session_rsvp_cancelled_org(nil, @receiver)
UserMailer.deliveries.length.should == 0
calls[:count].should == 0
@ -551,7 +578,7 @@ describe Notification do
it "sends no notification if user is nil" do
calls = count_publish_to_user_calls
notification = Notification.send_scheduled_session_rsvp_cancelled_org(session, nil)
notification = Notification.send_scheduled_session_rsvp_cancelled_org(@session, nil)
UserMailer.deliveries.length.should == 0
calls[:count].should == 0
@ -559,10 +586,6 @@ describe Notification do
end
describe "send scheduled session cancellation" do
let(:receiver) {FactoryGirl.create(:user)}
let(:sender) {FactoryGirl.create(:user)}
let(:session) {FactoryGirl.create(:music_session)}
# it "sends email when user is offline and subscribes to emails" do
# session.creator = sender
# session.save!
@ -598,7 +621,7 @@ describe Notification do
it "sends no notification if there are no rsvp requests" do
calls = count_publish_to_user_calls
notification = Notification.send_scheduled_session_cancelled(session)
notification = Notification.send_scheduled_session_cancelled(@session)
UserMailer.deliveries.length.should == 0
calls[:count].should == 0
@ -606,10 +629,6 @@ describe Notification do
end
describe "send scheduled session rescheduled" do
let(:receiver) {FactoryGirl.create(:user)}
let(:sender) {FactoryGirl.create(:user)}
let(:session) {FactoryGirl.create(:music_session)}
# it "sends email when user is offline and subscribes to emails" do
# session.creator = sender
# session.save!
@ -645,7 +664,7 @@ describe Notification do
it "sends no notification if there are no rsvp requests" do
calls = count_publish_to_user_calls
notification = Notification.send_scheduled_session_rescheduled(session)
notification = Notification.send_scheduled_session_rescheduled(@session)
UserMailer.deliveries.length.should == 0
calls[:count].should == 0
@ -653,10 +672,6 @@ describe Notification do
end
describe "send scheduled session reminder" do
let(:receiver) {FactoryGirl.create(:user)}
let(:sender) {FactoryGirl.create(:user)}
let(:session) {FactoryGirl.create(:music_session)}
# it "sends email when user is offline and subscribes to emails" do
# session.creator = sender
# session.save!
@ -692,7 +707,7 @@ describe Notification do
it "sends no notification if there are no rsvp requests" do
calls = count_publish_to_user_calls
notification = Notification.send_scheduled_session_reminder(session)
notification = Notification.send_scheduled_session_reminder(@session)
UserMailer.deliveries.length.should == 0
calls[:count].should == 0
@ -700,10 +715,6 @@ describe Notification do
end
describe "send scheduled session comment" do
let(:receiver) {FactoryGirl.create(:user)}
let(:sender) {FactoryGirl.create(:user)}
let(:session) {FactoryGirl.create(:music_session)}
# it "sends email when user is offline and subscribes to emails" do
# session.creator = sender
# session.save!
@ -731,7 +742,7 @@ describe Notification do
it "sends no notification if session is nil" do
calls = count_publish_to_user_calls
notification = Notification.send_scheduled_session_comment(nil, sender, 'when are we playing?')
notification = Notification.send_scheduled_session_comment(nil, @sender, 'when are we playing?')
UserMailer.deliveries.length.should == 0
calls[:count].should == 0
@ -739,7 +750,7 @@ describe Notification do
it "sends no notification if user is nil" do
calls = count_publish_to_user_calls
notification = Notification.send_scheduled_session_comment(session, nil, 'test')
notification = Notification.send_scheduled_session_comment(@session, nil, 'test')
UserMailer.deliveries.length.should == 0
calls[:count].should == 0
@ -747,7 +758,7 @@ describe Notification do
it "sends no notification if comment is empty" do
calls = count_publish_to_user_calls
notification = Notification.send_scheduled_session_comment(session, sender, '')
notification = Notification.send_scheduled_session_comment(@session, @sender, '')
UserMailer.deliveries.length.should == 0
calls[:count].should == 0
@ -756,11 +767,9 @@ describe Notification do
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 = Notification.send_text_message(message, @sender, @receiver)
notification.errors.any?.should be_false
UserMailer.deliveries.length.should == 1
@ -769,49 +778,43 @@ describe Notification do
it "success when online" do
receiver = FactoryGirl.create(:user)
receiver_connection = FactoryGirl.create(:connection, user: receiver)
sender = FactoryGirl.create(:user)
receiver_connection = FactoryGirl.create(:connection, user: @receiver)
message = "Just a test message!"
calls = count_publish_to_user_calls
notification = Notification.send_text_message(message, sender, receiver)
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.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)
receiver_connection = FactoryGirl.create(:connection, user: @receiver)
message = "0" * 203 # 200 is clip size
calls = count_publish_to_user_calls
notification = Notification.send_text_message(message, sender, receiver)
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.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 = Notification.send_text_message(message, @sender, @receiver)
notification.errors.any?.should be_true
notification.errors[:message].should == ['cannot contain profanity']
@ -820,11 +823,9 @@ describe Notification do
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 = Notification.send_text_message(message, @sender, @sender)
notification.errors.any?.should be_true
notification.errors[:target_user].should == [ValidationMessages::DIFFERENT_SOURCE_TARGET]
@ -833,11 +834,9 @@ describe Notification do
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 = Notification.send_text_message(message, @sender, @receiver)
notification.errors.any?.should be_true
notification.errors[:message].should == ['is too short (minimum is 1 characters)']