Skip to content
Extraits de code Groupes Projets
notification_spec.rb 3,04 ko
Newer Older
  • Learn to ignore specific revisions
  • maxwell's avatar
    maxwell a validé
    #   Copyright (c) 2010, Diaspora Inc.  This file is
    #   licensed under the Affero General Public License version 3 or later.  See
    #   the COPYRIGHT file.
    
    require 'spec_helper'
    
    
    Raphael's avatar
    Raphael a validé
    describe Notification do
    
    maxwell's avatar
    maxwell a validé
      before do
        @sm = Factory(:status_message)
        @person = Factory(:person)
    
        @user = alice
        @user2 = eve
    
    maxwell's avatar
    maxwell a validé
        @aspect  = @user.aspects.create(:name => "dudes")
    
    Raphael's avatar
    Raphael a validé
        @opts = {:target_id => @sm.id,
          :target_type => @sm.class.name,
    
          :actors => [@person],
    
    Raphael's avatar
    Raphael a validé
          :recipient_id => @user.id}
    
    maxwell's avatar
    maxwell a validé
        @note = Notification.new(@opts)
    
        @note.actors =[ @person]
    
    zhitomirskiyi's avatar
    zhitomirskiyi a validé
      it 'destoys the associated notification_actor' do
        @note.save
    
    zhitomirskiyi's avatar
    zhitomirskiyi a validé
        lambda{@note.destroy}.should change(NotificationActor, :count).by(-1)  
    
    zhitomirskiyi's avatar
    zhitomirskiyi a validé
      end
    
    
    maxwell's avatar
    maxwell a validé
      describe '.for' do
    
    maxwell's avatar
    maxwell a validé
        it 'returns all of a users notifications' do
    
          @opts.delete(:recipient_id)
    
    Raphael's avatar
    Raphael a validé
          Notification.create(@opts.merge(:recipient_id => user2.id))
    
    maxwell's avatar
    maxwell a validé
    
          Notification.for(@user).count.should == 4
        end
    
    maxwell's avatar
    maxwell a validé
      end
    
    maxwell's avatar
    maxwell a validé
    
      describe '.notify' do
    
    Raphael's avatar
    Raphael a validé
        it 'does not call Notification.create if the object does not have a notification_type' do
    
          Notification.should_not_receive(:make_notificatin)
    
    maxwell's avatar
    maxwell a validé
          Notification.notify(@user, @sm, @person)
        end
    
        context 'with a request' do
          before do
            @request = Request.diaspora_initialize(:from => @user.person, :to => @user2.person, :into => @aspect)
          end
          it 'calls Notification.create if the object has a notification_type' do
    
            Notification.should_receive(:make_notification).once
    
            Notification.notify(@user, @request, @person)
          end
    
          it 'sockets to the recipient' do
            opts = {:target_id => @request.id,
    
              :target_type => "Request",
              :action => @request.notification_type(@user, @person),
    
    zhitomirskiyi's avatar
    zhitomirskiyi a validé
              :actors => [@person],
    
              :recipient_id => @user.id}
    
            n = Notification.create(opts)
    
    zhitomirskiyi's avatar
    zhitomirskiyi a validé
            Notification.stub!(:make_notification).and_return n
    
            n.should_receive(:socket_to_user).once
            Notification.notify(@user, @request, @person)
          end
    
          describe '#emails_the_user' do
            it 'calls mail' do
              opts = {
                :action => "new_request",
    
    zhitomirskiyi's avatar
    zhitomirskiyi a validé
                :actors => [@person],
    
                :recipient_id => @user.id}
    
                n = Notification.new(opts)
    
                n.stub!(:recipient).and_return @user
    
                @user.should_receive(:mail)
    
                n.email_the_user("mock", @person)
    
    
          it "updates the notification with a more people if one already exists" do
            @user3 = bob
            sm = @user3.post(:status_message, :message => "comment!", :to => :all)
    
    zhitomirskiyi's avatar
    zhitomirskiyi a validé
            Postzord::Receiver.new(@user3, :person => @user2.person, :object => @user2.comment("hey", :on => sm)).receive_object
            Postzord::Receiver.new(@user3, :person => @user.person, :object => @user.comment("hey", :on => sm)).receive_object
            Notification.where(:recipient_id => @user3.id,:target_id => sm.id).first.actors.count.should == 2
    
    maxwell's avatar
    maxwell a validé
      end