From 2ae0cd8a8dc6d630ee1a7a84ddf6111609dbdeb7 Mon Sep 17 00:00:00 2001 From: HombreLaser Date: Thu, 30 Mar 2023 17:29:40 -0600 Subject: Mejora la estructura de los specs --- .../create_addresses_controller_spec.rb | 47 ------------------- spec/requests/addresses_controller/create_spec.rb | 47 +++++++++++++++++++ .../destroy_addresses_controller_spec.rb | 35 -------------- spec/requests/addresses_controller/destroy_spec.rb | 35 ++++++++++++++ .../index_addresses_controller_spec.rb | 19 -------- spec/requests/addresses_controller/index_spec.rb | 19 ++++++++ .../update_addresses_controller_spec.rb | 54 ---------------------- spec/requests/addresses_controller/update_spec.rb | 54 ++++++++++++++++++++++ spec/requests/cards_controller/index_spec.rb | 0 .../create_companies_controller_spec.rb | 34 -------------- spec/requests/companies_controller/create_spec.rb | 34 ++++++++++++++ .../index_companies_controller_spec.rb | 16 ------- spec/requests/companies_controller/index_spec.rb | 16 +++++++ .../show_companies_controller_spec.rb | 13 ------ spec/requests/companies_controller/show_spec.rb | 13 ++++++ .../update_companies_controller_spec.rb | 26 ----------- spec/requests/companies_controller/update_spec.rb | 26 +++++++++++ .../create_products_controller_spec.rb | 48 ------------------- spec/requests/products_controller/create_spec.rb | 48 +++++++++++++++++++ .../destroy_products_controller_spec.rb | 15 ------ spec/requests/products_controller/destroy_spec.rb | 15 ++++++ .../index_products_controller_spec.rb | 15 ------ spec/requests/products_controller/index_spec.rb | 15 ++++++ .../show_products_controller_spec.rb | 17 ------- spec/requests/products_controller/show_spec.rb | 17 +++++++ .../update_products_controller_spec.rb | 40 ---------------- spec/requests/products_controller/update_spec.rb | 40 ++++++++++++++++ .../post_refresh_tokens_controller_spec.rb | 26 ----------- .../refresh_tokens_controller/post_spec.rb | 26 +++++++++++ .../create_sessions_controller_spec.rb | 16 ------- spec/requests/sessions_controller/create_spec.rb | 16 +++++++ .../destroy_sessions_controller_spec.rb | 15 ------ spec/requests/sessions_controller/destroy_spec.rb | 15 ++++++ .../user_accounts_controller/create_spec.rb | 21 +++++++++ .../create_user_accounts_spec.rb | 21 --------- .../requests/user_accounts_controller/show_spec.rb | 12 +++++ .../show_user_accounts_spec.rb | 12 ----- .../user_accounts_controller/update_spec.rb | 23 +++++++++ .../update_user_accounts_spec.rb | 23 --------- 39 files changed, 492 insertions(+), 492 deletions(-) delete mode 100644 spec/requests/addresses_controller/create_addresses_controller_spec.rb create mode 100644 spec/requests/addresses_controller/create_spec.rb delete mode 100644 spec/requests/addresses_controller/destroy_addresses_controller_spec.rb create mode 100644 spec/requests/addresses_controller/destroy_spec.rb delete mode 100644 spec/requests/addresses_controller/index_addresses_controller_spec.rb create mode 100644 spec/requests/addresses_controller/index_spec.rb delete mode 100644 spec/requests/addresses_controller/update_addresses_controller_spec.rb create mode 100644 spec/requests/addresses_controller/update_spec.rb create mode 100644 spec/requests/cards_controller/index_spec.rb delete mode 100644 spec/requests/companies_controller/create_companies_controller_spec.rb create mode 100644 spec/requests/companies_controller/create_spec.rb delete mode 100644 spec/requests/companies_controller/index_companies_controller_spec.rb create mode 100644 spec/requests/companies_controller/index_spec.rb delete mode 100644 spec/requests/companies_controller/show_companies_controller_spec.rb create mode 100644 spec/requests/companies_controller/show_spec.rb delete mode 100644 spec/requests/companies_controller/update_companies_controller_spec.rb create mode 100644 spec/requests/companies_controller/update_spec.rb delete mode 100644 spec/requests/products_controller/create_products_controller_spec.rb create mode 100644 spec/requests/products_controller/create_spec.rb delete mode 100644 spec/requests/products_controller/destroy_products_controller_spec.rb create mode 100644 spec/requests/products_controller/destroy_spec.rb delete mode 100644 spec/requests/products_controller/index_products_controller_spec.rb create mode 100644 spec/requests/products_controller/index_spec.rb delete mode 100644 spec/requests/products_controller/show_products_controller_spec.rb create mode 100644 spec/requests/products_controller/show_spec.rb delete mode 100644 spec/requests/products_controller/update_products_controller_spec.rb create mode 100644 spec/requests/products_controller/update_spec.rb delete mode 100644 spec/requests/refresh_tokens_controller/post_refresh_tokens_controller_spec.rb create mode 100644 spec/requests/refresh_tokens_controller/post_spec.rb delete mode 100644 spec/requests/sessions_controller/create_sessions_controller_spec.rb create mode 100644 spec/requests/sessions_controller/create_spec.rb delete mode 100644 spec/requests/sessions_controller/destroy_sessions_controller_spec.rb create mode 100644 spec/requests/sessions_controller/destroy_spec.rb create mode 100644 spec/requests/user_accounts_controller/create_spec.rb delete mode 100644 spec/requests/user_accounts_controller/create_user_accounts_spec.rb create mode 100644 spec/requests/user_accounts_controller/show_spec.rb delete mode 100644 spec/requests/user_accounts_controller/show_user_accounts_spec.rb create mode 100644 spec/requests/user_accounts_controller/update_spec.rb delete mode 100644 spec/requests/user_accounts_controller/update_user_accounts_spec.rb (limited to 'spec') diff --git a/spec/requests/addresses_controller/create_addresses_controller_spec.rb b/spec/requests/addresses_controller/create_addresses_controller_spec.rb deleted file mode 100644 index befb80b..0000000 --- a/spec/requests/addresses_controller/create_addresses_controller_spec.rb +++ /dev/null @@ -1,47 +0,0 @@ -# frozen_string_literal: true - -require 'rails_helper' - -RSpec.describe 'POST /account/addresses', type: :request do - let(:address) { build(:address) } - let(:user) { create(:user_account, role: 'regular') } - let(:headers) { { 'Authorization' => "Bearer #{token['token']}" } } - let(:token) { jwt(user) } - let(:params) do - { number: address.number, street: address.street, country: address.country, zip_code: address.zip_code, - city: address.city } - end - - it_behaves_like 'a POST request' do - let(:route) { '/api/account/addresses' } - let(:expected_error_messages) do - ["Number can't be blank", "Street can't be blank", "Zip code can't be blank", - 'Country is invalid', "City can't be blank"] - end - let(:desired_error_status) { 422 } - let(:expected_text) { [address.number.to_s, address.zip_code.to_s, address.country, address.street, address.city] } - let(:wrong_params) { { number: '', street: '', zip_code: '', city: '', country: 'Not a country' } } - end - - context 'when a similar address already exists in the database' do - it 'builds a relationship between the user and the address, without creating another address' do - address.save - another_user = create(:user_account) - relate_user_and_address(another_user, address) - post('/api/account/addresses', params:, headers:) - expect(Address.count).to eq(1) - expect(another_user.addresses.first.id).to eq(user.addresses.first.id) - expect(UserAccountAddress.count).to eq(2) - end - end - - context 'when the user alread has the same address' do - it 'returns a 422 http status and a error message' do - address.save - relate_user_and_address(user, address) - post('/api/account/addresses', params:, headers:) - expect(response).to have_http_status(422) - expect(response.body).to include('Ya cuenta con esta dirección') - end - end -end diff --git a/spec/requests/addresses_controller/create_spec.rb b/spec/requests/addresses_controller/create_spec.rb new file mode 100644 index 0000000..befb80b --- /dev/null +++ b/spec/requests/addresses_controller/create_spec.rb @@ -0,0 +1,47 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe 'POST /account/addresses', type: :request do + let(:address) { build(:address) } + let(:user) { create(:user_account, role: 'regular') } + let(:headers) { { 'Authorization' => "Bearer #{token['token']}" } } + let(:token) { jwt(user) } + let(:params) do + { number: address.number, street: address.street, country: address.country, zip_code: address.zip_code, + city: address.city } + end + + it_behaves_like 'a POST request' do + let(:route) { '/api/account/addresses' } + let(:expected_error_messages) do + ["Number can't be blank", "Street can't be blank", "Zip code can't be blank", + 'Country is invalid', "City can't be blank"] + end + let(:desired_error_status) { 422 } + let(:expected_text) { [address.number.to_s, address.zip_code.to_s, address.country, address.street, address.city] } + let(:wrong_params) { { number: '', street: '', zip_code: '', city: '', country: 'Not a country' } } + end + + context 'when a similar address already exists in the database' do + it 'builds a relationship between the user and the address, without creating another address' do + address.save + another_user = create(:user_account) + relate_user_and_address(another_user, address) + post('/api/account/addresses', params:, headers:) + expect(Address.count).to eq(1) + expect(another_user.addresses.first.id).to eq(user.addresses.first.id) + expect(UserAccountAddress.count).to eq(2) + end + end + + context 'when the user alread has the same address' do + it 'returns a 422 http status and a error message' do + address.save + relate_user_and_address(user, address) + post('/api/account/addresses', params:, headers:) + expect(response).to have_http_status(422) + expect(response.body).to include('Ya cuenta con esta dirección') + end + end +end diff --git a/spec/requests/addresses_controller/destroy_addresses_controller_spec.rb b/spec/requests/addresses_controller/destroy_addresses_controller_spec.rb deleted file mode 100644 index df43329..0000000 --- a/spec/requests/addresses_controller/destroy_addresses_controller_spec.rb +++ /dev/null @@ -1,35 +0,0 @@ -# frozen_string_literal: true - -require 'rails_helper' - -RSpec.describe 'DELETE /api/products/product_id', type: :request do - let(:user) { create(:user_account, role: 'regular') } - let(:token) { jwt(user) } - let(:headers) { { 'Authorization' => "Bearer #{token['token']}" } } - let(:address) { create(:address) } - - before(:each) do - relate_user_and_address(user, address) - end - - it_behaves_like 'a DELETE request' do - let(:resource) { "/api/account/addresses/#{address.id}" } - let(:nonexistent_resource) { "/api/account/addresses/#{SecureRandom.hex(8)}" } - end - - context 'when the address belongs to other users' do - it "doesn't get deleted" do - another_user = create(:user_account, role: 'regular') - relate_user_and_address(another_user, address) - delete("/api/account/addresses/#{address.id}", headers:) - expect(Address.find_by(id: address.id)).to_not be_nil - end - end - - context "when the address doesn't belong to other users" do - it 'gets deleted' do - delete("/api/account/addresses/#{address.id}", headers:) - expect(Address.find_by(id: address.id)).to be_nil - end - end -end diff --git a/spec/requests/addresses_controller/destroy_spec.rb b/spec/requests/addresses_controller/destroy_spec.rb new file mode 100644 index 0000000..df43329 --- /dev/null +++ b/spec/requests/addresses_controller/destroy_spec.rb @@ -0,0 +1,35 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe 'DELETE /api/products/product_id', type: :request do + let(:user) { create(:user_account, role: 'regular') } + let(:token) { jwt(user) } + let(:headers) { { 'Authorization' => "Bearer #{token['token']}" } } + let(:address) { create(:address) } + + before(:each) do + relate_user_and_address(user, address) + end + + it_behaves_like 'a DELETE request' do + let(:resource) { "/api/account/addresses/#{address.id}" } + let(:nonexistent_resource) { "/api/account/addresses/#{SecureRandom.hex(8)}" } + end + + context 'when the address belongs to other users' do + it "doesn't get deleted" do + another_user = create(:user_account, role: 'regular') + relate_user_and_address(another_user, address) + delete("/api/account/addresses/#{address.id}", headers:) + expect(Address.find_by(id: address.id)).to_not be_nil + end + end + + context "when the address doesn't belong to other users" do + it 'gets deleted' do + delete("/api/account/addresses/#{address.id}", headers:) + expect(Address.find_by(id: address.id)).to be_nil + end + end +end diff --git a/spec/requests/addresses_controller/index_addresses_controller_spec.rb b/spec/requests/addresses_controller/index_addresses_controller_spec.rb deleted file mode 100644 index 63fc776..0000000 --- a/spec/requests/addresses_controller/index_addresses_controller_spec.rb +++ /dev/null @@ -1,19 +0,0 @@ -# frozen_string_literal: true - -require 'rails_helper' - -RSpec.describe 'GET /api/account/addresses', type: :request do - let(:user) { create(:user_account, role: 'regular') } - let(:token) { jwt(user) } - - before(:each) do - create_list(:address, 5) - relate_user_and_addresses(user, Address.all) - end - - it_behaves_like 'a GET index request' do - let(:headers) { { 'Authorization' => "Bearer #{token['token']}" } } - let(:route) { '/api/account/addresses' } - let(:pagination_size) { 5 } - end -end \ No newline at end of file diff --git a/spec/requests/addresses_controller/index_spec.rb b/spec/requests/addresses_controller/index_spec.rb new file mode 100644 index 0000000..63fc776 --- /dev/null +++ b/spec/requests/addresses_controller/index_spec.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe 'GET /api/account/addresses', type: :request do + let(:user) { create(:user_account, role: 'regular') } + let(:token) { jwt(user) } + + before(:each) do + create_list(:address, 5) + relate_user_and_addresses(user, Address.all) + end + + it_behaves_like 'a GET index request' do + let(:headers) { { 'Authorization' => "Bearer #{token['token']}" } } + let(:route) { '/api/account/addresses' } + let(:pagination_size) { 5 } + end +end \ No newline at end of file diff --git a/spec/requests/addresses_controller/update_addresses_controller_spec.rb b/spec/requests/addresses_controller/update_addresses_controller_spec.rb deleted file mode 100644 index 0c3662e..0000000 --- a/spec/requests/addresses_controller/update_addresses_controller_spec.rb +++ /dev/null @@ -1,54 +0,0 @@ -# frozen_string_literal: true - -require 'rails_helper' - -RSpec.describe 'PUT /api/account/addresses', type: :request do - let(:user) { create(:user_account, role: 'regular') } - let(:address) { create(:address) } - let(:new_address) { build(:address) } - let(:token) { jwt(user) } - let(:headers) { { 'Authorization' => "Bearer #{token['token']}" } } - let(:params) do - { number: new_address.number, street: new_address.street, country: new_address.country, zip_code: new_address.zip_code, - city: new_address.city } - end - - before(:each) do - relate_user_and_address(user, address) - end - - it_behaves_like 'a PUT request' do - let(:route) { "/api/account/addresses/#{address.id}" } - let(:wrong_route) { "/api/account/addresses/#{SecureRandom.hex(8)}" } - let(:expected_error_messages) do - ["Number can't be blank", "Street can't be blank", "Zip code can't be blank", - 'Country is invalid', "City can't be blank"] - end - let(:expected_text) do - [new_address.number.to_s, new_address.zip_code.to_s, new_address.country, new_address.street, - new_address.city] - end - let(:wrong_params) { { number: '', street: '', zip_code: '', city: '', country: 'Not a country' } } - end - - context 'when the update params coincide with an already existing address for the user' do - it 'returns a 422 http status and an error message' do - new_address.save - relate_user_and_address(user, new_address) - put("/api/account/addresses/#{address.id}", params:, headers:) - expect(response).to have_http_status(422) - expect(response.body).to include('Ya cuenta con esta dirección') - end - end - - context 'when the update params coincide with an already existing address' do - it 'builds a relationship between the user and the address, without creating another address' do - new_address.save - another_user = create(:user_account, role: 'regular') - relate_user_and_address(another_user, new_address) - put("/api/account/addresses/#{address.id}", params:, headers:) - expect(Address.count).to eq(1) - expect(another_user.addresses.first).to eq(user.addresses.find(new_address.id)) - end - end -end diff --git a/spec/requests/addresses_controller/update_spec.rb b/spec/requests/addresses_controller/update_spec.rb new file mode 100644 index 0000000..0c3662e --- /dev/null +++ b/spec/requests/addresses_controller/update_spec.rb @@ -0,0 +1,54 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe 'PUT /api/account/addresses', type: :request do + let(:user) { create(:user_account, role: 'regular') } + let(:address) { create(:address) } + let(:new_address) { build(:address) } + let(:token) { jwt(user) } + let(:headers) { { 'Authorization' => "Bearer #{token['token']}" } } + let(:params) do + { number: new_address.number, street: new_address.street, country: new_address.country, zip_code: new_address.zip_code, + city: new_address.city } + end + + before(:each) do + relate_user_and_address(user, address) + end + + it_behaves_like 'a PUT request' do + let(:route) { "/api/account/addresses/#{address.id}" } + let(:wrong_route) { "/api/account/addresses/#{SecureRandom.hex(8)}" } + let(:expected_error_messages) do + ["Number can't be blank", "Street can't be blank", "Zip code can't be blank", + 'Country is invalid', "City can't be blank"] + end + let(:expected_text) do + [new_address.number.to_s, new_address.zip_code.to_s, new_address.country, new_address.street, + new_address.city] + end + let(:wrong_params) { { number: '', street: '', zip_code: '', city: '', country: 'Not a country' } } + end + + context 'when the update params coincide with an already existing address for the user' do + it 'returns a 422 http status and an error message' do + new_address.save + relate_user_and_address(user, new_address) + put("/api/account/addresses/#{address.id}", params:, headers:) + expect(response).to have_http_status(422) + expect(response.body).to include('Ya cuenta con esta dirección') + end + end + + context 'when the update params coincide with an already existing address' do + it 'builds a relationship between the user and the address, without creating another address' do + new_address.save + another_user = create(:user_account, role: 'regular') + relate_user_and_address(another_user, new_address) + put("/api/account/addresses/#{address.id}", params:, headers:) + expect(Address.count).to eq(1) + expect(another_user.addresses.first).to eq(user.addresses.find(new_address.id)) + end + end +end diff --git a/spec/requests/cards_controller/index_spec.rb b/spec/requests/cards_controller/index_spec.rb new file mode 100644 index 0000000..e69de29 diff --git a/spec/requests/companies_controller/create_companies_controller_spec.rb b/spec/requests/companies_controller/create_companies_controller_spec.rb deleted file mode 100644 index 219868b..0000000 --- a/spec/requests/companies_controller/create_companies_controller_spec.rb +++ /dev/null @@ -1,34 +0,0 @@ -# frozen_string_literal: true - -require 'rails_helper' - -RSpec.describe 'POST /api/companies', type: :request do - let(:company) { build(:company) } - let(:logo) { fixture_file_upload('tres castillos-2.png', 'image/png') } - - it_behaves_like 'a POST request' do - let(:user) { create(:user_account, role: 'master') } - let(:token) { jwt(user) } - 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: } - end - let(:wrong_params) { JSON.generate({ name: '', short_name: '', country: '' }) } - end - - it_behaves_like 'a POST request that requires a master user' do - let(:user) { create(:user_account, role: 'regular') } - let(:token) { jwt(user) } - let(:headers) { { 'CONTENT_TYPE' => 'application/json', 'Authorization' => "Bearer #{token['token']}" } } - let(:route) { '/api/companies' } - let(:params) do - JSON.generate({ name: company.name, short_name: company.short_name, country: company.country }) - end - end -end diff --git a/spec/requests/companies_controller/create_spec.rb b/spec/requests/companies_controller/create_spec.rb new file mode 100644 index 0000000..219868b --- /dev/null +++ b/spec/requests/companies_controller/create_spec.rb @@ -0,0 +1,34 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe 'POST /api/companies', type: :request do + let(:company) { build(:company) } + let(:logo) { fixture_file_upload('tres castillos-2.png', 'image/png') } + + it_behaves_like 'a POST request' do + let(:user) { create(:user_account, role: 'master') } + let(:token) { jwt(user) } + 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: } + end + let(:wrong_params) { JSON.generate({ name: '', short_name: '', country: '' }) } + end + + it_behaves_like 'a POST request that requires a master user' do + let(:user) { create(:user_account, role: 'regular') } + let(:token) { jwt(user) } + let(:headers) { { 'CONTENT_TYPE' => 'application/json', 'Authorization' => "Bearer #{token['token']}" } } + let(:route) { '/api/companies' } + let(:params) do + JSON.generate({ name: company.name, short_name: company.short_name, country: company.country }) + end + end +end diff --git a/spec/requests/companies_controller/index_companies_controller_spec.rb b/spec/requests/companies_controller/index_companies_controller_spec.rb deleted file mode 100644 index c8fb439..0000000 --- a/spec/requests/companies_controller/index_companies_controller_spec.rb +++ /dev/null @@ -1,16 +0,0 @@ -# frozen_string_literal: true - -require 'rails_helper' - -RSpec.describe 'GET /api/companies', type: :request do - before(:each) do - create_list(:company, 12) - end - - it_behaves_like 'a GET index request' do - let(:headers) { {} } - let(:companies) { create_list(:company, 12) } - let(:route) { '/api/companies' } - let(:pagination_size) { 10 } - end -end diff --git a/spec/requests/companies_controller/index_spec.rb b/spec/requests/companies_controller/index_spec.rb new file mode 100644 index 0000000..c8fb439 --- /dev/null +++ b/spec/requests/companies_controller/index_spec.rb @@ -0,0 +1,16 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe 'GET /api/companies', type: :request do + before(:each) do + create_list(:company, 12) + end + + it_behaves_like 'a GET index request' do + let(:headers) { {} } + let(:companies) { create_list(:company, 12) } + let(:route) { '/api/companies' } + let(:pagination_size) { 10 } + end +end diff --git a/spec/requests/companies_controller/show_companies_controller_spec.rb b/spec/requests/companies_controller/show_companies_controller_spec.rb deleted file mode 100644 index fc5bbd0..0000000 --- a/spec/requests/companies_controller/show_companies_controller_spec.rb +++ /dev/null @@ -1,13 +0,0 @@ -# frozen_string_literal: true - -require 'rails_helper' - -RSpec.describe 'GET /api/companies/:short_name', type: :request do - it_behaves_like 'a GET resource request' do - let(:headers) { {} } - let(:company) { create(:company) } - let(:route) { "/api/companies/#{company.short_name}" } - let(:invalid_route) { "/api/companies/#{Faker::Lorem.word}" } - let(:expected_text) { [company.name, company.id.to_s, company.short_name, company.country] } - end -end diff --git a/spec/requests/companies_controller/show_spec.rb b/spec/requests/companies_controller/show_spec.rb new file mode 100644 index 0000000..fc5bbd0 --- /dev/null +++ b/spec/requests/companies_controller/show_spec.rb @@ -0,0 +1,13 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe 'GET /api/companies/:short_name', type: :request do + it_behaves_like 'a GET resource request' do + let(:headers) { {} } + let(:company) { create(:company) } + let(:route) { "/api/companies/#{company.short_name}" } + let(:invalid_route) { "/api/companies/#{Faker::Lorem.word}" } + let(:expected_text) { [company.name, company.id.to_s, company.short_name, company.country] } + end +end diff --git a/spec/requests/companies_controller/update_companies_controller_spec.rb b/spec/requests/companies_controller/update_companies_controller_spec.rb deleted file mode 100644 index fd23365..0000000 --- a/spec/requests/companies_controller/update_companies_controller_spec.rb +++ /dev/null @@ -1,26 +0,0 @@ -# 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) { { '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(: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) { { name: '', short_name: '', country: '' } } - end -end diff --git a/spec/requests/companies_controller/update_spec.rb b/spec/requests/companies_controller/update_spec.rb new file mode 100644 index 0000000..fd23365 --- /dev/null +++ b/spec/requests/companies_controller/update_spec.rb @@ -0,0 +1,26 @@ +# 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) { { '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(: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) { { name: '', short_name: '', country: '' } } + end +end diff --git a/spec/requests/products_controller/create_products_controller_spec.rb b/spec/requests/products_controller/create_products_controller_spec.rb deleted file mode 100644 index 400fd18..0000000 --- a/spec/requests/products_controller/create_products_controller_spec.rb +++ /dev/null @@ -1,48 +0,0 @@ -# frozen_string_literal: true - -require 'rails_helper' - -RSpec.describe 'POST /api/products', type: :request do - let(:product) { build(:product) } - let(:picture) { fixture_file_upload('800px-Stray_cat_on_wall.jpg', 'image/jpeg') } - let(:categories) do - c = '' - product.categories.each { |category| c += "#{category}," } - c - end - - it_behaves_like 'a POST request' do - let(:user) { create(:user_account, role: 'master') } - let(:token) { jwt(user) } - let(:headers) { { 'CONTENT_TYPE' => 'application/json', 'Authorization' => "Bearer #{token['token']}" } } - let(:route) { '/api/products' } - let(:expected_error_messages) do - ["Name can't be blank", 'Unitary price must be greater than 0', 'Bulk price must be greater than 0', - 'Available quantity must be greater than 0', 'Company must exist'] - end - let(:desired_error_status) { 422 } - let(:expected_text) do - product.categories.concat([product.name, product.unitary_price.to_i.to_s, product.bulk_price.to_i.to_s, - product.available_quantity.to_s, product.company.short_name, 'picture', 'http']) - end - let(:params) do - { name: product.name, unitary_price: product.unitary_price, bulk_price: product.bulk_price, picture:, - available_quantity: product.available_quantity, categories:, company_id: product.company_id } - end - let(:wrong_params) do - JSON.generate({ name: '', unitary_price: -10, bulk_price: -10, available_quantity: -10, categories: '', - company_id: nil }) - end - end - - it_behaves_like 'a POST request that requires a master user' do - let(:user) { create(:user_account, role: 'regular') } - let(:token) { jwt(user) } - let(:headers) { { 'CONTENT_TYPE' => 'application/json', 'Authorization' => "Bearer #{token['token']}" } } - let(:route) { '/api/products' } - let(:params) do - { name: product.name, unitary_price: product.unitary_price, bulk_price: product.bulk_price, picture:, - available_quantity: product.available_quantity, categories:, company_id: product.company_id } - end - end -end \ No newline at end of file diff --git a/spec/requests/products_controller/create_spec.rb b/spec/requests/products_controller/create_spec.rb new file mode 100644 index 0000000..400fd18 --- /dev/null +++ b/spec/requests/products_controller/create_spec.rb @@ -0,0 +1,48 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe 'POST /api/products', type: :request do + let(:product) { build(:product) } + let(:picture) { fixture_file_upload('800px-Stray_cat_on_wall.jpg', 'image/jpeg') } + let(:categories) do + c = '' + product.categories.each { |category| c += "#{category}," } + c + end + + it_behaves_like 'a POST request' do + let(:user) { create(:user_account, role: 'master') } + let(:token) { jwt(user) } + let(:headers) { { 'CONTENT_TYPE' => 'application/json', 'Authorization' => "Bearer #{token['token']}" } } + let(:route) { '/api/products' } + let(:expected_error_messages) do + ["Name can't be blank", 'Unitary price must be greater than 0', 'Bulk price must be greater than 0', + 'Available quantity must be greater than 0', 'Company must exist'] + end + let(:desired_error_status) { 422 } + let(:expected_text) do + product.categories.concat([product.name, product.unitary_price.to_i.to_s, product.bulk_price.to_i.to_s, + product.available_quantity.to_s, product.company.short_name, 'picture', 'http']) + end + let(:params) do + { name: product.name, unitary_price: product.unitary_price, bulk_price: product.bulk_price, picture:, + available_quantity: product.available_quantity, categories:, company_id: product.company_id } + end + let(:wrong_params) do + JSON.generate({ name: '', unitary_price: -10, bulk_price: -10, available_quantity: -10, categories: '', + company_id: nil }) + end + end + + it_behaves_like 'a POST request that requires a master user' do + let(:user) { create(:user_account, role: 'regular') } + let(:token) { jwt(user) } + let(:headers) { { 'CONTENT_TYPE' => 'application/json', 'Authorization' => "Bearer #{token['token']}" } } + let(:route) { '/api/products' } + let(:params) do + { name: product.name, unitary_price: product.unitary_price, bulk_price: product.bulk_price, picture:, + available_quantity: product.available_quantity, categories:, company_id: product.company_id } + end + end +end \ No newline at end of file diff --git a/spec/requests/products_controller/destroy_products_controller_spec.rb b/spec/requests/products_controller/destroy_products_controller_spec.rb deleted file mode 100644 index 54a57d2..0000000 --- a/spec/requests/products_controller/destroy_products_controller_spec.rb +++ /dev/null @@ -1,15 +0,0 @@ -# frozen_string_literal: true - -require 'rails_helper' - -RSpec.describe 'DELETE /api/products/product_id', type: :request do - let(:user) { create(:user_account, role: 'master') } - let(:token) { jwt(user) } - let(:headers) { { 'Authorization' => "Bearer #{token['token']}" } } - - it_behaves_like 'a DELETE request' do - let(:product) { create(:product, public_id: SecureRandom.hex(12)) } - let(:resource) { "/api/products/#{product.public_id}" } - let(:nonexistent_resource) { "/api/products/#{SecureRandom.hex(8)}" } - end -end diff --git a/spec/requests/products_controller/destroy_spec.rb b/spec/requests/products_controller/destroy_spec.rb new file mode 100644 index 0000000..54a57d2 --- /dev/null +++ b/spec/requests/products_controller/destroy_spec.rb @@ -0,0 +1,15 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe 'DELETE /api/products/product_id', type: :request do + let(:user) { create(:user_account, role: 'master') } + let(:token) { jwt(user) } + let(:headers) { { 'Authorization' => "Bearer #{token['token']}" } } + + it_behaves_like 'a DELETE request' do + let(:product) { create(:product, public_id: SecureRandom.hex(12)) } + let(:resource) { "/api/products/#{product.public_id}" } + let(:nonexistent_resource) { "/api/products/#{SecureRandom.hex(8)}" } + end +end diff --git a/spec/requests/products_controller/index_products_controller_spec.rb b/spec/requests/products_controller/index_products_controller_spec.rb deleted file mode 100644 index a4decce..0000000 --- a/spec/requests/products_controller/index_products_controller_spec.rb +++ /dev/null @@ -1,15 +0,0 @@ -# frozen_string_literal: true - -require 'rails_helper' - -RSpec.describe 'GET /api/products', type: :request do - before(:each) do - create_list(:product, 26) - end - - it_behaves_like 'a GET index request' do - let(:headers) { {} } - let(:route) { '/api/products' } - let(:pagination_size) { 25 } - end -end diff --git a/spec/requests/products_controller/index_spec.rb b/spec/requests/products_controller/index_spec.rb new file mode 100644 index 0000000..a4decce --- /dev/null +++ b/spec/requests/products_controller/index_spec.rb @@ -0,0 +1,15 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe 'GET /api/products', type: :request do + before(:each) do + create_list(:product, 26) + end + + it_behaves_like 'a GET index request' do + let(:headers) { {} } + let(:route) { '/api/products' } + let(:pagination_size) { 25 } + end +end diff --git a/spec/requests/products_controller/show_products_controller_spec.rb b/spec/requests/products_controller/show_products_controller_spec.rb deleted file mode 100644 index 9b2461c..0000000 --- a/spec/requests/products_controller/show_products_controller_spec.rb +++ /dev/null @@ -1,17 +0,0 @@ -# frozen_string_literal: true - -require 'rails_helper' - -RSpec.describe 'GET /api/products/:public_id', type: :request do - it_behaves_like 'a GET resource request' do - let(:headers) { {} } - let(:product) { create(:product, public_id: SecureRandom.hex(12)) } - let(:route) { "/api/products/#{product.public_id}" } - let(:invalid_route) { "/api/products/#{Faker::Lorem.word}" } - let(:expected_text) do - product.categories.concat([product.name, product.id.to_s, product.unitary_price.to_i.to_s, - product.bulk_price.to_i.to_s, product.available_quantity.to_s, product.company.name, - product.company.short_name]) - end - end -end diff --git a/spec/requests/products_controller/show_spec.rb b/spec/requests/products_controller/show_spec.rb new file mode 100644 index 0000000..9b2461c --- /dev/null +++ b/spec/requests/products_controller/show_spec.rb @@ -0,0 +1,17 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe 'GET /api/products/:public_id', type: :request do + it_behaves_like 'a GET resource request' do + let(:headers) { {} } + let(:product) { create(:product, public_id: SecureRandom.hex(12)) } + let(:route) { "/api/products/#{product.public_id}" } + let(:invalid_route) { "/api/products/#{Faker::Lorem.word}" } + let(:expected_text) do + product.categories.concat([product.name, product.id.to_s, product.unitary_price.to_i.to_s, + product.bulk_price.to_i.to_s, product.available_quantity.to_s, product.company.name, + product.company.short_name]) + end + end +end diff --git a/spec/requests/products_controller/update_products_controller_spec.rb b/spec/requests/products_controller/update_products_controller_spec.rb deleted file mode 100644 index f916d6a..0000000 --- a/spec/requests/products_controller/update_products_controller_spec.rb +++ /dev/null @@ -1,40 +0,0 @@ -# frozen_string_literal: true - -require 'rails_helper' - -RSpec.describe 'PUT /api/products/product_id', type: :request do - let(:user) { create(:user_account, role: 'master') } - let(:product) { create(:product, public_id: SecureRandom.hex(12)) } - let(:new_product) { build(:product) } - let(:picture) { fixture_file_upload('800px-Stray_cat_on_wall.jpg', 'image/png') } - let(:token) { jwt(user) } - let(:categories) do - c = '' - new_product.categories.each { |category| c += "#{category}," } - c - end - - it_behaves_like 'a PUT request' do - let(:headers) { { 'CONTENT_TYPE' => 'application/json', 'Authorization' => "Bearer #{token['token']}" } } - let(:route) { "/api/products/#{product.public_id}" } - let(:wrong_route) { "/api/products/#{SecureRandom.hex(8)}" } - let(:expected_error_messages) do - ["Name can't be blank", 'Unitary price must be greater than 0', 'Bulk price must be greater than 0', - 'Available quantity must be greater than 0', 'Company must exist'] - end - let(:desired_error_status) { 422 } - let(:expected_text) do - new_product.categories.concat([new_product.name, new_product.unitary_price.to_i.to_s, - new_product.bulk_price.to_i.to_s, new_product.available_quantity.to_s, - new_product.company.short_name, 'picture', 'http']) - end - let(:params) do - { name: new_product.name, unitary_price: new_product.unitary_price, bulk_price: new_product.bulk_price, picture:, - available_quantity: new_product.available_quantity, categories:, company_id: new_product.company_id } - end - let(:wrong_params) do - JSON.generate({ name: '', unitary_price: -10, bulk_price: -10, available_quantity: -10, - categories: '', company_id: nil }) - end - end -end diff --git a/spec/requests/products_controller/update_spec.rb b/spec/requests/products_controller/update_spec.rb new file mode 100644 index 0000000..f916d6a --- /dev/null +++ b/spec/requests/products_controller/update_spec.rb @@ -0,0 +1,40 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe 'PUT /api/products/product_id', type: :request do + let(:user) { create(:user_account, role: 'master') } + let(:product) { create(:product, public_id: SecureRandom.hex(12)) } + let(:new_product) { build(:product) } + let(:picture) { fixture_file_upload('800px-Stray_cat_on_wall.jpg', 'image/png') } + let(:token) { jwt(user) } + let(:categories) do + c = '' + new_product.categories.each { |category| c += "#{category}," } + c + end + + it_behaves_like 'a PUT request' do + let(:headers) { { 'CONTENT_TYPE' => 'application/json', 'Authorization' => "Bearer #{token['token']}" } } + let(:route) { "/api/products/#{product.public_id}" } + let(:wrong_route) { "/api/products/#{SecureRandom.hex(8)}" } + let(:expected_error_messages) do + ["Name can't be blank", 'Unitary price must be greater than 0', 'Bulk price must be greater than 0', + 'Available quantity must be greater than 0', 'Company must exist'] + end + let(:desired_error_status) { 422 } + let(:expected_text) do + new_product.categories.concat([new_product.name, new_product.unitary_price.to_i.to_s, + new_product.bulk_price.to_i.to_s, new_product.available_quantity.to_s, + new_product.company.short_name, 'picture', 'http']) + end + let(:params) do + { name: new_product.name, unitary_price: new_product.unitary_price, bulk_price: new_product.bulk_price, picture:, + available_quantity: new_product.available_quantity, categories:, company_id: new_product.company_id } + end + let(:wrong_params) do + JSON.generate({ name: '', unitary_price: -10, bulk_price: -10, available_quantity: -10, + categories: '', company_id: nil }) + end + end +end diff --git a/spec/requests/refresh_tokens_controller/post_refresh_tokens_controller_spec.rb b/spec/requests/refresh_tokens_controller/post_refresh_tokens_controller_spec.rb deleted file mode 100644 index 5d8f8f0..0000000 --- a/spec/requests/refresh_tokens_controller/post_refresh_tokens_controller_spec.rb +++ /dev/null @@ -1,26 +0,0 @@ -# frozen_string_literal: true - -require 'rails_helper' - -RSpec.describe 'POST /api/refresh_tokens', type: :request do - let(:user) { create(:user_account) } - let(:token) { jwt(user) } - - it 'generates a new JSON web token' do - headers = { 'CONTENT_TYPE' => 'application/json', 'Authorization' => "Bearer #{token['refresh']}" } - post('/api/refresh_tokens', headers:) - expect(response).to have_http_status(200) - expect(response.body).to include_strings(%w[refresh token]) - end - - context 'with an expired token' do - it 'returns an error message' do - service_params = { email: user.email, role: user.role } - jwt = Services::TokenGenerationService.new(service_params).call(DateTime.current - 5.days) - headers = { 'CONTENT_TYPE' => 'application/json', 'Authorization' => "Bearer #{jwt[:refresh]}" } - post('/api/refresh_tokens', headers:) - expect(response).to have_http_status(401) - expect(response.body).to include('error_message') - end - end -end diff --git a/spec/requests/refresh_tokens_controller/post_spec.rb b/spec/requests/refresh_tokens_controller/post_spec.rb new file mode 100644 index 0000000..5d8f8f0 --- /dev/null +++ b/spec/requests/refresh_tokens_controller/post_spec.rb @@ -0,0 +1,26 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe 'POST /api/refresh_tokens', type: :request do + let(:user) { create(:user_account) } + let(:token) { jwt(user) } + + it 'generates a new JSON web token' do + headers = { 'CONTENT_TYPE' => 'application/json', 'Authorization' => "Bearer #{token['refresh']}" } + post('/api/refresh_tokens', headers:) + expect(response).to have_http_status(200) + expect(response.body).to include_strings(%w[refresh token]) + end + + context 'with an expired token' do + it 'returns an error message' do + service_params = { email: user.email, role: user.role } + jwt = Services::TokenGenerationService.new(service_params).call(DateTime.current - 5.days) + headers = { 'CONTENT_TYPE' => 'application/json', 'Authorization' => "Bearer #{jwt[:refresh]}" } + post('/api/refresh_tokens', headers:) + expect(response).to have_http_status(401) + expect(response.body).to include('error_message') + end + end +end diff --git a/spec/requests/sessions_controller/create_sessions_controller_spec.rb b/spec/requests/sessions_controller/create_sessions_controller_spec.rb deleted file mode 100644 index cca04b1..0000000 --- a/spec/requests/sessions_controller/create_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) { {} } - 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/sessions_controller/create_spec.rb b/spec/requests/sessions_controller/create_spec.rb new file mode 100644 index 0000000..cca04b1 --- /dev/null +++ b/spec/requests/sessions_controller/create_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) { {} } + 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/sessions_controller/destroy_sessions_controller_spec.rb b/spec/requests/sessions_controller/destroy_sessions_controller_spec.rb deleted file mode 100644 index bc669fc..0000000 --- a/spec/requests/sessions_controller/destroy_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_spec.rb b/spec/requests/sessions_controller/destroy_spec.rb new file mode 100644 index 0000000..bc669fc --- /dev/null +++ b/spec/requests/sessions_controller/destroy_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/user_accounts_controller/create_spec.rb b/spec/requests/user_accounts_controller/create_spec.rb new file mode 100644 index 0000000..a45e02b --- /dev/null +++ b/spec/requests/user_accounts_controller/create_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) { {} } + 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) { build(: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: '', first_name: '', last_name: '' } } + 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 deleted file mode 100644 index a45e02b..0000000 --- a/spec/requests/user_accounts_controller/create_user_accounts_spec.rb +++ /dev/null @@ -1,21 +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) { {} } - 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) { build(: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: '', first_name: '', last_name: '' } } - end -end diff --git a/spec/requests/user_accounts_controller/show_spec.rb b/spec/requests/user_accounts_controller/show_spec.rb new file mode 100644 index 0000000..050fda0 --- /dev/null +++ b/spec/requests/user_accounts_controller/show_spec.rb @@ -0,0 +1,12 @@ +# frozen_string_literal: true + +RSpec.describe 'GET /api/account', type: :request do + let(:user) { create(:user_account) } + let(:token) { jwt(user) } + + it_behaves_like 'a GET resource request', sole_route: true do + let(:headers) { { 'Authorization' => "Bearer #{token['token']}"} } + let(:route) { '/api/account' } + let(:expected_text) { [user.email, user.first_name, user.last_name, user.role] } + end +end diff --git a/spec/requests/user_accounts_controller/show_user_accounts_spec.rb b/spec/requests/user_accounts_controller/show_user_accounts_spec.rb deleted file mode 100644 index 050fda0..0000000 --- a/spec/requests/user_accounts_controller/show_user_accounts_spec.rb +++ /dev/null @@ -1,12 +0,0 @@ -# frozen_string_literal: true - -RSpec.describe 'GET /api/account', type: :request do - let(:user) { create(:user_account) } - let(:token) { jwt(user) } - - it_behaves_like 'a GET resource request', sole_route: true do - let(:headers) { { 'Authorization' => "Bearer #{token['token']}"} } - let(:route) { '/api/account' } - let(:expected_text) { [user.email, user.first_name, user.last_name, user.role] } - end -end diff --git a/spec/requests/user_accounts_controller/update_spec.rb b/spec/requests/user_accounts_controller/update_spec.rb new file mode 100644 index 0000000..fcfb28e --- /dev/null +++ b/spec/requests/user_accounts_controller/update_spec.rb @@ -0,0 +1,23 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe 'PUT /api/account', type: :request do + let(:user) { create(:user_account) } + let(:data) { build(:user_account) } + let(:token) { jwt(user) } + + it_behaves_like 'a PUT request', account: true do + let(:route) { '/api/account' } + let(:headers) { { 'Authorization' => "Bearer #{token['token']}" } } + let(:params) do + { email: data.email, password: data.password, first_name: data.first_name, last_name: data.last_name } + end + let(:expected_text) { [] } + let(:desired_error_status) { 422 } + let(:wrong_params) { { email: 'notanemail', password: '', first_name: '', last_name: '' } } + let(:expected_error_messages) do + ['Email is invalid', "First name can't be blank", "Last name can't be blank"] + end + end +end diff --git a/spec/requests/user_accounts_controller/update_user_accounts_spec.rb b/spec/requests/user_accounts_controller/update_user_accounts_spec.rb deleted file mode 100644 index fcfb28e..0000000 --- a/spec/requests/user_accounts_controller/update_user_accounts_spec.rb +++ /dev/null @@ -1,23 +0,0 @@ -# frozen_string_literal: true - -require 'rails_helper' - -RSpec.describe 'PUT /api/account', type: :request do - let(:user) { create(:user_account) } - let(:data) { build(:user_account) } - let(:token) { jwt(user) } - - it_behaves_like 'a PUT request', account: true do - let(:route) { '/api/account' } - let(:headers) { { 'Authorization' => "Bearer #{token['token']}" } } - let(:params) do - { email: data.email, password: data.password, first_name: data.first_name, last_name: data.last_name } - end - let(:expected_text) { [] } - let(:desired_error_status) { 422 } - let(:wrong_params) { { email: 'notanemail', password: '', first_name: '', last_name: '' } } - let(:expected_error_messages) do - ['Email is invalid', "First name can't be blank", "Last name can't be blank"] - end - end -end -- cgit v1.2.3