Skip to content
Extraits de code Groupes Projets
notifications_controller_spec.rb 6,49 ko
Newer Older
  • Learn to ignore specific revisions
  • danielgrippi's avatar
    danielgrippi a validé
    #   Copyright (c) 2010-2011, Diaspora Inc.  This file is
    
    maxwell's avatar
    maxwell a validé
    #   licensed under the Affero General Public License version 3 or later.  See
    #   the COPYRIGHT file.
    
    require 'spec_helper'
    
    
    describe NotificationsController, :type => :controller do
    
    maxwell's avatar
    maxwell a validé
      before do
    
        sign_in :user, alice
    
    maxwell's avatar
    maxwell a validé
      end
    
    
    maxwell's avatar
    maxwell a validé
      describe '#update' do
    
        it 'marks a notification as read if it gets no other information' do
    
    khall's avatar
    khall a validé
          note = FactoryGirl.create(:notification)
    
          expect(Notification).to receive( :where ).and_return( [note] )
          expect(note).to receive( :set_read_state ).with( true )
    
          get :update, "id" => note.id, :format => :json
    
    khall's avatar
    khall a validé
    
    
        it 'marks a notification as read if it is told to' do
    
    khall's avatar
    khall a validé
          note = FactoryGirl.create(:notification)
    
          expect(Notification).to receive( :where ).and_return( [note] )
          expect(note).to receive( :set_read_state ).with( true )
    
          get :update, "id" => note.id, :set_unread => "false", :format => :json
    
        end
    
        it 'marks a notification as unread if it is told to' do
    
    khall's avatar
    khall a validé
          note = FactoryGirl.create(:notification)
    
          expect(Notification).to receive( :where ).and_return( [note] )
          expect(note).to receive( :set_read_state ).with( false )
    
          get :update, "id" => note.id, :set_unread => "true", :format => :json
    
    maxwell's avatar
    maxwell a validé
        end
    
    maxwell's avatar
    maxwell a validé
        it 'only lets you read your own notifications' do
    
    maxwell's avatar
    maxwell a validé
    
    
    Jonne Haß's avatar
    Jonne Haß a validé
          FactoryGirl.create(:notification, :recipient => alice)
          note = FactoryGirl.create(:notification, :recipient => user2)
    
    maxwell's avatar
    maxwell a validé
    
    
          get :update, "id" => note.id, :set_unread => "false", :format => :json
    
    maxwell's avatar
    maxwell a validé
    
    
          expect(Notification.find(note.id).unread).to eq(true)
    
    maxwell's avatar
    maxwell a validé
        end
      end
    
      describe '#index' do
    
    Jonne Haß's avatar
    Jonne Haß a validé
          @post = FactoryGirl.create(:status_message)
          FactoryGirl.create(:notification, :recipient => alice, :target => @post)
    
        it 'succeeds' do
          get :index
    
          expect(response).to be_success
          expect(assigns[:notifications].count).to eq(1)
    
        it 'succeeds for notification dropdown' do
          get :index, :format => :json
    
          expect(response).to be_success
          expect(response.body).to match(/note_html/)
    
        it 'succeeds on mobile' do
          get :index, :format => :mobile
    
          expect(response).to be_success
    
        it 'paginates the notifications' do
    
    Jonne Haß's avatar
    Jonne Haß a validé
          25.times { FactoryGirl.create(:notification, :recipient => alice, :target => @post) }
    
          expect(assigns[:notifications].count).to eq(25)
    
          expect(assigns[:notifications].count).to eq(1)
    
        it "supports a limit per_page parameter" do
    
          2.times { FactoryGirl.create(:notification, :recipient => alice, :target => @post) }
          get :index, "per_page" => 2
          expect(assigns[:notifications].count).to eq(2)
    
    
        describe "special case for start sharing notifications" do
          it "should not provide a contacts menu for standard notifications" do
    
            FactoryGirl.create(:notification, :recipient => alice, :target => @post)
    
            get :index, "per_page" => 5
    
    
            expect(Nokogiri(response.body).css('.aspect_membership')).to be_empty
    
          it "should provide a contacts menu for start sharing notifications" do
            eve.share_with(alice.person, eve.aspects.first)
            get :index, "per_page" => 5
    
    
            expect(Nokogiri(response.body).css('.aspect_membership')).not_to be_empty
    
    
          it 'succeeds on mobile' do
            eve.share_with(alice.person, eve.aspects.first)
            get :index, :format => :mobile
            expect(response).to be_success
          end
    
        end
    
        describe "filter notifications" do
          it "supports filtering by notification type" do
    
            FactoryGirl.create(:notification, :recipient => alice, :type => "Notifications::StartedSharing")
    
            get :index, "type" => "started_sharing"
    
            expect(assigns[:notifications].count).to eq(1)
    
          end
    
          it "supports filtering by read/unread" do
    
            FactoryGirl.create(:notification, :recipient => alice, :target => @post)
    
            FactoryGirl.create(:notification, :recipient => alice, :target => @post)
    
            get :index, "show" => "unread"
    
            expect(assigns[:notifications].count).to eq(1)
    
    
      describe "#read_all" do
        it 'marks all notifications as read' do
          request.env["HTTP_REFERER"] = "I wish I were spelled right"
    
          FactoryGirl.create(:notification, :recipient => alice, :target => @post)
          FactoryGirl.create(:notification, :recipient => alice, :target => @post)
    
          expect(Notification.where(:unread => true).count).to eq(2)
    
          expect(Notification.where(:unread => true).count).to eq(0)
    
        end
        it 'marks all notifications in the current filter as read' do
          request.env["HTTP_REFERER"] = "I wish I were spelled right"
    
          FactoryGirl.create(:notification, :recipient => alice, :target => @post)
          FactoryGirl.create(:notification, :recipient => alice, :type => "Notifications::StartedSharing")
    
          expect(Notification.where(:unread => true).count).to eq(2)
    
          get :read_all, "type" => "started_sharing"
    
          expect(Notification.where(:unread => true).count).to eq(1)
    
        end
        it "should redirect back in the html version if it has > 0 notifications" do
    
          FactoryGirl.create(:notification, :recipient => alice, :type => "Notifications::StartedSharing")
          get :read_all, :format => :html, "type" => "liked"
    
          expect(response).to redirect_to(notifications_path)
    
        end
        it "should redirect back in the mobile version if it has > 0 notifications" do
    
          FactoryGirl.create(:notification, :recipient => alice, :type => "Notifications::StartedSharing")
          get :read_all, :format => :mobile, "type" => "liked"
    
          expect(response).to redirect_to(notifications_path)
    
        end
        it "should redirect to stream in the html version if it has 0 notifications" do
    
          FactoryGirl.create(:notification, :recipient => alice, :type => "Notifications::StartedSharing")
          get :read_all, :format => :html, "type" => "started_sharing"
    
          expect(response).to redirect_to(stream_path)
    
        end
        it "should redirect back in the mobile version if it has 0 notifications" do
    
          FactoryGirl.create(:notification, :recipient => alice, :type => "Notifications::StartedSharing")
          get :read_all, :format => :mobile, "type" => "started_sharing"
    
          expect(response).to redirect_to(stream_path)
    
        end
        it "should return a dummy value in the json version" do
    
          FactoryGirl.create(:notification, :recipient => alice, :target => @post)
    
          get :read_all, :format => :json
    
          expect(response).not_to be_redirect