Skip to content

Commit

Permalink
Add doorkeeper tests
Browse files Browse the repository at this point in the history
  • Loading branch information
brunto committed Jan 29, 2024
1 parent 335b2b3 commit b4942ca
Show file tree
Hide file tree
Showing 14 changed files with 72 additions and 56 deletions.
6 changes: 2 additions & 4 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@ class ApplicationController < ActionController::API
end

include Pundit::Authorization if ENV['ENABLE_AUTHENTICATION'].present?
if ENV['ENABLE_AUTHENTICATION'].present?
before_action :doorkeeper_authorize!,
except: %i[info check_uuid password_forgotten change_password]
end
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
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
12 changes: 6 additions & 6 deletions test/controllers/pias_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
post duplicate_pia_url(@pia), headers: { 'Authorization' => "Bearer #{doorkeeper_token}" }
end
end
end
8 changes: 4 additions & 4 deletions test/controllers/revisions_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading

0 comments on commit b4942ca

Please sign in to comment.