crear piratas

This commit is contained in:
fauno 2019-04-05 19:14:20 -03:00
parent dd5da0bd81
commit 36d8a4d4dd
No known key found for this signature in database
GPG key ID: 456032D717A4CD9C
12 changed files with 113 additions and 11 deletions

View file

@ -1 +1 @@
2.5.5 2.5.3

View file

@ -3,7 +3,7 @@
source 'https://rubygems.org' source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" } git_source(:github) { |repo| "https://github.com/#{repo}.git" }
ruby '2.5.5' ruby '2.5.3'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails' # Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 5.2.3' gem 'rails', '~> 5.2.3'
@ -41,3 +41,7 @@ group :development do
gem 'spring' gem 'spring'
gem 'spring-watcher-listen', '~> 2.0.0' gem 'spring-watcher-listen', '~> 2.0.0'
end end
group :test do
gem 'database_cleaner'
end

View file

@ -51,6 +51,7 @@ GEM
coderay (1.1.2) coderay (1.1.2)
concurrent-ruby (1.1.5) concurrent-ruby (1.1.5)
crass (1.0.4) crass (1.0.4)
database_cleaner (1.7.0)
erubi (1.8.0) erubi (1.8.0)
factory_bot (5.0.2) factory_bot (5.0.2)
activesupport (>= 4.2.0) activesupport (>= 4.2.0)
@ -166,6 +167,7 @@ PLATFORMS
DEPENDENCIES DEPENDENCIES
bcrypt (~> 3.1.7) bcrypt (~> 3.1.7)
bootsnap (>= 1.1.0) bootsnap (>= 1.1.0)
database_cleaner
factory_bot_rails factory_bot_rails
jbuilder (~> 2.5) jbuilder (~> 2.5)
listen (>= 3.0.5, < 3.2) listen (>= 3.0.5, < 3.2)
@ -179,7 +181,7 @@ DEPENDENCIES
sqlite3 sqlite3
RUBY VERSION RUBY VERSION
ruby 2.5.5p157 ruby 2.5.3p105
BUNDLED WITH BUNDLED WITH
2.0.1 2.0.1

View file

@ -0,0 +1,16 @@
# frozen_string_literal: true
# Las piratas son las que activan el Miniloom
class PiratasController < ApplicationController
# Registra una cuenta
def create
@pirata = Pirata.new(params
.require(:pirata)
.permit(:email, :nick, :password))
return if @pirata.save
render json: { errors: @pirata.errors.full_messages },
status: :unprocessable_entity
end
end

10
app/models/pirata.rb Normal file
View file

@ -0,0 +1,10 @@
# frozen_string_literal: true
# Las piratas son las usuarias empoderadas del miniloom
class Pirata < ApplicationRecord
# Tiene una contraseña segura :P
has_secure_password
# Una por correo
validates :email, presence: true, uniqueness: true
validates :nick, presence: true, uniqueness: true
end

View file

@ -0,0 +1,3 @@
# frozen_string_literal: true
json.call(@pirata, :nick, :email)

View file

@ -1,5 +1,6 @@
# frozen_string_literal: true # frozen_string_literal: true
Rails.application.routes.draw do Rails.application.routes.draw do
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html # No queremos un índice de piratas
resources :piratas, only: %i[create]
end end

View file

@ -0,0 +1,13 @@
# frozen_string_literal: true
# Crear la tabla de piratas con la menor cantidad de metadata y d0x
# posible
class CreatePirata < ActiveRecord::Migration[5.2]
def change
create_table :pirata do |t|
t.string :nick, unique: true
t.string :email, unique: true
t.string :password_digest
end
end
end

View file

@ -0,0 +1,33 @@
# frozen_string_literal: true
require 'test_helper'
class PiratasControllerTest < ActionDispatch::IntegrationTest
test 'se pueden registrar' do
post piratas_url, as: :json, params: {
pirata: {
nick: 'rene',
email: 'rene@partidopirata.com.ar',
password: '12345678'
}
}
body = JSON.parse @response.body
pirata = Pirata.find_by_nick(body['nick'])
assert_equal 'rene@partidopirata.com.ar', pirata.email
assert_equal pirata, pirata.authenticate('12345678')
end
test 'no se pueden registrar dos veces' do
pirata = create :pirata
post piratas_url, as: :json, params: {
pirata: {
nick: pirata.nick,
email: pirata.email,
password: '12345678'
}
}
body = JSON.parse @response.body
assert_equal %w[errors], body.keys
end
end

9
test/factories/pirata.rb Normal file
View file

@ -0,0 +1,9 @@
# frozen_string_literal: true
FactoryBot.define do
factory :pirata do
email { SecureRandom.hex + '@partidopirata.com.ar' }
nick { SecureRandom.hex }
password { '12345678' }
end
end

View file

@ -0,0 +1,11 @@
# frozen_string_literal: true
require 'test_helper'
class PirataTest < ActiveSupport::TestCase
test 'se pueden crear' do
pirata = create :pirata
assert_equal true, pirata.valid?
end
end

View file

@ -3,13 +3,13 @@
ENV['RAILS_ENV'] ||= 'test' ENV['RAILS_ENV'] ||= 'test'
require_relative '../config/environment' require_relative '../config/environment'
require 'rails/test_help' require 'rails/test_help'
require 'database_cleaner'
module ActiveSupport class ActiveSupport::TestCase
class TestCase include FactoryBot::Syntax::Methods
# Setup all fixtures in test/fixtures/*.yml for all tests in
# alphabetical order.
fixtures :all
# Add more helper methods to be used by all tests here... DatabaseCleaner.strategy = :transaction
end
setup { DatabaseCleaner.start }
teardown { DatabaseCleaner.clean }
end end