BigW Consortium Gitlab

event_spec.rb 10 KB
Newer Older
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

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

20
    describe 'after_create :reset_project_activity' do
21
      it 'calls the reset_project_activity method' do
22
        expect_any_instance_of(described_class).to receive(:reset_project_activity)
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
        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
51 52 53 54
      end
    end
  end

randx committed
55
  describe "Push event" do
56
    let(:project) { create(:project, :private) }
57
    let(:user) { project.owner }
58
    let(:event) { create_push_event(project, user) }
59 60 61

    it do
      expect(event.push?).to be_truthy
62 63
      expect(event.visible_to_user?(user)).to be_truthy
      expect(event.visible_to_user?(nil)).to be_falsey
64 65 66
      expect(event.tag?).to be_falsey
      expect(event.branch_name).to eq("master")
      expect(event.author).to eq(user)
67 68
    end
  end
69

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

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

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

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

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

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

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

      it { is_expected.to be_note }
    end

106
    context 'merge request diff note event' do
107
      let(:target) { create(:legacy_diff_note_on_merge_request) }
108 109 110 111 112

      it { is_expected.to be_note }
    end
  end

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

    before do
      project.team << [member, :developer]
130
      project.team << [guest, :guest]
131
    end
132

133 134 135 136 137 138 139 140 141 142 143 144 145
    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
146
        let(:project) { create(:project, :private) }
147 148 149 150 151 152 153 154 155 156 157 158

        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

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

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

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

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

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

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

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

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

215
    context 'merge request diff note event' do
216
      let(:project) { create(:project, :public) }
217
      let(:merge_request) { create(:merge_request, source_project: project, author: author, assignee: assignee) }
218
      let(:note_on_merge_request) { create(:legacy_diff_note_on_merge_request, noteable: merge_request, project: project) }
219 220
      let(:target) { note_on_merge_request }

221 222 223 224 225 226 227 228
      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
229 230

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

233 234 235 236 237 238 239 240
        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
241
      end
242
    end
243 244
  end

245 246 247 248 249
  describe '.limit_recent' do
    let!(:event1) { create(:closed_issue_event) }
    let!(:event2) { create(:closed_issue_event) }

    describe 'without an explicit limit' do
250
      subject { described_class.limit_recent }
251 252 253 254 255

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

    describe 'with an explicit limit' do
256
      subject { described_class.limit_recent(1) }
257 258 259 260

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

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

    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)

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

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

280
        create_push_event(project, project.owner)
281

282
        project.reload
283

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

289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
  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

307 308 309 310 311 312 313 314 315 316
  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
317
  end
318
end