diff --git a/app/controllers/v1/learning_objects/attachment_controller.rb b/app/controllers/v1/learning_objects/attachment_controller.rb index 21c7b39256f101f86b2e02c25a5ffb2be290463e..b75dd85bfd4956688d2d876ae292389d5c3ce0c3 100644 --- a/app/controllers/v1/learning_objects/attachment_controller.rb +++ b/app/controllers/v1/learning_objects/attachment_controller.rb @@ -19,7 +19,9 @@ class V1::LearningObjects::AttachmentController < ApplicationController before_action :set_objects - before_action :authorize! + before_action :authenticate_user!, only: :update + before_action :authorize!, only: :destroy + before_action :authorize_update_attachment!, only: :update # DELETE /learning_objects/:learning_object_id/attachments/:id def destroy @@ -30,6 +32,16 @@ class V1::LearningObjects::AttachmentController < ApplicationController render status: :ok end + + # PUT /learning_objects/:learning_object_id/attachments/:id + def update + return render status: :not_found if @learning_object.nil? || @attachment.nil? + if @attachment.update(infohash: infohash_params[:infohash]) + render status: :ok + else + render status: :unprocessable_entity + end + end private @@ -37,6 +49,10 @@ class V1::LearningObjects::AttachmentController < ApplicationController params.permit(:learning_object_id, :id) end + def infohash_params + params.permit(:infohash) + end + def set_objects @learning_object = LearningObject.find(attachment_params[:learning_object_id]) @attachment = LearningObject::Attachment.find(attachment_params[:id]) @@ -47,4 +63,11 @@ class V1::LearningObjects::AttachmentController < ApplicationController authorize(@learning_object, :destroy?) end + + def authorize_update_attachment! + return render status: :unauthorized unless @learning_object.attachments.include? @attachment + + authorize(@learning_object, :add_infohash?) + end + end diff --git a/app/policies/learning_object_policy.rb b/app/policies/learning_object_policy.rb index ff55dfcec746a90f337062a1fd627bb5c1c15103..1b06e4dd5b188748a706c480fc2475fa9308870b 100644 --- a/app/policies/learning_object_policy.rb +++ b/app/policies/learning_object_policy.rb @@ -61,6 +61,10 @@ class LearningObjectPolicy < ApplicationPolicy record if owns? end + def add_infohash? + record if user.is_admin? + end + def show? return record if record.published? || ( !user.nil? && user_can_edit? ) return record if user == record.publisher diff --git a/config/routes.rb b/config/routes.rb index cb7691a91eb9f132edb30a4cfb8277139fbd2697..c378cdbf174471af8bfe15f2df45569c26d043bf 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -165,7 +165,7 @@ Rails.application.routes.draw do resource :upload, module: 'learning_objects', only: :create resource :publish, module: 'learning_objects', only: :create end - resources :attachment, module: 'learning_objects', only: :destroy, on: :member + resources :attachment, module: 'learning_objects', only: [:destroy, :update], on: :member end resources :institutions, concerns: :deletable do diff --git a/db/migrate/20170808132744_add_info_hash_to_learning_object_attachments.rb b/db/migrate/20170808132744_add_info_hash_to_learning_object_attachments.rb new file mode 100644 index 0000000000000000000000000000000000000000..0b0725c9987f26666f11f66fa740eeefb490362d --- /dev/null +++ b/db/migrate/20170808132744_add_info_hash_to_learning_object_attachments.rb @@ -0,0 +1,5 @@ +class AddInfoHashToLearningObjectAttachments < ActiveRecord::Migration[5.0] + def change + add_column :learning_object_attachments, :infohash, :string + end +end diff --git a/spec/acceptance/learning_objects_spec.rb b/spec/acceptance/learning_objects_spec.rb index dacb36a4916c9bb0fe33ebf7de34e43b8f75bf92..de43a4729992b711f6894c25fe2188eed4e9f8b1 100644 --- a/spec/acceptance/learning_objects_spec.rb +++ b/spec/acceptance/learning_objects_spec.rb @@ -225,5 +225,45 @@ resource 'Learning Objects' do end end + get '/v1/learning_objects/submissions' do + include_context "authenticate_user_curator" + + example 'Get a list of submissions' do + do_request + expect(status).to eq(200) + end + end + + get '/v1/learning_objects/:id/show_submission' do + include_context "authenticate_user_curator" + + let(:id) { @learning_object.id } + + before do + @learning_object = create(:learning_object) + @learning_object.update(state: LearningObject.states[:submitted]) + end + + example 'Show a submission' do + do_request + expect(status).to eq(200) + end + end + + post '/v1/learning_objects/:id/submit' do + include_context "authenticate_user_submitter" + + let(:id) { @learning_object.id } + + before do + @learning_object = create(:learning_object, publisher: @user) + @learning_object.update(state: LearningObject.states[:draft]) + end + + example 'Submit a learning object to curator' do + do_request + expect(status).to eq(200) + end + end end diff --git a/spec/acceptance/users_spec.rb b/spec/acceptance/users_spec.rb index 1bca1353fd616107955ea377aa9dafa063d7c0e0..226304f9575c1b356db2df32289658aa35d2a9df 100644 --- a/spec/acceptance/users_spec.rb +++ b/spec/acceptance/users_spec.rb @@ -310,4 +310,42 @@ resource 'Users' do end end + get '/v1/users/upload_requests' do + include_context "authenticate_user_supervisor" + + example 'Get all user that want be submitters' do + do_request + expect(status).to eq(200) + end + end + + post '/v1/users/submitter_request' do + include_context "authenticate_user" + + example 'Request to be a submitter' do + do_request + expect(status).to eq(200) + end + end + + post '/v1/users/:id/approve_request' do + include_context "authenticate_user_supervisor" + + parameter :id, 'The id of the user who asked to be a submitter' + parameter :approves, 'If the user can be a submitter or not' + + let(:id) {@user.id} + let(:approves) { true } + let(:raw_post) {params.to_json} + + before do + @user = create(:user, ) + @user.update(submitter_request: User.submitter_requests[:requested]) + end + + example 'Approve a submitter request' do + do_request + expect(status).to eq(200) + end + end end diff --git a/spec/controllers/v1/collections_controller_spec.rb b/spec/controllers/v1/collections_controller_spec.rb index 9b56ab238df3366b64cbfe098fe44be0ad86ffa0..4d00e40a193c4344ac04debb1dc38ac29708d0ac 100644 --- a/spec/controllers/v1/collections_controller_spec.rb +++ b/spec/controllers/v1/collections_controller_spec.rb @@ -64,4 +64,4 @@ RSpec.describe V1::CollectionsController, type: :controller do it { expect(response).to have_http_status(:ok) } end -end \ No newline at end of file +end