-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from codigofacilito/feat/register-and-login
Feat/register and login
- Loading branch information
Showing
13 changed files
with
199 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
class UsersController < ApplicationController | ||
|
||
def register | ||
user = User.new(user_params) | ||
user.pin = params[:pin] | ||
|
||
if user.save | ||
account = user.accounts.first | ||
render json: { user: user.first_name, email:user.email, cuenta: account.account_number, message: 'Usuario registrado exitosamente' }, status: :created | ||
else | ||
render json: { errors: user.errors.full_messages }, status: :unprocessable_entity | ||
end | ||
end | ||
|
||
def login | ||
account = Account.find_by(account_number: params[:account_number]) | ||
|
||
if account && account.user.authenticate_pin(params[:pin]) | ||
render json: { message: 'Inicio de sesión exitoso' }, status: :ok | ||
else | ||
render json: { errors: 'Número de cuenta o PIN incorrecto' }, status: :unauthorized | ||
end | ||
end | ||
|
||
|
||
private | ||
|
||
def user_params | ||
params.permit(:first_name, :last_name, :email) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class AddEmailToUsers < ActiveRecord::Migration[7.2] | ||
def change | ||
add_column :users, :email, :string | ||
end | ||
end |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,21 @@ | ||
# == Schema Information | ||
# | ||
# Table name: users | ||
# | ||
# id :integer not null, primary key | ||
# first_name :string | ||
# last_name :string | ||
# pin_digest :string | ||
# biometric_enabled :boolean | ||
# created_at :datetime not null | ||
# updated_at :datetime not null | ||
# email :string | ||
# | ||
FactoryBot.define do | ||
factory :user do | ||
first_name { "Admin" } | ||
last_name { "User" } | ||
email { "[email protected]" } | ||
pin_digest { "1234" } | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe User, type: :model do | ||
describe 'validations' do | ||
it 'is not valid without email' do | ||
user = build(:user, email: nil) | ||
expect(user).not_to be_valid | ||
expect(user.errors[:email]).to include("can't be blank") | ||
end | ||
|
||
it 'is not valid with duplicated email' do | ||
create(:user, email: '[email protected]', pin: '1234') | ||
user = build(:user, email: '[email protected]') | ||
expect(user).not_to be_valid | ||
expect(user.errors[:email]).to include('has already been taken') | ||
end | ||
|
||
it 'not valid without alphanumeric pin' do | ||
user = build(:user, pin: 'abcd') | ||
expect(user).not_to be_valid | ||
expect(user.errors[:pin]).to include('is not a number') | ||
end | ||
|
||
it 'not valid without 4 characters pin' do | ||
user = build(:user, pin: '123') | ||
expect(user).not_to be_valid | ||
expect(user.errors[:pin]).to include('is the wrong length (should be 4 characters)') | ||
end | ||
end | ||
|
||
describe 'callbacks' do | ||
it 'create an account after user creation' do | ||
user = create(:user, email:'[email protected]', pin: '1234') | ||
expect(user.accounts.count).to eq(1) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe 'Users API', type: :request do | ||
describe 'POST /register' do | ||
let(:valid_attributes) do | ||
{ | ||
first_name: 'Juan', | ||
last_name: 'Perez', | ||
email: '[email protected]', | ||
pin: '1234' | ||
} | ||
end | ||
|
||
context 'when the request is valid' do | ||
it 'creates a new user' do | ||
expect { | ||
post '/register', params: valid_attributes | ||
}.to change(User, :count).by(1) | ||
end | ||
|
||
it 'creates an associated account with an initial balance of $1,000' do | ||
post '/register', params: valid_attributes | ||
user = User.last | ||
account = user.accounts.first | ||
expect(account).not_to be_nil | ||
expect(account.balance).to eq(1000) | ||
end | ||
end | ||
|
||
context 'when the request is invalid' do | ||
it 'does not create a user without email' do | ||
invalid_attributes = valid_attributes.except(:email) | ||
expect { | ||
post '/register', params: invalid_attributes | ||
}.not_to change(User, :count) | ||
end | ||
end | ||
end | ||
|
||
describe 'POST /login' do | ||
let(:user) { create(:user, pin: '1234') } | ||
let(:account) { create(:account, user: user) } | ||
|
||
context 'with valid credentials' do | ||
it 'logs in successfully' do | ||
post '/login', params: { account_number: account.account_number, pin: '1234' } | ||
expect(response).to have_http_status(:ok) | ||
expect(JSON.parse(response.body)['message']).to eq('Inicio de sesión exitoso') | ||
end | ||
end | ||
|
||
context 'with invalid credentials' do | ||
it 'rejects login with incorrect PIN' do | ||
post '/login', params: { account_number: account.account_number, pin: '0000' } | ||
expect(response).to have_http_status(:unauthorized) | ||
expect(JSON.parse(response.body)['errors']).to eq('Número de cuenta o PIN incorrecto') | ||
end | ||
|
||
it 'rejects login with incorrect account number' do | ||
post '/login', params: { account_number: 'invalid', pin: '1234' } | ||
expect(response).to have_http_status(:unauthorized) | ||
expect(JSON.parse(response.body)['errors']).to eq('Número de cuenta o PIN incorrecto') | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters