From b174a733a5ba58f1d9802feaa4793b4a6c2df523 Mon Sep 17 00:00:00 2001 From: HombreLaser Date: Fri, 10 Mar 2023 21:40:37 -0600 Subject: AƱade specs del controlador de companies MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- spec/factories/company.rb | 9 ++++++++ spec/fixtures/files/tres castillos-2.png | Bin 0 -> 168179 bytes .../create_companies_controller_spec.rb | 24 +++++++++++++++++++++ .../create_sessions_controller_spec.rb | 16 ++++++++++++++ .../delete_sessions_controller_spec.rb | 15 ------------- .../destroy_sessions_controller_spec.rb | 15 +++++++++++++ .../post_sessions_controller_spec.rb | 16 -------------- .../create_user_accounts_spec.rb | 21 ++++++++++++++++++ .../post_user_accounts_spec.rb | 20 ----------------- .../shared_examples/requests/post_request.rb | 6 +++--- 10 files changed, 88 insertions(+), 54 deletions(-) create mode 100644 spec/factories/company.rb create mode 100755 spec/fixtures/files/tres castillos-2.png create mode 100644 spec/requests/companies_controller/create_companies_controller_spec.rb create mode 100644 spec/requests/sessions_controller/create_sessions_controller_spec.rb delete mode 100644 spec/requests/sessions_controller/delete_sessions_controller_spec.rb create mode 100644 spec/requests/sessions_controller/destroy_sessions_controller_spec.rb delete mode 100644 spec/requests/sessions_controller/post_sessions_controller_spec.rb create mode 100644 spec/requests/user_accounts_controller/create_user_accounts_spec.rb delete mode 100644 spec/requests/user_accounts_controller/post_user_accounts_spec.rb diff --git a/spec/factories/company.rb b/spec/factories/company.rb new file mode 100644 index 0000000..e4ae7d6 --- /dev/null +++ b/spec/factories/company.rb @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +FactoryBot.define do + factory :company, class: 'Company' do + name { Faker::Company.name } + short_name { Faker::Lorem.word } + country { ISO3166::Country.codes.sample } + end +end diff --git a/spec/fixtures/files/tres castillos-2.png b/spec/fixtures/files/tres castillos-2.png new file mode 100755 index 0000000..95650b8 Binary files /dev/null and b/spec/fixtures/files/tres castillos-2.png differ diff --git a/spec/requests/companies_controller/create_companies_controller_spec.rb b/spec/requests/companies_controller/create_companies_controller_spec.rb new file mode 100644 index 0000000..53677f2 --- /dev/null +++ b/spec/requests/companies_controller/create_companies_controller_spec.rb @@ -0,0 +1,24 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe 'POST /api/companies', type: :request do + let(:user) { create(:user_account, role: 'master') } + let(:company) { build(:company) } + let(:logo) { fixture_file_upload('tres castillos-2.png', 'image/png') } + let(:token) { jwt(user) } + + it_behaves_like 'a POST request' do + let(:headers) { { 'CONTENT_TYPE' => 'application/json', 'Authorization' => "Bearer #{token['token']}" } } + let(:route) { '/api/companies' } + 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) { [company.name, company.short_name, company.country, 'logo', 'http'] } + let(:params) do + { name: company.name, short_name: company.short_name, country: company.country, logo: logo } + end + let(:wrong_params) { JSON.generate({ name: '', short_name: '', country: '' }) } + end +end diff --git a/spec/requests/sessions_controller/create_sessions_controller_spec.rb b/spec/requests/sessions_controller/create_sessions_controller_spec.rb new file mode 100644 index 0000000..69b7b42 --- /dev/null +++ b/spec/requests/sessions_controller/create_sessions_controller_spec.rb @@ -0,0 +1,16 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe 'POST /api/login', type: :request do + it_behaves_like 'a POST request' do + let(:headers) { { 'CONTENT_TYPE' => 'application/json' } } + let(:route) { '/api/login' } + let(:expected_error_messages) { ['Credenciales incorrectas'] } + let(:desired_error_status) { 401 } + let(:expected_text) { %w[token refresh] } + let(:user) { create(:user_account) } + let(:params) { JSON.generate({ email: user.email, password: user.password }) } + let(:wrong_params) { JSON.generate({ email: user.email, password: 'wrong_password' }) } + end +end diff --git a/spec/requests/sessions_controller/delete_sessions_controller_spec.rb b/spec/requests/sessions_controller/delete_sessions_controller_spec.rb deleted file mode 100644 index bc669fc..0000000 --- a/spec/requests/sessions_controller/delete_sessions_controller_spec.rb +++ /dev/null @@ -1,15 +0,0 @@ -# frozen_string_literal: true - -require 'rails_helper' - -RSpec.describe 'DELETE /api/logout', type: :request do - let(:user) { create(:user_account) } - let(:token) { jwt(user) } - - it "returns status 204 and deletes the user's session key" do - delete('/api/logout', headers: { 'Authorization' => "Bearer #{token['token']}" }) - expect(response).to have_http_status(204) - user.reload - expect(user.session_key).to be_nil - end -end diff --git a/spec/requests/sessions_controller/destroy_sessions_controller_spec.rb b/spec/requests/sessions_controller/destroy_sessions_controller_spec.rb new file mode 100644 index 0000000..bc669fc --- /dev/null +++ b/spec/requests/sessions_controller/destroy_sessions_controller_spec.rb @@ -0,0 +1,15 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe 'DELETE /api/logout', type: :request do + let(:user) { create(:user_account) } + let(:token) { jwt(user) } + + it "returns status 204 and deletes the user's session key" do + delete('/api/logout', headers: { 'Authorization' => "Bearer #{token['token']}" }) + expect(response).to have_http_status(204) + user.reload + expect(user.session_key).to be_nil + end +end diff --git a/spec/requests/sessions_controller/post_sessions_controller_spec.rb b/spec/requests/sessions_controller/post_sessions_controller_spec.rb deleted file mode 100644 index c67b14d..0000000 --- a/spec/requests/sessions_controller/post_sessions_controller_spec.rb +++ /dev/null @@ -1,16 +0,0 @@ -# frozen_string_literal: true - -require 'rails_helper' - -RSpec.describe 'POST /api/login', type: :request do - it_behaves_like 'a POST request' do - let(:headers) { { 'CONTENT_TYPE' => 'application/json' } } - let(:route) { '/api/login' } - let(:expected_error_messages) { ['Credenciales incorrectas'] } - let(:desired_error_status) { 401 } - let(:expected_text) { %w[token refresh] } - let(:user) { create(:user_account) } - let(:params) { { email: user.email, password: user.password } } - let(:wrong_params) { { email: user.email, password: 'wrong_password' } } - end -end diff --git a/spec/requests/user_accounts_controller/create_user_accounts_spec.rb b/spec/requests/user_accounts_controller/create_user_accounts_spec.rb new file mode 100644 index 0000000..3fd5990 --- /dev/null +++ b/spec/requests/user_accounts_controller/create_user_accounts_spec.rb @@ -0,0 +1,21 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe 'POST /api/user_accounts', type: :request do + it_behaves_like 'a POST request' do + let(:headers) { { 'CONTENT_TYPE' => 'application/json' } } + let(:route) { '/api/user_accounts' } + let(:expected_error_messages) do + ["Password can't be blank", 'Email is invalid', "First name can't be blank", "Last name can't be blank" ] + end + let(:desired_error_status) { 422 } + let(:expected_text) { %w[token refresh] } + let(:user) { create(:user_account) } + let(:params) do + JSON.generate({ email: user.email, password: user.password, + first_name: user.first_name, last_name: user.last_name }) + end + let(:wrong_params) { JSON.generate({ email: 'notanemail', password: '' }) } + end +end diff --git a/spec/requests/user_accounts_controller/post_user_accounts_spec.rb b/spec/requests/user_accounts_controller/post_user_accounts_spec.rb deleted file mode 100644 index ba77ae5..0000000 --- a/spec/requests/user_accounts_controller/post_user_accounts_spec.rb +++ /dev/null @@ -1,20 +0,0 @@ -# frozen_string_literal: true - -require 'rails_helper' - -RSpec.describe 'POST /api/user_accounts', type: :request do - it_behaves_like 'a POST request' do - let(:headers) { { 'CONTENT_TYPE' => 'application/json' } } - let(:route) { '/api/user_accounts' } - let(:expected_error_messages) do - ["Password can't be blank", 'Email is invalid', "First name can't be blank", "Last name can't be blank" ] - end - let(:desired_error_status) { 422 } - let(:expected_text) { %w[token refresh] } - let(:user) { create(:user_account) } - let(:params) do - { email: user.email, password: user.password, first_name: user.first_name, last_name: user.last_name} - end - let(:wrong_params) { { email: 'notanemail', password: '' } } - end -end diff --git a/spec/support/shared_examples/requests/post_request.rb b/spec/support/shared_examples/requests/post_request.rb index b7226d0..c7b8d77 100644 --- a/spec/support/shared_examples/requests/post_request.rb +++ b/spec/support/shared_examples/requests/post_request.rb @@ -4,8 +4,8 @@ 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 to the posted data' do - post(route, params: params.to_json, headers:) + it 'returns a successful http status and a response with the posted data' do + post(route, params: params, headers:) expect(response).to have_http_status(200) expect(response.body).to include_strings(expected_text) end @@ -13,7 +13,7 @@ RSpec.shared_examples 'a POST request' do context 'with incorrect parameters' do it 'returns a client side error http status and a error message' do - post(route, params: wrong_params.to_json, headers:) + post(route, params: wrong_params, headers:) expect(response).to have_http_status(desired_error_status) expect(response.body).to include_strings(expected_error_messages) end -- cgit v1.2.3