diff --git a/.gitignore b/.gitignore index 1a1bf0d7..9da5764a 100644 --- a/.gitignore +++ b/.gitignore @@ -51,4 +51,4 @@ Dockerfile /data/ .env .DS_Store - +coverage/ diff --git a/Gemfile b/Gemfile index e4a4146d..210e2ea6 100644 --- a/Gemfile +++ b/Gemfile @@ -68,3 +68,5 @@ end # Windows does not include zoneinfo files, so bundle the tzinfo-data gem gem 'tzinfo-data', platforms: %i[mingw mswin x64_mingw jruby] gem "rubocop-rails-omakase", require: false, group: [ :development ] + +gem 'simplecov', require: false, group: :test diff --git a/Gemfile.lock b/Gemfile.lock index 6b2458dc..b99a432c 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -106,6 +106,7 @@ GEM devise_ldap_authenticatable (0.8.7) devise (>= 3.4.1) net-ldap (>= 0.16.0) + docile (1.4.0) doorkeeper (5.6.8) railties (>= 5) doorkeeper-i18n (5.2.7) @@ -251,6 +252,12 @@ GEM ruby-progressbar (1.13.0) ruby-vips (2.2.0) ffi (~> 1.12) + simplecov (0.22.0) + docile (~> 1.1) + simplecov-html (~> 0.11) + simplecov_json_formatter (~> 0.1) + simplecov-html (0.12.3) + simplecov_json_formatter (0.1.4) ssrf_filter (1.1.2) thor (1.3.0) timeout (0.4.1) @@ -295,6 +302,7 @@ DEPENDENCIES rubocop rubocop-rails rubocop-rails-omakase + simplecov tzinfo-data RUBY VERSION diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 34c1ae63..05f0a2a6 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -3,11 +3,9 @@ class ApplicationController < ActionController::API render text: exception, status: :internal_server_error end - include Pundit if ENV['ENABLE_AUTHENTICATION'].present? - if ENV['ENABLE_AUTHENTICATION'].present? - before_action :doorkeeper_authorize!, - except: %i[info check_uuid password_forgotten change_password] - end + include Pundit::Authorization if ENV['ENABLE_AUTHENTICATION'].present? + before_action :doorkeeper_authorize!, + except: %i[info check_uuid password_forgotten change_password] def info client_app = Doorkeeper::Application.find_by(uid: params["client_id"], secret: params["client_secret"]) diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb index e9e5aefc..1623da1b 100644 --- a/app/controllers/comments_controller.rb +++ b/app/controllers/comments_controller.rb @@ -25,7 +25,7 @@ def show # POST /comments def create @comment = Comment.new(comment_params) - @comment.user = current_user + @comment.user = current_user if ENV['ENABLE_AUTHENTICATION'].present? if @comment.save render json: serialize(@comment.reload), status: :created diff --git a/app/controllers/measures_controller.rb b/app/controllers/measures_controller.rb index 32cb8930..b552dd8e 100644 --- a/app/controllers/measures_controller.rb +++ b/app/controllers/measures_controller.rb @@ -14,13 +14,9 @@ class MeasuresController < ApplicationController # GET /measures def index - if params[:reference_to] - measures = serialize Measure.find_by(pia_id: params[:pia_id], reference_to: params[:reference_to]) - else - measures = [] - Measure.where(pia_id: params[:pia_id]).find_each do |measure| - measures << serialize(measure) - end + measures = [] + Measure.where(pia_id: params[:pia_id]).find_each do |measure| + measures << serialize(measure) end render json: measures diff --git a/app/models/pia.rb b/app/models/pia.rb index 16885592..590ae309 100644 --- a/app/models/pia.rb +++ b/app/models/pia.rb @@ -38,7 +38,6 @@ def self.import(json_string) end end current_pia.save - p current_pia end end diff --git a/config/routes.rb b/config/routes.rb index aa0b534e..ee3aef3a 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,5 +1,5 @@ Rails.application.routes.draw do - use_doorkeeper if ENV['ENABLE_AUTHENTICATION'].present? + use_doorkeeper post '/info', to: 'application#info' resources :users do diff --git a/test/controllers/answers_controller_test.rb b/test/controllers/answers_controller_test.rb index 2121d059..b7ada600 100644 --- a/test/controllers/answers_controller_test.rb +++ b/test/controllers/answers_controller_test.rb @@ -7,31 +7,31 @@ class AnswersControllerTest < ActionDispatch::IntegrationTest end test 'should get index' do - get pia_answers_url(@pia), as: :json + get pia_answers_url(@pia), headers: { 'Authorization' => "Bearer #{doorkeeper_token}" }, as: :json assert_response :success end test 'should create answer' do assert_difference('Answer.count') do - post pia_answers_url(@pia), params: { answer: { reference_to: '1.1.2' } }, as: :json + post pia_answers_url(@pia), params: { answer: { reference_to: '1.1.2' } }, headers: { 'Authorization' => "Bearer #{doorkeeper_token}" }, as: :json end assert_response 201 end test 'should show answer' do - get pia_answer_url(id: @answer.id, pia_id: @pia.id), as: :json + get pia_answer_url(id: @answer.id, pia_id: @pia.id), headers: { 'Authorization' => "Bearer #{doorkeeper_token}" }, as: :json assert_response :success end test 'should update answer' do - patch pia_answer_url(id: @answer, pia_id: @pia.id), params: { answer: {} }, as: :json + patch pia_answer_url(id: @answer, pia_id: @pia.id), params: { answer: {} }, headers: { 'Authorization' => "Bearer #{doorkeeper_token}" }, as: :json assert_response 200 end test 'should destroy answer' do assert_difference('Answer.count', -1) do - delete pia_answer_url(id: @answer.id, pia_id: @pia.id), as: :json + delete pia_answer_url(id: @answer.id, pia_id: @pia.id), headers: { 'Authorization' => "Bearer #{doorkeeper_token}" }, as: :json end assert_response 204 diff --git a/test/controllers/attachments_controller_test.rb b/test/controllers/attachments_controller_test.rb index f3d4bc52..76c5f876 100644 --- a/test/controllers/attachments_controller_test.rb +++ b/test/controllers/attachments_controller_test.rb @@ -7,7 +7,7 @@ class AttachmentsControllerTest < ActionDispatch::IntegrationTest end test 'should get index' do - get pia_attachments_url(@pia), as: :json + get pia_attachments_url(@pia), headers: { 'Authorization' => "Bearer #{doorkeeper_token}" }, as: :json assert_response :success end @@ -20,13 +20,13 @@ class AttachmentsControllerTest < ActionDispatch::IntegrationTest # end test 'should show attachment' do - get pia_attachment_url(id: @attachment.id, pia_id: @pia.id), as: :json + get pia_attachment_url(id: @attachment.id, pia_id: @pia.id), headers: { 'Authorization' => "Bearer #{doorkeeper_token}" }, as: :json assert_response :success end test 'should destroy attachment' do assert_difference('Attachment.count', -1) do - delete pia_attachment_url(id: @attachment.id, pia_id: @pia.id), as: :json + delete pia_attachment_url(id: @attachment.id, pia_id: @pia.id), headers: { 'Authorization' => "Bearer #{doorkeeper_token}" }, as: :json end assert_response 204 diff --git a/test/controllers/comments_controller_test.rb b/test/controllers/comments_controller_test.rb index 28957613..d8a42ee9 100644 --- a/test/controllers/comments_controller_test.rb +++ b/test/controllers/comments_controller_test.rb @@ -7,31 +7,31 @@ class CommentsControllerTest < ActionDispatch::IntegrationTest end test 'should get index' do - get pia_comments_url(@pia), as: :json + get pia_comments_url(@pia), headers: { 'Authorization' => "Bearer #{doorkeeper_token}" }, as: :json assert_response :success end test 'should create comment' do assert_difference('Comment.count') do - post pia_comments_url(@pia), params: { comment: { reference_to: '1.1.2' } }, as: :json + post pia_comments_url(@pia), params: { comment: { reference_to: '1.1.2' } }, headers: { 'Authorization' => "Bearer #{doorkeeper_token}" }, as: :json end assert_response 201 end test 'should show comment' do - get pia_comment_url(id: @comment.id, pia_id: @pia.id), as: :json + get pia_comment_url(id: @comment.id, pia_id: @pia.id), headers: { 'Authorization' => "Bearer #{doorkeeper_token}" }, as: :json assert_response :success end test 'should update comment' do - patch pia_comment_url(id: @comment.id, pia_id: @pia.id), params: { comment: {} }, as: :json + patch pia_comment_url(id: @comment.id, pia_id: @pia.id), params: { comment: {} }, headers: { 'Authorization' => "Bearer #{doorkeeper_token}" }, as: :json assert_response 200 end test 'should destroy comment' do assert_difference('Comment.count', -1) do - delete pia_comment_url(id: @comment.id, pia_id: @pia.id), as: :json + delete pia_comment_url(id: @comment.id, pia_id: @pia.id), headers: { 'Authorization' => "Bearer #{doorkeeper_token}" }, as: :json end assert_response 204 diff --git a/test/controllers/evaluations_controller_test.rb b/test/controllers/evaluations_controller_test.rb index 31626927..1ac2b848 100644 --- a/test/controllers/evaluations_controller_test.rb +++ b/test/controllers/evaluations_controller_test.rb @@ -7,31 +7,31 @@ class EvaluationsControllerTest < ActionDispatch::IntegrationTest end test 'should get index' do - get pia_evaluations_url(@pia), as: :json + get pia_evaluations_url(@pia), headers: { 'Authorization' => "Bearer #{doorkeeper_token}" }, as: :json assert_response :success end test 'should create evaluation' do assert_difference('Evaluation.count') do - post pia_evaluations_url(@pia), params: { evaluation: { reference_to: '1.1.2', evaluation_infos: '{}' } }, as: :json + post pia_evaluations_url(@pia), params: { evaluation: { reference_to: '1.1.2', evaluation_infos: '{}' } }, headers: { 'Authorization' => "Bearer #{doorkeeper_token}" }, as: :json end assert_response 201 end test 'should show evaluation' do - get pia_evaluation_url(id: @evaluation.id, pia_id: @pia.id), as: :json + get pia_evaluation_url(id: @evaluation.id, pia_id: @pia.id), headers: { 'Authorization' => "Bearer #{doorkeeper_token}" }, as: :json assert_response :success end test 'should update evaluation' do - patch pia_evaluation_url(id: @evaluation.id, pia_id: @pia.id), params: { evaluation: {} }, as: :json + patch pia_evaluation_url(id: @evaluation.id, pia_id: @pia.id), params: { evaluation: {} }, headers: { 'Authorization' => "Bearer #{doorkeeper_token}" }, as: :json assert_response 200 end test 'should destroy eva' do assert_difference('Evaluation.count', -1) do - delete pia_evaluation_url(id: @evaluation.id, pia_id: @pia.id), as: :json + delete pia_evaluation_url(id: @evaluation.id, pia_id: @pia.id), headers: { 'Authorization' => "Bearer #{doorkeeper_token}" }, as: :json end assert_response 204 diff --git a/test/controllers/knowledge_bases_controller_test.rb b/test/controllers/knowledge_bases_controller_test.rb index 1598b672..98a4c923 100644 --- a/test/controllers/knowledge_bases_controller_test.rb +++ b/test/controllers/knowledge_bases_controller_test.rb @@ -6,32 +6,36 @@ class KnowledgeBasesControllerTest < ActionDispatch::IntegrationTest end test 'should get index' do - get knowledge_bases_url, as: :json + get knowledge_bases_url, headers: { 'Authorization' => "Bearer #{doorkeeper_token}" }, as: :json assert_response :success end test 'should create KnowledgeBase' do assert_difference('KnowledgeBase.count') do post knowledge_bases_url, - params: { knowledge_base: { name: 'Knowledge Base', author: 'Author name', contributors: 'Contributors name' } }, as: :json + params: { knowledge_base: { name: 'Knowledge Base', author: 'Author name', contributors: 'Contributors name' } }, + headers: { 'Authorization' => "Bearer #{doorkeeper_token}" }, + as: :json end assert_response 201 end test 'should show KnowledgeBase' do - get knowledge_bases_url(@knowledge_base), as: :json + get knowledge_bases_url(@knowledge_base), headers: { 'Authorization' => "Bearer #{doorkeeper_token}" }, as: :json assert_response :success end test 'should update KnowledgeBase' do - patch knowledge_base_url(@knowledge_base), params: { knowledge_base: { name: 'Knowledge Base 2' } }, as: :json + patch knowledge_base_url(@knowledge_base), params: { knowledge_base: { name: 'Knowledge Base 2' } }, + headers: { 'Authorization' => "Bearer #{doorkeeper_token}" }, + as: :json assert_response 200 end test 'should destroy KnowledgeBase' do assert_difference('KnowledgeBase.count', -1) do - delete knowledge_base_url(@knowledge_base), as: :json + delete knowledge_base_url(@knowledge_base), headers: { 'Authorization' => "Bearer #{doorkeeper_token}" }, as: :json end assert_response 204 diff --git a/test/controllers/knowledges_controller_test.rb b/test/controllers/knowledges_controller_test.rb index 151f3596..93c37470 100644 --- a/test/controllers/knowledges_controller_test.rb +++ b/test/controllers/knowledges_controller_test.rb @@ -6,33 +6,41 @@ class KnowledgesControllerTest < ActionDispatch::IntegrationTest end test 'should get index' do - get knowledge_base_knowledges_url(knowledge_base_id: @knowledge.knowledge_base.id), as: :json + get knowledge_base_knowledges_url(knowledge_base_id: @knowledge.knowledge_base.id), headers: { 'Authorization' => "Bearer #{doorkeeper_token}" }, as: :json assert_response :success end test 'should create Knowledge' do assert_difference('Knowledge.count') do - post knowledge_base_knowledges_url(knowledge_base_id: @knowledge.knowledge_base.id), params: { knowledge: { name: 'Knowledge' } }, - as: :json + post knowledge_base_knowledges_url(knowledge_base_id: @knowledge.knowledge_base.id), + params: { knowledge: { name: 'Knowledge' } }, + headers: { 'Authorization' => "Bearer #{doorkeeper_token}" }, + as: :json end assert_response 201 end test 'should show Knowledge' do - get knowledge_base_knowledge_url(knowledge_base_id: @knowledge.knowledge_base.id, id: @knowledge), as: :json + get knowledge_base_knowledge_url(knowledge_base_id: @knowledge.knowledge_base.id, id: @knowledge), + headers: { 'Authorization' => "Bearer #{doorkeeper_token}" }, + as: :json assert_response :success end test 'should update Knowledge' do patch knowledge_base_knowledge_url(knowledge_base_id: @knowledge.knowledge_base.id, id: @knowledge.id), - params: { knowledge: { name: 'Knowledge 2' } }, as: :json + params: { knowledge: { name: 'Knowledge 2' } }, + headers: { 'Authorization' => "Bearer #{doorkeeper_token}" }, + as: :json assert_response 200 end test 'should destroy Knowledge' do assert_difference('Knowledge.count', -1) do - delete knowledge_base_knowledge_url(knowledge_base_id: @knowledge.knowledge_base.id, id: @knowledge.id), as: :json + delete knowledge_base_knowledge_url(knowledge_base_id: @knowledge.knowledge_base.id, id: @knowledge.id), + headers: { 'Authorization' => "Bearer #{doorkeeper_token}" }, + as: :json end assert_response 204 diff --git a/test/controllers/measures_controller_test.rb b/test/controllers/measures_controller_test.rb index 18f69b39..bde59f45 100644 --- a/test/controllers/measures_controller_test.rb +++ b/test/controllers/measures_controller_test.rb @@ -7,31 +7,31 @@ class MeasuresControllerTest < ActionDispatch::IntegrationTest end test 'should get index' do - get pia_measures_url(@pia), as: :json + get pia_measures_url(@pia), headers: { 'Authorization' => "Bearer #{doorkeeper_token}" }, as: :json assert_response :success end test 'should create measure' do assert_difference('Measure.count') do - post pia_measures_url(@pia), params: { measure: {} }, as: :json + post pia_measures_url(@pia), params: { measure: {} }, headers: { 'Authorization' => "Bearer #{doorkeeper_token}" }, as: :json end assert_response 201 end test 'should show measure' do - get pia_measure_url(id: @measure.id, pia_id: @pia.id), as: :json + get pia_measure_url(id: @measure.id, pia_id: @pia.id), headers: { 'Authorization' => "Bearer #{doorkeeper_token}" }, as: :json assert_response :success end test 'should update measure' do - patch pia_measure_url(id: @measure.id, pia_id: @pia.id), params: { measure: {} }, as: :json + patch pia_measure_url(id: @measure.id, pia_id: @pia.id), params: { measure: {} }, headers: { 'Authorization' => "Bearer #{doorkeeper_token}" }, as: :json assert_response 200 end test 'should destroy measure' do assert_difference('Measure.count', -1) do - delete pia_measure_url(id: @measure.id, pia_id: @pia.id), as: :json + delete pia_measure_url(id: @measure.id, pia_id: @pia.id), headers: { 'Authorization' => "Bearer #{doorkeeper_token}" }, as: :json end assert_response 204 diff --git a/test/controllers/pias_controller_test.rb b/test/controllers/pias_controller_test.rb index b4719b8d..95ecd184 100644 --- a/test/controllers/pias_controller_test.rb +++ b/test/controllers/pias_controller_test.rb @@ -6,40 +6,40 @@ class PiasControllerTest < ActionDispatch::IntegrationTest end test 'should get index' do - get pias_url, as: :json + get pias_url, headers: { 'Authorization' => "Bearer #{doorkeeper_token}" }, as: :json assert_response :success end test 'should create pia' do pia_build = FactoryBot.build(:pia) assert_difference('Pia.count') do - post pias_url, params: { pia: { name: 'PIA' } }, as: :json + post pias_url, params: { pia: { name: 'PIA' } }, headers: { 'Authorization' => "Bearer #{doorkeeper_token}" }, as: :json end assert_response 201 end test 'should show pia' do - get pia_url(@pia), as: :json + get pia_url(@pia), headers: { 'Authorization' => "Bearer #{doorkeeper_token}" }, as: :json assert_response :success end test 'should update pia' do - patch pia_url(@pia), params: { pia: {} }, as: :json + patch pia_url(@pia), params: { pia: {} }, headers: { 'Authorization' => "Bearer #{doorkeeper_token}" }, as: :json assert_response 200 end test 'should destroy pia' do assert_difference('Pia.count', -1) do - delete pia_url(@pia), as: :json + delete pia_url(@pia), headers: { 'Authorization' => "Bearer #{doorkeeper_token}" }, as: :json end assert_response 204 end test 'should duplicate pia' do - assert_difference('Pia.where(name: "PIA ONE").count') do - post duplicate_pia_url(@pia) + assert_difference('Pia.count') do + post duplicate_pia_url(@pia), headers: { 'Authorization' => "Bearer #{doorkeeper_token}" } end end end diff --git a/test/controllers/revisions_controller_test.rb b/test/controllers/revisions_controller_test.rb index 7a7e2a2b..71050146 100644 --- a/test/controllers/revisions_controller_test.rb +++ b/test/controllers/revisions_controller_test.rb @@ -7,26 +7,26 @@ class RevisionsControllerTest < ActionDispatch::IntegrationTest end test 'should get index' do - get pia_revisions_url(@pia), as: :json + get pia_revisions_url(@pia), headers: { 'Authorization' => "Bearer #{doorkeeper_token}" }, as: :json assert_response :success end test 'should create revision' do assert_difference('Revision.count') do - post pia_revisions_url(@pia), params: { revision: { export: { pia: [] }.to_json } }, as: :json + post pia_revisions_url(@pia), params: { revision: { export: { pia: [] }.to_json } }, headers: { 'Authorization' => "Bearer #{doorkeeper_token}" }, as: :json end assert_response 201 end test 'should show revision' do - get pia_revision_url(id: @revision, pia_id: @pia.id), as: :json + get pia_revision_url(id: @revision, pia_id: @pia.id), headers: { 'Authorization' => "Bearer #{doorkeeper_token}" }, as: :json assert_response :success end test 'should destroy revision' do assert_difference('Revision.count', -1) do - delete pia_revision_url(id: @revision, pia_id: @pia.id), as: :json + delete pia_revision_url(id: @revision, pia_id: @pia.id), headers: { 'Authorization' => "Bearer #{doorkeeper_token}" }, as: :json end assert_response 204 diff --git a/test/controllers/structures_controller_test.rb b/test/controllers/structures_controller_test.rb index a1714abb..91f85d9e 100644 --- a/test/controllers/structures_controller_test.rb +++ b/test/controllers/structures_controller_test.rb @@ -6,33 +6,35 @@ class StructuresControllerTest < ActionDispatch::IntegrationTest end test 'should get index' do - get structures_url, as: :json + get structures_url, headers: { 'Authorization' => "Bearer #{doorkeeper_token}" }, as: :json assert_response :success end test 'should create structure' do structure_build = FactoryBot.build(:structure) assert_difference('Structure.count') do - post structures_url, params: { structure: { name: 'Structure 1', sector_name: 'Structure Sector Name 1', data: '{"sections":[]}' } }, - as: :json + post structures_url, + params: { structure: { name: 'Structure 1', sector_name: 'Structure Sector Name 1', data: '{"sections":[]}' } }, + headers: { 'Authorization' => "Bearer #{doorkeeper_token}" }, + as: :json end assert_response 201 end test 'should show structure' do - get structure_url(@structure), as: :json + get structure_url(@structure), headers: { 'Authorization' => "Bearer #{doorkeeper_token}" }, as: :json assert_response :success end test 'should update structure' do - patch structure_url(@structure), params: { structure: { name: 'Structure 1 updated' } }, as: :json + patch structure_url(@structure), params: { structure: { name: 'Structure 1 updated' } }, headers: { 'Authorization' => "Bearer #{doorkeeper_token}" }, as: :json assert_response 200 end test 'should destroy structure' do assert_difference('Structure.count', -1) do - delete structure_url(@structure), as: :json + delete structure_url(@structure), headers: { 'Authorization' => "Bearer #{doorkeeper_token}" }, as: :json end assert_response 204 diff --git a/test/factories/pias.rb b/test/factories/pias.rb index a739b603..c7a9fde4 100644 --- a/test/factories/pias.rb +++ b/test/factories/pias.rb @@ -1,7 +1,7 @@ FactoryBot.define do factory :pia do status { 0 } - name { 'PIA ONE' } + name { "PIA #{SecureRandom.uuid}" } author_name { 'William S. Burroughs' } evaluator_name { 'William Lee' } validator_name { 'Norman Mailer' } diff --git a/test/models/pia_test.rb b/test/models/pia_test.rb index b239f8c0..b9c677e2 100644 --- a/test/models/pia_test.rb +++ b/test/models/pia_test.rb @@ -1,7 +1,24 @@ require 'test_helper' class PiaTest < ActiveSupport::TestCase - # test "the truth" do - # assert true - # end + test "import should save a new PIA" do + data = [{ + name: "PIA #{SecureRandom.uuid}", + answers: [], + evaluations: [], + comments: [], + measures: [] + }] + + assert Pia.import(data.to_json) + assert Pia.find_by(name: data.first[:name]) + end + + test 'duplicate a PIA' do + pia = FactoryBot.create(:pia) + pia.duplicate + + assert_equal pia.name, Pia.last.name + assert_not_equal pia.id, Pia.last.id + end end diff --git a/test/test_helper.rb b/test/test_helper.rb index d83b17bd..9112bba7 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -1,4 +1,8 @@ ENV["RAILS_ENV"] ||= "test" + +require 'simplecov' +SimpleCov.start + require_relative "../config/environment" require "rails/test_help" @@ -13,5 +17,9 @@ class TestCase # fixtures :all # Add more helper methods to be used by all tests here... + def doorkeeper_token + oauth_application = Doorkeeper::Application.create!(name: "PIA", redirect_uri: "urn:ietf:wg:oauth:2.0:oob", scopes: %w[read write]) + Doorkeeper::AccessToken.create!(application: oauth_application).token + end end end