event_spec.rb 11.5 KB
Newer Older
D
Dmitriy Zaporozhets 已提交
1 2
require 'spec_helper'

3
describe Event do
4
  describe "Associations" do
5 6
    it { is_expected.to belong_to(:project) }
    it { is_expected.to belong_to(:target) }
7 8
  end

D
Dmitriy Zaporozhets 已提交
9
  describe "Respond to" do
10 11 12 13
    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) }
D
Dmitriy Zaporozhets 已提交
14 15
  end

16
  describe 'Callbacks' do
17
    let(:project) { create(:project) }
18

19
    describe 'after_create :reset_project_activity' do
20
      it 'calls the reset_project_activity method' do
21
        expect_any_instance_of(described_class).to receive(:reset_project_activity)
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
        create_push_event(project, project.owner)
      end
    end

    describe 'after_create :set_last_repository_updated_at' do
      context 'with a push event' do
        it 'updates the project last_repository_updated_at' do
          project.update(last_repository_updated_at: 1.year.ago)

          create_push_event(project, project.owner)

          project.reload

          expect(project.last_repository_updated_at).to be_within(1.minute).of(Time.now)
        end
      end

      context 'without a push event' do
        it 'does not update the project last_repository_updated_at' do
          project.update(last_repository_updated_at: 1.year.ago)

          create(:closed_issue_event, project: project, author: project.owner)

          project.reload

          expect(project.last_repository_updated_at).to be_within(1.minute).of(1.year.ago)
        end
50 51 52 53
      end
    end
  end

R
randx 已提交
54
  describe "Push event" do
55
    let(:project) { create(:project, :private) }
56
    let(:user) { project.owner }
57
    let(:event) { create_push_event(project, user) }
58 59 60

    it do
      expect(event.push?).to be_truthy
61 62
      expect(event.visible_to_user?(user)).to be_truthy
      expect(event.visible_to_user?(nil)).to be_falsey
63 64 65
      expect(event.tag?).to be_falsey
      expect(event.branch_name).to eq("master")
      expect(event.author).to eq(user)
D
Dmitriy Zaporozhets 已提交
66 67
    end
  end
68

69 70
  describe '#membership_changed?' do
    context "created" do
71
      subject { build(:event, :created).membership_changed? }
72 73 74 75
      it { is_expected.to be_falsey }
    end

    context "updated" do
76
      subject { build(:event, :updated).membership_changed? }
77 78 79 80
      it { is_expected.to be_falsey }
    end

    context "expired" do
81
      subject { build(:event, :expired).membership_changed? }
82 83 84 85
      it { is_expected.to be_truthy }
    end

    context "left" do
86
      subject { build(:event, :left).membership_changed? }
87 88 89 90
      it { is_expected.to be_truthy }
    end

    context "joined" do
91
      subject { build(:event, :joined).membership_changed? }
92 93 94 95
      it { is_expected.to be_truthy }
    end
  end

96
  describe '#note?' do
97
    subject { described_class.new(project: target.project, target: target) }
98 99 100 101 102 103 104

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

      it { is_expected.to be_note }
    end

105
    context 'merge request diff note event' do
D
Douwe Maan 已提交
106
      let(:target) { create(:legacy_diff_note_on_merge_request) }
107 108 109 110 111

      it { is_expected.to be_note }
    end
  end

112
  describe '#visible_to_user?' do
113
    let(:project) { create(:project, :public) }
114
    let(:non_member) { create(:user) }
115 116
    let(:member) { create(:user) }
    let(:guest) { create(:user) }
117 118 119
    let(:author) { create(:author) }
    let(:assignee) { create(:user) }
    let(:admin) { create(:admin) }
120 121
    let(:issue) { create(:issue, project: project, author: author, assignees: [assignee]) }
    let(:confidential_issue) { create(:issue, :confidential, project: project, author: author, assignees: [assignee]) }
122
    let(:note_on_commit) { create(:note_on_commit, project: project) }
123 124
    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) }
125
    let(:event) { described_class.new(project: project, target: target, author_id: author.id) }
126 127

    before do
128 129
      project.add_developer(member)
      project.add_guest(guest)
130
    end
131

132 133 134 135 136 137 138 139 140 141 142 143 144
    context 'commit note event' do
      let(:target) { note_on_commit }

      it do
        aggregate_failures do
          expect(event.visible_to_user?(non_member)).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
      end

      context 'private project' do
145
        let(:project) { create(:project, :private) }
146 147 148 149 150 151 152 153 154 155 156 157

        it do
          aggregate_failures do
            expect(event.visible_to_user?(non_member)).to eq false
            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
        end
      end
    end

158
    context 'issue event' do
159
      context 'for non confidential issues' do
160
        let(:target) { issue }
161

162 163 164 165 166 167 168 169
        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
170 171 172
      end

      context 'for confidential issues' do
173 174
        let(:target) { confidential_issue }

175 176 177 178 179 180 181 182
        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
183 184 185
      end
    end

186
    context 'issue note event' do
187 188 189
      context 'on non confidential issues' do
        let(:target) { note_on_issue }

190 191 192 193 194 195 196 197
        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
198 199 200 201
      end

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

203 204 205 206 207 208 209 210
        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
211 212
      end
    end
213

214
    context 'merge request diff note event' do
215
      let(:project) { create(:project, :public) }
216
      let(:merge_request) { create(:merge_request, source_project: project, author: author, assignee: assignee) }
D
Douwe Maan 已提交
217
      let(:note_on_merge_request) { create(:legacy_diff_note_on_merge_request, noteable: merge_request, project: project) }
218 219
      let(:target) { note_on_merge_request }

220 221 222 223 224 225 226 227
      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
V
Valery Sizov 已提交
228 229

      context 'private project' do
230
        let(:project) { create(:project, :private) }
V
Valery Sizov 已提交
231

232 233 234 235 236 237 238 239
        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
V
Valery Sizov 已提交
240
      end
241
    end
242 243
  end

Y
Yorick Peterse 已提交
244 245 246 247 248
  describe '.limit_recent' do
    let!(:event1) { create(:closed_issue_event) }
    let!(:event2) { create(:closed_issue_event) }

    describe 'without an explicit limit' do
249
      subject { described_class.limit_recent }
Y
Yorick Peterse 已提交
250 251 252 253 254

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

    describe 'with an explicit limit' do
255
      subject { described_class.limit_recent(1) }
Y
Yorick Peterse 已提交
256 257 258 259

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

261
  describe '#reset_project_activity' do
262
    let(:project) { create(:project) }
263 264 265 266 267

    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)

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

271
        create_push_event(project, project.owner)
272 273 274 275 276 277 278
      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)

279
        create_push_event(project, project.owner)
280

281
        project.reload
282

283
        expect(project.last_activity_at).to be_within(1.minute).of(Time.now)
284 285 286 287
      end
    end
  end

Y
Yorick Peterse 已提交
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
  describe '#authored_by?' do
    let(:event) { build(:event) }

    it 'returns true when the event author and user are the same' do
      expect(event.authored_by?(event.author)).to eq(true)
    end

    it 'returns false when passing nil as an argument' do
      expect(event.authored_by?(nil)).to eq(false)
    end

    it 'returns false when the given user is not the author of the event' do
      user = double(:user, id: -1)

      expect(event.authored_by?(user)).to eq(false)
    end
  end

306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
  describe '#body?' do
    let(:push_event) do
      event = build(:push_event)

      allow(event).to receive(:push?).and_return(true)

      event
    end

    it 'returns true for a push event with commits' do
      allow(push_event).to receive(:push_with_commits?).and_return(true)

      expect(push_event).to be_body
    end

    it 'returns false for a push event without a valid commit range' do
      allow(push_event).to receive(:push_with_commits?).and_return(false)

      expect(push_event).not_to be_body
    end

    it 'returns true for a Note event' do
      event = build(:event)

      allow(event).to receive(:note?).and_return(true)

      expect(event).to be_body
    end

    it 'returns true if the target responds to #title' do
      event = build(:event)

      allow(event).to receive(:target).and_return(double(:target, title: 'foo'))

      expect(event).to be_body
    end

    it 'returns false for a regular event without a target' do
      event = build(:event)

      expect(event).not_to be_body
    end
  end

350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
  describe '#target' do
    it 'eager loads the author of an event target' do
      create(:closed_issue_event)

      events = described_class.preload(:target).all.to_a
      count = ActiveRecord::QueryRecorder
        .new { events.first.target.author }.count

      # This expectation exists to make sure the test doesn't pass when the
      # author is for some reason not loaded at all.
      expect(events.first.target.author).to be_an_instance_of(User)

      expect(count).to be_zero
    end
  end

366 367 368 369 370 371 372 373 374 375
  def create_push_event(project, user)
    event = create(:push_event, project: project, author: user)

    create(:push_event_payload,
           event: event,
           commit_to: '1cf19a015df3523caf0a1f9d40c98a267d6a2fc2',
           commit_count: 0,
           ref: 'master')

    event
376
  end
D
Dmitriy Zaporozhets 已提交
377
end