Skip to content

Commit

Permalink
Add project_id to Aggregations to allow Doorkeeper to scope by project
Browse files Browse the repository at this point in the history
  • Loading branch information
zwolf committed May 24, 2024
1 parent 25e1843 commit 50f14c1
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 10 deletions.
6 changes: 4 additions & 2 deletions app/controllers/api/v1/aggregations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@
class Api::V1::AggregationsController < Api::ApiController
include JsonApiController::PunditPolicy

require_authentication :index, :show, :update, :create, scopes: [:workflow]
require_authentication :index, :show, :update, :create, scopes: [:project]
resource_actions :index, :show, :create, :update
schema_type :json_schema

def create
workflow = Workflow.find(create_params['links']['workflow'])
project_id = workflow.project.id
create_params['links']['project'] = project_id
response = AggregationClient.new.send_aggregation_request(
workflow.project.id,
project_id,
workflow.id,
create_params['links']['user']
)
Expand Down
3 changes: 2 additions & 1 deletion app/models/aggregation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

class Aggregation < ApplicationRecord
belongs_to :workflow
belongs_to :project
belongs_to :user
validates :project, presence: true
validates :workflow, presence: true
validates :user, presence: true
validates :user_id, uniqueness: { scope: :workflow_id }
Expand All @@ -15,5 +17,4 @@ class Aggregation < ApplicationRecord
completed: 2,
failed: 3
}

end
4 changes: 4 additions & 0 deletions app/policies/aggregation_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ def linkable_workflows
policy_for(Workflow).scope_for(:update)
end

def linkable_projects
policy_for(Project).scope_for(:update)
end

def linkable_users
policy_for(User).scope_for(:update)
end
Expand Down
4 changes: 2 additions & 2 deletions app/serializers/aggregation_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ class AggregationSerializer
include CachedSerializer

attributes :id, :href, :created_at, :updated_at, :uuid, :task_id, :status
can_include :workflow, :user
can_include :project, :workflow, :user

can_filter_by :workflow
can_filter_by :project, :workflow
end
4 changes: 4 additions & 0 deletions db/migrate/20240304201959_refactor_aggregation_model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ def up
safety_assured { remove_column :aggregations, :aggregation }

# and the new aggregations columns
add_column :aggregations, :project_id, :integer
add_foreign_key :aggregations, :projects, column: :project_id, validate: false

add_column :aggregations, :user_id, :integer
add_foreign_key :aggregations, :users, column: :user_id, validate: false

Expand All @@ -20,6 +23,7 @@ def down
add_column :aggregations, :aggregation, :jsonb

remove_column :aggregations, :user_id
remove_column :aggregations, :project_id
remove_column :aggregations, :uuid
remove_column :aggregations, :task_id
remove_column :aggregations, :status
Expand Down
11 changes: 10 additions & 1 deletion db/structure.sql
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,11 @@ CREATE TABLE public.aggregations (
workflow_id integer,
created_at timestamp without time zone,
updated_at timestamp without time zone,
project_id integer,
user_id integer,
uuid character varying,
task_id character varying,
status integer
status integer DEFAULT 0
);


Expand Down Expand Up @@ -4209,6 +4210,14 @@ ALTER TABLE ONLY public.organization_versions
ADD CONSTRAINT fk_rails_be858ed31d FOREIGN KEY (organization_id) REFERENCES public.organizations(id);


--
-- Name: aggregations fk_rails_c7d229ada4; Type: FK CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.aggregations
ADD CONSTRAINT fk_rails_c7d229ada4 FOREIGN KEY (project_id) REFERENCES public.projects(id) NOT VALID;


--
-- Name: subject_set_imports fk_rails_d596712569; Type: FK CONSTRAINT; Schema: public; Owner: -
--
Expand Down
7 changes: 6 additions & 1 deletion spec/controllers/api/v1/aggregations_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
let(:api_resource_attributes) { %w[id created_at updated_at uuid task_id status] }
let(:api_resource_links) { %w[aggregations.workflow] }

let(:scopes) { %w[public workflow] }
let(:scopes) { %w[project] }
let(:resource_class) { Aggregation }

describe '#index' do
Expand Down Expand Up @@ -55,6 +55,11 @@

it_behaves_like 'is creatable'

it 'saves the project id' do
post :create, params: create_params
expect(Aggregation.first.project_id).to eq(workflow.project.id)
end

it 'makes a request to the aggregation service' do
post :create, params: create_params
expect(mock_agg).to have_received(:send_aggregation_request)
Expand Down
1 change: 1 addition & 0 deletions spec/factories/aggregations.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
FactoryBot.define do
factory :aggregation do
workflow
project
user
status { Aggregation.statuses[:pending] }
end
Expand Down
8 changes: 5 additions & 3 deletions spec/models/aggregation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,16 @@
expect(build(:aggregation, workflow: nil)).not_to be_valid
end

it 'is not be valid without a project' do
expect(build(:aggregation, project: nil)).not_to be_valid
end

it 'is not be valid without a user' do
expect(build(:aggregation, user: nil)).not_to be_valid
end

context 'when there is a duplicate user_id workflow_id entry' do
before(:each) do
aggregation.save
end
before { aggregation.save }
let(:duplicate) do
build(:aggregation, workflow: aggregation.workflow,
user: aggregation.user)
Expand Down

0 comments on commit 50f14c1

Please sign in to comment.