Skip to content

Commit 8b673ea

Browse files
committed
Added controller spec & fixed intractors
1 parent fe2755c commit 8b673ea

File tree

14 files changed

+4702
-1033
lines changed

14 files changed

+4702
-1033
lines changed

app/controllers/api/v1/users_controller.rb

+7-3
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,15 @@ def create
1818

1919
def update
2020
authorize @user
21-
# result = User::Update.call(params: user_params.except(:email))
21+
if @user.update(user_params)
22+
render json: { message: I18n.t('generic.update.success')}, status: :ok
23+
else
24+
render json: { errors: @user.error_string }, status: :unprocessable_entity
25+
end
2226
end
2327

2428
def login
25-
user = User.find_by_email(login_params[:email])
29+
user = User.verified.where(email: login_params[:email]).first
2630
if user&.authenticate(login_params[:password])
2731
token = JsonWebToken.encode(user_id: user.id)
2832
time = Time.zone.now + 100.hours
@@ -38,7 +42,7 @@ def login
3842

3943
def verify
4044
user = User.find_by(reset_password_token: params[:token])
41-
return render json: { error: I18n.t('user.errors.verify') }, status: :not_found unless user
45+
return render json: { errors: I18n.t('user.errors.verify') }, status: :not_found unless user
4246

4347
user.update(reset_password_token: nil)
4448
render json: { success: true }, status: :ok

app/controllers/application_controller.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def authenticate_user
2121
render json: { errors: 'Session expired. Please login again!' }, status: :unauthorized
2222
end
2323
else
24-
render json: { errors: 'Authorization token not provided' }, status: :unauthorized
24+
render json: { errors: 'Authentication token not provided' }, status: :unauthorized
2525
end
2626
end
2727

@@ -36,6 +36,6 @@ def record_not_found
3636
end
3737

3838
def unauthorized_access
39-
return render json: { message: I18n.t('authorization.error') }, status: :forbidden
39+
return render json: { errors: I18n.t('authorization.error') }, status: :forbidden
4040
end
4141
end

app/interactors/user/add_to_system.rb

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
# frozen_string_literal: true
22

3-
module User
4-
class AddToSystem
5-
include Interactor::Organizer
3+
class User::AddToSystem
4+
include Interactor::Organizer
65

7-
organize User::Create, Mail::VerifyUser
8-
end
6+
organize User::Create, Mail::VerifyUser
97
end

app/interactors/user/create.rb

+12-14
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
11
# frozen_string_literal: true
22

3-
module User
4-
class Create
5-
include Interactor
3+
class User::Create
4+
include Interactor
65

7-
def call
8-
params = context.params.merge(
9-
reset_password_token: SecureRandom.urlsafe_base64,
10-
reset_password_sent_at: Time.now
11-
)
12-
user = User.new(params)
13-
if user.save
14-
context.user = user
15-
else
16-
context.fail!(error: user.error_string)
17-
end
6+
def call
7+
params = context.params.merge(
8+
reset_password_token: SecureRandom.urlsafe_base64,
9+
reset_password_sent_at: Time.now
10+
)
11+
user = User.new(params)
12+
if user.save
13+
context.user = user
14+
else
15+
context.fail!(error: { errors: user.error_string })
1816
end
1917
end
2018
end

app/models/user.rb

+2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ class User < ApplicationRecord
3131
validates :email, presence: true, uniqueness: { case_sensitive: false }
3232
validates :password, length: { minimum: 8 }, on: :create
3333

34+
scope :verified, -> { where(reset_password_token: nil) }
35+
3436
def full_name
3537
"#{first_name} #{last_name}"
3638
end

config/environments/production.rb

+2
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@
7676
# Use a different logger for distributed setups.
7777
# require "syslog/logger"
7878
# config.logger = ActiveSupport::TaggedLogging.new(Syslog::Logger.new "app-name")
79+
config.active_job.queue_adapter = :good_job
80+
config.good_job.execution_mode = :external
7981

8082
if ENV['RAILS_LOG_TO_STDOUT'].present?
8183
logger = ActiveSupport::Logger.new($stdout)

config/environments/test.rb

+3
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@
2929
config.action_controller.perform_caching = false
3030
config.cache_store = :null_store
3131

32+
config.active_job.queue_adapter = :good_job
33+
config.good_job.execution_mode = :inline
34+
3235
# Raise exceptions instead of rendering exception templates.
3336
config.action_dispatch.show_exceptions = false
3437

config/locales/en.yml

+4
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,9 @@ en:
66
not_found: User not found with the given ID.
77
generic:
88
record_not_found: Record not found.
9+
create:
10+
success: Created successfully.
11+
update:
12+
success: Updated successfully.
913
authorization:
1014
error: Unauthorized access.

coverage/.last_run.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
22
"result": {
3-
"line": 94.05
3+
"line": 89.0
44
}
55
}

0 commit comments

Comments
 (0)