2019-03-30 07:23:56 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2014-09-14 14:54:10 +00:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2017-07-10 14:24:02 +00:00
|
|
|
describe Member do
|
2014-09-14 14:54:10 +00:00
|
|
|
describe "Associations" do
|
2015-02-12 18:17:35 +00:00
|
|
|
it { is_expected.to belong_to(:user) }
|
2014-09-14 14:54:10 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
describe "Validation" do
|
2017-07-25 17:09:00 +00:00
|
|
|
subject { described_class.new(access_level: Member::GUEST) }
|
2014-09-14 14:54:10 +00:00
|
|
|
|
2015-02-12 18:17:35 +00:00
|
|
|
it { is_expected.to validate_presence_of(:user) }
|
|
|
|
it { is_expected.to validate_presence_of(:source) }
|
2016-06-23 15:14:31 +00:00
|
|
|
it { is_expected.to validate_inclusion_of(:access_level).in_array(Gitlab::Access.all_values) }
|
2015-04-10 13:09:37 +00:00
|
|
|
|
2016-02-09 14:51:06 +00:00
|
|
|
it_behaves_like 'an object with email-formated attributes', :invite_email do
|
|
|
|
subject { build(:project_member) }
|
|
|
|
end
|
|
|
|
|
2015-04-10 13:09:37 +00:00
|
|
|
context "when an invite email is provided" do
|
|
|
|
let(:member) { build(:project_member, invite_email: "user@example.com", user: nil) }
|
|
|
|
|
|
|
|
it "doesn't require a user" do
|
|
|
|
expect(member).to be_valid
|
|
|
|
end
|
|
|
|
|
|
|
|
it "requires a valid invite email" do
|
|
|
|
member.invite_email = "nope"
|
|
|
|
|
|
|
|
expect(member).not_to be_valid
|
|
|
|
end
|
|
|
|
|
|
|
|
it "requires a unique invite email scoped to this source" do
|
|
|
|
create(:project_member, source: member.source, invite_email: member.invite_email)
|
|
|
|
|
|
|
|
expect(member).not_to be_valid
|
|
|
|
end
|
|
|
|
|
|
|
|
it "is valid otherwise" do
|
|
|
|
expect(member).to be_valid
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when an invite email is not provided" do
|
|
|
|
let(:member) { build(:project_member) }
|
|
|
|
|
|
|
|
it "requires a user" do
|
|
|
|
member.user = nil
|
|
|
|
|
|
|
|
expect(member).not_to be_valid
|
|
|
|
end
|
|
|
|
|
|
|
|
it "is valid otherwise" do
|
|
|
|
expect(member).to be_valid
|
|
|
|
end
|
|
|
|
end
|
2018-12-06 13:15:29 +00:00
|
|
|
|
|
|
|
context "when a child member inherits its access level" do
|
|
|
|
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) }
|
|
|
|
|
|
|
|
it "requires a higher level" do
|
|
|
|
child_member.access_level = GroupMember::REPORTER
|
|
|
|
|
|
|
|
child_member.validate
|
|
|
|
|
|
|
|
expect(child_member).not_to be_valid
|
|
|
|
end
|
|
|
|
|
2019-04-30 18:39:15 +00:00
|
|
|
# Membership in a subgroup confers certain access rights, such as being
|
|
|
|
# able to merge or push code to protected branches.
|
|
|
|
it "is valid with an equal level" do
|
|
|
|
child_member.access_level = GroupMember::DEVELOPER
|
|
|
|
|
|
|
|
child_member.validate
|
|
|
|
|
|
|
|
expect(child_member).to be_valid
|
|
|
|
end
|
|
|
|
|
2018-12-06 13:15:29 +00:00
|
|
|
it "is valid with a higher level" do
|
|
|
|
child_member.access_level = GroupMember::MAINTAINER
|
|
|
|
|
|
|
|
child_member.validate
|
|
|
|
|
|
|
|
expect(child_member).to be_valid
|
|
|
|
end
|
|
|
|
end
|
2014-09-14 14:54:10 +00:00
|
|
|
end
|
2014-09-14 16:32:51 +00:00
|
|
|
|
2016-06-02 16:05:06 +00:00
|
|
|
describe 'Scopes & finders' do
|
2016-04-18 16:53:32 +00:00
|
|
|
before do
|
2017-08-02 19:55:11 +00:00
|
|
|
project = create(:project, :public, :access_requestable)
|
2016-06-02 16:05:06 +00:00
|
|
|
group = create(:group)
|
|
|
|
@owner_user = create(:user).tap { |u| group.add_owner(u) }
|
|
|
|
@owner = group.members.find_by(user_id: @owner_user.id)
|
|
|
|
|
2018-07-11 14:36:08 +00:00
|
|
|
@maintainer_user = create(:user).tap { |u| project.add_maintainer(u) }
|
|
|
|
@maintainer = project.members.find_by(user_id: @maintainer_user.id)
|
2016-06-02 16:05:06 +00:00
|
|
|
|
2016-09-05 15:37:26 +00:00
|
|
|
@blocked_user = create(:user).tap do |u|
|
2018-07-11 14:36:08 +00:00
|
|
|
project.add_maintainer(u)
|
2017-12-22 08:18:28 +00:00
|
|
|
project.add_developer(u)
|
2016-09-05 15:37:26 +00:00
|
|
|
|
|
|
|
u.block!
|
|
|
|
end
|
2018-07-11 14:36:08 +00:00
|
|
|
@blocked_maintainer = project.members.find_by(user_id: @blocked_user.id, access_level: Gitlab::Access::MAINTAINER)
|
2016-09-05 15:37:26 +00:00
|
|
|
@blocked_developer = project.members.find_by(user_id: @blocked_user.id, access_level: Gitlab::Access::DEVELOPER)
|
|
|
|
|
2016-09-16 15:54:21 +00:00
|
|
|
@invited_member = create(:project_member, :developer,
|
|
|
|
project: project,
|
|
|
|
invite_token: '1234',
|
|
|
|
invite_email: 'toto1@example.com')
|
2016-06-02 16:05:06 +00:00
|
|
|
|
2016-09-05 15:37:26 +00:00
|
|
|
accepted_invite_user = build(:user, state: :active)
|
2016-09-16 15:54:21 +00:00
|
|
|
@accepted_invite_member = create(:project_member, :developer,
|
|
|
|
project: project,
|
|
|
|
invite_token: '1234',
|
2017-06-21 13:48:12 +00:00
|
|
|
invite_email: 'toto2@example.com')
|
|
|
|
.tap { |u| u.accept_invite!(accepted_invite_user) }
|
2016-04-18 16:53:32 +00:00
|
|
|
|
|
|
|
requested_user = create(:user).tap { |u| project.request_access(u) }
|
2016-06-27 14:20:57 +00:00
|
|
|
@requested_member = project.requesters.find_by(user_id: requested_user.id)
|
2016-06-02 16:05:06 +00:00
|
|
|
|
2016-04-18 16:53:32 +00:00
|
|
|
accepted_request_user = create(:user).tap { |u| project.request_access(u) }
|
2016-06-27 14:20:57 +00:00
|
|
|
@accepted_request_member = project.requesters.find_by(user_id: accepted_request_user.id).tap { |m| m.accept_request }
|
2016-04-18 16:53:32 +00:00
|
|
|
end
|
|
|
|
|
2016-07-27 00:20:19 +00:00
|
|
|
describe '.access_for_user_ids' do
|
|
|
|
it 'returns the right access levels' do
|
2018-07-11 14:36:08 +00:00
|
|
|
users = [@owner_user.id, @maintainer_user.id, @blocked_user.id]
|
2016-07-27 00:20:19 +00:00
|
|
|
expected = {
|
|
|
|
@owner_user.id => Gitlab::Access::OWNER,
|
2018-07-11 14:36:08 +00:00
|
|
|
@maintainer_user.id => Gitlab::Access::MAINTAINER
|
2016-07-27 00:20:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
expect(described_class.access_for_user_ids(users)).to eq(expected)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-06-02 16:05:06 +00:00
|
|
|
describe '.invite' do
|
2018-07-11 14:36:08 +00:00
|
|
|
it { expect(described_class.invite).not_to include @maintainer }
|
2016-04-18 16:53:32 +00:00
|
|
|
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
|
|
|
|
|
2016-06-02 16:05:06 +00:00
|
|
|
describe '.non_invite' do
|
2018-07-11 14:36:08 +00:00
|
|
|
it { expect(described_class.non_invite).to include @maintainer }
|
2016-06-02 16:05:06 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
describe '.request' do
|
2018-07-11 14:36:08 +00:00
|
|
|
it { expect(described_class.request).not_to include @maintainer }
|
2016-04-18 16:53:32 +00:00
|
|
|
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-02-08 15:02:25 +00:00
|
|
|
describe '.non_request' do
|
2018-07-11 14:36:08 +00:00
|
|
|
it { expect(described_class.non_request).to include @maintainer }
|
2017-02-08 15:02:25 +00:00
|
|
|
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
|
|
|
|
|
2019-08-09 07:33:42 +00:00
|
|
|
describe '.search_invite_email' do
|
2019-08-12 05:56:11 +00:00
|
|
|
it 'returns only members the matching e-mail' do
|
2019-05-18 15:06:20 +00:00
|
|
|
create(:group_member, :invited)
|
|
|
|
|
2019-08-09 07:33:42 +00:00
|
|
|
invited = described_class.search_invite_email(@invited_member.invite_email)
|
2019-05-18 15:06:20 +00:00
|
|
|
|
|
|
|
expect(invited.count).to eq(1)
|
|
|
|
expect(invited.first).to eq(@invited_member)
|
2019-08-09 07:33:42 +00:00
|
|
|
|
|
|
|
expect(described_class.search_invite_email('bad-email@example.com').count).to eq(0)
|
2019-05-18 15:06:20 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-09-05 15:37:26 +00:00
|
|
|
describe '.developers' do
|
|
|
|
subject { described_class.developers.to_a }
|
|
|
|
|
|
|
|
it { is_expected.not_to include @owner }
|
2018-07-11 14:36:08 +00:00
|
|
|
it { is_expected.not_to include @maintainer }
|
2016-09-05 15:37:26 +00:00
|
|
|
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-07-11 14:36:08 +00:00
|
|
|
it { is_expected.not_to include @blocked_maintainer }
|
2016-09-05 15:37:26 +00:00
|
|
|
it { is_expected.not_to include @blocked_developer }
|
|
|
|
end
|
|
|
|
|
2018-07-11 14:36:08 +00:00
|
|
|
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-05 15:37:26 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
describe '.has_access' do
|
|
|
|
subject { described_class.has_access.to_a }
|
|
|
|
|
|
|
|
it { is_expected.to include @owner }
|
2018-07-11 14:36:08 +00:00
|
|
|
it { is_expected.to include @maintainer }
|
2016-09-05 15:37:26 +00:00
|
|
|
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-07-11 14:36:08 +00:00
|
|
|
it { is_expected.not_to include @blocked_maintainer }
|
2016-09-05 15:37:26 +00:00
|
|
|
it { is_expected.not_to include @blocked_developer }
|
2016-06-02 16:05:06 +00:00
|
|
|
end
|
2016-04-18 16:53:32 +00:00
|
|
|
end
|
|
|
|
|
2014-09-14 16:32:51 +00:00
|
|
|
describe "Delegate methods" do
|
2015-02-12 18:17:35 +00:00
|
|
|
it { is_expected.to respond_to(:user_name) }
|
|
|
|
it { is_expected.to respond_to(:user_email) }
|
2014-09-14 16:32:51 +00:00
|
|
|
end
|
2015-04-10 13:09:37 +00:00
|
|
|
|
2016-09-16 15:54:21 +00:00
|
|
|
describe '.add_user' do
|
|
|
|
%w[project group].each do |source_type|
|
|
|
|
context "when source is a #{source_type}" do
|
2016-11-11 12:51:50 +00:00
|
|
|
let!(:source) { create(source_type, :public, :access_requestable) }
|
2016-09-16 15:54:21 +00:00
|
|
|
let!(:user) { create(:user) }
|
|
|
|
let!(:admin) { create(:admin) }
|
2015-04-14 10:33:27 +00:00
|
|
|
|
2016-09-16 15:54:21 +00:00
|
|
|
it 'returns a <Source>Member object' do
|
2018-07-11 14:36:08 +00:00
|
|
|
member = described_class.add_user(source, user, :maintainer)
|
2015-04-14 10:33:27 +00:00
|
|
|
|
2016-09-16 15:54:21 +00:00
|
|
|
expect(member).to be_a "#{source_type.classify}Member".constantize
|
|
|
|
expect(member).to be_persisted
|
|
|
|
end
|
2015-04-14 10:33:27 +00:00
|
|
|
|
2016-09-16 15:54:21 +00:00
|
|
|
it 'sets members.created_by to the given current_user' do
|
2018-07-11 14:36:08 +00:00
|
|
|
member = described_class.add_user(source, user, :maintainer, current_user: admin)
|
2015-04-14 10:33:27 +00:00
|
|
|
|
2016-09-16 15:54:21 +00:00
|
|
|
expect(member.created_by).to eq(admin)
|
|
|
|
end
|
2015-04-14 10:33:27 +00:00
|
|
|
|
2016-09-16 15:54:21 +00:00
|
|
|
it 'sets members.expires_at to the given expires_at' do
|
2018-07-11 14:36:08 +00:00
|
|
|
member = described_class.add_user(source, user, :maintainer, expires_at: Date.new(2016, 9, 22))
|
2015-04-14 10:33:27 +00:00
|
|
|
|
2016-09-16 15:54:21 +00:00
|
|
|
expect(member.expires_at).to eq(Date.new(2016, 9, 22))
|
|
|
|
end
|
|
|
|
|
|
|
|
described_class.access_levels.each do |sym_key, int_access_level|
|
|
|
|
it "accepts the :#{sym_key} symbol as access level" do
|
|
|
|
expect(source.users).not_to include(user)
|
|
|
|
|
|
|
|
member = described_class.add_user(source, user.id, sym_key)
|
|
|
|
|
|
|
|
expect(member.access_level).to eq(int_access_level)
|
|
|
|
expect(source.users.reload).to include(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "accepts the #{int_access_level} integer as access level" do
|
|
|
|
expect(source.users).not_to include(user)
|
|
|
|
|
|
|
|
member = described_class.add_user(source, user.id, int_access_level)
|
|
|
|
|
|
|
|
expect(member.access_level).to eq(int_access_level)
|
|
|
|
expect(source.users.reload).to include(user)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with no current_user' do
|
|
|
|
context 'when called with a known user id' do
|
|
|
|
it 'adds the user as a member' do
|
|
|
|
expect(source.users).not_to include(user)
|
|
|
|
|
2018-07-11 14:36:08 +00:00
|
|
|
described_class.add_user(source, user.id, :maintainer)
|
2016-09-16 15:54:21 +00:00
|
|
|
|
|
|
|
expect(source.users.reload).to include(user)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when called with an unknown user id' do
|
|
|
|
it 'adds the user as a member' do
|
|
|
|
expect(source.users).not_to include(user)
|
|
|
|
|
2018-07-11 14:36:08 +00:00
|
|
|
described_class.add_user(source, 42, :maintainer)
|
2016-09-16 15:54:21 +00:00
|
|
|
|
|
|
|
expect(source.users.reload).not_to include(user)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when called with a user object' do
|
|
|
|
it 'adds the user as a member' do
|
|
|
|
expect(source.users).not_to include(user)
|
|
|
|
|
2018-07-11 14:36:08 +00:00
|
|
|
described_class.add_user(source, user, :maintainer)
|
2016-09-16 15:54:21 +00:00
|
|
|
|
|
|
|
expect(source.users.reload).to include(user)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when called with a requester user object' do
|
|
|
|
before do
|
|
|
|
source.request_access(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'adds the requester as a member' do
|
|
|
|
expect(source.users).not_to include(user)
|
|
|
|
expect(source.requesters.exists?(user_id: user)).to be_truthy
|
|
|
|
|
2018-07-11 14:36:08 +00:00
|
|
|
expect { described_class.add_user(source, user, :maintainer) }
|
2017-06-21 13:48:12 +00:00
|
|
|
.to raise_error(Gitlab::Access::AccessDeniedError)
|
2016-09-16 15:54:21 +00:00
|
|
|
|
|
|
|
expect(source.users.reload).not_to include(user)
|
|
|
|
expect(source.requesters.reload.exists?(user_id: user)).to be_truthy
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when called with a known user email' do
|
|
|
|
it 'adds the user as a member' do
|
|
|
|
expect(source.users).not_to include(user)
|
|
|
|
|
2018-07-11 14:36:08 +00:00
|
|
|
described_class.add_user(source, user.email, :maintainer)
|
2016-09-16 15:54:21 +00:00
|
|
|
|
|
|
|
expect(source.users.reload).to include(user)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when called with an unknown user email' do
|
|
|
|
it 'creates an invited member' do
|
|
|
|
expect(source.users).not_to include(user)
|
|
|
|
|
2018-07-11 14:36:08 +00:00
|
|
|
described_class.add_user(source, 'user@example.com', :maintainer)
|
2016-09-16 15:54:21 +00:00
|
|
|
|
|
|
|
expect(source.members.invite.pluck(:invite_email)).to include('user@example.com')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when current_user can update member' do
|
|
|
|
it 'creates the member' do
|
|
|
|
expect(source.users).not_to include(user)
|
|
|
|
|
2018-07-11 14:36:08 +00:00
|
|
|
described_class.add_user(source, user, :maintainer, current_user: admin)
|
2016-09-16 15:54:21 +00:00
|
|
|
|
|
|
|
expect(source.users.reload).to include(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when called with a requester user object' do
|
|
|
|
before do
|
|
|
|
source.request_access(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'adds the requester as a member' do
|
|
|
|
expect(source.users).not_to include(user)
|
|
|
|
expect(source.requesters.exists?(user_id: user)).to be_truthy
|
|
|
|
|
2018-07-11 14:36:08 +00:00
|
|
|
described_class.add_user(source, user, :maintainer, current_user: admin)
|
2016-09-16 15:54:21 +00:00
|
|
|
|
|
|
|
expect(source.users.reload).to include(user)
|
|
|
|
expect(source.requesters.reload.exists?(user_id: user)).to be_falsy
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when current_user cannot update member' do
|
|
|
|
it 'does not create the member' do
|
|
|
|
expect(source.users).not_to include(user)
|
|
|
|
|
2018-07-11 14:36:08 +00:00
|
|
|
member = described_class.add_user(source, user, :maintainer, current_user: user)
|
2016-09-16 15:54:21 +00:00
|
|
|
|
|
|
|
expect(source.users.reload).not_to include(user)
|
|
|
|
expect(member).not_to be_persisted
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when called with a requester user object' do
|
|
|
|
before do
|
|
|
|
source.request_access(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not destroy the requester' do
|
|
|
|
expect(source.users).not_to include(user)
|
|
|
|
expect(source.requesters.exists?(user_id: user)).to be_truthy
|
|
|
|
|
2018-07-11 14:36:08 +00:00
|
|
|
described_class.add_user(source, user, :maintainer, current_user: user)
|
2016-09-16 15:54:21 +00:00
|
|
|
|
|
|
|
expect(source.users.reload).not_to include(user)
|
|
|
|
expect(source.requesters.exists?(user_id: user)).to be_truthy
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when member already exists' do
|
|
|
|
before do
|
|
|
|
source.add_user(user, :developer)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with no current_user' do
|
|
|
|
it 'updates the member' do
|
|
|
|
expect(source.users).to include(user)
|
|
|
|
|
2018-07-11 14:36:08 +00:00
|
|
|
described_class.add_user(source, user, :maintainer)
|
2016-09-16 15:54:21 +00:00
|
|
|
|
2018-07-11 14:36:08 +00:00
|
|
|
expect(source.members.find_by(user_id: user).access_level).to eq(Gitlab::Access::MAINTAINER)
|
2016-09-16 15:54:21 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when current_user can update member' do
|
|
|
|
it 'updates the member' do
|
|
|
|
expect(source.users).to include(user)
|
|
|
|
|
2018-07-11 14:36:08 +00:00
|
|
|
described_class.add_user(source, user, :maintainer, current_user: admin)
|
2016-09-16 15:54:21 +00:00
|
|
|
|
2018-07-11 14:36:08 +00:00
|
|
|
expect(source.members.find_by(user_id: user).access_level).to eq(Gitlab::Access::MAINTAINER)
|
2016-09-16 15:54:21 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when current_user cannot update member' do
|
|
|
|
it 'does not update the member' do
|
|
|
|
expect(source.users).to include(user)
|
2015-04-14 10:33:27 +00:00
|
|
|
|
2018-07-11 14:36:08 +00:00
|
|
|
described_class.add_user(source, user, :maintainer, current_user: user)
|
2015-04-14 10:33:27 +00:00
|
|
|
|
2016-09-16 15:54:21 +00:00
|
|
|
expect(source.members.find_by(user_id: user).access_level).to eq(Gitlab::Access::DEVELOPER)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2015-04-14 10:33:27 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-04-21 14:07:42 +00:00
|
|
|
describe '.add_users' do
|
|
|
|
%w[project group].each do |source_type|
|
|
|
|
context "when source is a #{source_type}" do
|
|
|
|
let!(:source) { create(source_type, :public, :access_requestable) }
|
|
|
|
let!(:admin) { create(:admin) }
|
2017-04-28 08:50:11 +00:00
|
|
|
let(:user1) { create(:user) }
|
|
|
|
let(:user2) { create(:user) }
|
2017-04-21 14:07:42 +00:00
|
|
|
|
|
|
|
it 'returns a <Source>Member objects' do
|
2018-07-11 14:36:08 +00:00
|
|
|
members = described_class.add_users(source, [user1, user2], :maintainer)
|
2017-04-21 14:07:42 +00:00
|
|
|
|
|
|
|
expect(members).to be_a Array
|
2017-04-28 08:50:11 +00:00
|
|
|
expect(members.size).to eq(2)
|
2017-04-21 14:07:42 +00:00
|
|
|
expect(members.first).to be_a "#{source_type.classify}Member".constantize
|
|
|
|
expect(members.first).to be_persisted
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns an empty array' do
|
2018-07-11 14:36:08 +00:00
|
|
|
members = described_class.add_users(source, [], :maintainer)
|
2017-04-21 14:07:42 +00:00
|
|
|
|
|
|
|
expect(members).to be_a Array
|
|
|
|
expect(members).to be_empty
|
|
|
|
end
|
2017-09-05 16:03:24 +00:00
|
|
|
|
|
|
|
it 'supports differents formats' do
|
|
|
|
list = ['joe@local.test', admin, user1.id, user2.id.to_s]
|
|
|
|
|
2018-07-11 14:36:08 +00:00
|
|
|
members = described_class.add_users(source, list, :maintainer)
|
2017-09-05 16:03:24 +00:00
|
|
|
|
|
|
|
expect(members.size).to eq(4)
|
|
|
|
expect(members.first).to be_invite
|
|
|
|
end
|
2017-04-21 14:07:42 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-04-18 16:53:32 +00:00
|
|
|
describe '#accept_request' do
|
2016-06-02 16:05:06 +00:00
|
|
|
let(:member) { create(:project_member, requested_at: Time.now.utc) }
|
2016-04-18 16:53:32 +00:00
|
|
|
|
2016-06-02 16:05:06 +00:00
|
|
|
it { expect(member.accept_request).to be_truthy }
|
2016-04-18 16:53:32 +00:00
|
|
|
|
|
|
|
it 'clears requested_at' do
|
|
|
|
member.accept_request
|
|
|
|
|
|
|
|
expect(member.requested_at).to be_nil
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'calls #after_accept_request' do
|
|
|
|
expect(member).to receive(:after_accept_request)
|
|
|
|
|
|
|
|
member.accept_request
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-06-02 16:05:06 +00:00
|
|
|
describe '#invite?' do
|
|
|
|
subject { create(:project_member, invite_email: "user@example.com", user: nil) }
|
2016-04-18 16:53:32 +00:00
|
|
|
|
2016-06-02 16:05:06 +00:00
|
|
|
it { is_expected.to be_invite }
|
|
|
|
end
|
2016-04-18 16:53:32 +00:00
|
|
|
|
2016-06-02 16:05:06 +00:00
|
|
|
describe '#request?' do
|
|
|
|
subject { create(:project_member, requested_at: Time.now.utc) }
|
2016-04-18 16:53:32 +00:00
|
|
|
|
2016-06-02 16:05:06 +00:00
|
|
|
it { is_expected.to be_request }
|
|
|
|
end
|
2016-04-18 16:53:32 +00:00
|
|
|
|
2016-06-02 16:05:06 +00:00
|
|
|
describe '#pending?' do
|
|
|
|
let(:invited_member) { create(:project_member, invite_email: "user@example.com", user: nil) }
|
|
|
|
let(:requester) { create(:project_member, requested_at: Time.now.utc) }
|
2016-04-18 16:53:32 +00:00
|
|
|
|
2016-06-02 16:05:06 +00:00
|
|
|
it { expect(invited_member).to be_invite }
|
|
|
|
it { expect(requester).to be_pending }
|
2016-04-18 16:53:32 +00:00
|
|
|
end
|
|
|
|
|
2015-04-10 13:09:37 +00:00
|
|
|
describe "#accept_invite!" do
|
|
|
|
let!(:member) { create(:project_member, invite_email: "user@example.com", user: nil) }
|
|
|
|
let(:user) { create(:user) }
|
|
|
|
|
|
|
|
it "resets the invite token" do
|
|
|
|
member.accept_invite!(user)
|
|
|
|
|
|
|
|
expect(member.invite_token).to be_nil
|
|
|
|
end
|
|
|
|
|
|
|
|
it "sets the invite accepted timestamp" do
|
|
|
|
member.accept_invite!(user)
|
|
|
|
|
|
|
|
expect(member.invite_accepted_at).not_to be_nil
|
|
|
|
end
|
|
|
|
|
|
|
|
it "sets the user" do
|
|
|
|
member.accept_invite!(user)
|
|
|
|
|
|
|
|
expect(member.user).to eq(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "calls #after_accept_invite" do
|
|
|
|
expect(member).to receive(:after_accept_invite)
|
|
|
|
|
|
|
|
member.accept_invite!(user)
|
|
|
|
end
|
2016-10-11 12:25:17 +00:00
|
|
|
|
2018-01-17 11:30:25 +00:00
|
|
|
it "refreshes user's authorized projects", :delete do
|
2016-10-11 12:25:17 +00:00
|
|
|
project = member.source
|
|
|
|
|
|
|
|
expect(user.authorized_projects).not_to include(project)
|
|
|
|
|
|
|
|
member.accept_invite!(user)
|
|
|
|
|
|
|
|
expect(user.authorized_projects.reload).to include(project)
|
|
|
|
end
|
2015-04-10 13:09:37 +00:00
|
|
|
end
|
|
|
|
|
2015-04-10 14:37:02 +00:00
|
|
|
describe "#decline_invite!" do
|
|
|
|
let!(:member) { create(:project_member, invite_email: "user@example.com", user: nil) }
|
|
|
|
|
|
|
|
it "destroys the member" do
|
|
|
|
member.decline_invite!
|
|
|
|
|
|
|
|
expect(member).to be_destroyed
|
|
|
|
end
|
|
|
|
|
|
|
|
it "calls #after_decline_invite" do
|
|
|
|
expect(member).to receive(:after_decline_invite)
|
|
|
|
|
|
|
|
member.decline_invite!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-04-10 13:09:37 +00:00
|
|
|
describe "#generate_invite_token" do
|
|
|
|
let!(:member) { create(:project_member, invite_email: "user@example.com", user: nil) }
|
2016-02-09 14:51:06 +00:00
|
|
|
|
2015-04-10 13:09:37 +00:00
|
|
|
it "sets the invite token" do
|
|
|
|
expect { member.generate_invite_token }.to change { member.invite_token}
|
|
|
|
end
|
|
|
|
end
|
2016-10-11 12:25:17 +00:00
|
|
|
|
2018-01-17 11:30:25 +00:00
|
|
|
describe "destroying a record", :delete do
|
2016-10-11 12:25:17 +00:00
|
|
|
it "refreshes user's authorized projects" do
|
2017-08-02 19:55:11 +00:00
|
|
|
project = create(:project, :private)
|
2016-10-11 12:25:17 +00:00
|
|
|
user = create(:user)
|
2017-12-22 08:18:28 +00:00
|
|
|
member = project.add_reporter(user)
|
2016-10-11 12:25:17 +00:00
|
|
|
|
|
|
|
member.destroy
|
|
|
|
|
|
|
|
expect(user.authorized_projects).not_to include(project)
|
|
|
|
end
|
|
|
|
end
|
2014-09-14 14:54:10 +00:00
|
|
|
end
|