summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/contributions_calendar_spec.rb
blob: 01b2a55b63c43bfcf31270b944ee5e2cf56aa264 (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
require 'spec_helper'

describe Gitlab::ContributionsCalendar do
  let(:contributor) { create(:user) }
  let(:user) { create(:user) }

  let(:private_project) do
    create(:empty_project, :private) do |project|
      create(:project_member, user: contributor, project: project)
    end
  end

  let(:public_project) do
    create(:empty_project, :public) do |project|
      create(:project_member, user: contributor, project: project)
    end
  end

  let(:feature_project) do
    create(:empty_project, :public, issues_access_level: ProjectFeature::PRIVATE) do |project|
      create(:project_member, user: contributor, project: project).project
    end
  end

  let(:today) { Time.now.to_date }
  let(:last_week) { today - 7.days }
  let(:last_year) { today - 1.year }

  before do
    travel_to today
  end

  after do
    travel_back
  end

  def calendar(current_user = nil)
    described_class.new(contributor, current_user)
  end

  def create_event(project, day)
    @targets ||= {}
    @targets[project] ||= create(:issue, project: project, author: contributor)

    Event.create!(
      project: project,
      action: Event::CREATED,
      target: @targets[project],
      author: contributor,
      created_at: day,
    )
  end

  describe '#activity_dates' do
    it "returns a hash of date => count" do
      create_event(public_project, last_week)
      create_event(public_project, last_week)
      create_event(public_project, today)

      expect(calendar.activity_dates).to eq(last_week => 2, today => 1)
    end

    it "only shows private events to authorized users" do
      create_event(private_project, today)
      create_event(feature_project, today)

      expect(calendar.activity_dates[today]).to eq(0)
      expect(calendar(user).activity_dates[today]).to eq(0)
      expect(calendar(contributor).activity_dates[today]).to eq(2)
    end
  end

  describe '#events_by_date' do
    it "returns all events for a given date" do
      e1 = create_event(public_project, today)
      e2 = create_event(public_project, today)
      create_event(public_project, last_week)

      expect(calendar.events_by_date(today)).to contain_exactly(e1, e2)
    end

    it "only shows private events to authorized users" do
      e1 = create_event(public_project, today)
      e2 = create_event(private_project, today)
      e3 = create_event(feature_project, today)
      create_event(public_project, last_week)

      expect(calendar.events_by_date(today)).to contain_exactly(e1)
      expect(calendar(contributor).events_by_date(today)).to contain_exactly(e1, e2, e3)
    end
  end

  describe '#starting_year' do
    it "should be the start of last year" do
      expect(calendar.starting_year).to eq(last_year.year)
    end
  end

  describe '#starting_month' do
    it "should be the start of this month" do
      expect(calendar.starting_month).to eq(today.month)
    end
  end
end