2019-10-25 00:06:14 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-02-17 14:58:12 +00:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 06:09:01 +00:00
|
|
|
RSpec.describe UserPolicy do
|
2017-02-17 14:58:12 +00:00
|
|
|
let(:current_user) { create(:user) }
|
|
|
|
let(:user) { create(:user) }
|
|
|
|
|
2017-07-25 17:09:00 +00:00
|
|
|
subject { described_class.new(current_user, user) }
|
2017-02-17 14:58:12 +00:00
|
|
|
|
|
|
|
describe "reading a user's information" do
|
2017-04-06 21:09:58 +00:00
|
|
|
it { is_expected.to be_allowed(:read_user) }
|
2017-02-17 14:58:12 +00:00
|
|
|
end
|
|
|
|
|
2020-08-06 21:10:15 +00:00
|
|
|
describe "reading a different user's Personal Access Tokens" do
|
|
|
|
let(:token) { create(:personal_access_token, user: user) }
|
|
|
|
|
|
|
|
context 'when user is admin' do
|
|
|
|
let(:current_user) { create(:user, :admin) }
|
|
|
|
|
|
|
|
context 'when admin mode is enabled', :enable_admin_mode do
|
|
|
|
it { is_expected.to be_allowed(:read_user_personal_access_tokens) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when admin mode is disabled' do
|
|
|
|
it { is_expected.not_to be_allowed(:read_user_personal_access_tokens) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when user is not an admin' do
|
|
|
|
context 'requesting their own personal access tokens' do
|
|
|
|
subject { described_class.new(current_user, current_user) }
|
|
|
|
|
|
|
|
it { is_expected.to be_allowed(:read_user_personal_access_tokens) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context "requesting a different user's personal access tokens" do
|
|
|
|
it { is_expected.not_to be_allowed(:read_user_personal_access_tokens) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-11-09 12:09:24 +00:00
|
|
|
describe "creating a different user's Personal Access Tokens" do
|
|
|
|
context 'when current_user is admin' do
|
|
|
|
let(:current_user) { create(:user, :admin) }
|
|
|
|
|
|
|
|
context 'when admin mode is enabled and current_user is not blocked', :enable_admin_mode do
|
|
|
|
it { is_expected.to be_allowed(:create_user_personal_access_token) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when admin mode is enabled and current_user is blocked', :enable_admin_mode do
|
|
|
|
let(:current_user) { create(:admin, :blocked) }
|
|
|
|
|
|
|
|
it { is_expected.not_to be_allowed(:create_user_personal_access_token) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when admin mode is disabled' do
|
|
|
|
it { is_expected.not_to be_allowed(:create_user_personal_access_token) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when current_user is not an admin' do
|
|
|
|
context 'creating their own personal access tokens' do
|
|
|
|
subject { described_class.new(current_user, current_user) }
|
|
|
|
|
|
|
|
context 'when current_user is not blocked' do
|
|
|
|
it { is_expected.to be_allowed(:create_user_personal_access_token) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when current_user is blocked' do
|
|
|
|
let(:current_user) { create(:user, :blocked) }
|
|
|
|
|
|
|
|
it { is_expected.not_to be_allowed(:create_user_personal_access_token) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "creating a different user's personal access tokens" do
|
|
|
|
it { is_expected.not_to be_allowed(:create_user_personal_access_token) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-04-25 14:54:26 +00:00
|
|
|
shared_examples 'changing a user' do |ability|
|
2017-02-17 14:58:12 +00:00
|
|
|
context "when a regular user tries to destroy another regular user" do
|
2018-04-25 14:54:26 +00:00
|
|
|
it { is_expected.not_to be_allowed(ability) }
|
2017-02-17 14:58:12 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
context "when a regular user tries to destroy themselves" do
|
|
|
|
let(:current_user) { user }
|
|
|
|
|
2018-04-25 14:54:26 +00:00
|
|
|
it { is_expected.to be_allowed(ability) }
|
2017-02-17 14:58:12 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
context "when an admin user tries to destroy a regular user" do
|
|
|
|
let(:current_user) { create(:user, :admin) }
|
|
|
|
|
2020-05-15 15:08:04 +00:00
|
|
|
context 'when admin mode is enabled', :enable_admin_mode do
|
|
|
|
it { is_expected.to be_allowed(ability) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when admin mode is disabled' do
|
|
|
|
it { is_expected.to be_disallowed(ability) }
|
|
|
|
end
|
2017-02-17 14:58:12 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
context "when an admin user tries to destroy a ghost user" do
|
|
|
|
let(:current_user) { create(:user, :admin) }
|
|
|
|
let(:user) { create(:user, :ghost) }
|
|
|
|
|
2018-04-25 14:54:26 +00:00
|
|
|
it { is_expected.not_to be_allowed(ability) }
|
2017-02-17 14:58:12 +00:00
|
|
|
end
|
|
|
|
end
|
2018-04-25 14:54:26 +00:00
|
|
|
|
2018-07-13 15:52:31 +00:00
|
|
|
describe "updating a user's status" do
|
|
|
|
it_behaves_like 'changing a user', :update_user_status
|
|
|
|
end
|
|
|
|
|
2018-04-25 14:54:26 +00:00
|
|
|
describe "destroying a user" do
|
|
|
|
it_behaves_like 'changing a user', :destroy_user
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "updating a user" do
|
|
|
|
it_behaves_like 'changing a user', :update_user
|
|
|
|
end
|
2020-08-20 15:10:18 +00:00
|
|
|
|
|
|
|
describe 'disabling two-factor authentication' do
|
|
|
|
context 'disabling their own two-factor authentication' do
|
|
|
|
let(:user) { current_user }
|
|
|
|
|
|
|
|
it { is_expected.to be_allowed(:disable_two_factor) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'disabling the two-factor authentication of another user' do
|
|
|
|
context 'when the executor is an admin', :enable_admin_mode do
|
|
|
|
let(:current_user) { create(:user, :admin) }
|
|
|
|
|
|
|
|
it { is_expected.to be_allowed(:disable_two_factor) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the executor is not an admin' do
|
|
|
|
it { is_expected.not_to be_allowed(:disable_two_factor) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2020-10-30 18:08:56 +00:00
|
|
|
|
|
|
|
describe "reading a user's group count" do
|
|
|
|
context "when current_user is an admin", :enable_admin_mode do
|
|
|
|
let(:current_user) { create(:user, :admin) }
|
|
|
|
|
|
|
|
it { is_expected.to be_allowed(:read_group_count) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context "for self users" do
|
|
|
|
let(:user) { current_user }
|
|
|
|
|
|
|
|
it { is_expected.to be_allowed(:read_group_count) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when accessing a different user's group count" do
|
|
|
|
it { is_expected.not_to be_allowed(:read_group_count) }
|
|
|
|
end
|
|
|
|
end
|
2017-02-17 14:58:12 +00:00
|
|
|
end
|