summaryrefslogtreecommitdiff
path: root/spec
diff options
context:
space:
mode:
authorRémy Coutable <remy@rymai.me>2016-07-28 19:28:56 +0200
committerRémy Coutable <remy@rymai.me>2016-09-22 11:12:17 +0200
commit6b02127f03b1f8e1dbbecccfc2ba16e62aadcbed (patch)
tree203e8df9de809ffcc5a8d31c76885ecd0d088ab2 /spec
parent98b3d6ce695a9751f72adc35bc09f82eb2f624a4 (diff)
downloadgitlab-ce-6b02127f03b1f8e1dbbecccfc2ba16e62aadcbed.tar.gz
New Members::RequestAccessService
Signed-off-by: Rémy Coutable <remy@rymai.me>
Diffstat (limited to 'spec')
-rw-r--r--spec/models/concerns/access_requestable_spec.rb40
-rw-r--r--spec/models/group_spec.rb7
-rw-r--r--spec/models/project_spec.rb8
-rw-r--r--spec/requests/api/access_requests_spec.rb18
-rw-r--r--spec/services/members/request_access_service_spec.rb57
5 files changed, 88 insertions, 42 deletions
diff --git a/spec/models/concerns/access_requestable_spec.rb b/spec/models/concerns/access_requestable_spec.rb
deleted file mode 100644
index 96eee0e8bdd..00000000000
--- a/spec/models/concerns/access_requestable_spec.rb
+++ /dev/null
@@ -1,40 +0,0 @@
-require 'spec_helper'
-
-describe AccessRequestable do
- describe 'Group' do
- describe '#request_access' do
- let(:group) { create(:group, :public) }
- let(:user) { create(:user) }
-
- it { expect(group.request_access(user)).to be_a(GroupMember) }
- it { expect(group.request_access(user).user).to eq(user) }
- end
-
- describe '#access_requested?' do
- let(:group) { create(:group, :public) }
- let(:user) { create(:user) }
-
- before { group.request_access(user) }
-
- it { expect(group.requesters.exists?(user_id: user)).to be_truthy }
- end
- end
-
- describe 'Project' do
- describe '#request_access' do
- let(:project) { create(:empty_project, :public) }
- let(:user) { create(:user) }
-
- it { expect(project.request_access(user)).to be_a(ProjectMember) }
- end
-
- describe '#access_requested?' do
- let(:project) { create(:empty_project, :public) }
- let(:user) { create(:user) }
-
- before { project.request_access(user) }
-
- it { expect(project.requesters.exists?(user_id: user)).to be_truthy }
- end
- end
-end
diff --git a/spec/models/group_spec.rb b/spec/models/group_spec.rb
index 0b3ef9b98fd..c015147f0db 100644
--- a/spec/models/group_spec.rb
+++ b/spec/models/group_spec.rb
@@ -105,6 +105,13 @@ describe Group, models: true do
it { expect(group.human_name).to eq(group.name) }
end
+ describe '#request_access' do
+ let(:user) { create(:user) }
+
+ it { expect(group.request_access(user)).to be_a(GroupMember) }
+ it { expect(group.request_access(user).user).to eq(user) }
+ end
+
describe '#add_user' do
let(:user) { create(:user) }
before { group.add_user(user, GroupMember::MASTER) }
diff --git a/spec/models/project_spec.rb b/spec/models/project_spec.rb
index 83f61f0af0a..f88636e1c19 100644
--- a/spec/models/project_spec.rb
+++ b/spec/models/project_spec.rb
@@ -942,6 +942,14 @@ describe Project, models: true do
end
end
+ describe '#request_access' do
+ let(:project) { create(:empty_project, :public) }
+ let(:user) { create(:user) }
+
+ it { expect(project.request_access(user)).to be_a(ProjectMember) }
+ it { expect(project.request_access(user).user).to eq(user) }
+ end
+
describe '.search' do
let(:project) { create(:project, description: 'kitten mittens') }
diff --git a/spec/requests/api/access_requests_spec.rb b/spec/requests/api/access_requests_spec.rb
index d78494b76fa..905a7311372 100644
--- a/spec/requests/api/access_requests_spec.rb
+++ b/spec/requests/api/access_requests_spec.rb
@@ -64,12 +64,12 @@ describe API::AccessRequests, api: true do
context 'when authenticated as a member' do
%i[developer master].each do |type|
context "as a #{type}" do
- it 'returns 400' do
+ it 'returns 403' do
expect do
user = public_send(type)
post api("/#{source_type.pluralize}/#{source.id}/access_requests", user)
- expect(response).to have_http_status(400)
+ expect(response).to have_http_status(403)
end.not_to change { source.requesters.count }
end
end
@@ -87,6 +87,20 @@ describe API::AccessRequests, api: true do
end
context 'when authenticated as a stranger' do
+ context "when access request is disabled for the #{source_type}" do
+ before do
+ source.update(request_access_enabled: false)
+ end
+
+ it 'returns 403' do
+ expect do
+ post api("/#{source_type.pluralize}/#{source.id}/access_requests", stranger)
+
+ expect(response).to have_http_status(403)
+ end.not_to change { source.requesters.count }
+ end
+ end
+
it 'returns 201' do
expect do
post api("/#{source_type.pluralize}/#{source.id}/access_requests", stranger)
diff --git a/spec/services/members/request_access_service_spec.rb b/spec/services/members/request_access_service_spec.rb
new file mode 100644
index 00000000000..dff5b4917ae
--- /dev/null
+++ b/spec/services/members/request_access_service_spec.rb
@@ -0,0 +1,57 @@
+require 'spec_helper'
+
+describe Members::RequestAccessService, services: true do
+ let(:user) { create(:user) }
+ let(:project) { create(:project, :private) }
+ let(:group) { create(:group, :private) }
+
+ shared_examples 'a service raising Gitlab::Access::AccessDeniedError' do
+ it 'raises Gitlab::Access::AccessDeniedError' do
+ expect { described_class.new(source, user).execute }.to raise_error(Gitlab::Access::AccessDeniedError)
+ end
+ end
+
+ shared_examples 'a service creating a access request' do
+ it 'succeeds' do
+ expect { described_class.new(source, user).execute }.to change { source.requesters.count }.by(1)
+ end
+
+ it 'returns a <Source>Member' do
+ member = described_class.new(source, user).execute
+
+ expect(member).to be_a "#{source.class.to_s}Member".constantize
+ expect(member.requested_at).to be_present
+ end
+ end
+
+ context 'when source is nil' do
+ it_behaves_like 'a service raising Gitlab::Access::AccessDeniedError' do
+ let(:source) { nil }
+ end
+ end
+
+ context 'when current user cannot request access to the project' do
+ it_behaves_like 'a service raising Gitlab::Access::AccessDeniedError' do
+ let(:source) { project }
+ end
+
+ it_behaves_like 'a service raising Gitlab::Access::AccessDeniedError' do
+ let(:source) { group }
+ end
+ end
+
+ context 'when current user can request access to the project' do
+ before do
+ project.update(visibility_level: Gitlab::VisibilityLevel::PUBLIC)
+ group.update(visibility_level: Gitlab::VisibilityLevel::PUBLIC)
+ end
+
+ it_behaves_like 'a service creating a access request' do
+ let(:source) { project }
+ end
+
+ it_behaves_like 'a service creating a access request' do
+ let(:source) { group }
+ end
+ end
+end