Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .ruby-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.4.0
2.6.3
4 changes: 2 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }

ruby '2.4.0'
ruby '2.6.3'

gem 'bootsnap', '>= 1.1.0', require: false
gem 'coffee-rails', '~> 4.2'
Expand Down Expand Up @@ -29,6 +29,6 @@ group :test do
gem 'faker'
gem 'rails-controller-testing'
gem 'rspec-rails'
gem 'shoulda-matchers', '~> 3.1.2'
gem 'shoulda-matchers'
gem 'should_not'
end
12 changes: 6 additions & 6 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ GEM
coffee-script-source
execjs
coffee-script-source (1.12.2)
concurrent-ruby (1.1.3)
concurrent-ruby (1.1.5)
crass (1.0.4)
diff-lcs (1.3)
erubi (1.7.1)
Expand All @@ -71,7 +71,7 @@ GEM
ffi (1.9.25)
globalid (0.4.1)
activesupport (>= 4.2.0)
i18n (1.1.1)
i18n (1.6.0)
concurrent-ruby (~> 1.0)
jbuilder (2.8.0)
activesupport (>= 4.2.0)
Expand Down Expand Up @@ -169,8 +169,8 @@ GEM
sprockets-rails (>= 2.0, < 4.0)
tilt (>= 1.1, < 3)
should_not (1.1.0)
shoulda-matchers (3.1.2)
activesupport (>= 4.0.0)
shoulda-matchers (4.0.1)
activesupport (>= 4.2.0)
spring (2.0.2)
activesupport (>= 4.2)
spring-watcher-listen (2.0.1)
Expand Down Expand Up @@ -220,7 +220,7 @@ DEPENDENCIES
rspec-rails
sass-rails (~> 5.0)
should_not
shoulda-matchers (~> 3.1.2)
shoulda-matchers
spring
spring-watcher-listen (~> 2.0.0)
sqlite3
Expand All @@ -229,7 +229,7 @@ DEPENDENCIES
web-console (>= 3.3.0)

RUBY VERSION
ruby 2.4.0p0
ruby 2.6.3p62

BUNDLED WITH
2.0.1
2 changes: 2 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

class User < ApplicationRecord
# -- Validations
validates :email, presence: true, uniqueness: true
Expand Down
12 changes: 7 additions & 5 deletions app/services/api_login_manager.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
# frozen_string_literal: true

class ApiLoginError < StandardError; end

class ApiLoginManager
ERRORS = [
USER_NOT_FOUND = 'El usuario no existe'.freeze,
EMPTY_EMAIL = 'El email no puede estar en blanco.'.freeze,
EMPTY_PASSWORD = 'La contraseña no puede estar en blanco.'.freeze,
WRONG_PASSWORD = 'La contraseña es incorrecta'.freeze,
EXTERNAL_VALIDATOR = 'El usuario ya no tiene cuota disponible'.freeze
USER_NOT_FOUND = 'El usuario no existe',
EMPTY_EMAIL = 'El email no puede estar en blanco',
EMPTY_PASSWORD = 'La contraseña no puede estar en blanco',
WRONG_PASSWORD = 'La contraseña es incorrecta',
EXTERNAL_VALIDATOR = 'El usuario ya no tiene cuota disponible'
].freeze

attr_reader :error
Expand Down
7 changes: 6 additions & 1 deletion spec/factories/posts.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
FactoryBot.define do
# Implementar factory
factory :post do
association :user

title { Faker::Lorem.unique.sentence }
body { Faker::Lorem.paragraph }
end
end
26 changes: 24 additions & 2 deletions spec/models/post_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,28 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe Post, type: :model do
# En base al modelo de test propuesto para el modelo User,
# implementar los tests para el modelo Post.
describe 'Factory' do
subject { build(:post) }

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pese a que el test pasa y está bien, podemos usar un build_stubbed para no persistir al Post en la base de datos (para mejorar la performance). El build es mejor en este sentido que el create y lo veo muy bien, pero aún así persiste aquellas asociaciones que tenga definido el factory. Es sólo un detalle, pero está bueno conocerlo.

subject { build_stubbed(:post) }


it 'has a valid factory' do
is_expected.to be_valid
end
end

describe 'Associations' do
it { should belong_to(:user) }
end

describe 'Presence validations' do
it { should validate_presence_of(:title) }
it { should validate_presence_of(:body) }
end

describe 'Uniqueness validations' do
subject { create(:post) }

it { should validate_uniqueness_of(:title) }
end
end
44 changes: 29 additions & 15 deletions spec/models/user_spec.rb
Original file line number Diff line number Diff line change
@@ -1,41 +1,55 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe User, type: :model do
describe 'Factory' do
subject { build(:user) }

it 'has a valid factory' do
# Testear que el factory definido es válido.
is_expected.to be_valid
end
end

describe 'Associations' do
# Testear asociaciones (shoulda-matchers).
# https://github.com/thoughtbot/shoulda-matchers#activerecord-matchers
it { should have_many(:posts).dependent(:destroy) }
end

describe 'Presence validations' do
# Testear validaciones de presencia (shoulda-matchers).
# https://github.com/thoughtbot/shoulda-matchers#activemodel-matchers
it { should validate_presence_of(:email) }
it { should validate_presence_of(:password) }
it { should validate_presence_of(:role) }
end

describe 'Uniqueness validations' do
# Testear validaciones de unicidad (shoulda-matchers).
# https://github.com/thoughtbot/shoulda-matchers#activemodel-matchers
subject { create(:user) }

it { should validate_uniqueness_of(:email) }
end

describe 'Length validations' do
# Testear validaciones de longitud (shoulda-matchers).
# https://github.com/thoughtbot/shoulda-matchers#activemodel-matchers
it { should validate_length_of(:password).is_at_least(8) }
end

describe 'Enumeratives' do
# Testear definición de enumerativos (shoulda-matchers).
it { should define_enum_for(:role).with_values([:admin, :regular]) }
end

# Testear métodos de instancia y de clase como para el caso de cualquier
# otra clase Ruby (similar a testear ApiLoginManager).
describe '#valid_password?' do
# Testear funcionamiento de método. Podemos definir dos contexts:
# - 'when given value is different from password'
# - 'when given value is equal to password'
subject { user.valid_password?(password) }

let(:user) { create(:user, password: 'password_secret') }

context 'when given value is different from password' do
let(:password) { 'password_wrong' }

it { is_expected.to be_falsey }

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Está bien, pero sabemos que si falla el resultado es false, por lo que creo que sería más específico con be false.

end

context 'when given value is equal to password' do
let(:password) { 'password_secret' }

it { is_expected.to be_truthy }

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lo mismo acá, sabemos que debería retornar true.

end
end
end
Loading