debian-mirror-gitlab/spec/models/member_spec.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

1134 lines
40 KiB
Ruby
Raw Permalink Normal View History

2019-07-07 11:18:12 +05:30
# frozen_string_literal: true
2015-04-26 12:48:37 +05:30
require 'spec_helper'
2023-04-23 21:23:45 +05:30
RSpec.describe Member, feature_category: :subgroups do
2020-04-22 19:07:51 +05:30
include ExclusiveLeaseHelpers
using RSpec::Parameterized::TableSyntax
2023-01-13 00:05:48 +05:30
describe 'default values' do
subject(:member) { build(:project_member) }
it { expect(member.notification_level).to eq(NotificationSetting.levels[:global]) }
end
2021-11-18 22:05:49 +05:30
describe 'Associations' do
2015-04-26 12:48:37 +05:30
it { is_expected.to belong_to(:user) }
2022-03-02 08:16:31 +05:30
it { is_expected.to belong_to(:member_namespace) }
2021-12-11 22:18:48 +05:30
it { is_expected.to have_one(:member_task) }
2015-04-26 12:48:37 +05:30
end
2021-11-18 22:05:49 +05:30
describe 'Validation' do
2017-09-10 17:25:29 +05:30
subject { described_class.new(access_level: Member::GUEST) }
2015-04-26 12:48:37 +05:30
it { is_expected.to validate_presence_of(:user) }
it { is_expected.to validate_presence_of(:source) }
2020-10-04 03:57:07 +05:30
context 'expires_at' do
it { is_expected.not_to allow_value(Date.yesterday).for(:expires_at) }
it { is_expected.to allow_value(Date.tomorrow).for(:expires_at) }
it { is_expected.to allow_value(Date.today).for(:expires_at) }
it { is_expected.to allow_value(nil).for(:expires_at) }
end
2021-04-17 20:07:23 +05:30
it_behaves_like 'an object with email-formatted attributes', :invite_email do
2016-04-02 18:10:28 +05:30
subject { build(:project_member) }
end
2021-11-18 22:05:49 +05:30
context 'when an invite email is provided' do
2020-11-24 15:15:51 +05:30
let_it_be(:project) { create(:project) }
2021-09-30 23:02:18 +05:30
2020-11-24 15:15:51 +05:30
let(:member) { build(:project_member, source: project, invite_email: "user@example.com", user: nil) }
2015-04-26 12:48:37 +05:30
it "doesn't require a user" do
expect(member).to be_valid
end
2021-11-18 22:05:49 +05:30
it 'requires a valid invite email' do
2015-04-26 12:48:37 +05:30
member.invite_email = "nope"
expect(member).not_to be_valid
end
2021-11-18 22:05:49 +05:30
it 'requires a unique invite email scoped to this source' do
2015-04-26 12:48:37 +05:30
create(:project_member, source: member.source, invite_email: member.invite_email)
expect(member).not_to be_valid
end
end
2021-11-18 22:05:49 +05:30
context 'when an invite email is not provided' do
2015-04-26 12:48:37 +05:30
let(:member) { build(:project_member) }
2021-11-18 22:05:49 +05:30
it 'requires a user' do
2015-04-26 12:48:37 +05:30
member.user = nil
expect(member).not_to be_valid
end
2021-11-18 22:05:49 +05:30
it 'is valid otherwise' do
2015-04-26 12:48:37 +05:30
expect(member).to be_valid
end
end
2019-02-15 15:39:39 +05:30
2021-10-27 15:23:28 +05:30
context 'with admin signup restrictions' do
2021-11-18 22:05:49 +05:30
let(:expected_message) { _('is not allowed for this group. Check with your administrator.') }
2021-10-27 15:23:28 +05:30
context 'when allowed domains for signup is enabled' do
before do
stub_application_setting(domain_allowlist: ['example.com'])
end
it 'adds an error message when email is not accepted' do
member = build(:group_member, :invited, invite_email: 'info@gitlab.com')
expect(member).not_to be_valid
2021-11-18 22:05:49 +05:30
expect(member.errors.messages[:user].first).to eq(expected_message)
2021-10-27 15:23:28 +05:30
end
end
context 'when denylist is enabled' do
before do
stub_application_setting(domain_denylist_enabled: true)
stub_application_setting(domain_denylist: ['example.org'])
end
it 'adds an error message when email is denied' do
member = build(:group_member, :invited, invite_email: 'denylist@example.org')
expect(member).not_to be_valid
2021-11-18 22:05:49 +05:30
expect(member.errors.messages[:user].first).to eq(expected_message)
2021-10-27 15:23:28 +05:30
end
end
context 'when email restrictions is enabled' do
before do
stub_application_setting(email_restrictions_enabled: true)
stub_application_setting(email_restrictions: '([\+]|\b(\w*gitlab.com\w*)\b)')
end
it 'adds an error message when email is not accepted' do
member = build(:group_member, :invited, invite_email: 'info@gitlab.com')
expect(member).not_to be_valid
2021-11-18 22:05:49 +05:30
expect(member.errors.messages[:user].first).to eq(expected_message)
2021-10-27 15:23:28 +05:30
end
end
end
2021-11-18 22:05:49 +05:30
context 'when a child member inherits its access level' do
2019-02-15 15:39:39 +05:30
let(:user) { create(:user) }
let(:member) { create(:group_member, :developer, user: user) }
let(:child_group) { create(:group, parent: member.group) }
let(:child_member) { build(:group_member, group: child_group, user: user) }
2021-11-18 22:05:49 +05:30
it 'requires a higher level' do
2019-02-15 15:39:39 +05:30
child_member.access_level = GroupMember::REPORTER
child_member.validate
expect(child_member).not_to be_valid
end
2019-07-07 11:18:12 +05:30
# Membership in a subgroup confers certain access rights, such as being
# able to merge or push code to protected branches.
2021-11-18 22:05:49 +05:30
it 'is valid with an equal level' do
2019-07-07 11:18:12 +05:30
child_member.access_level = GroupMember::DEVELOPER
child_member.validate
expect(child_member).to be_valid
end
2021-11-18 22:05:49 +05:30
it 'is valid with a higher level' do
2019-02-15 15:39:39 +05:30
child_member.access_level = GroupMember::MAINTAINER
child_member.validate
expect(child_member).to be_valid
end
end
2020-07-28 23:09:34 +05:30
context 'project bots' do
let_it_be(:project_bot) { create(:user, :project_bot) }
2021-09-30 23:02:18 +05:30
2020-07-28 23:09:34 +05:30
let(:new_member) { build(:project_member, user_id: project_bot.id) }
context 'not a member of any group or project' do
it 'is valid' do
expect(new_member).to be_valid
end
end
context 'already member of a project' do
before do
unrelated_project = create(:project)
unrelated_project.add_maintainer(project_bot)
end
it 'is not valid' do
expect(new_member).not_to be_valid
end
end
end
2015-04-26 12:48:37 +05:30
end
describe 'Scopes & finders' do
2020-10-24 23:57:45 +05:30
let_it_be(:project) { create(:project, :public) }
let_it_be(:group) { create(:group) }
2023-01-13 00:05:48 +05:30
let_it_be(:blocked_pending_approval_user) { create(:user, :blocked_pending_approval) }
2021-11-18 22:05:49 +05:30
let_it_be(:blocked_pending_approval_project_member) { create(:project_member, :invited, :developer, project: project, invite_email: blocked_pending_approval_user.email) }
2022-04-04 11:22:00 +05:30
let_it_be(:awaiting_group_member) { create(:group_member, :awaiting, group: group) }
let_it_be(:awaiting_project_member) { create(:project_member, :awaiting, project: project) }
2020-09-03 11:15:55 +05:30
2020-10-24 23:57:45 +05:30
before_all do
@owner_user = create(:user).tap { |u| group.add_owner(u) }
@owner = group.members.find_by(user_id: @owner_user.id)
2018-11-18 11:00:15 +05:30
@maintainer_user = create(:user).tap { |u| project.add_maintainer(u) }
@maintainer = project.members.find_by(user_id: @maintainer_user.id)
2021-04-17 20:07:23 +05:30
@blocked_maintainer_user = create(:user).tap do |u|
2018-11-18 11:00:15 +05:30
project.add_maintainer(u)
2021-04-17 20:07:23 +05:30
u.block!
end
@blocked_developer_user = create(:user).tap do |u|
2018-03-17 18:26:18 +05:30
project.add_developer(u)
2016-09-29 09:46:39 +05:30
u.block!
end
2021-04-17 20:07:23 +05:30
@blocked_maintainer = project.members.find_by(user_id: @blocked_maintainer_user.id, access_level: Gitlab::Access::MAINTAINER)
@blocked_developer = project.members.find_by(user_id: @blocked_developer_user.id, access_level: Gitlab::Access::DEVELOPER)
2016-09-29 09:46:39 +05:30
2021-06-08 01:23:25 +05:30
@invited_member = create(:project_member, :invited, :developer, project: project)
2016-09-29 09:46:39 +05:30
accepted_invite_user = build(:user, state: :active)
2021-06-08 01:23:25 +05:30
@accepted_invite_member = create(:project_member, :invited, :developer, project: project)
2023-03-04 22:38:38 +05:30
.tap { |u| u.accept_invite!(accepted_invite_user) }
requested_user = create(:user).tap { |u| project.request_access(u) }
2016-08-24 12:49:21 +05:30
@requested_member = project.requesters.find_by(user_id: requested_user.id)
accepted_request_user = create(:user).tap { |u| project.request_access(u) }
2023-03-17 16:20:25 +05:30
@accepted_request_member = project.requesters.find_by(user_id: accepted_request_user.id).tap { |m| m.accept_request(@owner_user) }
2020-11-24 15:15:51 +05:30
@member_with_minimal_access = create(:group_member, :minimal_access, source: group)
end
2016-09-13 17:45:13 +05:30
describe '.access_for_user_ids' do
it 'returns the right access levels' do
2021-04-17 20:07:23 +05:30
users = [@owner_user.id, @maintainer_user.id, @blocked_maintainer_user.id]
2016-09-13 17:45:13 +05:30
expected = {
@owner_user.id => Gitlab::Access::OWNER,
2018-11-18 11:00:15 +05:30
@maintainer_user.id => Gitlab::Access::MAINTAINER
2016-09-13 17:45:13 +05:30
}
expect(described_class.access_for_user_ids(users)).to eq(expected)
end
end
2021-02-11 23:33:58 +05:30
describe '.in_hierarchy' do
let(:root_ancestor) { create(:group) }
let(:project) { create(:project, group: root_ancestor) }
let(:subgroup) { create(:group, parent: root_ancestor) }
let(:subgroup_project) { create(:project, group: subgroup) }
let!(:root_ancestor_member) { create(:group_member, group: root_ancestor) }
let!(:project_member) { create(:project_member, project: project) }
let!(:subgroup_member) { create(:group_member, group: subgroup) }
let!(:subgroup_project_member) { create(:project_member, project: subgroup_project) }
let(:hierarchy_members) do
[
root_ancestor_member,
project_member,
subgroup_member,
subgroup_project_member
]
end
subject { Member.in_hierarchy(project) }
it { is_expected.to contain_exactly(*hierarchy_members) }
context 'with scope prefix' do
subject { Member.where.not(source: project).in_hierarchy(subgroup) }
it { is_expected.to contain_exactly(root_ancestor_member, subgroup_member, subgroup_project_member) }
end
context 'with scope suffix' do
subject { Member.in_hierarchy(project).where.not(source: project) }
it { is_expected.to contain_exactly(root_ancestor_member, subgroup_member, subgroup_project_member) }
end
end
describe '.invite' do
2018-11-18 11:00:15 +05:30
it { expect(described_class.invite).not_to include @maintainer }
it { expect(described_class.invite).to include @invited_member }
it { expect(described_class.invite).not_to include @accepted_invite_member }
it { expect(described_class.invite).not_to include @requested_member }
it { expect(described_class.invite).not_to include @accepted_request_member }
end
describe '.non_invite' do
2018-11-18 11:00:15 +05:30
it { expect(described_class.non_invite).to include @maintainer }
it { expect(described_class.non_invite).not_to include @invited_member }
it { expect(described_class.non_invite).to include @accepted_invite_member }
it { expect(described_class.non_invite).to include @requested_member }
it { expect(described_class.non_invite).to include @accepted_request_member }
end
2020-11-24 15:15:51 +05:30
describe '.non_minimal_access' do
it { expect(described_class.non_minimal_access).to include @maintainer }
it { expect(described_class.non_minimal_access).to include @invited_member }
it { expect(described_class.non_minimal_access).to include @accepted_invite_member }
it { expect(described_class.non_minimal_access).to include @requested_member }
it { expect(described_class.non_minimal_access).to include @accepted_request_member }
it { expect(described_class.non_minimal_access).not_to include @member_with_minimal_access }
end
describe '.request' do
2018-11-18 11:00:15 +05:30
it { expect(described_class.request).not_to include @maintainer }
it { expect(described_class.request).not_to include @invited_member }
it { expect(described_class.request).not_to include @accepted_invite_member }
it { expect(described_class.request).to include @requested_member }
it { expect(described_class.request).not_to include @accepted_request_member }
end
2017-08-17 22:00:37 +05:30
describe '.non_request' do
2018-11-18 11:00:15 +05:30
it { expect(described_class.non_request).to include @maintainer }
2017-08-17 22:00:37 +05:30
it { expect(described_class.non_request).to include @invited_member }
it { expect(described_class.non_request).to include @accepted_invite_member }
it { expect(described_class.non_request).not_to include @requested_member }
it { expect(described_class.non_request).to include @accepted_request_member }
end
2021-01-03 14:25:43 +05:30
describe '.not_accepted_invitations' do
let_it_be(:not_accepted_invitation) { create(:project_member, :invited) }
let_it_be(:accepted_invitation) { create(:project_member, :invited, invite_accepted_at: Date.today) }
subject { described_class.not_accepted_invitations }
it { is_expected.to include(not_accepted_invitation) }
it { is_expected.not_to include(accepted_invitation) }
end
2020-09-03 11:15:55 +05:30
describe '.not_accepted_invitations_by_user' do
let(:invited_by_user) { create(:project_member, :invited, project: project, created_by: @owner_user) }
before do
create(:project_member, :invited, invite_email: 'test@test.com', project: project, created_by: @owner_user, invite_accepted_at: Time.zone.now)
create(:project_member, :invited, invite_email: 'test2@test.com', project: project, created_by: @maintainer_user)
end
subject { described_class.not_accepted_invitations_by_user(@owner_user) }
it { is_expected.to contain_exactly(invited_by_user) }
end
2021-01-03 14:25:43 +05:30
describe '.not_expired' do
let_it_be(:expiring_yesterday) { create(:group_member, expires_at: 1.day.from_now) }
let_it_be(:expiring_today) { create(:group_member, expires_at: 2.days.from_now) }
let_it_be(:expiring_tomorrow) { create(:group_member, expires_at: 3.days.from_now) }
let_it_be(:not_expiring) { create(:group_member) }
subject { described_class.not_expired }
around do |example|
travel_to(2.days.from_now) { example.run }
end
it { is_expected.not_to include(expiring_yesterday, expiring_today) }
it { is_expected.to include(expiring_tomorrow, not_expiring) }
end
2021-02-11 23:33:58 +05:30
describe '.created_today' do
let_it_be(:now) { Time.current }
let_it_be(:created_today) { create(:group_member, created_at: now.beginning_of_day) }
let_it_be(:created_yesterday) { create(:group_member, created_at: now - 1.day) }
before do
travel_to now
end
subject { described_class.created_today }
it { is_expected.not_to include(created_yesterday) }
it { is_expected.to include(created_today) }
end
2021-01-03 14:25:43 +05:30
describe '.last_ten_days_excluding_today' do
2021-01-29 00:20:46 +05:30
let_it_be(:now) { Time.current }
let_it_be(:created_today) { create(:group_member, created_at: now.beginning_of_day) }
let_it_be(:created_yesterday) { create(:group_member, created_at: now - 1.day) }
let_it_be(:created_eleven_days_ago) { create(:group_member, created_at: now - 11.days) }
2021-01-03 14:25:43 +05:30
subject { described_class.last_ten_days_excluding_today }
2021-01-29 00:20:46 +05:30
before do
travel_to now
end
2021-01-03 14:25:43 +05:30
it { is_expected.to include(created_yesterday) }
it { is_expected.not_to include(created_today, created_eleven_days_ago) }
end
2019-10-12 21:52:04 +05:30
describe '.search_invite_email' do
it 'returns only members the matching e-mail' do
2021-06-08 01:23:25 +05:30
invited_member = create(:group_member, :invited, invite_email: 'invited@example.com')
2019-10-12 21:52:04 +05:30
2021-06-08 01:23:25 +05:30
invited = described_class.search_invite_email(invited_member.invite_email)
2019-10-12 21:52:04 +05:30
expect(invited.count).to eq(1)
2021-06-08 01:23:25 +05:30
expect(invited.first).to eq(invited_member)
2019-10-12 21:52:04 +05:30
expect(described_class.search_invite_email('bad-email@example.com').count).to eq(0)
end
end
2022-07-16 23:28:13 +05:30
describe '.by_access_level' do
subject { described_class.by_access_level(access_levels) }
context 'by owner' do
let(:access_levels) { [Gitlab::Access::OWNER] }
it { is_expected.to include @owner }
it { is_expected.not_to include @maintainer }
it { is_expected.not_to include @invited_member }
it { is_expected.not_to include @accepted_invite_member }
it { is_expected.not_to include @requested_member }
it { is_expected.not_to include @accepted_requested_member }
it { is_expected.not_to include @blocked_maintainer }
it { is_expected.not_to include @blocked_developer }
end
context 'by maintainer' do
let(:access_levels) { [Gitlab::Access::MAINTAINER] }
it { is_expected.not_to include @owner }
it { is_expected.to include @maintainer }
it { is_expected.not_to include @invited_member }
it { is_expected.not_to include @accepted_invite_member }
it { is_expected.not_to include @requested_member }
it { is_expected.not_to include @accepted_requested_member }
it { is_expected.not_to include @blocked_maintainer }
it { is_expected.not_to include @blocked_developer }
end
context 'by developer' do
let(:access_levels) { [Gitlab::Access::DEVELOPER] }
it { is_expected.not_to include @owner }
it { is_expected.not_to include @maintainer }
it { is_expected.to include @invited_member }
it { is_expected.to include @accepted_invite_member }
it { is_expected.not_to include @requested_member }
it { is_expected.not_to include @accepted_requested_member }
it { is_expected.not_to include @blocked_maintainer }
it { is_expected.not_to include @blocked_developer }
end
context 'by owner and maintainer' do
let(:access_levels) { [Gitlab::Access::OWNER, Gitlab::Access::MAINTAINER] }
it { is_expected.to include @owner }
it { is_expected.to include @maintainer }
it { is_expected.not_to include @invited_member }
it { is_expected.not_to include @accepted_invite_member }
it { is_expected.not_to include @requested_member }
it { is_expected.not_to include @accepted_requested_member }
it { is_expected.not_to include @blocked_maintainer }
it { is_expected.not_to include @blocked_developer }
end
context 'by owner, maintainer and developer' do
let(:access_levels) { [Gitlab::Access::OWNER, Gitlab::Access::MAINTAINER, Gitlab::Access::DEVELOPER] }
it { is_expected.to include @owner }
it { is_expected.to include @maintainer }
it { is_expected.to include @invited_member }
it { is_expected.to include @accepted_invite_member }
it { is_expected.not_to include @requested_member }
it { is_expected.not_to include @accepted_requested_member }
it { is_expected.not_to include @blocked_maintainer }
it { is_expected.not_to include @blocked_developer }
end
end
2016-09-29 09:46:39 +05:30
describe '.developers' do
subject { described_class.developers.to_a }
it { is_expected.not_to include @owner }
2018-11-18 11:00:15 +05:30
it { is_expected.not_to include @maintainer }
2016-09-29 09:46:39 +05:30
it { is_expected.to include @invited_member }
it { is_expected.to include @accepted_invite_member }
it { is_expected.not_to include @requested_member }
it { is_expected.to include @accepted_request_member }
2018-11-18 11:00:15 +05:30
it { is_expected.not_to include @blocked_maintainer }
2016-09-29 09:46:39 +05:30
it { is_expected.not_to include @blocked_developer }
end
2018-11-18 11:00:15 +05:30
describe '.owners_and_maintainers' do
it { expect(described_class.owners_and_maintainers).to include @owner }
it { expect(described_class.owners_and_maintainers).to include @maintainer }
it { expect(described_class.owners_and_maintainers).not_to include @invited_member }
it { expect(described_class.owners_and_maintainers).not_to include @accepted_invite_member }
it { expect(described_class.owners_and_maintainers).not_to include @requested_member }
it { expect(described_class.owners_and_maintainers).not_to include @accepted_request_member }
it { expect(described_class.owners_and_maintainers).not_to include @blocked_maintainer }
2016-09-29 09:46:39 +05:30
end
describe '.has_access' do
subject { described_class.has_access.to_a }
it { is_expected.to include @owner }
2018-11-18 11:00:15 +05:30
it { is_expected.to include @maintainer }
2016-09-29 09:46:39 +05:30
it { is_expected.to include @invited_member }
it { is_expected.to include @accepted_invite_member }
it { is_expected.not_to include @requested_member }
it { is_expected.to include @accepted_request_member }
2018-11-18 11:00:15 +05:30
it { is_expected.not_to include @blocked_maintainer }
2016-09-29 09:46:39 +05:30
it { is_expected.not_to include @blocked_developer }
end
2020-11-24 15:15:51 +05:30
describe '.active' do
subject { described_class.active.to_a }
it { is_expected.to include @owner }
it { is_expected.to include @maintainer }
it { is_expected.to include @invited_member }
it { is_expected.to include @accepted_invite_member }
it { is_expected.not_to include @requested_member }
it { is_expected.to include @accepted_request_member }
it { is_expected.not_to include @blocked_maintainer }
it { is_expected.not_to include @blocked_developer }
it { is_expected.not_to include @member_with_minimal_access }
end
2021-04-17 20:07:23 +05:30
describe '.blocked' do
subject { described_class.blocked.to_a }
it { is_expected.not_to include @owner }
it { is_expected.not_to include @maintainer }
it { is_expected.not_to include @invited_member }
it { is_expected.not_to include @accepted_invite_member }
it { is_expected.not_to include @requested_member }
it { is_expected.not_to include @accepted_request_member }
it { is_expected.to include @blocked_maintainer }
it { is_expected.to include @blocked_developer }
it { is_expected.not_to include @member_with_minimal_access }
end
2020-11-24 15:15:51 +05:30
describe '.active_without_invites_and_requests' do
subject { described_class.active_without_invites_and_requests.to_a }
it { is_expected.to include @owner }
it { is_expected.to include @maintainer }
it { is_expected.not_to include @invited_member }
it { is_expected.to include @accepted_invite_member }
it { is_expected.not_to include @requested_member }
it { is_expected.to include @accepted_request_member }
it { is_expected.not_to include @blocked_maintainer }
it { is_expected.not_to include @blocked_developer }
it { is_expected.not_to include @member_with_minimal_access }
end
2021-04-29 21:17:54 +05:30
2021-09-04 01:27:46 +05:30
describe '.without_invites_and_requests' do
subject { described_class.without_invites_and_requests.to_a }
it { is_expected.to include @owner }
it { is_expected.to include @maintainer }
it { is_expected.not_to include @invited_member }
it { is_expected.to include @accepted_invite_member }
it { is_expected.not_to include @requested_member }
it { is_expected.to include @accepted_request_member }
it { is_expected.to include @blocked_maintainer }
it { is_expected.to include @blocked_developer }
it { is_expected.not_to include @member_with_minimal_access }
2022-04-04 11:22:00 +05:30
it { is_expected.not_to include awaiting_group_member }
it { is_expected.not_to include awaiting_project_member }
2021-09-04 01:27:46 +05:30
end
2021-06-08 01:23:25 +05:30
describe '.connected_to_user' do
subject { described_class.connected_to_user.to_a }
it { is_expected.to include @owner }
it { is_expected.to include @maintainer }
it { is_expected.to include @accepted_invite_member }
it { is_expected.to include @accepted_request_member }
it { is_expected.to include @blocked_maintainer }
it { is_expected.to include @blocked_developer }
it { is_expected.to include @requested_member }
it { is_expected.to include @member_with_minimal_access }
it { is_expected.not_to include @invited_member }
end
describe '.authorizable' do
subject { described_class.authorizable.to_a }
it 'includes the member who has an associated user record,'\
2023-03-04 22:38:38 +05:30
'but also having an invite_token' do
member = create(:project_member,
:developer,
:invited,
user: create(:user))
2021-06-08 01:23:25 +05:30
2023-03-04 22:38:38 +05:30
expect(subject).to include(member)
end
2021-06-08 01:23:25 +05:30
it { is_expected.to include @owner }
it { is_expected.to include @maintainer }
it { is_expected.to include @accepted_invite_member }
it { is_expected.to include @accepted_request_member }
it { is_expected.to include @blocked_maintainer }
it { is_expected.to include @blocked_developer }
it { is_expected.not_to include @invited_member }
it { is_expected.not_to include @requested_member }
it { is_expected.not_to include @member_with_minimal_access }
2022-04-04 11:22:00 +05:30
it { is_expected.not_to include awaiting_group_member }
it { is_expected.not_to include awaiting_project_member }
2021-06-08 01:23:25 +05:30
end
2021-04-29 21:17:54 +05:30
describe '.distinct_on_user_with_max_access_level' do
let_it_be(:other_group) { create(:group) }
let_it_be(:member_with_lower_access_level) { create(:group_member, :developer, group: other_group, user: @owner_user) }
subject { described_class.default_scoped.distinct_on_user_with_max_access_level.to_a }
it { is_expected.not_to include member_with_lower_access_level }
it { is_expected.to include @owner }
it { is_expected.to include @maintainer }
it { is_expected.to include @invited_member }
it { is_expected.to include @accepted_invite_member }
it { is_expected.to include @requested_member }
it { is_expected.to include @accepted_request_member }
it { is_expected.to include @blocked_maintainer }
it { is_expected.to include @blocked_developer }
it { is_expected.to include @member_with_minimal_access }
2021-09-30 23:02:18 +05:30
context 'with where conditions' do
let_it_be(:example_member) { create(:group_member, invite_email: 'user@example.com') }
subject do
described_class
.default_scoped
.where(invite_email: 'user@example.com')
.distinct_on_user_with_max_access_level
.to_a
end
it { is_expected.to eq [example_member] }
end
2021-04-29 21:17:54 +05:30
end
2021-11-18 22:05:49 +05:30
describe '.with_invited_user_state' do
subject(:with_invited_user_state) { described_class.with_invited_user_state }
it { is_expected.to include @owner }
it { is_expected.to include @maintainer }
it { is_expected.to include @invited_member }
it { is_expected.to include @accepted_invite_member }
it { is_expected.to include @requested_member }
it { is_expected.to include @accepted_request_member }
context 'with invited pending members' do
it 'includes invited user state' do
invited_pending_members = with_invited_user_state.select { |m| m.invited_user_state.present? }
expect(invited_pending_members.count).to eq 1
expect(invited_pending_members).to include blocked_pending_approval_project_member
end
end
end
2022-04-04 11:22:00 +05:30
describe '.active_state' do
let_it_be(:active_group_member) { create(:group_member, group: group) }
let_it_be(:active_project_member) { create(:project_member, project: project) }
it 'includes members with an active state' do
expect(group.members.active_state).to include active_group_member
expect(project.members.active_state).to include active_project_member
end
it 'does not include members with an awaiting state' do
expect(group.members.active_state).not_to include awaiting_group_member
expect(project.members.active_state).not_to include awaiting_project_member
end
end
2022-07-16 23:28:13 +05:30
describe '.excluding_users' do
let_it_be(:active_group_member) { create(:group_member, group: group) }
it 'excludes members with given user ids' do
expect(group.members.excluding_users([])).to include active_group_member
expect(group.members.excluding_users(active_group_member.user_id)).not_to include active_group_member
end
end
end
2021-11-18 22:05:49 +05:30
describe 'Delegate methods' do
2015-04-26 12:48:37 +05:30
it { is_expected.to respond_to(:user_name) }
it { is_expected.to respond_to(:user_email) }
end
2021-04-29 21:17:54 +05:30
describe '.valid_email?' do
it 'is a valid email format' do
expect(described_class.valid_email?('foo')).to eq(false)
end
it 'is not a valid email format' do
expect(described_class.valid_email?('foo@example.com')).to eq(true)
end
end
2023-05-27 22:25:52 +05:30
describe '.filter_by_user_type' do
let_it_be(:service_account) { create(:user, :service_account) }
let_it_be(:service_account_member) { create(:group_member, user: service_account) }
let_it_be(:other_member) { create(:group_member) }
context 'when the user type is valid' do
it 'returns service accounts' do
expect(described_class.filter_by_user_type('service_account')).to match_array([service_account_member])
end
end
context 'when the user type is invalid' do
it 'returns nil' do
expect(described_class.filter_by_user_type('invalid_type')).to eq(nil)
end
end
end
describe '#accept_request' do
2020-06-23 00:09:42 +05:30
let(:member) { create(:project_member, requested_at: Time.current.utc) }
2023-03-17 16:20:25 +05:30
it { expect(member.accept_request(@owner_user)).to be_truthy }
it { expect(member.accept_request(nil)).to be_truthy }
it 'clears requested_at' do
2023-03-17 16:20:25 +05:30
member.accept_request(@owner_user)
expect(member.requested_at).to be_nil
end
2023-03-17 16:20:25 +05:30
it 'saves the approving user' do
member.accept_request(@owner_user)
expect(member.created_by).to eq(@owner_user)
end
it 'calls #after_accept_request' do
expect(member).to receive(:after_accept_request)
2023-03-17 16:20:25 +05:30
member.accept_request(@owner_user)
end
end
describe '#invite?' do
subject { create(:project_member, invite_email: "user@example.com", user: nil) }
it { is_expected.to be_invite }
end
describe '#request?' do
2023-03-17 16:20:25 +05:30
shared_examples 'calls notification service and todo service' do
subject { create(source_type, requested_at: Time.current.utc) }
2023-03-04 22:38:38 +05:30
2023-03-17 16:20:25 +05:30
specify do
2023-03-04 22:38:38 +05:30
expect_next_instance_of(NotificationService) do |instance|
expect(instance).to receive(:new_access_request)
end
2023-03-17 16:20:25 +05:30
expect_next_instance_of(TodoService) do |instance|
expect(instance).to receive(:create_member_access_request_todos)
end
2023-03-04 22:38:38 +05:30
is_expected.to be_request
end
end
2023-03-17 16:20:25 +05:30
context 'when requests for project and group are raised' do
%i[project_member group_member].each do |source_type|
it_behaves_like 'calls notification service and todo service' do
let_it_be(:source_type) { source_type }
2023-03-04 22:38:38 +05:30
end
end
end
end
describe '#pending?' do
let(:invited_member) { create(:project_member, invite_email: "user@example.com", user: nil) }
2020-06-23 00:09:42 +05:30
let(:requester) { create(:project_member, requested_at: Time.current.utc) }
2021-09-04 01:27:46 +05:30
it { expect(invited_member).to be_pending }
it { expect(requester).to be_pending }
end
2021-09-04 01:27:46 +05:30
describe '#hook_prerequisites_met?' do
let(:member) { create(:project_member) }
context 'when the member does not have an associated user' do
it 'returns false' do
member.update_column(:user_id, nil)
expect(member.reload.hook_prerequisites_met?).to eq(false)
end
end
context 'when the member has an associated user' do
it 'returns true' do
expect(member.hook_prerequisites_met?).to eq(true)
end
end
end
2021-11-18 22:05:49 +05:30
describe '#accept_invite!' do
2015-04-26 12:48:37 +05:30
let!(:member) { create(:project_member, invite_email: "user@example.com", user: nil) }
let(:user) { create(:user) }
2021-11-18 22:05:49 +05:30
it 'resets the invite token' do
2015-04-26 12:48:37 +05:30
member.accept_invite!(user)
expect(member.invite_token).to be_nil
end
2021-11-18 22:05:49 +05:30
it 'sets the invite accepted timestamp' do
2015-04-26 12:48:37 +05:30
member.accept_invite!(user)
expect(member.invite_accepted_at).not_to be_nil
end
2021-11-18 22:05:49 +05:30
it 'sets the user' do
2015-04-26 12:48:37 +05:30
member.accept_invite!(user)
expect(member.user).to eq(user)
end
2021-11-18 22:05:49 +05:30
it 'calls #after_accept_invite' do
2015-04-26 12:48:37 +05:30
expect(member).to receive(:after_accept_invite)
member.accept_invite!(user)
end
2017-08-17 22:00:37 +05:30
2022-06-21 17:19:12 +05:30
context 'authorized projects' do
let(:project) { member.source }
2017-08-17 22:00:37 +05:30
2022-06-21 17:19:12 +05:30
before do
expect(user.authorized_projects).not_to include(project)
end
2017-08-17 22:00:37 +05:30
2023-04-23 21:23:45 +05:30
it 'successfully completes a refresh', :delete, :sidekiq_inline do
expect(member).to receive(:refresh_member_authorized_projects).and_call_original
2022-06-21 17:19:12 +05:30
member.accept_invite!(user)
expect(user.authorized_projects.reload).to include(project)
end
2017-08-17 22:00:37 +05:30
end
2021-11-11 11:23:49 +05:30
it 'does not accept the invite if saving a new user fails' do
invalid_user = User.new(first_name: '', last_name: '')
member.accept_invite! invalid_user
expect(member.invite_accepted_at).to be_nil
expect(member.invite_token).not_to be_nil
expect_any_instance_of(Member).not_to receive(:after_accept_invite)
end
2021-12-11 22:18:48 +05:30
it 'schedules a TasksToBeDone::CreateWorker task' do
member_task = create(:member_task, member: member, project: member.project)
expect(TasksToBeDone::CreateWorker)
.to receive(:perform_async)
.with(member_task.id, member.created_by_id, [user.id])
.once
member.accept_invite!(user)
end
2015-04-26 12:48:37 +05:30
end
2021-11-18 22:05:49 +05:30
describe '#decline_invite!' do
2015-04-26 12:48:37 +05:30
let!(:member) { create(:project_member, invite_email: "user@example.com", user: nil) }
2021-11-18 22:05:49 +05:30
it 'destroys the member' do
2015-04-26 12:48:37 +05:30
member.decline_invite!
expect(member).to be_destroyed
end
2021-11-18 22:05:49 +05:30
it 'calls #after_decline_invite' do
2015-04-26 12:48:37 +05:30
expect(member).to receive(:after_decline_invite)
member.decline_invite!
end
end
2021-11-18 22:05:49 +05:30
describe '#generate_invite_token' do
2015-04-26 12:48:37 +05:30
let!(:member) { create(:project_member, invite_email: "user@example.com", user: nil) }
2016-04-02 18:10:28 +05:30
2021-11-18 22:05:49 +05:30
it 'sets the invite token' do
2021-10-27 15:23:28 +05:30
expect { member.generate_invite_token }.to change { member.invite_token }
end
end
describe 'generate invite token on create' do
2022-10-11 01:57:18 +05:30
let(:project) { create(:project) }
let!(:member) { build(:project_member, invite_email: "user@example.com", project: project) }
2021-10-27 15:23:28 +05:30
2021-11-18 22:05:49 +05:30
it 'sets the invite token' do
2021-10-27 15:23:28 +05:30
expect { member.save! }.to change { member.invite_token }.to(kind_of(String))
end
context 'when invite was already accepted' do
2021-11-18 22:05:49 +05:30
it 'does not set invite token' do
2021-10-27 15:23:28 +05:30
member.invite_accepted_at = 1.day.ago
expect { member.save! }.not_to change { member.invite_token }.from(nil)
end
2015-04-26 12:48:37 +05:30
end
end
2017-08-17 22:00:37 +05:30
2020-11-24 15:15:51 +05:30
describe '.find_by_invite_token' do
let!(:member) { create(:project_member, invite_email: "user@example.com", user: nil) }
it 'finds the member' do
expect(described_class.find_by_invite_token(member.raw_invite_token)).to eq member
end
end
2023-06-20 00:43:36 +05:30
describe '.pluck_user_ids' do
let(:member) { create(:group_member) }
it 'plucks the user ids' do
expect(described_class.where(id: member).pluck_user_ids).to match([member.user_id])
end
end
2021-01-03 14:25:43 +05:30
describe '#send_invitation_reminder' do
subject { member.send_invitation_reminder(0) }
context 'an invited group member' do
let!(:member) { create(:group_member, :invited) }
it 'sends a reminder' do
expect_any_instance_of(NotificationService).to receive(:invite_member_reminder).with(member, member.raw_invite_token, 0)
subject
end
end
context 'an invited member without a raw invite token set' do
let!(:member) { create(:group_member, :invited) }
before do
member.instance_variable_set(:@raw_invite_token, nil)
allow_any_instance_of(NotificationService).to receive(:invite_member_reminder)
end
it 'generates a new token' do
expect(member).to receive(:generate_invite_token!)
subject
end
end
context 'an uninvited member' do
let!(:member) { create(:group_member) }
it 'does not send a reminder' do
expect_any_instance_of(NotificationService).not_to receive(:invite_member_reminder)
subject
end
end
end
2021-11-18 22:05:49 +05:30
describe '#invite_to_unknown_user?' do
2020-11-24 15:15:51 +05:30
subject { member.invite_to_unknown_user? }
let(:member) { create(:project_member, invite_email: "user@example.com", invite_token: '1234', user: user) }
context 'when user is nil' do
let(:user) { nil }
it { is_expected.to eq(true) }
end
context 'when user is set' do
let(:user) { build(:user) }
it { is_expected.to eq(false) }
end
end
2021-11-18 22:05:49 +05:30
describe 'destroying a record', :delete, :sidekiq_inline do
2017-08-17 22:00:37 +05:30
it "refreshes user's authorized projects" do
2017-09-10 17:25:29 +05:30
project = create(:project, :private)
2017-08-17 22:00:37 +05:30
user = create(:user)
2018-03-17 18:26:18 +05:30
member = project.add_reporter(user)
2017-08-17 22:00:37 +05:30
2021-06-08 01:23:25 +05:30
member.destroy!
2017-08-17 22:00:37 +05:30
expect(user.authorized_projects).not_to include(project)
end
end
2020-04-22 19:07:51 +05:30
context 'when after_commit :update_highest_role' do
2021-09-30 23:02:18 +05:30
let_it_be(:user) { create(:user) }
2020-04-22 19:07:51 +05:30
let(:user_id) { user.id }
where(:member_type, :source_type) do
:project_member | :project
:group_member | :group
end
with_them do
describe 'create member' do
2021-06-08 01:23:25 +05:30
let!(:source) { create(source_type) } # rubocop:disable Rails/SaveBang
2020-04-22 19:07:51 +05:30
2020-11-24 15:15:51 +05:30
subject { create(member_type, :guest, user: user, source: source) }
2020-04-22 19:07:51 +05:30
include_examples 'update highest role with exclusive lease'
end
context 'when member exists' do
let!(:member) { create(member_type, user: user) }
describe 'update member' do
context 'when access level was changed' do
2021-06-08 01:23:25 +05:30
subject { member.update!(access_level: Gitlab::Access::GUEST) }
2020-04-22 19:07:51 +05:30
include_examples 'update highest role with exclusive lease'
end
context 'when access level was not changed' do
2021-06-08 01:23:25 +05:30
subject { member.update!(notification_level: NotificationSetting.levels[:disabled]) }
2020-04-22 19:07:51 +05:30
include_examples 'does not update the highest role'
end
end
describe 'destroy member' do
2021-09-30 23:02:18 +05:30
subject { member.reload.destroy! }
2020-04-22 19:07:51 +05:30
include_examples 'update highest role with exclusive lease'
end
end
end
end
2021-10-27 15:23:28 +05:30
describe 'log_invitation_token_cleanup' do
let_it_be(:project) { create :project }
context 'when on gitlab.com' do
before do
allow(Gitlab).to receive(:com?).and_return true
end
it "doesn't log info for members without invitation or accepted invitation" do
expect(Gitlab::ErrorTracking).not_to receive(:track_exception)
create :project_member
create :project_member, :invited, invite_accepted_at: nil
create :project_member, invite_token: nil, invite_accepted_at: Time.zone.now
end
it 'logs error for accepted members with token and creates membership' do
expect(Gitlab::ErrorTracking).to receive(:track_exception).with(kind_of(StandardError), kind_of(Hash))
expect do
create :project_member, :invited, source: project, invite_accepted_at: Time.zone.now
end.to change { Member.count }.by(1)
end
end
context 'when not on gitlab.com' do
before do
allow(Gitlab).to receive(:com?).and_return false
end
it 'does not log error for accepted members with token and creates membership' do
expect(Gitlab::ErrorTracking).not_to receive(:track_exception)
expect do
create :project_member, :invited, source: project, invite_accepted_at: Time.zone.now
end.to change { Member.count }.by(1)
end
end
end
2022-04-04 11:22:00 +05:30
describe '#set_member_namespace_id' do
let(:group) { create(:group) }
let(:member) { create(:group_member, group: group) }
describe 'on create' do
it 'sets the member_namespace_id' do
expect(member.member_namespace_id).to eq group.id
end
end
end
2022-06-21 17:19:12 +05:30
describe '.sort_by_attribute' do
let_it_be(:user1) { create(:user, created_at: Date.today, last_sign_in_at: Date.today, last_activity_on: Date.today, name: 'Alpha') }
let_it_be(:user2) { create(:user, created_at: Date.today - 1, last_sign_in_at: Date.today - 1, last_activity_on: Date.today - 1, name: 'Omega') }
let_it_be(:user3) { create(:user, created_at: Date.today - 2, name: 'Beta') }
let_it_be(:group) { create(:group) }
let_it_be(:member1) { create(:group_member, :reporter, group: group, user: user1) }
let_it_be(:member2) { create(:group_member, :developer, group: group, user: user2) }
let_it_be(:member3) { create(:group_member, :maintainer, group: group, user: user3) }
it 'sort users in ascending order by access-level' do
expect(described_class.sort_by_attribute('access_level_asc')).to eq([member1, member2, member3])
end
it 'sort users in descending order by access-level' do
expect(described_class.sort_by_attribute('access_level_desc')).to eq([member3, member2, member1])
end
context 'when sort by recent_sign_in' do
subject { described_class.sort_by_attribute('recent_sign_in') }
it 'sorts users by recent sign-in time' do
expect(subject.first).to eq(member1)
expect(subject.second).to eq(member2)
end
it 'pushes users who never signed in to the end' do
expect(subject.third).to eq(member3)
end
end
context 'when sort by oldest_sign_in' do
subject { described_class.sort_by_attribute('oldest_sign_in') }
it 'sorts users by the oldest sign-in time' do
expect(subject.first).to eq(member2)
expect(subject.second).to eq(member1)
end
it 'pushes users who never signed in to the end' do
expect(subject.third).to eq(member3)
end
end
it 'sorts users in descending order by their creation time' do
expect(described_class.sort_by_attribute('recent_created_user')).to eq([member1, member2, member3])
end
it 'sorts users in ascending order by their creation time' do
expect(described_class.sort_by_attribute('oldest_created_user')).to eq([member3, member2, member1])
end
it 'sort users by recent last activity' do
expect(described_class.sort_by_attribute('recent_last_activity')).to eq([member1, member2, member3])
end
it 'sort users by oldest last activity' do
expect(described_class.sort_by_attribute('oldest_last_activity')).to eq([member3, member2, member1])
end
end
2015-04-26 12:48:37 +05:30
end