2019-09-08 23:38:42 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 02:09:01 -04:00
|
|
|
RSpec.describe API::Statistics, 'Statistics' do
|
2019-09-08 23:38:42 -04:00
|
|
|
include ProjectForksHelper
|
2020-05-19 11:08:04 -04:00
|
|
|
tables_to_analyze = %w[
|
2019-09-08 23:38:42 -04:00
|
|
|
projects
|
|
|
|
users
|
|
|
|
namespaces
|
|
|
|
issues
|
|
|
|
merge_requests
|
|
|
|
notes
|
|
|
|
snippets
|
|
|
|
fork_networks
|
|
|
|
fork_network_members
|
|
|
|
keys
|
|
|
|
milestones
|
|
|
|
].freeze
|
|
|
|
|
|
|
|
let(:path) { "/application/statistics" }
|
|
|
|
|
|
|
|
describe "GET /application/statistics" do
|
|
|
|
context 'when no user' do
|
|
|
|
it "returns authentication error" do
|
|
|
|
get api(path, nil)
|
|
|
|
|
2020-03-02 13:07:42 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:unauthorized)
|
2019-09-08 23:38:42 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when not an admin" do
|
|
|
|
let(:user) { create(:user) }
|
|
|
|
|
|
|
|
it "returns forbidden error" do
|
|
|
|
get api(path, user)
|
|
|
|
|
2020-03-02 13:07:42 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:forbidden)
|
2019-09-08 23:38:42 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when authenticated as admin' do
|
|
|
|
let(:admin) { create(:admin) }
|
|
|
|
|
|
|
|
it 'matches the response schema' do
|
|
|
|
get api(path, admin)
|
|
|
|
|
2020-03-02 13:07:42 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
2019-09-08 23:38:42 -04:00
|
|
|
expect(response).to match_response_schema('statistics')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'gives the right statistics' do
|
|
|
|
projects = create_list(:project, 4, namespace: create(:namespace, owner: admin))
|
|
|
|
issues = create_list(:issue, 2, project: projects.first, updated_by: admin)
|
|
|
|
|
|
|
|
create_list(:snippet, 2, :public, author: admin)
|
|
|
|
create_list(:note, 2, author: admin, project: projects.first, noteable: issues.first)
|
|
|
|
create_list(:milestone, 3, project: projects.first)
|
|
|
|
create(:key, user: admin)
|
|
|
|
create(:merge_request, source_project: projects.first)
|
|
|
|
fork_project(projects.first, admin)
|
|
|
|
|
|
|
|
# Make sure the reltuples have been updated
|
|
|
|
# to get a correct count on postgresql
|
2020-05-19 11:08:04 -04:00
|
|
|
tables_to_analyze.each do |table|
|
2021-08-01 23:09:42 -04:00
|
|
|
ApplicationRecord.connection.execute("ANALYZE #{table}")
|
2019-09-08 23:38:42 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
get api(path, admin)
|
|
|
|
|
|
|
|
expected_statistics = {
|
|
|
|
issues: 2,
|
|
|
|
merge_requests: 1,
|
|
|
|
notes: 2,
|
|
|
|
snippets: 2,
|
|
|
|
forks: 1,
|
|
|
|
ssh_keys: 1,
|
|
|
|
milestones: 3,
|
|
|
|
users: 1,
|
|
|
|
projects: 5,
|
|
|
|
groups: 1,
|
|
|
|
active_users: 1
|
|
|
|
}
|
|
|
|
|
|
|
|
expected_statistics.each do |entity, count|
|
|
|
|
expect(json_response[entity.to_s]).to eq(count.to_s)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|