2019-10-12 21:52:04 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-11-24 13:41:30 +05:30
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
RSpec.describe Gitlab::ContributionsCalendar do
|
2016-11-24 13:41:30 +05:30
|
|
|
let(:contributor) { create(:user) }
|
|
|
|
let(:user) { create(:user) }
|
2021-12-11 22:18:48 +05:30
|
|
|
let(:travel_time) { nil }
|
2016-11-24 13:41:30 +05:30
|
|
|
|
|
|
|
let(:private_project) do
|
2017-09-10 17:25:29 +05:30
|
|
|
create(:project, :private) do |project|
|
2016-11-24 13:41:30 +05:30
|
|
|
create(:project_member, user: contributor, project: project)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:public_project) do
|
2018-03-27 19:54:05 +05:30
|
|
|
create(:project, :public, :repository) do |project|
|
2016-11-24 13:41:30 +05:30
|
|
|
create(:project_member, user: contributor, project: project)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:feature_project) do
|
2017-09-10 17:25:29 +05:30
|
|
|
create(:project, :public, :issues_private) do |project|
|
2016-11-24 13:41:30 +05:30
|
|
|
create(:project_member, user: contributor, project: project).project
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
let(:today) { Time.now.utc.to_date }
|
|
|
|
let(:yesterday) { today - 1.day }
|
|
|
|
let(:tomorrow) { today + 1.day }
|
2016-11-24 13:41:30 +05:30
|
|
|
let(:last_week) { today - 7.days }
|
|
|
|
let(:last_year) { today - 1.year }
|
|
|
|
|
|
|
|
before do
|
2021-12-11 22:18:48 +05:30
|
|
|
travel_to travel_time || Time.now.utc.end_of_day
|
2016-11-24 13:41:30 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
after do
|
|
|
|
travel_back
|
|
|
|
end
|
|
|
|
|
|
|
|
def calendar(current_user = nil)
|
|
|
|
described_class.new(contributor, current_user)
|
|
|
|
end
|
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
def create_event(project, day, hour = 0, action = :created, target_symbol = :issue)
|
2016-11-24 13:41:30 +05:30
|
|
|
@targets ||= {}
|
2018-03-27 19:54:05 +05:30
|
|
|
@targets[project] ||= create(target_symbol, project: project, author: contributor)
|
2016-11-24 13:41:30 +05:30
|
|
|
|
|
|
|
Event.create!(
|
|
|
|
project: project,
|
2018-03-27 19:54:05 +05:30
|
|
|
action: action,
|
2016-11-24 13:41:30 +05:30
|
|
|
target: @targets[project],
|
|
|
|
author: contributor,
|
2017-09-10 17:25:29 +05:30
|
|
|
created_at: DateTime.new(day.year, day.month, day.day, hour)
|
2016-11-24 13:41:30 +05:30
|
|
|
)
|
|
|
|
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
|
|
|
|
|
2018-11-20 20:47:30 +05:30
|
|
|
context "when the user has opted-in for private contributions" do
|
|
|
|
it "shows private and public events to all users" do
|
|
|
|
user.update_column(:include_private_contributions, true)
|
|
|
|
create_event(private_project, today)
|
|
|
|
create_event(public_project, today)
|
2016-11-24 13:41:30 +05:30
|
|
|
|
2018-11-20 20:47:30 +05:30
|
|
|
expect(calendar.activity_dates[today]).to eq(1)
|
|
|
|
expect(calendar(user).activity_dates[today]).to eq(1)
|
|
|
|
expect(calendar(contributor).activity_dates[today]).to eq(2)
|
|
|
|
end
|
2016-11-24 13:41:30 +05:30
|
|
|
end
|
2017-09-10 17:25:29 +05:30
|
|
|
|
2018-03-27 19:54:05 +05:30
|
|
|
it "counts the diff notes on merge request" do
|
2020-06-23 00:09:42 +05:30
|
|
|
create_event(public_project, today, 0, :commented, :diff_note_on_merge_request)
|
2018-03-27 19:54:05 +05:30
|
|
|
|
|
|
|
expect(calendar(contributor).activity_dates[today]).to eq(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "counts the discussions on merge requests and issues" do
|
2020-06-23 00:09:42 +05:30
|
|
|
create_event(public_project, today, 0, :commented, :discussion_note_on_merge_request)
|
|
|
|
create_event(public_project, today, 2, :commented, :discussion_note_on_issue)
|
2018-03-27 19:54:05 +05:30
|
|
|
|
|
|
|
expect(calendar(contributor).activity_dates[today]).to eq(2)
|
|
|
|
end
|
|
|
|
|
2021-12-11 22:18:48 +05:30
|
|
|
context "when events fall under different dates depending on the system time zone" do
|
2017-09-10 17:25:29 +05:30
|
|
|
before do
|
|
|
|
create_event(public_project, today, 1)
|
|
|
|
create_event(public_project, today, 4)
|
|
|
|
create_event(public_project, today, 10)
|
|
|
|
create_event(public_project, today, 16)
|
|
|
|
create_event(public_project, today, 23)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "renders correct event counts within the UTC timezone" do
|
|
|
|
Time.use_zone('UTC') do
|
|
|
|
expect(calendar.activity_dates).to eq(today => 5)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "renders correct event counts within the Sydney timezone" do
|
|
|
|
Time.use_zone('Sydney') do
|
|
|
|
expect(calendar.activity_dates).to eq(today => 3, tomorrow => 2)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "renders correct event counts within the US Central timezone" do
|
|
|
|
Time.use_zone('Central Time (US & Canada)') do
|
|
|
|
expect(calendar.activity_dates).to eq(yesterday => 2, today => 3)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2021-12-11 22:18:48 +05:30
|
|
|
|
|
|
|
context "when events fall under different dates depending on the contributor's time zone" do
|
|
|
|
before do
|
|
|
|
create_event(public_project, today, 1)
|
|
|
|
create_event(public_project, today, 4)
|
|
|
|
create_event(public_project, today, 10)
|
|
|
|
create_event(public_project, today, 16)
|
|
|
|
create_event(public_project, today, 23)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "renders correct event counts within the UTC timezone" do
|
|
|
|
Time.use_zone('UTC') do
|
|
|
|
contributor.timezone = 'UTC'
|
|
|
|
expect(calendar.activity_dates).to eq(today => 5)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "renders correct event counts within the Sydney timezone" do
|
|
|
|
Time.use_zone('UTC') do
|
|
|
|
contributor.timezone = 'Sydney'
|
|
|
|
expect(calendar.activity_dates).to eq(today => 3, tomorrow => 2)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "renders correct event counts within the US Central timezone" do
|
|
|
|
Time.use_zone('UTC') do
|
|
|
|
contributor.timezone = 'Central Time (US & Canada)'
|
|
|
|
expect(calendar.activity_dates).to eq(yesterday => 2, today => 3)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-11-24 13:41:30 +05:30
|
|
|
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)
|
|
|
|
|
2018-11-20 20:47:30 +05:30
|
|
|
expect(calendar.events_by_date(today)).to contain_exactly(e1, e3)
|
2016-11-24 13:41:30 +05:30
|
|
|
expect(calendar(contributor).events_by_date(today)).to contain_exactly(e1, e2, e3)
|
|
|
|
end
|
2018-03-27 19:54:05 +05:30
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
context 'when the user cannot read cross project' do
|
2018-03-27 19:54:05 +05:30
|
|
|
before do
|
|
|
|
allow(Ability).to receive(:allowed?).and_call_original
|
|
|
|
expect(Ability).to receive(:allowed?).with(user, :read_cross_project) { false }
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not return any events' do
|
|
|
|
create_event(public_project, today)
|
|
|
|
|
|
|
|
expect(calendar(user).events_by_date(today)).to be_empty
|
|
|
|
end
|
|
|
|
end
|
2016-11-24 13:41:30 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
describe '#starting_year' do
|
2021-12-11 22:18:48 +05:30
|
|
|
let(:travel_time) { Time.find_zone('UTC').local(2020, 12, 31, 19, 0, 0) }
|
|
|
|
|
|
|
|
context "when the contributor's timezone is not set" do
|
|
|
|
it "is the start of last year in the system timezone" do
|
|
|
|
expect(calendar.starting_year).to eq(2019)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when the contributor's timezone is set to Sydney" do
|
|
|
|
let(:contributor) { create(:user, { timezone: 'Sydney' }) }
|
|
|
|
|
|
|
|
it "is the start of last year in Sydney" do
|
|
|
|
expect(calendar.starting_year).to eq(2020)
|
|
|
|
end
|
2016-11-24 13:41:30 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#starting_month' do
|
2021-12-11 22:18:48 +05:30
|
|
|
let(:travel_time) { Time.find_zone('UTC').local(2020, 12, 31, 19, 0, 0) }
|
|
|
|
|
|
|
|
context "when the contributor's timezone is not set" do
|
|
|
|
it "is the start of this month in the system timezone" do
|
|
|
|
expect(calendar.starting_month).to eq(12)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when the contributor's timezone is set to Sydney" do
|
|
|
|
let(:contributor) { create(:user, { timezone: 'Sydney' }) }
|
|
|
|
|
|
|
|
it "is the start of this month in Sydney" do
|
|
|
|
expect(calendar.starting_month).to eq(1)
|
|
|
|
end
|
2016-11-24 13:41:30 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|