Commit 00b5f547 authored by Bob Van Landuyt's avatar Bob Van Landuyt

Spec instance statistics

parent 702a09c1
# frozen_string_literal: true # frozen_string_literal: true
class InstanceStatistics::ApplicationController < ApplicationController class InstanceStatistics::ApplicationController < ApplicationController
before_action :authenticate_user! before_action :authorize_read_instance_statistics!
layout 'instance_statistics' layout 'instance_statistics'
def authenticate_user! def authorize_read_instance_statistics!
render_404 unless can?(current_user, :read_instance_statistics) render_404 unless can?(current_user, :read_instance_statistics)
end end
end end
# frozen_string_literal: true
class InstanceStatistics::CohortsController < InstanceStatistics::ApplicationController class InstanceStatistics::CohortsController < InstanceStatistics::ApplicationController
def index def index
if Gitlab::CurrentSettings.usage_ping_enabled if Gitlab::CurrentSettings.usage_ping_enabled
......
...@@ -20,7 +20,9 @@ class GlobalPolicy < BasePolicy ...@@ -20,7 +20,9 @@ class GlobalPolicy < BasePolicy
end end
condition(:private_instance_statistics, score: 0) { Gitlab::CurrentSettings.instance_statistics_visibility_private? } condition(:private_instance_statistics, score: 0) { Gitlab::CurrentSettings.instance_statistics_visibility_private? }
rule { admin | ~private_instance_statistics }.enable :read_instance_statistics
rule { admin | (~private_instance_statistics & ~anonymous) }
.enable :read_instance_statistics
rule { anonymous }.policy do rule { anonymous }.policy do
prevent :log_in prevent :log_in
......
# frozen_string_literal: true # frozen_string_literal: true
namespace :instance_statistics do namespace :instance_statistics do
root to: redirect('-/instance_statistics/conversational_development_index') root to: redirect('/-/instance_statistics/conversational_development_index')
resources :cohorts, only: :index resources :cohorts, only: :index
resources :conversational_development_index, only: :index resources :conversational_development_index, only: :index
......
# frozen_string_literal: true
require 'spec_helper'
describe InstanceStatistics::CohortsController do
it_behaves_like 'instance statistics availability'
end
# frozen_string_literal: true
require 'spec_helper'
describe InstanceStatistics::ConversationalDevelopmentIndexController do
it_behaves_like 'instance statistics availability'
end
...@@ -7,32 +7,38 @@ RSpec.describe 'Dashboard Active Tab', :js do ...@@ -7,32 +7,38 @@ RSpec.describe 'Dashboard Active Tab', :js do
shared_examples 'page has active tab' do |title| shared_examples 'page has active tab' do |title|
it "#{title} tab" do it "#{title} tab" do
subject
expect(page).to have_selector('.navbar-sub-nav li.active', count: 1) expect(page).to have_selector('.navbar-sub-nav li.active', count: 1)
expect(find('.navbar-sub-nav li.active')).to have_content(title) expect(find('.navbar-sub-nav li.active')).to have_content(title)
end end
end end
context 'on dashboard projects' do context 'on dashboard projects' do
before do it_behaves_like 'page has active tab', 'Projects' do
visit dashboard_projects_path subject { visit dashboard_projects_path }
end end
it_behaves_like 'page has active tab', 'Projects'
end end
context 'on dashboard groups' do context 'on dashboard groups' do
before do it_behaves_like 'page has active tab', 'Groups' do
visit dashboard_groups_path subject { visit dashboard_groups_path }
end end
it_behaves_like 'page has active tab', 'Groups'
end end
context 'on activity projects' do context 'on activity projects' do
before do it_behaves_like 'page has active tab', 'Activity' do
visit activity_dashboard_path subject { visit activity_dashboard_path }
end
end end
it_behaves_like 'page has active tab', 'Activity' context 'on instance statistics' do
subject { visit instance_statistics_root_path }
it 'shows Instance Statistics` as active' do
subject
expect(find('.navbar-sub-nav li.active')).to have_link('Instance Statistics')
end
end end
end end
# frozen_string_literal: true
require 'spec_helper'
describe 'Showing instance statistics' do
before do
sign_in user if user
end
# Using a path that is publicly accessible
subject { visit explore_projects_path }
context 'for unauthenticated users' do
let(:user) { nil }
it 'does not show the instance statistics link' do
subject
expect(page).not_to have_link('Instance Statistics')
end
end
context 'for regular users' do
let(:user) { create(:user) }
context 'when instance statistics are publicly available' do
before do
stub_application_setting(instance_statistics_visibility_private: false)
end
it 'shows the instance statistics link' do
subject
expect(page).to have_link('Instance Statistics')
end
end
context 'when instance statistics are not publicly available' do
before do
stub_application_setting(instance_statistics_visibility_private: true)
end
it 'shows the instance statistics link' do
subject
expect(page).not_to have_link('Instance Statistics')
end
end
end
context 'for admins' do
let(:user) { create(:admin) }
it 'shows the instance statistics link' do
subject
expect(page).to have_link('Instance Statistics')
end
end
end
...@@ -180,4 +180,38 @@ describe GlobalPolicy do ...@@ -180,4 +180,38 @@ describe GlobalPolicy do
end end
end end
end end
describe 'read instance statistics' do
context 'regular user' do
it { is_expected.to be_allowed(:read_instance_statistics) }
context 'when instance statistics are set to private' do
before do
stub_application_setting(instance_statistics_visibility_private: true)
end
it { is_expected.not_to be_allowed(:read_instance_statistics) }
end
end
context 'admin' do
let(:current_user) { create(:admin) }
it { is_expected.to be_allowed(:read_instance_statistics) }
context 'when instance statistics are set to private' do
before do
stub_application_setting(instance_statistics_visibility_private: true)
end
it { is_expected.to be_allowed(:read_instance_statistics) }
end
end
context 'anonymous' do
let(:current_user) { nil }
it { is_expected.not_to be_allowed(:read_instance_statistics) }
end
end
end end
# frozen_string_literal: true
require 'spec_helper'
describe 'Instance Statistics', 'routing' do
include RSpec::Rails::RequestExampleGroup
it "routes '/-/instance_statistics' to conversational development index" do
expect(get('/-/instance_statistics')).to redirect_to('/-/instance_statistics/conversational_development_index')
end
end
# frozen_string_literal: true
shared_examples 'instance statistics availability' do
let(:user) { create(:user) }
before do
sign_in(user)
end
describe 'GET #index' do
it 'is available when the feature is available publicly' do
get :index
expect(response).to have_gitlab_http_status(:success)
end
it 'renders a 404 when the feature is not available publicly' do
stub_application_setting(instance_statistics_visibility_private: true)
get :index
expect(response).to have_gitlab_http_status(:not_found)
end
context 'for admins' do
let(:user) { create(:admin) }
it 'allows access when the feature is not available publicly' do
stub_application_setting(instance_statistics_visibility_private: true)
get :index
expect(response).to have_gitlab_http_status(:success)
end
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment