From 898d91cca5721db785e991e0eab9f05010925b1a Mon Sep 17 00:00:00 2001 From: HombreLaser Date: Fri, 3 Mar 2023 20:58:25 -0600 Subject: Reacomoda directorio de specs --- .../concerns/token_generation_concern.rb | 23 ------------------- .../controllers/authentications_controller_spec.rb | 8 ------- spec/requests/authentications_spec.rb | 16 ------------- spec/requests/post_refresh_tokens_spec.rb | 26 ---------------------- spec/requests/post_user_accounts_spec.rb | 22 ------------------ .../post_refresh_tokens_controller_spec.rb | 26 ++++++++++++++++++++++ .../post_sessions_controller_spec.rb | 16 +++++++++++++ .../post_user_accounts_spec.rb | 22 ++++++++++++++++++ .../shared_examples/requests/delete_request.rb | 5 +++++ 9 files changed, 69 insertions(+), 95 deletions(-) delete mode 100644 app/controllers/concerns/token_generation_concern.rb delete mode 100644 spec/controllers/authentications_controller_spec.rb delete mode 100644 spec/requests/authentications_spec.rb delete mode 100644 spec/requests/post_refresh_tokens_spec.rb delete mode 100644 spec/requests/post_user_accounts_spec.rb create mode 100644 spec/requests/refresh_tokens_controller/post_refresh_tokens_controller_spec.rb create mode 100644 spec/requests/sessions_controller/post_sessions_controller_spec.rb create mode 100644 spec/requests/user_accounts_controller/post_user_accounts_spec.rb create mode 100644 spec/support/shared_examples/requests/delete_request.rb diff --git a/app/controllers/concerns/token_generation_concern.rb b/app/controllers/concerns/token_generation_concern.rb deleted file mode 100644 index beab467..0000000 --- a/app/controllers/concerns/token_generation_concern.rb +++ /dev/null @@ -1,23 +0,0 @@ -# frozen_string_literal: true - -# Common logic for all the logics that require the token generation -# service. -module TokenGenerationConcern - extend ActiveSupport::Concern - - def service - @service ||= Services::TokenGenerationService.new(service_params) - end - - def generate_user_session_key(user) - user.update_attribute(:session_key, SecureRandom.hex(16)) - end - - def generate_token - { token: service.call(DateTime.current + 30.minutes), refresh: service.call(DateTime.current + 3.days) } - end - - def service_params - { email: @user_account.email, role: @user_account.role, session_key: @user_account.session_key } - end -end diff --git a/spec/controllers/authentications_controller_spec.rb b/spec/controllers/authentications_controller_spec.rb deleted file mode 100644 index 43e7439..0000000 --- a/spec/controllers/authentications_controller_spec.rb +++ /dev/null @@ -1,8 +0,0 @@ -# frozen_string_literal: true - -require 'rails_helper' - -RSpec.describe Api::AuthenticationsController, type: :controller do - it { should route(:post, '/api/authenticate').to(action: :create) } - it { should route(:delete, '/api/logout').to(action: :destroy) } -end diff --git a/spec/requests/authentications_spec.rb b/spec/requests/authentications_spec.rb deleted file mode 100644 index 6b33402..0000000 --- a/spec/requests/authentications_spec.rb +++ /dev/null @@ -1,16 +0,0 @@ -# frozen_string_literal: true - -require 'rails_helper' - -RSpec.describe 'POST /api/authenticate', type: :request do - it_behaves_like 'a POST request' do - let(:headers) { { 'CONTENT_TYPE' => 'application/json' } } - let(:route) { '/api/authenticate' } - let(:expected_error_messages) { ['Credenciales incorrectas'] } - let(:desired_error_status) { 401 } - let(:expected_text) { %w[token refresh] } - let(:user) { create(:user_account) } - let(:params) { { credentials: { email: user.email, password: user.password } } } - let(:wrong_params) { { credentials: { email: user.email, password: 'wrong_password' } } } - end -end diff --git a/spec/requests/post_refresh_tokens_spec.rb b/spec/requests/post_refresh_tokens_spec.rb deleted file mode 100644 index 5d8f8f0..0000000 --- a/spec/requests/post_refresh_tokens_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/post_user_accounts_spec.rb b/spec/requests/post_user_accounts_spec.rb deleted file mode 100644 index 7c5aacc..0000000 --- a/spec/requests/post_user_accounts_spec.rb +++ /dev/null @@ -1,22 +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", - "Role can't be blank"] - end - let(:desired_error_status) { 422 } - let(:expected_text) { %w[token refresh] } - let(:user) { create(:user_account) } - let(:params) do - { user_account: JSON.parse(user.to_json).except('session_key', 'created_at', 'updated_at', 'id', - 'password_digest').merge({ password: user.password }) } - end - let(:wrong_params) { { user_account: { email: 'notanemail', password: '' } } } - 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 new file mode 100644 index 0000000..5d8f8f0 --- /dev/null +++ b/spec/requests/refresh_tokens_controller/post_refresh_tokens_controller_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/post_sessions_controller_spec.rb b/spec/requests/sessions_controller/post_sessions_controller_spec.rb new file mode 100644 index 0000000..31cef31 --- /dev/null +++ b/spec/requests/sessions_controller/post_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) { { credentials: { email: user.email, password: user.password } } } + let(:wrong_params) { { credentials: { email: user.email, password: 'wrong_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 new file mode 100644 index 0000000..7c5aacc --- /dev/null +++ b/spec/requests/user_accounts_controller/post_user_accounts_spec.rb @@ -0,0 +1,22 @@ +# 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", + "Role can't be blank"] + end + let(:desired_error_status) { 422 } + let(:expected_text) { %w[token refresh] } + let(:user) { create(:user_account) } + let(:params) do + { user_account: JSON.parse(user.to_json).except('session_key', 'created_at', 'updated_at', 'id', + 'password_digest').merge({ password: user.password }) } + end + let(:wrong_params) { { user_account: { email: 'notanemail', password: '' } } } + end +end diff --git a/spec/support/shared_examples/requests/delete_request.rb b/spec/support/shared_examples/requests/delete_request.rb new file mode 100644 index 0000000..ea921ea --- /dev/null +++ b/spec/support/shared_examples/requests/delete_request.rb @@ -0,0 +1,5 @@ +# frozen_string_literal: true + +RSpec.shared_examples 'a DELETE request' do + let(:headers) { { 'CONTENT_TYPE' => 'application/json' } } +end -- cgit v1.2.3