summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--app/controllers/api/authentications_controller/create_logic.rb12
-rw-r--r--spec/requests/authentications_spec.rb25
2 files changed, 29 insertions, 8 deletions
diff --git a/app/controllers/api/authentications_controller/create_logic.rb b/app/controllers/api/authentications_controller/create_logic.rb
index 173db58..e3af94d 100644
--- a/app/controllers/api/authentications_controller/create_logic.rb
+++ b/app/controllers/api/authentications_controller/create_logic.rb
@@ -1,6 +1,6 @@
# frozen_string_literal: true
-module Services
+module Api
class AuthenticationsController
# The logic for the create method of AuthenticationsController.
class CreateLogic
@@ -11,23 +11,19 @@ module Services
end
def call
- return false if @user_account.nil? && wrong_user_password?
+ jwt = { token: service.call(DateTime.now + 30.minutes), refresh: service.call(DateTime.now + 3.days) }
- { token: service.call(DateTime.now + 30.minutes), refresh: service.call(DateTime.now + 3.days) }
+ return jwt if @user_account&.authenticate(@password)
end
private
- def wrong_user_password?
- @user_account.password != @password
- end
-
def service
@service ||= Services::TokenGenerationService.new(service_params)
end
def service_params
- { email: @email, role: @user_account.role}
+ { email: @email, role: @user_account.role }
end
end
end
diff --git a/spec/requests/authentications_spec.rb b/spec/requests/authentications_spec.rb
new file mode 100644
index 0000000..2ee8ead
--- /dev/null
+++ b/spec/requests/authentications_spec.rb
@@ -0,0 +1,25 @@
+# frozen_string_literal: true
+
+require 'rails_helper'
+
+RSpec.describe 'Authentications', type: :request do
+ let(:user) { create(:user_account) }
+ let(:params) { { credentials: { email: user.email, password: user.password } } }
+ let(:headers) { { 'CONTENT_TYPE' => 'application/json' } }
+
+ describe 'POST /api/authenticate' do
+ scenario 'successfully' do
+ post('/api/authenticate', params: params.to_json, headers:)
+ expect(response).to have_http_status(200)
+ expect(response.body).to include('token')
+ expect(response.body).to include('refresh')
+ end
+
+ scenario 'unsuccessfully' do
+ params[:credentials][:password] = 'wrong_password'
+ post('/api/authenticate', params: params.to_json, headers:)
+ expect(response).to have_http_status(401)
+ expect(response.body).to include('Credenciales incorrectas')
+ end
+ end
+end