event_spec.rb 5.32 KB
Newer Older
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
1 2
require 'spec_helper'

Douwe Maan's avatar
Douwe Maan committed
3
describe Event, models: true do
4
  describe "Associations" do
5 6
    it { is_expected.to belong_to(:project) }
    it { is_expected.to belong_to(:target) }
7 8
  end

9
  describe "Respond to" do
10 11 12 13 14
    it { is_expected.to respond_to(:author_name) }
    it { is_expected.to respond_to(:author_email) }
    it { is_expected.to respond_to(:issue_title) }
    it { is_expected.to respond_to(:merge_request_title) }
    it { is_expected.to respond_to(:commits) }
15 16
  end

17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
  describe 'Callbacks' do
    describe 'after_create :reset_project_activity' do
      let(:project) { create(:project) }

      context "project's last activity was less than 5 minutes ago" do
        it 'does not update project.last_activity_at if it has been touched less than 5 minutes ago' do
          create_event(project, project.owner)
          project.update_column(:last_activity_at, 5.minutes.ago)
          project_last_activity_at = project.last_activity_at

          create_event(project, project.owner)

          expect(project.last_activity_at).to eq(project_last_activity_at)
        end
      end
    end
  end

randx's avatar
randx committed
35 36
  describe "Push event" do
    before do
37
      project = create(:project)
38
      @user = project.owner
39
      @event = create_event(project, @user)
40 41
    end

42
    it { expect(@event.push?).to be_truthy }
43
    it { expect(@event.visible_to_user?).to be_truthy }
44 45 46
    it { expect(@event.tag?).to be_falsey }
    it { expect(@event.branch_name).to eq("master") }
    it { expect(@event.author).to eq(@user) }
47
  end
48

49
  describe '#visible_to_user?' do
50 51 52
    let(:project) { create(:empty_project, :public) }
    let(:non_member) { create(:user) }
    let(:member)  { create(:user) }
53
    let(:guest)  { create(:user) }
54 55 56 57 58 59 60 61 62 63 64
    let(:author) { create(:author) }
    let(:assignee) { create(:user) }
    let(:admin) { create(:admin) }
    let(:issue) { create(:issue, project: project, author: author, assignee: assignee) }
    let(:confidential_issue) { create(:issue, :confidential, project: project, author: author, assignee: assignee) }
    let(:note_on_issue) { create(:note_on_issue, noteable: issue, project: project) }
    let(:note_on_confidential_issue) { create(:note_on_issue, noteable: confidential_issue, project: project) }
    let(:event) { Event.new(project: project, target: target, author_id: author.id) }

    before do
      project.team << [member, :developer]
65
      project.team << [guest, :guest]
66
    end
67

68
    context 'issue event' do
69
      context 'for non confidential issues' do
70
        let(:target) { issue }
71

72 73 74 75
        it { expect(event.visible_to_user?(non_member)).to eq true }
        it { expect(event.visible_to_user?(author)).to eq true }
        it { expect(event.visible_to_user?(assignee)).to eq true }
        it { expect(event.visible_to_user?(member)).to eq true }
76
        it { expect(event.visible_to_user?(guest)).to eq true }
77
        it { expect(event.visible_to_user?(admin)).to eq true }
78 79 80
      end

      context 'for confidential issues' do
81 82
        let(:target) { confidential_issue }

83 84 85 86
        it { expect(event.visible_to_user?(non_member)).to eq false }
        it { expect(event.visible_to_user?(author)).to eq true }
        it { expect(event.visible_to_user?(assignee)).to eq true }
        it { expect(event.visible_to_user?(member)).to eq true }
87
        it { expect(event.visible_to_user?(guest)).to eq false }
88
        it { expect(event.visible_to_user?(admin)).to eq true }
89 90 91 92 93 94 95
      end
    end

    context 'note event' do
      context 'on non confidential issues' do
        let(:target) { note_on_issue }

96 97 98 99
        it { expect(event.visible_to_user?(non_member)).to eq true }
        it { expect(event.visible_to_user?(author)).to eq true }
        it { expect(event.visible_to_user?(assignee)).to eq true }
        it { expect(event.visible_to_user?(member)).to eq true }
100
        it { expect(event.visible_to_user?(guest)).to eq true }
101
        it { expect(event.visible_to_user?(admin)).to eq true }
102 103 104 105
      end

      context 'on confidential issues' do
        let(:target) { note_on_confidential_issue }
106

107 108 109 110
        it { expect(event.visible_to_user?(non_member)).to eq false }
        it { expect(event.visible_to_user?(author)).to eq true }
        it { expect(event.visible_to_user?(assignee)).to eq true }
        it { expect(event.visible_to_user?(member)).to eq true }
111
        it { expect(event.visible_to_user?(guest)).to eq false }
112
        it { expect(event.visible_to_user?(admin)).to eq true }
113 114 115 116
      end
    end
  end

Yorick Peterse's avatar
Yorick Peterse committed
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
  describe '.limit_recent' do
    let!(:event1) { create(:closed_issue_event) }
    let!(:event2) { create(:closed_issue_event) }

    describe 'without an explicit limit' do
      subject { Event.limit_recent }

      it { is_expected.to eq([event2, event1]) }
    end

    describe 'with an explicit limit' do
      subject { Event.limit_recent(1) }

      it { is_expected.to eq([event2]) }
    end
  end
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156

  def create_event(project, user, attrs = {})
    data = {
      before: Gitlab::Git::BLANK_SHA,
      after: "0220c11b9a3e6c69dc8fd35321254ca9a7b98f7e",
      ref: "refs/heads/master",
      user_id: user.id,
      user_name: user.name,
      repository: {
        name: project.name,
        url: "localhost/rubinius",
        description: "",
        homepage: "localhost/rubinius",
        private: true
      }
    }

    Event.create({
      project: project,
      action: Event::PUSHED,
      data: data,
      author_id: user.id
    }.merge(attrs))
  end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
157
end