note_observer_spec.rb 3.65 KB
Newer Older
1 2 3 4 5
require 'spec_helper'

describe NoteObserver do
  subject { NoteObserver.instance }

6 7 8
  let(:team_without_author) { (1..2).map { |n| double :user, id: n } }
  let(:delivery_success) { double deliver: true }

9
  describe '#after_create' do
10
    let(:note) { double :note }
11 12 13 14 15 16 17 18 19

    it 'is called after a note is created' do
      subject.should_receive :after_create

      Note.observers.enable :note_observer do
        Factory.create(:note)
      end
    end

20 21 22 23 24 25 26 27 28 29
    it 'sends out notifications' do
      subject.should_receive(:send_notify_mails).with(note)

      subject.after_create(note)
    end
  end

  describe "#send_notify_mails" do
    let(:note) { double :note, notify: false, notify_author: false }

30 31
    it 'notifies team of new note when flagged to notify' do
      note.stub(:notify).and_return(true)
32
      subject.should_receive(:notify_team).with(note)
33 34 35

      subject.after_create(note)
    end
36

37
    it 'does not notify team of new note when not flagged to notify' do
38
      subject.should_not_receive(:notify_team).with(note)
39 40 41

      subject.after_create(note)
    end
42

43 44 45 46 47
    it 'notifies the author of a commit when flagged to notify the author' do
      note.stub(:notify_author).and_return(true)
      note.stub(:id).and_return(42)
      author = double :user, id: 1
      note.stub(:commit_author).and_return(author)
48
      Notify.should_receive(:note_commit_email).and_return(delivery_success)
49 50 51

      subject.after_create(note)
    end
52

53 54 55 56 57
    it 'does not notify the author of a commit when not flagged to notify the author' do
      Notify.should_not_receive(:note_commit_email)

      subject.after_create(note)
    end
58

59 60 61 62 63
    it 'does nothing if no notify flags are set' do
      subject.after_create(note).should be_nil
    end
  end

64
  describe '#notify_team' do
65 66 67 68 69 70 71 72 73
    let(:note) { double :note, id: 1 }

    before :each do
      subject.stub(:team_without_note_author).with(note).and_return(team_without_author)
    end

    context 'notifies team of a new note on' do
      it 'a commit' do
        note.stub(:noteable_type).and_return('Commit')
74
        Notify.should_receive(:note_commit_email).twice.and_return(delivery_success)
75

76
        subject.send(:notify_team, note)
77
      end
78

79 80
      it 'an issue' do
        note.stub(:noteable_type).and_return('Issue')
81
        Notify.should_receive(:note_issue_email).twice.and_return(delivery_success)
82

83
        subject.send(:notify_team, note)
84
      end
85

86 87
      it 'a wiki page' do
        note.stub(:noteable_type).and_return('Wiki')
88
        Notify.should_receive(:note_wiki_email).twice.and_return(delivery_success)
89

90
        subject.send(:notify_team, note)
91
      end
92

93 94
      it 'a merge request' do
        note.stub(:noteable_type).and_return('MergeRequest')
95
        Notify.should_receive(:note_merge_request_email).twice.and_return(delivery_success)
96

97
        subject.send(:notify_team, note)
98
      end
99

100
      it 'a wall' do
101
        # Note: wall posts have #noteable_type of nil
102
        note.stub(:noteable_type).and_return(nil)
103
        Notify.should_receive(:note_wall_email).twice.and_return(delivery_success)
104

105
        subject.send(:notify_team, note)
106 107 108 109 110 111
      end
    end

    it 'does nothing for a new note on a snippet' do
        note.stub(:noteable_type).and_return('Snippet')

112
        subject.send(:notify_team, note).should be_nil
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
    end
  end


  describe '#team_without_note_author' do
    let(:author) { double :user, id: 4 }

    let(:users) { team_without_author + [author] }
    let(:project)  { double :project, users: users }
    let(:note) { double :note, project: project, author: author }

    it 'returns the projects user without the note author included' do
      subject.send(:team_without_note_author, note).should == team_without_author
    end
  end
end