-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge PR #202 from TreinaDev/feat/api-endpoint-recusa-solicitação-con…
…vite Feat/api endpoint recusa solicitação convite Co-authored-by: [email protected] <[email protected]>
- Loading branch information
Showing
5 changed files
with
163 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
module Api | ||
module V1 | ||
class InvitationRequestController < ApiController | ||
def update | ||
raise ActiveRecord::ParameterMissing if params[:id].blank? | ||
|
||
invitation_request = InvitationRequest.find(params[:id]) | ||
if invitation_request.pending? | ||
invitation_request.refused! | ||
render status: :ok, json: invitation_request.as_json(except: %i[created_at updated_at]) | ||
else | ||
render status: :conflict, json: { errors: I18n.t('.api_not_pending_error') } | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
spec/requests/apis/v1/invitation_requests/leader_denies_invitation_request_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
require 'rails_helper' | ||
|
||
describe 'API solicitações' do | ||
context 'PATCH /api/v1/invitation_requests/:id' do | ||
it 'altera o status para recusado com sucesso' do | ||
user = create(:user) | ||
invitation_request = create(:invitation_request, profile: user.profile, status: :pending, message: 'oi projeto') | ||
|
||
patch "/api/v1/invitation_request/#{invitation_request.id}" | ||
invitation_request.reload | ||
|
||
expect(response).to have_http_status(200) | ||
expect(response.content_type).to include('application/json') | ||
json_response = JSON.parse(response.body) | ||
expect(json_response.class).to eq Hash | ||
expect(json_response['id']).to eq 1 | ||
expect(json_response['profile_id']).to eq 1 | ||
expect(json_response['message']).to eq 'oi projeto' | ||
expect(json_response['project_id']).to eq 1 | ||
expect(json_response.keys).not_to include 'created_at' | ||
expect(json_response.keys).not_to include 'updated_at' | ||
expect(json_response['status']).to eq 'refused' | ||
end | ||
|
||
it 'retorna erro 409 se a solicitação não estiver pendente' do | ||
request1 = create(:invitation_request, status: :processing) | ||
request2 = create(:invitation_request, status: :accepted) | ||
request3 = create(:invitation_request, status: :refused) | ||
request4 = create(:invitation_request, status: :error) | ||
request5 = create(:invitation_request, status: :aborted) | ||
|
||
response_status1 = patch "/api/v1/invitation_request/#{request1.id}" | ||
response_status2 = patch "/api/v1/invitation_request/#{request2.id}" | ||
response_status3 = patch "/api/v1/invitation_request/#{request3.id}" | ||
response_status4 = patch "/api/v1/invitation_request/#{request4.id}" | ||
response_status5 = patch "/api/v1/invitation_request/#{request5.id}" | ||
request1.reload | ||
request2.reload | ||
request3.reload | ||
request4.reload | ||
request5.reload | ||
|
||
expect(response_status1).to eq 409 | ||
expect(response_status2).to eq 409 | ||
expect(response_status3).to eq 409 | ||
expect(response_status4).to eq 409 | ||
expect(response_status5).to eq 409 | ||
expect(request1).to be_processing | ||
expect(request2).to be_accepted | ||
expect(request3).to be_refused | ||
expect(request4).to be_error | ||
expect(request5).to be_aborted | ||
expect(response.content_type).to include 'application/json' | ||
json_last_response = JSON.parse(response.body) | ||
expect(json_last_response.class).to eq Hash | ||
expect(json_last_response['errors']).to include 'Solicitação de convite não está pendente para ser recusada.' | ||
end | ||
|
||
it 'retorna erro 404 se a solicitação não for encontrada' do | ||
patch '/api/v1/invitation_request/1939' | ||
|
||
expect(response.status).to eq 404 | ||
expect(response.content_type).to include 'application/json' | ||
json_response = JSON.parse(response.body) | ||
expect(json_response['error']).to include 'Não encontrado' | ||
end | ||
|
||
it 'retorna erro 500, com mensagem identificando erro do servidor' do | ||
allow(InvitationRequest).to receive(:find).and_raise(ActiveRecord::ActiveRecordError) | ||
invitation_request = create(:invitation_request) | ||
patch "/api/v1/invitation_request/#{invitation_request.id}" | ||
|
||
expect(response.status).to eq 500 | ||
expect(response.content_type).to include 'application/json' | ||
json_response = JSON.parse(response.body) | ||
expect(json_response['error']).to include 'Houve um erro interno no servidor ao processar sua solicitação.' | ||
end | ||
end | ||
end |