Commit e0ec69d9 authored by Tomasz Maczukin's avatar Tomasz Maczukin

Change 'trigger_id' to 'token' as resource ID in triggers API

parent b0a77a22
...@@ -17,7 +17,6 @@ Parameters: ...@@ -17,7 +17,6 @@ Parameters:
{ {
"created_at": "2015-12-23T16:24:34.716Z", "created_at": "2015-12-23T16:24:34.716Z",
"deleted_at": null, "deleted_at": null,
"id": 1,
"last_used": "2016-01-04T15:41:21.986Z", "last_used": "2016-01-04T15:41:21.986Z",
"token": "fbdb730c2fbdb095a0862dbd8ab88b", "token": "fbdb730c2fbdb095a0862dbd8ab88b",
"updated_at": "2015-12-23T16:24:34.716Z" "updated_at": "2015-12-23T16:24:34.716Z"
...@@ -25,7 +24,6 @@ Parameters: ...@@ -25,7 +24,6 @@ Parameters:
{ {
"created_at": "2015-12-23T16:25:56.760Z", "created_at": "2015-12-23T16:25:56.760Z",
"deleted_at": null, "deleted_at": null,
"id": 2,
"last_used": null, "last_used": null,
"token": "7b9148c158980bbd9bcea92c17522d", "token": "7b9148c158980bbd9bcea92c17522d",
"updated_at": "2015-12-23T16:25:56.760Z" "updated_at": "2015-12-23T16:25:56.760Z"
...@@ -38,19 +36,18 @@ Parameters: ...@@ -38,19 +36,18 @@ Parameters:
Get details of trigger of a project Get details of trigger of a project
``` ```
GET /projects/:id/triggers/:trigger_id GET /projects/:id/triggers/:token
``` ```
Parameters: Parameters:
- `id` (required) - The ID of a project - `id` (required) - The ID of a project
- `trigger_id` (required) - The ID of a trigger - `token` (required) - The `token` of a trigger
```json ```json
{ {
"created_at": "2015-12-23T16:25:56.760Z", "created_at": "2015-12-23T16:25:56.760Z",
"deleted_at": null, "deleted_at": null,
"id": 2,
"last_used": null, "last_used": null,
"token": "7b9148c158980bbd9bcea92c17522d", "token": "7b9148c158980bbd9bcea92c17522d",
"updated_at": "2015-12-23T16:25:56.760Z" "updated_at": "2015-12-23T16:25:56.760Z"
...@@ -73,7 +70,6 @@ Parameters: ...@@ -73,7 +70,6 @@ Parameters:
{ {
"created_at": "2016-01-07T09:53:58.235Z", "created_at": "2016-01-07T09:53:58.235Z",
"deleted_at": null, "deleted_at": null,
"id": 5,
"last_used": null, "last_used": null,
"token": "6d056f63e50fe6f8c5f8f4aa10edb7", "token": "6d056f63e50fe6f8c5f8f4aa10edb7",
"updated_at": "2016-01-07T09:53:58.235Z" "updated_at": "2016-01-07T09:53:58.235Z"
...@@ -85,10 +81,10 @@ Parameters: ...@@ -85,10 +81,10 @@ Parameters:
Remove a trigger of a project Remove a trigger of a project
``` ```
DELETE /projects/:id/triggers/:trigger_id DELETE /projects/:id/triggers/:token
``` ```
Parameters: Parameters:
- `id` (required) - The ID of a project - `id` (required) - The ID of a project
- `trigger_id` (required) - The ID of a trigger - `token` (required) - The `token` of a trigger
...@@ -367,7 +367,7 @@ module API ...@@ -367,7 +367,7 @@ module API
end end
class Trigger < Grape::Entity class Trigger < Grape::Entity
expose :id, :token, :created_at, :updated_at, :deleted_at expose :token, :created_at, :updated_at, :deleted_at
expose :last_used do |repo_obj, _options| expose :last_used do |repo_obj, _options|
if repo_obj.respond_to?(:last_trigger_request) if repo_obj.respond_to?(:last_trigger_request)
request = repo_obj.last_trigger_request request = repo_obj.last_trigger_request
......
...@@ -66,23 +66,14 @@ module API ...@@ -66,23 +66,14 @@ module API
# #
# Parameters: # Parameters:
# id (required) - The ID of a project # 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 # token (required) - The `token` of a trigger
# treated as ID other ways it's reated as `key`
# Example Request: # Example Request:
# GET /projects/:id/triggers/:trigger_id # GET /projects/:id/triggers/:token
get ':id/triggers/:trigger_id' do get ':id/triggers/:token' do
authenticate! authenticate!
authorize_admin_project authorize_admin_project
trigger_id = params[:trigger_id] triggers = user_project.triggers.where(token: params[:token])
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? return not_found!('Trigger') if triggers.empty?
present triggers.first, with: Entities::Trigger present triggers.first, with: Entities::Trigger
...@@ -108,14 +99,14 @@ module API ...@@ -108,14 +99,14 @@ module API
# #
# Parameters: # Parameters:
# id (required) - The ID of a project # id (required) - The ID of a project
# trigger_id - The ID of trigger to delete # token (required) - The `token` of a trigger
# Example Request: # Example Request:
# DELETE /projects/:id/triggers/:trigger_id # DELETE /projects/:id/triggers/:token
delete ':id/triggers/:trigger_id' do delete ':id/triggers/:token' do
authenticate! authenticate!
authorize_admin_project authorize_admin_project
trigger = user_project.triggers.where(id: params[:trigger_id].to_i).first trigger = user_project.triggers.where(token: params[:token]).first
return not_found!('Trigger') unless trigger return not_found!('Trigger') unless trigger
trigger.destroy trigger.destroy
......
...@@ -93,8 +93,7 @@ describe API::API do ...@@ -93,8 +93,7 @@ describe API::API do
expect(response.status).to eq(200) expect(response.status).to eq(200)
expect(json_response).to be_a(Array) expect(json_response).to be_a(Array)
expect(json_response[0]['token']).to eq(trigger_token) expect(json_response[0]).to have_key('token')
expect(json_response[1]['token']).to eq(trigger_token_2)
end end
end end
...@@ -115,30 +114,17 @@ describe API::API do ...@@ -115,30 +114,17 @@ describe API::API do
end end
end end
describe 'GET /projects/:id/triggers/:triggers_id' do describe 'GET /projects/:id/triggers/:token' do
context 'authenticated user with valid permissions' 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 it 'should return trigger details' do
get api("/projects/#{project.id}/triggers/#{trigger.token}", user) get api("/projects/#{project.id}/triggers/#{trigger.token}", user)
expect(response.status).to eq(200) expect(response.status).to eq(200)
expect(json_response).to be_a(Hash) expect(json_response).to be_a(Hash)
expect(json_response['id']).to eq(trigger.id)
end
end end
it 'should responde with 404 Not Found if requesting non-existing trigger' do it 'should responde with 404 Not Found if requesting non-existing trigger' do
get api("/projects/#{project.id}/triggers/9999", user) get api("/projects/#{project.id}/triggers/abcdef012345", user)
expect(response.status).to eq(404) expect(response.status).to eq(404)
end end
...@@ -146,7 +132,7 @@ describe API::API do ...@@ -146,7 +132,7 @@ describe API::API do
context 'authenticated user with invalid permissions' do context 'authenticated user with invalid permissions' do
it 'should not return triggers list' do it 'should not return triggers list' do
get api("/projects/#{project.id}/triggers/#{trigger.id}", user2) get api("/projects/#{project.id}/triggers/#{trigger.token}", user2)
expect(response.status).to eq(403) expect(response.status).to eq(403)
end end
...@@ -154,7 +140,7 @@ describe API::API do ...@@ -154,7 +140,7 @@ describe API::API do
context 'unauthentikated user' do context 'unauthentikated user' do
it 'should not return triggers list' do it 'should not return triggers list' do
get api("/projects/#{project.id}/triggers/#{trigger.id}") get api("/projects/#{project.id}/triggers/#{trigger.token}")
expect(response.status).to eq(401) expect(response.status).to eq(401)
end end
...@@ -190,17 +176,17 @@ describe API::API do ...@@ -190,17 +176,17 @@ describe API::API do
end end
end end
describe 'DELETE /projects/:id/triggers/:trigger_id' do describe 'DELETE /projects/:id/triggers/:token' do
context 'authenticated user with valid permissions' do context 'authenticated user with valid permissions' do
it 'should delete trigger' do it 'should delete trigger' do
expect do expect do
delete api("/projects/#{project.id}/triggers/#{trigger.id}", user) delete api("/projects/#{project.id}/triggers/#{trigger.token}", user)
end.to change{project.triggers.count}.by(-1) end.to change{project.triggers.count}.by(-1)
expect(response.status).to eq(200) expect(response.status).to eq(200)
end end
it 'should responde with 404 Not Found if requesting non-existing trigger' do it 'should responde with 404 Not Found if requesting non-existing trigger' do
delete api("/projects/#{project.id}/triggers/9999", user) delete api("/projects/#{project.id}/triggers/abcdef012345", user)
expect(response.status).to eq(404) expect(response.status).to eq(404)
end end
...@@ -208,7 +194,7 @@ describe API::API do ...@@ -208,7 +194,7 @@ describe API::API do
context 'authenticated user with invalid permissions' do context 'authenticated user with invalid permissions' do
it 'should not delete trigger' do it 'should not delete trigger' do
delete api("/projects/#{project.id}/triggers/#{trigger.id}", user2) delete api("/projects/#{project.id}/triggers/#{trigger.token}", user2)
expect(response.status).to eq(403) expect(response.status).to eq(403)
end end
...@@ -216,7 +202,7 @@ describe API::API do ...@@ -216,7 +202,7 @@ describe API::API do
context 'unauthentikated user' do context 'unauthentikated user' do
it 'should not delete trigger' do it 'should not delete trigger' do
delete api("/projects/#{project.id}/triggers/#{trigger.id}") delete api("/projects/#{project.id}/triggers/#{trigger.token}")
expect(response.status).to eq(401) expect(response.status).to eq(401)
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment