From 6b3cc5c3a33643c0e42396142a2d3204ada2bb82 Mon Sep 17 00:00:00 2001 From: HombreLaser Date: Sat, 11 Mar 2023 11:42:32 -0600 Subject: AƱade spec de update company MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../update_companies_controller_spec.rb | 27 ++++++++++++++++++++++ .../shared_examples/requests/post_request.rb | 4 +--- .../shared_examples/requests/put_request.rb | 26 +++++++++++++++++++++ 3 files changed, 54 insertions(+), 3 deletions(-) create mode 100644 spec/requests/companies_controller/update_companies_controller_spec.rb create mode 100644 spec/support/shared_examples/requests/put_request.rb diff --git a/spec/requests/companies_controller/update_companies_controller_spec.rb b/spec/requests/companies_controller/update_companies_controller_spec.rb new file mode 100644 index 0000000..76c60e4 --- /dev/null +++ b/spec/requests/companies_controller/update_companies_controller_spec.rb @@ -0,0 +1,27 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe 'PUT /api/companies/company_id', type: :request do + let(:user) { create(:user_account, role: 'master') } + let(:company) { create(:company) } + let(:new_company) { build(:company) } + let(:logo) { fixture_file_upload('tres castillos-2.png', 'image/png') } + let(:token) { jwt(user) } + + it_behaves_like 'a PUT request' do + let(:headers) { { 'CONTENT_TYPE' => 'application/json', 'Authorization' => "Bearer #{token['token']}" } } + let(:route) { "/api/companies/#{company.short_name}" } + let(:wrong_route) { "/api/companies/#{SecureRandom.hex(8)}" } + let(:expected_error_messages) do + ["Name can't be blank", "Short name can't be blank", "Country can't be blank"] + end + let(:desired_error_status) { 422 } + let(:expected_text) { [new_company.name, new_company.short_name, new_company.country, 'logo', 'http'] } + let(:params) do + { name: new_company.name, short_name: new_company.short_name, country: new_company.country, + logo: fixture_file_upload('tres castillos-2.png', 'image/png') } + end + let(:wrong_params) { JSON.generate({ name: '', short_name: '', country: '' }) } + end +end diff --git a/spec/support/shared_examples/requests/post_request.rb b/spec/support/shared_examples/requests/post_request.rb index c7b8d77..55d35a4 100644 --- a/spec/support/shared_examples/requests/post_request.rb +++ b/spec/support/shared_examples/requests/post_request.rb @@ -1,11 +1,9 @@ # frozen_string_literal: true RSpec.shared_examples 'a POST request' do - let(:headers) { { 'CONTENT_TYPE' => 'application/json' } } - context 'with correct parameters' do it 'returns a successful http status and a response with the posted data' do - post(route, params: params, headers:) + post(route, params:, headers:) expect(response).to have_http_status(200) expect(response.body).to include_strings(expected_text) end diff --git a/spec/support/shared_examples/requests/put_request.rb b/spec/support/shared_examples/requests/put_request.rb new file mode 100644 index 0000000..9e46235 --- /dev/null +++ b/spec/support/shared_examples/requests/put_request.rb @@ -0,0 +1,26 @@ +# frozen_string_literal: true + +RSpec.shared_examples 'a PUT request' do + context 'with correct parameters' do + it 'returns a successful http status and a response with the putted data' do + put(route, params:, headers:) + expect(response).to have_http_status(200) + expect(response.body).to include_strings(expected_text) + end + end + + context 'with incorrect parameters' do + it 'returns a client side error http status and an error message' do + put(route, params: wrong_params, headers:) + expect(response).to have_http_status(desired_error_status) + expect(response.body).to include_strings(expected_error_messages) + end + end + + context 'to an nonexistent resource' do + it 'returns a 404 status' do + put(wrong_route, params:, headers:) + expect(response).to have_http_status(404) + end + end +end -- cgit v1.2.3