From 5c899df07753a31543f8dad725a26ccddc44dfe2 Mon Sep 17 00:00:00 2001 From: HombreLaser Date: Mon, 13 Mar 2023 19:48:40 -0600 Subject: Añade método update a UserAccountsController MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- spec/models/company_spec.rb | 8 ++++++++ .../create_user_accounts_spec.rb | 12 +++++------ .../update_user_accounts_spec.rb | 24 ++++++++++++++++++++++ .../shared_examples/requests/put_request.rb | 12 ++++++----- 4 files changed, 45 insertions(+), 11 deletions(-) create mode 100644 spec/requests/user_accounts_controller/update_user_accounts_spec.rb (limited to 'spec') diff --git a/spec/models/company_spec.rb b/spec/models/company_spec.rb index 7fe3a87..0d254a8 100644 --- a/spec/models/company_spec.rb +++ b/spec/models/company_spec.rb @@ -3,9 +3,17 @@ require 'rails_helper' RSpec.describe Company, type: :model do + let(:company) { create(:company) } + it { should validate_presence_of(:name) } it { should validate_presence_of(:short_name) } it { should validate_presence_of(:country) } it { should validate_uniqueness_of(:short_name).ignoring_case_sensitivity } it { should have_one_attached(:logo) } + + describe '#to_param' do + it "returns the model's url param" do + expect(company.to_param).to eq(company.short_name) + end + 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 index 3fd5990..a45e02b 100644 --- a/spec/requests/user_accounts_controller/create_user_accounts_spec.rb +++ b/spec/requests/user_accounts_controller/create_user_accounts_spec.rb @@ -4,18 +4,18 @@ 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(: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" ] + ["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(:user) { build(:user_account) } let(:params) do - JSON.generate({ email: user.email, password: user.password, - first_name: user.first_name, last_name: user.last_name }) + { 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: '' }) } + let(:wrong_params) { { email: 'notanemail', password: '', first_name: '', last_name: '' } } 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 new file mode 100644 index 0000000..ba9cc69 --- /dev/null +++ b/spec/requests/user_accounts_controller/update_user_accounts_spec.rb @@ -0,0 +1,24 @@ +# 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' do + let(:account) { true } + 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 + ["Password can't be blank", 'Email is invalid', "First name can't be blank", "Last name can't be blank"] + end + end +end diff --git a/spec/support/shared_examples/requests/put_request.rb b/spec/support/shared_examples/requests/put_request.rb index 9e46235..d329b78 100644 --- a/spec/support/shared_examples/requests/put_request.rb +++ b/spec/support/shared_examples/requests/put_request.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -RSpec.shared_examples 'a PUT request' do +RSpec.shared_examples 'a PUT request' do |account: false| context 'with correct parameters' do it 'returns a successful http status and a response with the putted data' do put(route, params:, headers:) @@ -17,10 +17,12 @@ RSpec.shared_examples 'a PUT request' do 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) + if account + 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 end -- cgit v1.2.3