BigW Consortium Gitlab

event_spec.rb 8.04 KB
Newer Older
1 2
require 'spec_helper'

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
  describe 'Callbacks' do
    describe 'after_create :reset_project_activity' do
19
      let(:project) { create(:empty_project) }
20

21 22
      it 'calls the reset_project_activity method' do
        expect_any_instance_of(Event).to receive(:reset_project_activity)
23

24
        create_event(project, project.owner)
25 26 27 28
      end
    end
  end

randx committed
29
  describe "Push event" do
30 31 32 33 34 35 36 37 38 39
    let(:project) { create(:project) }
    let(:user) { project.owner }
    let(:event) { create_event(project, user) }

    it do
      expect(event.push?).to be_truthy
      expect(event.visible_to_user?).to be_truthy
      expect(event.tag?).to be_falsey
      expect(event.branch_name).to eq("master")
      expect(event.author).to eq(user)
40 41
    end
  end
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
  describe '#membership_changed?' do
    context "created" do
      subject { build(:event, action: Event::CREATED).membership_changed? }
      it { is_expected.to be_falsey }
    end

    context "updated" do
      subject { build(:event, action: Event::UPDATED).membership_changed? }
      it { is_expected.to be_falsey }
    end

    context "expired" do
      subject { build(:event, action: Event::EXPIRED).membership_changed? }
      it { is_expected.to be_truthy }
    end

    context "left" do
      subject { build(:event, action: Event::LEFT).membership_changed? }
      it { is_expected.to be_truthy }
    end

    context "joined" do
      subject { build(:event, action: Event::JOINED).membership_changed? }
      it { is_expected.to be_truthy }
    end
  end

70 71 72 73 74 75 76 77 78
  describe '#note?' do
    subject { Event.new(project: target.project, target: target) }

    context 'issue note event' do
      let(:target) { create(:note_on_issue) }

      it { is_expected.to be_note }
    end

79
    context 'merge request diff note event' do
80
      let(:target) { create(:legacy_diff_note_on_merge_request) }
81 82 83 84 85

      it { is_expected.to be_note }
    end
  end

86
  describe '#visible_to_user?' do
87 88
    let(:project) { create(:empty_project, :public) }
    let(:non_member) { create(:user) }
89 90
    let(:member) { create(:user) }
    let(:guest) { create(:user) }
91 92 93 94 95 96 97 98 99 100 101
    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]
102
      project.team << [guest, :guest]
103
    end
104

105
    context 'issue event' do
106
      context 'for non confidential issues' do
107
        let(:target) { issue }
108

109 110 111 112 113 114 115 116
        it do
          expect(event.visible_to_user?(non_member)).to eq true
          expect(event.visible_to_user?(author)).to eq true
          expect(event.visible_to_user?(assignee)).to eq true
          expect(event.visible_to_user?(member)).to eq true
          expect(event.visible_to_user?(guest)).to eq true
          expect(event.visible_to_user?(admin)).to eq true
        end
117 118 119
      end

      context 'for confidential issues' do
120 121
        let(:target) { confidential_issue }

122 123 124 125 126 127 128 129
        it do
          expect(event.visible_to_user?(non_member)).to eq false
          expect(event.visible_to_user?(author)).to eq true
          expect(event.visible_to_user?(assignee)).to eq true
          expect(event.visible_to_user?(member)).to eq true
          expect(event.visible_to_user?(guest)).to eq false
          expect(event.visible_to_user?(admin)).to eq true
        end
130 131 132
      end
    end

133
    context 'issue note event' do
134 135 136
      context 'on non confidential issues' do
        let(:target) { note_on_issue }

137 138 139 140 141 142 143 144
        it do
          expect(event.visible_to_user?(non_member)).to eq true
          expect(event.visible_to_user?(author)).to eq true
          expect(event.visible_to_user?(assignee)).to eq true
          expect(event.visible_to_user?(member)).to eq true
          expect(event.visible_to_user?(guest)).to eq true
          expect(event.visible_to_user?(admin)).to eq true
        end
145 146 147 148
      end

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

150 151 152 153 154 155 156 157
        it do
          expect(event.visible_to_user?(non_member)).to eq false
          expect(event.visible_to_user?(author)).to eq true
          expect(event.visible_to_user?(assignee)).to eq true
          expect(event.visible_to_user?(member)).to eq true
          expect(event.visible_to_user?(guest)).to eq false
          expect(event.visible_to_user?(admin)).to eq true
        end
158 159
      end
    end
160

161
    context 'merge request diff note event' do
162 163
      let(:project) { create(:project, :public) }
      let(:merge_request) { create(:merge_request, source_project: project, author: author, assignee: assignee) }
164
      let(:note_on_merge_request) { create(:legacy_diff_note_on_merge_request, noteable: merge_request, project: project) }
165 166
      let(:target) { note_on_merge_request }

167 168 169 170 171 172 173 174
      it do
        expect(event.visible_to_user?(non_member)).to eq true
        expect(event.visible_to_user?(author)).to eq true
        expect(event.visible_to_user?(assignee)).to eq true
        expect(event.visible_to_user?(member)).to eq true
        expect(event.visible_to_user?(guest)).to eq true
        expect(event.visible_to_user?(admin)).to eq true
      end
175 176 177 178

      context 'private project' do
        let(:project) { create(:project, :private) }

179 180 181 182 183 184 185 186
        it do
          expect(event.visible_to_user?(non_member)).to eq false
          expect(event.visible_to_user?(author)).to eq true
          expect(event.visible_to_user?(assignee)).to eq true
          expect(event.visible_to_user?(member)).to eq true
          expect(event.visible_to_user?(guest)).to eq false
          expect(event.visible_to_user?(admin)).to eq true
        end
187
      end
188
    end
189 190
  end

191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
  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
207

208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
  describe '#reset_project_activity' do
    let(:project) { create(:empty_project) }

    context 'when a project was updated less than 1 hour ago' do
      it 'does not update the project' do
        project.update(last_activity_at: Time.now)

        expect(project).not_to receive(:update_column).
          with(:last_activity_at, a_kind_of(Time))

        create_event(project, project.owner)
      end
    end

    context 'when a project was updated more than 1 hour ago' do
      it 'updates the project' do
        project.update(last_activity_at: 1.year.ago)

226
        create_event(project, project.owner)
227

228
        project.reload
229

230
        project.last_activity_at <= 1.minute.ago
231 232 233 234
      end
    end
  end

235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
  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
256
    }.merge!(attrs))
257
  end
258
end