debian-mirror-gitlab/spec/requests/api/project_statistics_spec.rb

55 lines
2.6 KiB
Ruby
Raw Normal View History

2019-07-07 11:18:12 +05:30
# frozen_string_literal: true
require 'spec_helper'
2023-03-04 22:38:38 +05:30
RSpec.describe API::ProjectStatistics, feature_category: :source_code_management do
2021-09-04 01:27:46 +05:30
let_it_be(:reporter) { create(:user) }
2020-05-24 23:13:21 +05:30
let_it_be(:public_project) { create(:project, :public) }
2019-07-07 11:18:12 +05:30
before do
2021-09-04 01:27:46 +05:30
public_project.add_reporter(reporter)
2019-07-07 11:18:12 +05:30
end
describe 'GET /projects/:id/statistics' do
2020-05-24 23:13:21 +05:30
let_it_be(:fetch_statistics1) { create(:project_daily_statistic, project: public_project, fetch_count: 30, date: 29.days.ago) }
let_it_be(:fetch_statistics2) { create(:project_daily_statistic, project: public_project, fetch_count: 4, date: 3.days.ago) }
let_it_be(:fetch_statistics3) { create(:project_daily_statistic, project: public_project, fetch_count: 3, date: 2.days.ago) }
let_it_be(:fetch_statistics4) { create(:project_daily_statistic, project: public_project, fetch_count: 2, date: 1.day.ago) }
let_it_be(:fetch_statistics5) { create(:project_daily_statistic, project: public_project, fetch_count: 1, date: Date.today) }
let_it_be(:fetch_statistics_other_project) { create(:project_daily_statistic, project: create(:project), fetch_count: 29, date: 29.days.ago) }
2019-07-07 11:18:12 +05:30
it 'returns the fetch statistics of the last 30 days' do
2021-09-04 01:27:46 +05:30
get api("/projects/#{public_project.id}/statistics", reporter)
2019-07-07 11:18:12 +05:30
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:ok)
2019-07-07 11:18:12 +05:30
fetches = json_response['fetches']
expect(fetches['total']).to eq(40)
expect(fetches['days'].length).to eq(5)
expect(fetches['days'].first).to eq({ 'count' => fetch_statistics5.fetch_count, 'date' => fetch_statistics5.date.to_s })
expect(fetches['days'].last).to eq({ 'count' => fetch_statistics1.fetch_count, 'date' => fetch_statistics1.date.to_s })
end
it 'excludes the fetch statistics older than 30 days' do
create(:project_daily_statistic, fetch_count: 31, project: public_project, date: 30.days.ago)
2021-09-04 01:27:46 +05:30
get api("/projects/#{public_project.id}/statistics", reporter)
2019-07-07 11:18:12 +05:30
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:ok)
2019-07-07 11:18:12 +05:30
fetches = json_response['fetches']
expect(fetches['total']).to eq(40)
expect(fetches['days'].length).to eq(5)
expect(fetches['days'].last).to eq({ 'count' => fetch_statistics1.fetch_count, 'date' => fetch_statistics1.date.to_s })
end
2021-09-04 01:27:46 +05:30
it 'responds with 403 when the user is not a reporter of the repository' do
2020-05-24 23:13:21 +05:30
guest = create(:user)
public_project.add_guest(guest)
2019-07-07 11:18:12 +05:30
2020-05-24 23:13:21 +05:30
get api("/projects/#{public_project.id}/statistics", guest)
2019-07-07 11:18:12 +05:30
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:forbidden)
2019-07-07 11:18:12 +05:30
expect(json_response['message']).to eq('403 Forbidden')
end
end
end