diff options
author | Tomasz Maczukin <tomasz@maczukin.pl> | 2016-01-05 12:25:16 +0100 |
---|---|---|
committer | Tomasz Maczukin <tomasz@maczukin.pl> | 2016-01-05 12:25:16 +0100 |
commit | 8675664655c4e0f1e043afa88ff1fd75ae5a6a9e (patch) | |
tree | 41a3cc4176cf0243fb5c54cead4f497f6b27b39a | |
parent | 49c8bf4e9b510be51859dcc301cb46b29b750cb0 (diff) | |
download | gitlab-ce-8675664655c4e0f1e043afa88ff1fd75ae5a6a9e.tar.gz |
Get show details feature to triggers API
-rw-r--r-- | lib/api/triggers.rb | 26 | ||||
-rw-r--r-- | spec/requests/api/triggers_spec.rb | 56 |
2 files changed, 77 insertions, 5 deletions
diff --git a/lib/api/triggers.rb b/lib/api/triggers.rb index 38cf1e9a2e0..0e548b936cd 100644 --- a/lib/api/triggers.rb +++ b/lib/api/triggers.rb @@ -62,6 +62,32 @@ module API present triggers, with: Entities::Trigger end + # Get specific trigger of a project + # + # Parameters: + # id (required) - The ID of a project + # trigger_id (required) - The ID or `token` of a trigger to show; if trigger_id contains only digits it's + # treated as ID other ways it's reated as `key` + # Example Request: + # GET /projects/:id/triggers/:trigger_id + get ':id/triggers/:trigger_id' do + authenticate! + authorize_admin_project + + trigger_id = params[:trigger_id] + triggers = user_project.triggers + triggers = + if trigger_id.match(/^\d+$/) + triggers.where(id: trigger_id.to_i) + else + triggers.where(token: trigger_id) + end + + return not_found!('Trigger') if triggers.empty? + + present triggers.first, with: Entities::Trigger + end + # Create trigger # # Parameters: diff --git a/spec/requests/api/triggers_spec.rb b/spec/requests/api/triggers_spec.rb index 316c2ae958d..c1c2bb04b29 100644 --- a/spec/requests/api/triggers_spec.rb +++ b/spec/requests/api/triggers_spec.rb @@ -5,8 +5,8 @@ describe API::API do let(:user) { create(:user) } let(:user2) { create(:user) } - let!(:trigger_token) { 'secure token' } - let!(:trigger_token_2) { 'secure token 2' } + let!(:trigger_token) { 'secure_token' } + let!(:trigger_token_2) { 'secure_token_2' } let!(:project) { create(:project, creator_id: user.id) } let!(:master) { create(:project_member, user: user, project: project, access_level: ProjectMember::MASTER) } let!(:developer) { create(:project_member, user: user2, project: project, access_level: ProjectMember::DEVELOPER) } @@ -86,7 +86,7 @@ describe API::API do end end - describe 'GET /projects/:id/triggets' do + describe 'GET /projects/:id/triggers' do context 'authenticated user with valid permissions' do it 'should return list of triggers' do get api("/projects/#{project.id}/triggers", user) @@ -115,7 +115,53 @@ describe API::API do end end - describe 'POST /projects/:id/triggets' do + describe 'GET /projects/:id/triggers/:triggers_id' do + context 'authenticated user with valid permissions' do + context 'ID is used as :trigger_id' do + it 'should return trigger details' do + get api("/projects/#{project.id}/triggers/#{trigger.id}", user) + + expect(response.status).to eq(200) + expect(json_response).to be_a(Hash) + expect(json_response['token']).to eq(trigger_token) + end + end + + context '`token` is used as :trigger_id' do + it 'should return trigger details' do + get api("/projects/#{project.id}/triggers/#{trigger.token}", user) + + expect(response.status).to eq(200) + expect(json_response).to be_a(Hash) + expect(json_response['id']).to eq(trigger.id) + end + end + + it 'should responde with 404 Not Found if requesting non-existing trigger' do + get api("/projects/#{project.id}/triggers/9999", user) + + expect(response.status).to eq(404) + end + end + + context 'authenticated user with invalid permissions' do + it 'should not return triggers list' do + get api("/projects/#{project.id}/triggers/#{trigger.id}", user2) + + expect(response.status).to eq(403) + end + end + + context 'unauthentikated user' do + it 'should not return triggers list' do + get api("/projects/#{project.id}/triggers/#{trigger.id}") + + expect(response.status).to eq(401) + end + end + end + + describe 'POST /projects/:id/triggers' do context 'authenticated user with valid permissions' do it 'should create trigger' do expect do @@ -144,7 +190,7 @@ describe API::API do end end - describe 'DELETE /projects/:id/triggets/:trigger_id' do + describe 'DELETE /projects/:id/triggers/:trigger_id' do context 'authenticated user with valid permissions' do it 'should delete trigger' do expect do |