Ricardo Garcia

Add more specs

source 'https://rubygems.org'
group :development do
gem "sqlite3"
gem 'sqlite3'
end
gemspec
......
# A sample Guardfile
# More info at https://github.com/guard/guard#readme
## Uncomment and set this to only include directories you want to watch
# directories %w(app lib config test spec features) \
# .select{|d| Dir.exists?(d) ? d : UI.warning("Directory #{d} does not exist")}
## Note: if you are using the `directories` clause above and you are not
## watching the project directory ('.'), then you will want to move
## the Guardfile to a watched dir and symlink it back, e.g.
#
# $ mkdir config
# $ mv Guardfile config/
# $ ln -s config/Guardfile .
#
# and, you'll have to watch "config/Guardfile" instead of "Guardfile"
# Note: The cmd option is now required due to the increasing number of ways
# rspec may be run, below are examples of the most common uses.
# * bundler: 'bundle exec rspec'
# * bundler binstubs: 'bin/rspec'
# * spring: 'bin/rspec' (This will use spring if running and you have
# installed the spring binstubs per the docs)
# * zeus: 'zeus rspec' (requires the server to be started separately)
# * 'just' rspec: 'rspec'
guard :rspec, cmd: 'bundle exec rspec --color --format d' do
require 'guard/rspec/dsl'
dsl = Guard::RSpec::Dsl.new(self)
# Feel free to open issues for suggestions and improvements
# RSpec files
rspec = dsl.rspec
watch(rspec.spec_helper) { rspec.spec_dir }
watch(rspec.spec_support) { rspec.spec_dir }
watch(rspec.spec_files)
# Ruby files
ruby = dsl.ruby
dsl.watch_spec_files_for(ruby.lib_files)
# Rails files
rails = dsl.rails(view_extensions: %w(erb haml slim))
dsl.watch_spec_files_for(rails.app_files)
dsl.watch_spec_files_for(rails.views)
watch(rails.controllers) do |m|
[
rspec.spec.call("routing/#{m[1]}_routing"),
rspec.spec.call("controllers/#{m[1]}_controller"),
rspec.spec.call("acceptance/#{m[1]}")
]
end
# Rails config changes
watch(rails.spec_helper) { rspec.spec_dir }
watch(rails.routes) { "#{rspec.spec_dir}/routing" }
watch(rails.app_controller) { "#{rspec.spec_dir}/controllers" }
# Capybara features specs
watch(rails.view_dirs) { |m| rspec.spec.call("features/#{m[1]}") }
watch(rails.layouts) { |m| rspec.spec.call("features/#{m[1]}") }
# Turnip features and steps
watch(%r{^spec/acceptance/(.+)\.feature$})
watch(%r{^spec/acceptance/steps/(.+)_steps\.rb$}) do |m|
Dir[File.join("**/#{m[1]}.feature")][0] || 'spec/acceptance'
end
end
......@@ -3,13 +3,14 @@
require 'bundler/setup'
require 'active_record'
require 'active_support'
require 'var'
require_relative '../spec/mocks/var_database_mock.rb'
require_relative '../spec/mocks/var_models_mock.rb'
require 'var'
ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
ActiveRecord::Schema.verbose = false
VarDatabaseMock.setup_db
require "pry"
require 'pry'
Pry.start
......
......@@ -52,7 +52,8 @@ module ActsAsChargeable
line_items: [{
description: sync(:conekta, 'description'), quantity: 1,
unit_price: sync(:conekta, 'amount'), name: sync(:conekta, 'name')
}] }
}]
}
}.merge(conekta_type_of_charge(options)))
end
......@@ -61,8 +62,7 @@ module ActsAsChargeable
{ card: options[:card_token] }
elsif options[:conekta_type] == 'oxxo'
{ cash: { type: 'oxxo',
expires_at: (Time.zone.today + 3.days).strftime('%Y-%m-%d') }
}
expires_at: (Time.zone.today + 3.days).strftime('%Y-%m-%d') } }
end
end
......@@ -176,13 +176,11 @@ module ActsAsChargeable
private
def clean_var_variables
if update_columns(var_status: nil, var_barcode: nil, var_barcode_url: nil,
var_id: nil, var_service: nil, var_payment_at: nil,
return { object: code } if update(var_status: nil, var_barcode: nil,
var_barcode_url: nil, var_id: nil,
var_service: nil, var_payment_at: nil,
var_payment_expires_at: nil)
{ object: code }
else
{ error_message: 'Something went wrong' }
end
end
end
end
......
......@@ -5,16 +5,10 @@ require 'acts_as_chargeable'
# Main Module
module Var
VALID_SERVICES = [:conekta]
VALID_SERVICES = [:conekta].freeze
@@var_classes = []
class << self
def valid_services
VALID_SERVICES
end
def var_classes
@@var_classes
end
......@@ -24,26 +18,31 @@ module Var
end
def create_charge(service, object, options = {})
return { error_message: 'Service is not supported' } unless VALID_SERVICES.include? service
return { error_message: "#{object.class} doesn't support charges" } unless object.respond_to?(:charge_with)
charge = object.charge_with(service, options)
charge
raise 'Service is not supported' unless supported_service? service
raise "#{object.class} doesn't support charges" unless object.respond_to? :charge_with
object.charge_with(service, options)
end
def conekta_webhook(params)
payment = params[:data][:object]
object = Var.find_charge payment[:id]
update_object_var_data(object, payment)
object
# rescue Exception => exception
# puts exception
# false
end
def update_object_var_data(object, data)
payment = data
object.update_columns(var_status: payment[:status])
if object.var_payed?
return object unless object.var_payed?
time = Time.strptime payment[:paid_at].to_s, '%s'
amount = payment[:amount].to_f / 100.0
fee = payment[:fee].to_f / 100.0
object.update(var_fee: fee, var_paid_amount: amount, var_payment_method: payment[:payment_method][:object], var_payment_at: time)
end
object
rescue Exception => exception
puts exception
false
object.update(var_fee: fee, var_paid_amount: amount,
var_payment_method: payment[:payment_method][:object],
var_payment_at: time)
end
def find_charge(id)
......@@ -52,6 +51,13 @@ module Var
class_name.where(var_id: id)
end.flatten.first
end
private
def supported_service?(service)
VALID_SERVICES.include? service
end
end
end
......
# Var Version
module Var
VERSION = '0.3.0'
VERSION = '0.3.0'.freeze
end
......
require 'spec_helper'
describe ActsAsChargeable do
context 'as a module' do
it 'can be implemented in ActiveRecord' do
expect(Product).to respond_to(:acts_as_chargeable)
end
end
end
# Tests for mock on Chargeable class
describe Product do
it { is_expected.to respond_to(:charge_with) }
it { is_expected.to respond_to(:charge_with_conekta) }
it { is_expected.to respond_to(:conekta_charge) }
it { is_expected.to respond_to(:conekta_type_of_charge) }
it { is_expected.to respond_to(:update_conekta_barcode) }
it { is_expected.to respond_to(:manual_charge) }
it { is_expected.to respond_to(:manual_discharge) }
it { is_expected.to respond_to(:find_charge) }
it { is_expected.to respond_to(:charged?) }
it { is_expected.to respond_to(:find_conekta_charge) }
it { is_expected.to respond_to(:instance_support?) }
it { is_expected.to respond_to(:sync) }
it { is_expected.to respond_to(:conekta_attributes) }
it { is_expected.to respond_to(:paypal_attributes) }
it { is_expected.to respond_to(:var_payed?) }
it { is_expected.to respond_to(:cancel_oxxo_payment) }
it { is_expected.to respond_to(:var_expired_by) }
end
class Product < ActiveRecord::Base
acts_as_chargeable
end
......
require 'pry'
require 'active_record'
require 'active_support'
require 'Var'
require 'mocks/var_database_mock'
require 'mocks/var_models_mock'
require 'Var'
ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:")
ActiveRecord::Base.establish_connection(adapter: 'sqlite3',
database: ':memory:')
ActiveRecord::Schema.verbose = false
VarDatabaseMock.setup_db
......
......@@ -2,15 +2,129 @@ require 'spec_helper'
describe Var do
context 'as a class' do
it { is_expected.to respond_to(:valid_services) }
it { is_expected.to respond_to(:var_classes) }
it { is_expected.to respond_to(:add_var_class) }
it { is_expected.to respond_to(:create_charge) }
it { is_expected.to respond_to(:conekta_webhook) }
it { is_expected.to respond_to(:find_charge) }
end
it 'supports conekta' do
expect(Var.send(:supported_service?, :conekta)).to be true
end
it 'holds var classes' do
expect(Var.var_classes).to eq([Product])
class Dummy < ActiveRecord::Base
acts_as_chargeable
end
expect(Var.var_classes).to eq([Product, Dummy])
end
context 'when creating a charge' do
it 'is delegates the call' do
product = instance_double('Product')
allow(product).to receive(:charge_with).and_return(:success)
charge = Var.create_charge(:conekta, product, {})
expect(charge).to eq :success
end
it 'raises an error if service is not supported' do
product = instance_double('Product')
expect do
Var.create_charge(:random_payment_service, product, {})
end.to raise_error 'Service is not supported'
end
it 'raises an error if object cannot be chargable' do
product = instance_double('Product')
expect do
Var.create_charge(:conekta, product, {})
end.to raise_error "#{product.class} doesn't support charges"
end
end
context 'when receiving a webhook from conekta' do
let!(:chargable_product) do
Product.new # Weird hack to enable connection
instance_double('Product')
end
it 'handles Cargo creado con tarjeta' do
params = {"data":{"object":{"id":"5511d4ce2412294cf6000081","livemode":false,"created_at":1427231950,"status":"pending_payment","currency":"MXN","description":"Stogies","reference_id":"9839-wolf_pack","failure_code":nil,"failure_message":nil,"monthly_installments":nil,"object":"charge","amount":20000,"paid_at":nil,"fee":963,"customer_id":"","refunds":[],"payment_method":{"name":"Jorge Lopez","exp_month":"12","exp_year":"19","auth_code":nil,"object":"card_payment","last4":"4242","brand":"visa"},"details":{"name":nil,"phone":nil,"email":nil,"line_items":[]}},"previous_attributes":{}},"livemode":false,"webhook_status":"pending","id":"5511d4ce2412294cf6000084","object":"event","type":"charge.created","created_at":1427231950,"webhook_logs":[{"id":"webhl_nPzGMBeQmMUu7aQ","url":"http://requestb.in/1em0jsx1","failed_attempts":0,"last_http_response_status":-1,"object":"webhook_log","last_attempted_at":0}]}
allow(Var).to receive(:find_charge).and_return chargable_product
allow(chargable_product).to receive(:update_columns).and_return true
allow(chargable_product).to receive(:var_payed?).and_return false
allow(chargable_product).to receive(:var_id).and_return '5511d4ce2412294cf6000081'
result = Var.conekta_webhook params
expect(result.var_id).to eq('5511d4ce2412294cf6000081')
# TODO: Define what is actually being teted
pending
end
it 'handles Cargo pagado con tarjeta' do
pending
end
it 'handles Cargo de OXXO creado' do
pending
end
it 'handles Cargo de OXXO pagado' do
pending
end
xit 'handles Cargo de SPEI creado' do
pending
end
xit 'handles Cargo de SPEI pagado' do
pending
end
xit 'handles Plan creado' do
pending
end
xit 'handles Crear un customer sin tarjeta y sin plan' do
pending
end
xit 'handles Customer creado con una tarjeta y sin plan' do
pending
end
xit 'handles Customer creado con tarjeta y con plan (suscripción)' do
pending
end
xit 'handles Suscripción creada' do
pending
end
xit 'handles Suscripción pagada' do
pending
end
xit 'handles Suscripción activa' do
pending
end
xit 'handles Contracargo creado' do
pending
end
xit 'handles Contracargo ganado' do
pending
end
it 'returns conekta as a valid service' do
expect(Var.valid_services).to eq([:conekta])
xit 'handles Contracargo perdido' do
pending
end
end
end
......
......@@ -5,7 +5,7 @@ require 'var/version'
Gem::Specification.new do |spec|
spec.name = 'var'
spec.version = Var::VERSION
spec.authors = ['abrahamrq', 'chelord', 'rgp']
spec.authors = %w(abrahamrq chelord rgp)
spec.email = ['abraham.rq03@gmail.com']
spec.summary = 'Payment gateway for Conekta, Paypal'
......@@ -17,7 +17,7 @@ Gem::Specification.new do |spec|
if spec.respond_to?(:metadata)
spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
else
fail 'RubyGems 2.0+ is required to protect against public gem pushes.'
raise 'RubyGems 2.0+ is required to protect against public gem pushes.'
end
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
......@@ -28,15 +28,15 @@ Gem::Specification.new do |spec|
spec.add_dependency 'conekta', '~> 0.5'
spec.add_dependency 'paypal-sdk-rest'
spec.add_dependency 'activerecord', '>= 4.0', '< 5.1'
spec.add_dependency "activesupport"
spec.add_dependency 'activesupport'
spec.add_development_dependency 'bundler', '~> 1.10.a'
spec.add_development_dependency 'rake', '~> 10.0'
spec.add_development_dependency "rspec"
spec.add_development_dependency "rspec-nc"
spec.add_development_dependency "guard"
spec.add_development_dependency "guard-rspec"
spec.add_development_dependency "pry"
spec.add_development_dependency "pry-remote"
spec.add_development_dependency "pry-nav"
spec.add_development_dependency 'rspec'
spec.add_development_dependency 'rspec-nc'
spec.add_development_dependency 'guard'
spec.add_development_dependency 'guard-rspec'
spec.add_development_dependency 'pry'
spec.add_development_dependency 'pry-remote'
spec.add_development_dependency 'pry-nav'
end
......