summaryrefslogtreecommitdiff
path: root/spec/models/event_spec.rb
blob: 5fe442467385f9f75f0ab24dd669e92ad429ff18 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# == Schema Information
#
# Table name: events
#
#  id          :integer          not null, primary key
#  target_type :string(255)
#  target_id   :integer
#  title       :string(255)
#  data        :text
#  project_id  :integer
#  created_at  :datetime
#  updated_at  :datetime
#  action      :integer
#  author_id   :integer
#

require 'spec_helper'

describe Event, models: true do
  describe "Associations" do
    it { is_expected.to belong_to(:project) }
    it { is_expected.to belong_to(:target) }
  end

  describe "Respond to" do
    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) }
  end

  describe "Push event" do
    before do
      project = create(:project)
      @user = project.owner

      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 = Event.create(
        project: project,
        action: Event::PUSHED,
        data: data,
        author_id: @user.id
      )
    end

    it { expect(@event.push?).to be_truthy }
    it { expect(@event.proper?).to be_truthy }
    it { expect(@event.tag?).to be_falsey }
    it { expect(@event.branch_name).to eq("master") }
    it { expect(@event.author).to eq(@user) }
  end

  describe '#proper?' do
    context 'issue event' do
      let(:project) { create(:empty_project, :public) }
      let(:non_member) { create(:user) }
      let(:member)  { create(:user) }
      let(:author) { create(:author) }
      let(:assignee) { create(:user) }
      let(:admin) { create(:admin) }
      let(:event) { Event.new(project: project, action: Event::CREATED, target: issue, author_id: author.id) }

      before do
        project.team << [member, :developer]
      end

      context 'for non confidential issues' do
        let(:issue) { create(:issue, project: project, author: author, assignee: assignee) }

        it { expect(event.proper?(non_member)).to eq true }
        it { expect(event.proper?(author)).to eq true }
        it { expect(event.proper?(assignee)).to eq true }
        it { expect(event.proper?(member)).to eq true }
        it { expect(event.proper?(admin)).to eq true }
      end

      context 'for confidential issues' do
        let(:issue) { create(:issue, :confidential, project: project, author: author, assignee: assignee) }

        it { expect(event.proper?(non_member)).to eq false }
        it { expect(event.proper?(author)).to eq true }
        it { expect(event.proper?(assignee)).to eq true }
        it { expect(event.proper?(member)).to eq true }
        it { expect(event.proper?(admin)).to eq true }
      end
    end
  end

  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
end