From ff15cf6480a43778c62a05ffb2ad1e29542bcd2f Mon Sep 17 00:00:00 2001 From: Borja Aparicio Date: Tue, 25 Oct 2016 14:54:18 +0200 Subject: [PATCH 1/2] Added API spec test for projects/owned endpoint --- spec/requests/api/projects_spec.rb | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/spec/requests/api/projects_spec.rb b/spec/requests/api/projects_spec.rb index 54ba32f6bc457d..84dbea5ca5b156 100644 --- a/spec/requests/api/projects_spec.rb +++ b/spec/requests/api/projects_spec.rb @@ -175,6 +175,29 @@ end end + describe 'GET /projects/owned' do + before do + project4 + end + + context 'when unauthenticated' do + it 'returns authentication error' do + get api('/projects/owned') + expect(response).to have_http_status(401) + end + end + + context 'when authenticated as project owner' do + it 'returns an array of projects the user owns' do + get api('/projects/owned', user4) + expect(response).to have_http_status(200) + expect(json_response).to be_an Array + expect(json_response.first['name']).to eq(project4.name) + expect(json_response.first['owner']['username']).to eq(user4.username) + end + end + end + describe 'GET /projects/visible' do let(:public_project) { create(:project, :public) } -- GitLab From 539209c38a3b382e89d2c9ae8f14d52184494a34 Mon Sep 17 00:00:00 2001 From: Borja Aparicio Date: Tue, 25 Oct 2016 15:22:12 +0200 Subject: [PATCH 2/2] Added API endpoint groups/owned --- CHANGELOG.md | 1 + doc/api/groups.md | 8 ++++++++ lib/api/groups.rb | 10 ++++++++++ spec/requests/api/groups_spec.rb | 19 +++++++++++++++++++ 4 files changed, 38 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index ee55f73dda29dd..3d501f6e8cb71a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -122,6 +122,7 @@ Please view this file on the master branch, on stable branches it's out of date. - API: all unknown routing will be handled with 404 Not Found - Add docs for request profiling - Make guests unable to view MRs on private projects + - Add `/groups/owned` API endpoint (Borja Aparicio) ## 8.12.7 diff --git a/doc/api/groups.md b/doc/api/groups.md index 95a9c5e69ddd5a..dd3ded89140500 100644 --- a/doc/api/groups.md +++ b/doc/api/groups.md @@ -26,6 +26,14 @@ GET /groups You can search for groups by name or path, see below. +## List owned groups + +Get a list of groups which are owned by the authenticated user. + +``` +GET /groups/owned +``` + ## List a group's projects Get a list of projects in this group. diff --git a/lib/api/groups.rb b/lib/api/groups.rb index 85129d5b701f8f..482ee3d3452df9 100644 --- a/lib/api/groups.rb +++ b/lib/api/groups.rb @@ -26,6 +26,16 @@ class Groups < Grape::API present @groups, with: Entities::Group end + # Get list of owned groups for authenticated user + # + # Example Request: + # GET /groups/owned + get '/owned' do + @groups = current_user.owned_groups + @groups = paginate @groups + present @groups, with: Entities::Group, user: current_user + end + # Create group. Available only for users who can create groups. # # Parameters: diff --git a/spec/requests/api/groups_spec.rb b/spec/requests/api/groups_spec.rb index 850f0d24257ef4..a77813f9790685 100644 --- a/spec/requests/api/groups_spec.rb +++ b/spec/requests/api/groups_spec.rb @@ -78,6 +78,25 @@ end end + describe 'GET /groups/owned' do + + context 'when unauthenticated' do + it 'returns authentication error' do + get api('/groups/owned') + expect(response).to have_http_status(401) + end + end + + context 'when authenticated as group owner' do + it 'returns an array of groups the user owns' do + get api('/groups/owned', user2) + expect(response).to have_http_status(200) + expect(json_response).to be_an Array + expect(json_response.first['name']).to eq(group2.name) + end + end + end + describe "GET /groups/:id" do context "when authenticated as user" do it "returns one of user1's groups" do -- GitLab