Skip to content

Commit

Permalink
Doorkeeper tests (#248)
Browse files Browse the repository at this point in the history
* Update Pundit inclusion logic (Deprecation)

* Add doorkeeper tests

* Add Simplecov and more tests

* Remove unecessary code

* rubocop
  • Loading branch information
brunto authored Jan 31, 2024
1 parent 0b51cdd commit 488df60
Show file tree
Hide file tree
Showing 21 changed files with 113 additions and 71 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,4 @@ Dockerfile
/data/
.env
.DS_Store

coverage/
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
8 changes: 8 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -295,6 +302,7 @@ DEPENDENCIES
rubocop
rubocop-rails
rubocop-rails-omakase
simplecov
tzinfo-data

RUBY VERSION
Expand Down
8 changes: 3 additions & 5 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"])
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/comments_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 3 additions & 7 deletions app/controllers/measures_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion app/models/pia.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ def self.import(json_string)
end
end
current_pia.save
p current_pia
end
end

Expand Down
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
10 changes: 5 additions & 5 deletions test/controllers/answers_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions test/controllers/attachments_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down
10 changes: 5 additions & 5 deletions test/controllers/comments_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions test/controllers/evaluations_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 9 additions & 5 deletions test/controllers/knowledge_bases_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 14 additions & 6 deletions test/controllers/knowledges_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions test/controllers/measures_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading

0 comments on commit 488df60

Please sign in to comment.